Draw a triangle!

This commit is contained in:
Fries 2024-03-09 17:14:56 -08:00
parent 482e000e54
commit 91b636e365
4 changed files with 150 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/build
/builddir
.cache

33
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/builddir/src/main",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild"
}

View file

@ -4,6 +4,27 @@
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void process_input(GLFWwindow* window);
int check_shader_compilation_status(unsigned int shaderId);
const char* vertexShaderCode = R"(
// Set the GLSL version to 3.3 and use the OpenGL core profile
#version 330 core
layout (location = 0) in vec3 aPos;
void main() {
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
)";
const char* fragmentShaderCode = R"(
// Set the GLSL version to 3.3 and use the OpenGL core profile
#version 330 core
out vec4 FragColor;
void main() {
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
)";
int main() {
glfwInit();
@ -39,6 +60,85 @@ int main() {
// Register a callback for window size changes.
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
unsigned int vertexShader;
// Create a OpenGL shader object.
vertexShader = glCreateShader(GL_VERTEX_SHADER);
// Pass the source code to the state.
glShaderSource(vertexShader, 1, &vertexShaderCode, nullptr);
// Compile the shader
glCompileShader(vertexShader);
int success;
char infoLog[512];
// Check if the shader successfully compiled.
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!check_shader_compilation_status(vertexShader)) {
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILIATION_FAILED\n" << infoLog << std::endl;
}
unsigned int fragmentShader;
// Create a OpenGL Shader object.
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
// Give the object the source code of the shader, sent with a single string.
glShaderSource(fragmentShader, 1, &fragmentShaderCode, NULL);
// Compile the shader.
glCompileShader(fragmentShader);
if (!check_shader_compilation_status(fragmentShader)) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
unsigned int shaderProgram;
// Create a OpenGL Shader Program object.
shaderProgram = glCreateProgram();
// Attach the Vertex Shader to the Shader Program.
glAttachShader(shaderProgram, vertexShader);
// Attach the Fragment Shader to the Shader Program.
glAttachShader(shaderProgram, fragmentShader);
// Link the Shader Program.
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::LINKING::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// An array of verticies containing data for a triangle.
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
unsigned int vertexBufferObject, vertexArrayObject;
// Create a Vertex Array Object.
glGenVertexArrays(1, &vertexArrayObject);
// Create a Vertex Buffer Object and write the id to the vertexBufferObject variable.
glGenBuffers(1, &vertexBufferObject);
// Bind the vertex array object so the attributes are stored in that object.
glBindVertexArray(vertexArrayObject);
// Bind the GL_ARRAY_BUFFER target to the verterBufferObject object id.
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
// Copy the verticies data into the vertexBufferObject object and tell the GPU it will only be written once but read many times.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Tell OpenGL how to process the Vertex attributes so it can pass it to the Vertex shader.
// This will pass the vertex to the location "0". We tell OpenGL the size of the array is 3 elements.
// We tell OpenGL that we're using floating point types.
// We tell OpenGL we don't want our data to be normalized as its already normalized.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Create a render loop, which keeps the program open until glfw tells the loop that the window should close.
while (!glfwWindowShouldClose(window)) {
// Process input.
@ -49,6 +149,13 @@ int main() {
// Clear the screen and use the color from the state.
glClear(GL_COLOR_BUFFER_BIT);
// Use the shaderProgram shader program for drawing verticies.
glUseProgram(shaderProgram);
// Bind the state to use the vertexArrayObject object so OpenGL knows what to do with the verticies.
glBindVertexArray(vertexArrayObject);
// Draw the verticies.
glDrawArrays(GL_TRIANGLES, 0, 3);
// This swaps completely drawn frames from the "second buffer" to the front one which is displayed on the screen.
glfwSwapBuffers(window);
// Check if any events are triggered.
@ -69,3 +176,9 @@ void process_input(GLFWwindow* window) {
glfwSetWindowShouldClose(window, true);
}
}
int check_shader_compilation_status(unsigned int shaderId) {
int success;
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
return success;
}