- Timestamp:
- Jun 28, 2017, 3:12:16 AM (8 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 4813066, 669c4e6
- Parents:
- 92bc4fe
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
game.cpp
r92bc4fe rfe70cd3 22 22 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); 23 23 24 GLint compileVertexShader(const GLchar**); 25 GLint compileFragmentShader(const GLchar**); 26 27 GLint linkShaderProgram(initializer_list<GLint>); 28 24 29 const GLuint WIDTH = 800, HEIGHT = 600; 30 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"; 25 44 26 45 int main(int argc, char* argv[]) { … … 68 87 glViewport(0, 0, width, height); 69 88 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 70 143 // Game loop 71 144 while (!glfwWindowShouldClose(window)) { … … 78 151 glClear(GL_COLOR_BUFFER_BIT); 79 152 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 80 160 // Swap the screen buffers 81 161 glfwSwapBuffers(window); 82 162 } 163 164 // Properly de-allocate all resources once they've outlived their purpose 165 glDeleteVertexArrays(1, &VAO); 166 glDeleteBuffers(1, &VBO); 167 //glDeleteBuffers(1, &EBO); 83 168 84 169 glfwTerminate(); … … 92 177 glfwSetWindowShouldClose(window, GL_TRUE); 93 178 } 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 }
Note:
See TracChangeset
for help on using the changeset viewer.