#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 into 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, 0.0f, // Lower left corner
0.5f, -0.5f, 0.0f, // Lower right corner
-0.5f, 0.5f, 0.0f, // Upper left corner
-0.5f, 0.5f, 0.0f, // Upper left corner
0.5f, 0.5f, 0.0f, // Upper right corner
0.5f, -0.5f, 0.0f // Lower right corner
};
// Create reference for the Vartex Array Object (VAO) and Vertex Buffer Object (VBO)
GLuint VAO, VBO;
// Generate 1 VAO object and 1 VBO object
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// 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);
// Configure the Vertex Attribute to let OpenGL to 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 to 0 to prevent accidentally modifying the VAO and VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
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 63)
glUseProgram(shaderProgram);
// Bind the VAO to let OpenGL know to use it
glBindVertexArray(VAO);
// Draw a square using 2 primitive GL_TRIANGLES
glDrawArrays(GL_TRIANGLES, 0, 6);
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
glfwPollEvents();
}
// Delete all the objects
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
glfwDestroyWindow(window);
glfwTerminate();
}
/*
run:
*/