#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define SCR_WIDTH 800
#define SCR_HEIGHT 800
// Vertex Shader source code
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"
"}\n\0";
// Fragment Shader source code
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(0.8f, 0.3f, 0.02f, 1.0f);\n"
"}\n\0";
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
// CORE profile = modern OpenGL functions
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "OpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window (glfwCreateWindow)\n";
glfwTerminate();
return -1;
}
// Connect the window to the current context
glfwMakeContextCurrent(window);
gladLoadGL();
// Set viewport of OpenGL in the Window
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
// Create Vertex Shader Object
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
// Attach Vertex Shader source to Vertex Shader Object
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
// Compile the Vertex Shader into machine code
glCompileShader(vertexShader);
// Create Fragment Shader Object
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
// Attach Fragment Shader source to Fragment Shader Object
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
// Compile the Fragment Shader into machine code
glCompileShader(fragmentShader);
// Create Shader Program Object
GLuint shaderProgram = glCreateProgram();
// Attach the Vertex and Fragment Shaders to Shader Program
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
// Link all the shaders together into the Shader Program
glLinkProgram(shaderProgram);
// After Attach and Link Delete the Vertex and Fragment Shader objects
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
GLfloat vertices[] =
{
-0.5f , -0.5f * float(sqrt(3)) / 3 , 0.0f, // Lower left corner
0.5f , -0.5f * float(sqrt(3)) / 3 , 0.0f, // Lower right corner
0.0f , 0.5f * float(sqrt(3)) * 2 / 3 , 0.0f, // Upper corner
-0.5f / 2 , 0.5f * float(sqrt(3)) / 6 , 0.0f, // Inner left
0.5f / 2 , 0.5f * float(sqrt(3)) / 6 , 0.0f, // Inner right
0.0f , -0.5f * float(sqrt(3)) / 3 , 0.0f // Inner down
};
// Indices for vertices
GLuint indices[] =
{
0, 3, 5, // Lower left triangle
3, 2, 4, // Upper triangle
5, 4, 1 // Lower right triangle
};
// Create a reference for the Vartex Array Object (VAO) ,Vertex Buffer Object (VBO), and Element Buffer Object (EBO)
GLuint VAO, VBO, EBO;
// Generate the VAO, VBO, and EBO with only 1 object each
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// Make the VAO the current Vertex Array Object
glBindVertexArray(VAO);
// Bind the VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// Conect the vertices into the VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Bind the EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// Connect the indices to the EBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Configure the Vertex Attribute to let OpenGL know how to read the VBO
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
// Enable the Vertex Attribute to let OpenGL know that he needs to use it
glEnableVertexAttribArray(0);
// Bind the VBO and VAO and EBO after VAO to 0 (unbind it) to prevent accidentally modifying the VAO, VBO and EBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // EBO
while (!glfwWindowShouldClose(window)) {
// Specify the background color
glClearColor(0.3f, 0.4f, 0.5f, 1.0f);
// Clean the back buffer and assign the background color to it
glClear(GL_COLOR_BUFFER_BIT);
// Tell OpenGL to use the Shader Program Object we create (line 64)
glUseProgram(shaderProgram);
// Bind the VAO to let OpenGL know to use it
glBindVertexArray(VAO);
// Draw the 3 triangles in the shape of 1 big triangle with an empty triangle in the middle
glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, 0);
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
glfwPollEvents();
}
// Delete all the objects
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteProgram(shaderProgram);
glfwDestroyWindow(window);
glfwTerminate();
}
/*
run:
*/
