You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.9 KiB
108 lines
2.9 KiB
// |
|
// Created by olivier on 5/3/22. |
|
// |
|
|
|
#include <iostream> |
|
|
|
#include <glad/glad.h> |
|
#include <GLFW/glfw3.h> |
|
#include "src/Shader.h" |
|
|
|
|
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height); |
|
|
|
int main() { |
|
// Initialize GLFW |
|
glfwInit(); |
|
// Set window hints |
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); |
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); |
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); |
|
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); |
|
|
|
//Create the window |
|
GLFWwindow *window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr); |
|
if(window == nullptr) { |
|
std::cout << "Failed to create GLFW window" << std::endl; |
|
glfwTerminate(); |
|
return -1; |
|
} |
|
glfwMakeContextCurrent(window); |
|
|
|
// Setup GLAD |
|
if(!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { |
|
std::cout << "Failed to initialize GLAD" << std::endl; |
|
return -1; |
|
} |
|
|
|
// Setup viewport |
|
glViewport(0, 0, 800, 600); |
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); |
|
|
|
//// Setup geometry |
|
float vertices[] = { |
|
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, // top right |
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right |
|
-0.5f, -0.5f, 0.0f,0.0f, 0.0f, 1.0f, // bottom left |
|
-0.5f, 0.5f, 0.0f,1.0f, 0.0f, 0.0f // top left |
|
}; |
|
unsigned int indices[] = { |
|
0, 1, 3, // first triangle |
|
1, 2, 3 // second triangle |
|
}; |
|
|
|
unsigned int VBO, VAO, EBO; |
|
|
|
|
|
//generate buffers |
|
glGenBuffers(1, &VBO); |
|
glGenVertexArrays(1, &VAO); |
|
glGenBuffers(1, &EBO); |
|
|
|
//Bind buffers |
|
glBindBuffer(GL_ARRAY_BUFFER, VBO); |
|
glBindVertexArray(VAO); |
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); |
|
|
|
//copy data to buffers |
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); |
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); |
|
|
|
//SetVertexAttribPointer |
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr); |
|
glEnableVertexAttribArray(0); |
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float))); |
|
glEnableVertexAttribArray(1); |
|
|
|
|
|
//Create shaders |
|
Shader ourShader("src/shaders/shader.vert", "src/shaders/shader.frag"); |
|
ourShader.use(); |
|
|
|
|
|
|
|
|
|
// Render loop |
|
while(!glfwWindowShouldClose(window)) { |
|
// Render |
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); |
|
glClear(GL_COLOR_BUFFER_BIT); |
|
|
|
ourShader.use(); |
|
glBindVertexArray(VAO); |
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); |
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); |
|
|
|
|
|
// Swap buffers |
|
glfwSwapBuffers(window); |
|
glfwPollEvents(); |
|
} |
|
glfwTerminate(); |
|
return 0; |
|
} |
|
|
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { |
|
glViewport(0, 0, width, height); |
|
} |