feat: hello triangle
Compile a vertex and fragment shader from the same directory as hello.cpp. Renders an orange triangle on a blue background.
This commit is contained in:
parent
8eface137b
commit
1c5350b67d
|
@ -43,3 +43,8 @@ elseif(UNIX)
|
|||
target_link_libraries(${PROJECT_NAME} GLEW::GLEW)
|
||||
target_link_libraries(${PROJECT_NAME} OpenGL::GL)
|
||||
endif()
|
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/vert.glsl
|
||||
${CMAKE_CURRENT_BINARY_DIR}/vert.glsl COPYONLY)
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/frag.glsl
|
||||
${CMAKE_CURRENT_BINARY_DIR}/frag.glsl COPYONLY)
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0, 0.5, 0.2, 1.0);
|
||||
}
|
102
hello.cpp
102
hello.cpp
|
@ -36,9 +36,55 @@
|
|||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
std::string readFile(const std::string& filePath)
|
||||
{
|
||||
std::ifstream fileStream(filePath, std::ios::in);
|
||||
|
||||
if (!fileStream.is_open())
|
||||
{
|
||||
std::cerr << "Could not read file " << filePath <<
|
||||
". File does not exist." << std::endl;
|
||||
return "";
|
||||
}
|
||||
|
||||
std::stringstream sstr;
|
||||
sstr << fileStream.rdbuf();
|
||||
fileStream.close();
|
||||
|
||||
return sstr.str();
|
||||
}
|
||||
|
||||
GLuint compileShader(const std::string& shaderPath, GLenum shaderType)
|
||||
{
|
||||
GLuint shader;
|
||||
GLint success;
|
||||
|
||||
std::string shaderSource = readFile(shaderPath);
|
||||
const char* source = shaderSource.c_str();
|
||||
|
||||
shader = glCreateShader(shaderType);
|
||||
glShaderSource(shader, 1, &source, NULL);
|
||||
glCompileShader(shader);
|
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
GLchar infoLog[512];
|
||||
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
||||
std::cerr << "shader compilation failed" << std::endl
|
||||
<< infoLog << std::endl;
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Set up GLFW, OpenGL and GLEW.
|
||||
if (!glfwInit())
|
||||
return -1;
|
||||
|
||||
|
@ -58,16 +104,68 @@ int main()
|
|||
return -1;
|
||||
}
|
||||
|
||||
// VAO, VBO
|
||||
float vertices[] =
|
||||
{
|
||||
-0.5, -0.5, 0.0, // left
|
||||
0.5, -0.5, 0.0, // right
|
||||
0.0, 0.5, 0.0 // top
|
||||
};
|
||||
|
||||
GLuint VBO, VAO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
// Shaders
|
||||
GLuint vertShader = compileShader("./vert.glsl", GL_VERTEX_SHADER);
|
||||
GLuint fragShader = compileShader("./frag.glsl", GL_FRAGMENT_SHADER);
|
||||
|
||||
GLuint shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, vertShader);
|
||||
glAttachShader(shaderProgram, fragShader);
|
||||
glLinkProgram(shaderProgram);
|
||||
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
|
||||
std::cerr << "shader linking failed" << std::endl
|
||||
<< infoLog << std::endl;
|
||||
}
|
||||
|
||||
// We no longer need the individual shaders
|
||||
glDeleteShader(vertShader);
|
||||
glDeleteShader(fragShader);
|
||||
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClearColor(0.2, 0.3, 0.3, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glUseProgram(shaderProgram);
|
||||
glBindVertexArray(VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue