Skip to content

Instantly share code, notes, and snippets.

@GermanHoyos
Last active April 5, 2025 16:02
Show Gist options
  • Save GermanHoyos/f1897fc3ad15d9fc34efa6fc4779182b to your computer and use it in GitHub Desktop.
Save GermanHoyos/f1897fc3ad15d9fc34efa6fc4779182b to your computer and use it in GitHub Desktop.
Core Mode OpenGL Single Triangle
// Written by German Adrian Hoyos
// Linkedin: https://www.linkedin.com/in/adrian-i-81a32615b/
#include "../include/masterheader.h" // Common c++ headers + custom headers
// Declared in "shapes2D.h"
// float triangle2D[] = {
// -0.5f, -0.5f, 0.0f,
// 0.5f, -0.5f, 0.0f,
// 0.0f, 0.5f, 0.0f
// };
/*******************
** VERTEX SHADER **
** START **
** **
*******************/
const char *vertexShaderSource =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
/*******************
** VERTEX SHADER **
** END **
** **
*******************/
/*******************
** FRAGMENT SHADER**
** START **
** **
*******************/
const char *fragmentShaderSource =
"#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\0";
/*******************
** FRAGMENT SHADER**
** END **
** **
*******************/
int main()
{
GLFWwindow* window = initializeOpenGLWindow(800, 600, "OpenGL Program"); if (!window) return -1;
/*******************
** VERTEX SHADER **
** START **
** **
*******************/
// In order to use the *vertexShaderSource compiled at runtime we need to
// make a shader object and attach it to the *vertexShaderSource
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); // 1 = 1 string *vertexShaderSource
glCompileShader(vertexShader);
// Check if glCompilerShader worked
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" <<
infoLog << std::endl;
}
/*******************
** VERTEX SHADER **
** END **
** **
*******************/
/*******************
** FRAGMENT SHADER**
** START **
** **
*******************/
// In order to use the *fragmentShaderSource compiled at runtime we need to
// make a shader object and attach it to the *fragmentShaderSource
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// Create a fragment shader program
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
// Link this fragment shader program to the vertex shader above and to this currently running program
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// Check to make sure that the fragment shader was successfully made
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
cout << "fragment shader compile failed\n" << infoLog << std::endl;
}
/*******************
** FRAGMENT SHADER**
** END **
** **
*******************/
// Vertex buffer objects can store a large number of vertices in the GPU’s memory
unsigned int VBO;
unsigned int VAO; // Vertex Array Object to store and manage multiple VBOs
glGenVertexArrays(1, &VAO); // Generate a Vertex Array Object (VAO) to store VBOs and their configurations
glBindVertexArray(VAO);
glGenBuffers(1, &VBO); // Generate a buffer
glBindBuffer(GL_ARRAY_BUFFER, VBO); // Use bound VBO buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle2D), triangle2D, GL_STATIC_DRAW); // Fill buffer with my vertices
// Tell OpenGL how it should interpret the vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Delete shaders as they are now linked into the program
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// ^^^
// Interesting so in essense, I create objects kinda as a blueprint,
// then I pass those objects to a shader program which then makes a
// shader based on said objects and then I delete the original objects....
// so like a c++ class but backwards
/******************************************************************************************
** MAIN LOOP *************************************************************************
** START *************************************************************************
** *************************************************************************
*******************************************************************************************/
while (!glfwWindowShouldClose(window))
{
// Handle events such as resize or close
processInput(window);
// Clear screen and draw this color with this alpha
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw traingle2D
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
// Draw front buffer while back buffer is built
glfwSwapBuffers(window);
// Listen to events such as resize or close
glfwPollEvents();
}
/******************************************************************************************
** MAIN LOOP *************************************************************************
** END *************************************************************************
** *************************************************************************
*******************************************************************************************/
// Clean up memory
glfwTerminate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment