[cfda3b2] | 1 | #include <iostream>
|
---|
| 2 |
|
---|
| 3 | // GLEW
|
---|
| 4 | #define GLEW_STATIC
|
---|
| 5 | #include <GL/glew.h>
|
---|
| 6 |
|
---|
| 7 | // GLFW
|
---|
| 8 | #include <GLFW/glfw3.h>
|
---|
| 9 |
|
---|
[15c7ed9] | 10 | #if defined(__linux__)
|
---|
| 11 | #define LINUX
|
---|
[4d52702] | 12 | #elif defined(_WIN32)
|
---|
| 13 | #define WINDOWS
|
---|
[15c7ed9] | 14 | #elif defined(__APPLE__)
|
---|
| 15 | #define MAC
|
---|
[5540132] | 16 | #endif
|
---|
| 17 |
|
---|
[b33f87b] | 18 | #define FULLSCREEN false
|
---|
| 19 |
|
---|
[cfda3b2] | 20 | using namespace std;
|
---|
| 21 |
|
---|
| 22 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
|
---|
| 23 |
|
---|
[fe70cd3] | 24 | GLint compileVertexShader(const GLchar**);
|
---|
| 25 | GLint compileFragmentShader(const GLchar**);
|
---|
| 26 |
|
---|
| 27 | GLint linkShaderProgram(initializer_list<GLint>);
|
---|
| 28 |
|
---|
[cfda3b2] | 29 | const GLuint WIDTH = 800, HEIGHT = 600;
|
---|
| 30 |
|
---|
[fe70cd3] | 31 | // Shaders
|
---|
| 32 | const GLchar* vertexShaderSource = "#version 330 core\n"
|
---|
| 33 | "layout (location = 0) in vec3 position;\n"
|
---|
| 34 | "void main()\n"
|
---|
| 35 | "{\n"
|
---|
| 36 | "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
|
---|
| 37 | "}\0";
|
---|
| 38 | const GLchar* fragmentShaderSource = "#version 330 core\n"
|
---|
| 39 | "out vec4 color;\n"
|
---|
| 40 | "void main()\n"
|
---|
| 41 | "{\n"
|
---|
| 42 | "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
|
---|
| 43 | "}\n\0";
|
---|
| 44 |
|
---|
[cfda3b2] | 45 | int main(int argc, char* argv[]) {
|
---|
| 46 | cout << "Starting OpenGL game..." << endl;
|
---|
| 47 |
|
---|
| 48 | cout << "Starting GLFW context, OpenGL 3.3" << endl;
|
---|
| 49 | // Init GLFW
|
---|
| 50 | glfwInit();
|
---|
| 51 |
|
---|
| 52 | // Set all the required options for GLFW
|
---|
| 53 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 54 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 55 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 56 | glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
---|
| 57 |
|
---|
[4d52702] | 58 | #ifdef MAC // required in OSX
|
---|
[5540132] | 59 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 60 | #endif
|
---|
| 61 |
|
---|
[cfda3b2] | 62 | // Create a GLFWwindow object that we can use for GLFW's functions
|
---|
[b33f87b] | 63 | GLFWmonitor* monitor = FULLSCREEN ? glfwGetPrimaryMonitor() : nullptr;
|
---|
| 64 |
|
---|
| 65 | GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", monitor, nullptr);
|
---|
[cfda3b2] | 66 | glfwMakeContextCurrent(window);
|
---|
| 67 | if (window == NULL) {
|
---|
| 68 | cout << "Failed to create GLFW window" << endl;
|
---|
| 69 | glfwTerminate();
|
---|
[b33f87b] | 70 | return EXIT_FAILURE;
|
---|
[cfda3b2] | 71 | }
|
---|
| 72 |
|
---|
| 73 | // Set the required callback functions
|
---|
| 74 | glfwSetKeyCallback(window, key_callback);
|
---|
| 75 |
|
---|
| 76 | // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
|
---|
| 77 | glewExperimental = GL_TRUE;
|
---|
| 78 | // Initialize GLEW to setup the OpenGL Function pointers
|
---|
| 79 | if (glewInit() != GLEW_OK) {
|
---|
| 80 | cout << "Failed to initialize GLEW" << endl;
|
---|
[b33f87b] | 81 | return EXIT_FAILURE;
|
---|
[cfda3b2] | 82 | }
|
---|
| 83 |
|
---|
| 84 | // Define the viewport dimensions
|
---|
| 85 | int width, height;
|
---|
| 86 | glfwGetFramebufferSize(window, &width, &height);
|
---|
| 87 | glViewport(0, 0, width, height);
|
---|
| 88 |
|
---|
[fe70cd3] | 89 | GLint vertexShader = compileVertexShader(&vertexShaderSource);
|
---|
| 90 | GLint fragmentShader = compileFragmentShader(&fragmentShaderSource);
|
---|
| 91 |
|
---|
| 92 | GLint shaderProgram = linkShaderProgram({vertexShader, fragmentShader});
|
---|
| 93 |
|
---|
| 94 | glDeleteShader(vertexShader);
|
---|
| 95 | glDeleteShader(fragmentShader);
|
---|
| 96 |
|
---|
| 97 | // Set up vertex data (and buffer(s)) and attribute pointers
|
---|
| 98 | //GLfloat vertices[] = {
|
---|
| 99 | // // First triangle
|
---|
| 100 | // 0.5f, 0.5f, // Top Right
|
---|
| 101 | // 0.5f, -0.5f, // Bottom Right
|
---|
| 102 | // -0.5f, 0.5f, // Top Left
|
---|
| 103 | // // Second triangle
|
---|
| 104 | // 0.5f, -0.5f, // Bottom Right
|
---|
| 105 | // -0.5f, -0.5f, // Bottom Left
|
---|
| 106 | // -0.5f, 0.5f // Top Left
|
---|
| 107 | //};
|
---|
| 108 | GLfloat vertices[] = {
|
---|
| 109 | 0.5f, 0.5f, 0.0f, // Top Right
|
---|
| 110 | 0.5f, -0.5f, 0.0f, // Bottom Right
|
---|
| 111 | -0.5f, 0.5f, 0.0f, // Top Left
|
---|
| 112 | 0.5f, -0.5f, 0.0f, // Bottom Right
|
---|
| 113 | -0.5f, -0.5f, 0.0f, // Bottom Left
|
---|
| 114 | -0.5f, 0.5f, 0.0f // Top Left
|
---|
| 115 | };
|
---|
| 116 | GLuint indices[] = { // Note that we start from 0!
|
---|
| 117 | 0, 1, 3, // First Triangle
|
---|
| 118 | 1, 2, 3 // Second Triangle
|
---|
| 119 | };
|
---|
| 120 | GLuint VBO, VAO, EBO;
|
---|
| 121 | glGenVertexArrays(1, &VAO);
|
---|
| 122 | glGenBuffers(1, &VBO);
|
---|
| 123 | //glGenBuffers(1, &EBO);
|
---|
| 124 | // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
|
---|
| 125 | glBindVertexArray(VAO);
|
---|
| 126 |
|
---|
| 127 | glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
---|
| 128 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
---|
| 129 |
|
---|
| 130 | //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
---|
| 131 | //glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
---|
| 132 |
|
---|
| 133 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), (GLvoid*)0);
|
---|
| 134 | glEnableVertexAttribArray(0);
|
---|
| 135 |
|
---|
| 136 | glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
|
---|
| 137 |
|
---|
| 138 | glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
|
---|
| 139 |
|
---|
| 140 | // Uncommenting this call will result in wireframe polygons.
|
---|
| 141 | //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
---|
| 142 |
|
---|
[cfda3b2] | 143 | // Game loop
|
---|
| 144 | while (!glfwWindowShouldClose(window)) {
|
---|
| 145 | // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
|
---|
| 146 | glfwPollEvents();
|
---|
| 147 |
|
---|
| 148 | // Render
|
---|
| 149 | // Clear the colorbuffer
|
---|
| 150 | glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
---|
| 151 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
| 152 |
|
---|
[fe70cd3] | 153 | // Draw our first triangle
|
---|
| 154 | glUseProgram(shaderProgram);
|
---|
| 155 | glBindVertexArray(VAO);
|
---|
| 156 | glDrawArrays(GL_TRIANGLES, 0, 6);
|
---|
| 157 | //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
---|
| 158 | glBindVertexArray(0);
|
---|
| 159 |
|
---|
[cfda3b2] | 160 | // Swap the screen buffers
|
---|
| 161 | glfwSwapBuffers(window);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[fe70cd3] | 164 | // Properly de-allocate all resources once they've outlived their purpose
|
---|
| 165 | glDeleteVertexArrays(1, &VAO);
|
---|
| 166 | glDeleteBuffers(1, &VBO);
|
---|
| 167 | //glDeleteBuffers(1, &EBO);
|
---|
| 168 |
|
---|
[cfda3b2] | 169 | glfwTerminate();
|
---|
[b33f87b] | 170 | return EXIT_SUCCESS;
|
---|
[cfda3b2] | 171 | }
|
---|
| 172 |
|
---|
| 173 | // Is called whenever a key is pressed/released via GLFW
|
---|
| 174 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
|
---|
| 175 | std::cout << key << std::endl;
|
---|
| 176 | if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
---|
| 177 | glfwSetWindowShouldClose(window, GL_TRUE);
|
---|
| 178 | }
|
---|
[fe70cd3] | 179 |
|
---|
| 180 | GLint compileVertexShader(const GLchar** shaderSource) {
|
---|
| 181 | GLint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
---|
| 182 | glShaderSource(vertexShader, 1, shaderSource, NULL);
|
---|
| 183 | glCompileShader(vertexShader);
|
---|
| 184 |
|
---|
| 185 | // Check for compile time errors
|
---|
| 186 | GLint success;
|
---|
| 187 | GLchar infoLog[512];
|
---|
| 188 | glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
---|
| 189 | if (!success) {
|
---|
| 190 | glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
---|
| 191 | cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED" << endl << infoLog << endl;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | return vertexShader;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | GLint compileFragmentShader(const GLchar** shaderSource) {
|
---|
| 198 | GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
| 199 | glShaderSource(fragmentShader, 1, shaderSource, NULL);
|
---|
| 200 | glCompileShader(fragmentShader);
|
---|
| 201 |
|
---|
| 202 | // Check for compile time errors
|
---|
| 203 | GLint success;
|
---|
| 204 | GLchar infoLog[512];
|
---|
| 205 | glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
---|
| 206 | if (!success) {
|
---|
| 207 | glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
---|
| 208 | cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << endl;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | return fragmentShader;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | GLint linkShaderProgram(initializer_list<GLint> shaders) {
|
---|
| 215 | GLint shaderProgram = glCreateProgram();
|
---|
| 216 |
|
---|
| 217 | for (auto shader : shaders) {
|
---|
| 218 | glAttachShader(shaderProgram, shader);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | glLinkProgram(shaderProgram);
|
---|
| 222 |
|
---|
| 223 | // Check for linking errors
|
---|
| 224 | GLint success;
|
---|
| 225 | GLchar infoLog[512];
|
---|
| 226 | glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
---|
| 227 | if (!success) {
|
---|
| 228 | glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
|
---|
| 229 | cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << endl;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | return shaderProgram;
|
---|
| 233 | }
|
---|