#include "GLFW/glfw3.h" #include "GLProgramLoader.hh" #include "Quad.hh" #include #include #include #include #include #include #include // This has to come last. #define GLAD_GL_IMPLEMENTATION #include #include #include #include #include static void dbg_log(const char* str) { std::cout << str << std::endl; } void GLAPIENTRY MessageCallback( GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam ) { fprintf( stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = \n\t%s\n", ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ), type, severity, message ); } static void key_callback(GLFWwindow *w, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { glfwSetWindowShouldClose(w, GL_TRUE); } } #define VERTEX_SHADER_LOCATION "shaders/shader.vert" #define FRAGMENT_SHADER_LOCATION "shaders/shader.frag" GLuint g_program_id = 0; static GLuint translate_unif; static GLuint rotate_unif; static void init_shaders() { dbg_log("Initialising shaders"); const std::vector> shaders = { {GL_VERTEX_SHADER, VERTEX_SHADER_LOCATION}, {GL_FRAGMENT_SHADER, FRAGMENT_SHADER_LOCATION}, }; try { g_program_id = compile_and_link_program(shaders); } catch (GLProgramException e) { std::cerr << e.what() << std::endl; } catch (...) { std::cerr << "Unexpected exception while compiling shaders." << std::endl; } glUseProgram(g_program_id); translate_unif = glGetUniformLocation(g_program_id, "world_pos"); rotate_unif = glGetUniformLocation(g_program_id, "rotation"); } static GLuint vertex_buf[2]; static GLuint array_buf[2]; static const GLuint vertex_data_position = 0; static Quad *o1; static std::array verts({ // Tri 2 {0.0f, 0.0f}, {0.5f, 0.0f}, {0.5f, 0.5f}, // Tri 1 {0.0f, 0.0f}, {0.5f, 0.5f}, {0.0f, 0.5f}, }); // Do the work of initialising opengl so we can draw stuff static void init_opengl() { dbg_log("Initialising opengl"); // During init, enable debug output glEnable ( GL_DEBUG_OUTPUT ); glDebugMessageCallback( MessageCallback, 0 ); // Configure the context's capabilities glClearColor(0.0, 0.0, 0.0, 1.0); init_shaders(); o1 = new Quad(std::span(verts), (Uniform){.world_pos = translate_unif, .rotation = rotate_unif}); } int main() { dbg_log("Initialising glfw"); // Initialise GLFW if (!glfwInit()) { exit(EXIT_FAILURE); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Create our context GLFWwindow *w = glfwCreateWindow(640, 480, "Test", NULL, NULL); if (!w) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(w); glfwSwapInterval(1); // Essentially vsync gladLoadGL(glfwGetProcAddress); // Load openGL using glfw // Set up the key handler glfwSetKeyCallback(w, key_callback); init_opengl(); // Main Loop while(!glfwWindowShouldClose(w)) { glClear(GL_COLOR_BUFFER_BIT); o1->draw(); glfwSwapBuffers(w); glfwPollEvents(); } delete(o1); glfwDestroyWindow(w); glfwTerminate(); exit(EXIT_SUCCESS); }