2022-08-07 12:10:12 +08:00
|
|
|
#include <iostream>
|
|
|
|
// GLEW
|
|
|
|
#define GLEW_STATIC
|
|
|
|
#include <GL/glew.h>
|
|
|
|
// GLFW
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
// Other includes
|
|
|
|
#include "shader.h"
|
|
|
|
|
|
|
|
// Function prototypes
|
|
|
|
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
|
|
|
|
|
|
|
|
// Window dimensions
|
|
|
|
const GLuint WIDTH = 800, HEIGHT = 600;
|
|
|
|
|
|
|
|
// The MAIN function, from here we start the application and run the game loop
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Init GLFW
|
|
|
|
glfwInit();
|
|
|
|
// Set all the required options for GLFW
|
|
|
|
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 a GLFWwindow object that we can use for GLFW's functions
|
|
|
|
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
|
|
|
|
// Set the required callback functions
|
|
|
|
glfwSetKeyCallback(window, key_callback);
|
|
|
|
|
|
|
|
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
|
|
|
|
glewExperimental = GL_TRUE;
|
|
|
|
// Initialize GLEW to setup the OpenGL Function pointers
|
|
|
|
glewInit();
|
|
|
|
|
|
|
|
// Define the viewport dimensions
|
|
|
|
glViewport(0, 0, WIDTH, HEIGHT);
|
|
|
|
|
|
|
|
|
|
|
|
Shader ourShader("shader.vs", "shader.frag");
|
|
|
|
|
|
|
|
GLfloat vertices[] = {
|
|
|
|
// Positions // Colors
|
|
|
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Right
|
|
|
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // Bottom Left
|
|
|
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // Top
|
|
|
|
};
|
2023-02-22 13:38:31 +08:00
|
|
|
GLuint VBO, VAO;
|
|
|
|
glGenVertexArrays(1, &VAO);
|
|
|
|
glGenBuffers(1, &VBO);
|
2022-08-07 12:10:12 +08:00
|
|
|
|
2023-02-22 13:38:31 +08:00
|
|
|
glBindVertexArray(VAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
2022-08-07 12:10:12 +08:00
|
|
|
|
2023-02-22 13:38:31 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
2022-08-07 12:10:12 +08:00
|
|
|
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
|
2023-02-22 13:38:31 +08:00
|
|
|
glEnableVertexAttribArray(0);
|
2022-08-07 12:10:12 +08:00
|
|
|
|
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
|
2023-02-22 13:38:31 +08:00
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
2022-08-07 12:10:12 +08:00
|
|
|
|
|
|
|
// Game loop
|
|
|
|
while (!glfwWindowShouldClose(window))
|
|
|
|
{
|
|
|
|
glfwPollEvents();
|
|
|
|
|
|
|
|
// Render
|
|
|
|
// Clear the colorbuffer
|
2023-02-22 13:38:31 +08:00
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2022-08-07 12:10:12 +08:00
|
|
|
|
|
|
|
// Draw the triangle
|
2023-02-22 13:38:31 +08:00
|
|
|
ourShader.Use();
|
|
|
|
glBindVertexArray(VAO);
|
2022-08-07 12:10:12 +08:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
2023-02-22 13:38:31 +08:00
|
|
|
glBindVertexArray(0);
|
2022-08-07 12:10:12 +08:00
|
|
|
|
|
|
|
// Swap the screen buffers
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
}
|
|
|
|
// Properly de-allocate all resources once they've outlived their purpose
|
|
|
|
glDeleteVertexArrays(1, &VAO);
|
|
|
|
glDeleteBuffers(1, &VBO);
|
|
|
|
// Terminate GLFW, clearing any resources allocated by GLFW.
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Is called whenever a key is pressed/released via GLFW
|
|
|
|
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
|
|
|
|
{
|
|
|
|
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
|
|
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
|
|
|
}
|