1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <iostream>
// GLAD
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
// GLFW
#include <GLFW/glfw3.h>
// This example is taken from http://learnopengl.com/
// http://learnopengl.com/code_viewer.php?code=getting-started/hellowindow2
// The code originally used GLEW, I replaced it with Glad
// Compile:
// g++ example/c++/hellowindow2.cpp -Ibuild/include build/src/glad.c -lglfw -ldl
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
#ifdef GLAD_OPTION_GL_DEBUG
// Define a custom callback for demonstration purposes
void pre_gl_call(const char *name, void *funcptr, int len_args, ...) {
#ifdef GLAD_OPTION_GL_MX
printf("Current GL Context: %p -> ", gladGetGLContext());
#endif
printf("Calling: %s at %p (%d arguments)\n", name, funcptr, len_args);
}
#endif
// The MAIN function, from here we start the application and run the game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// 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", NULL, NULL);
glfwMakeContextCurrent(window);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
#ifdef GLAD_OPTION_GL_LOADER
printf("Using internal loader.\n");
#endif
#ifdef GLAD_OPTION_GL_MX
GladGLContext context = {};
#ifdef GLAD_OPTION_GL_LOADER
int version = gladLoaderLoadGLContext(&context);
#else
int version = gladLoadGLContext(&context, glfwGetProcAddress);
#endif
#else
#ifdef GLAD_OPTION_GL_LOADER
int version = gladLoaderLoadGL();
#else
int version = gladLoadGL(glfwGetProcAddress);
#endif
#endif
if (version == 0)
{
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
std::cout << "Loaded OpenGL " << GLAD_VERSION_MAJOR(version) << "." << GLAD_VERSION_MINOR(version) << std::endl;
#ifdef GLAD_OPTION_GL_DEBUG
// before every opengl call call pre_gl_call
glad_set_gl_pre_callback(pre_gl_call);
// don't use the callbacks for glClear and glClearColor
#ifdef GLAD_OPTION_GL_MX_GLOBAL
glad_debug_glClear = gladGetGLContext()->Clear;
glad_debug_glClearColor = gladGetGLContext()->ClearColor;
#else
glad_debug_glClear = glad_glClear;
glad_debug_glClearColor = glad_glClearColor;
#endif
#endif
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminates 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);
}
|