Skip to content

Instantly share code, notes, and snippets.

@Krymancer
Last active August 29, 2023 19:25
Show Gist options
  • Save Krymancer/7007b511563302ee11fbe356743574eb to your computer and use it in GitHub Desktop.
Save Krymancer/7007b511563302ee11fbe356743574eb to your computer and use it in GitHub Desktop.
Rotate Cube OpenGL
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
const char* vertexShaderSource = R"(
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0);
})";
const char* fragmentShaderSource = R"(
#version 330 core
out vec4 FragColor;
void main() {
FragColor = vec4(1.0, 0.0, 0.0, 0.5);
})";
int main() {
glfwInit(); // init the glwf lib
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // sets the version of glfw hint
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // sets the version of glfw hint
GLFWwindow* window = glfwCreateWindow(800, 600, "Rotating Cube", nullptr, nullptr); // create a window for us
glfwMakeContextCurrent(window); // set the window as the context in openGL
glewInit(); // init the glew lib
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); // Create a vertextShader
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr); // Sets the string source as the source of the vertext shadder
glCompileShader(vertexShader); // Compile the shadder
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); // Create a framentShadder
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr); // Sets the string source as the source of the fragment shadder
glCompileShader(fragmentShader); // compile the shadder
GLuint shaderProgram = glCreateProgram(); // Create a Shader Program
glAttachShader(shaderProgram, vertexShader); // Attatch the vertex shader
glAttachShader(shaderProgram, fragmentShader); // Attach the framentShader
glLinkProgram(shaderProgram); // Link the shadder
glDeleteShader(vertexShader); // As we now have the compiled linked shadder we don't need the vertext or fragment shadder
glDeleteShader(fragmentShader);
// Create vertices for the cube
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f
};
// Create indices for the cube
GLuint indices[] = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4,
0, 1, 5, 5, 4, 0,
2, 3, 7, 7, 6, 2,
1, 2, 6, 6, 5, 1,
4, 7, 3, 3, 0, 4
};
// Create VBO, VAO and EBO ids
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO); // Get the vertex array
glGenBuffers(1, &VBO); // Get the vertex buffer
glGenBuffers(1, &EBO); // Get the element buffer
glBindVertexArray(VAO); // bind the vertex array to opengl
glBindBuffer(GL_ARRAY_BUFFER, VBO); // bind the verter buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // set the vertices data into the buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); // bind the element buffer
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // set the indices data into the buffer
// Config to use the buffers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Enable depth
glEnable(GL_DEPTH_TEST);
// Render loop
while (!glfwWindowShouldClose(window)) {
// Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use the shader we compile
glUseProgram(shaderProgram);
// Rotate the cube
// this is a bunch of complexes matrix operations so we use a lib for this
glm::mat4 model = glm::rotate(glm::mat4(1.0f), (GLfloat)glfwGetTime(), glm::vec3(0.5f, 1.0f, 0.0f)); // rotate the cube
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); // consider camera view
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); // deals with aspect ratio distorcion
// Shadder stuff
GLuint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLuint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLuint projectionLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
// Bind the vertex array to the context
glBindVertexArray(VAO);
// Draw Cube
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
// Unbind the vertex array
glBindVertexArray(0);
// Unbind the shader
glUseProgram(0);
// Free buffers and do some backendsutff in openGL
glfwSwapBuffers(window);
glfwPollEvents();
}
// Delete everthing
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
// Get rid of openGL and screen
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment