generated from karl/cpp-template
the example from https://learnopengl.com/Advanced-OpenGL/Geometry-Shader is functional; built with SCons.
109 lines
3.6 KiB
C++
109 lines
3.6 KiB
C++
|
|
#include <glad/glad.h>
|
|
|
|
// Needs to be second
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include "Shader.h"
|
|
|
|
#include <iostream>
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
|
|
|
// settings
|
|
const unsigned int SCR_WIDTH = 800;
|
|
const unsigned int SCR_HEIGHT = 600;
|
|
|
|
int main() {
|
|
// glfw: initialize and configure
|
|
// ------------------------------
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
#ifdef __APPLE__
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
#endif
|
|
|
|
// glfw window creation
|
|
// --------------------
|
|
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
|
if (window == NULL) {
|
|
std::cout << "Failed to create GLFW window" << std::endl;
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
glfwMakeContextCurrent(window);
|
|
|
|
// glad: load all OpenGL function pointers
|
|
// ---------------------------------------
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
// configure global opengl state
|
|
// -----------------------------
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
// build and compile shaders
|
|
// -------------------------
|
|
Shader shader("Shader/mc.vs", "Shader/mc.fs", "Shader/mc.gs");
|
|
|
|
// set up vertex data (and buffer(s)) and configure vertex attributes
|
|
// ------------------------------------------------------------------
|
|
float points[] = {
|
|
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // top-left
|
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // top-right
|
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // bottom-right
|
|
-0.5f, -0.5f, 1.0f, 1.0f, 0.0f // bottom-left
|
|
};
|
|
unsigned int VBO, VAO;
|
|
glGenBuffers(1, &VBO);
|
|
glGenVertexArrays(1, &VAO);
|
|
glBindVertexArray(VAO);
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(points), &points, GL_STATIC_DRAW);
|
|
glEnableVertexAttribArray(0);
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
|
|
glEnableVertexAttribArray(1);
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(2 * sizeof(float)));
|
|
glBindVertexArray(0);
|
|
|
|
// render loop
|
|
// -----------
|
|
while (!glfwWindowShouldClose(window)) {
|
|
// render
|
|
// ------
|
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
// draw points
|
|
shader.use();
|
|
glBindVertexArray(VAO);
|
|
glDrawArrays(GL_POINTS, 0, 4);
|
|
|
|
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
|
// -------------------------------------------------------------------------------
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
// optional: de-allocate all resources once they've outlived their purpose:
|
|
// ------------------------------------------------------------------------
|
|
glDeleteVertexArrays(1, &VAO);
|
|
glDeleteBuffers(1, &VBO);
|
|
|
|
glfwTerminate();
|
|
return 0;
|
|
}
|
|
|
|
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
|
// ---------------------------------------------------------------------------------------------
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
|
// make sure the viewport matches the new window dimensions; note that width and
|
|
// height will be significantly larger than specified on retina displays.
|
|
glViewport(0, 0, width, height);
|
|
}
|