Changeset 92bc4fe in opengl-game
- Timestamp:
- Jun 25, 2017, 7:15:12 PM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- fe70cd3
- Parents:
- 8a6d19d
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
common/controls-new.cpp
r8a6d19d r92bc4fe 2 2 #include <GLFW/glfw3.h> 3 3 extern GLFWwindow* window; // The "extern" keyword here is to access the variable "window" declared in tutorialXXX.cpp. This is a hack to keep the tutorials simple. Please avoid this. 4 5 #include <iostream> 6 7 using namespace std; 4 8 5 9 // Include GLM … … 22 26 23 27 // Initial position : on +Z 24 glm::vec3 position = glm::vec3(4, 3,-3); //glm::vec3( 0, 0, 5 );28 glm::vec3 position = glm::vec3(4,6,-3); //glm::vec3( 0, 0, 5 ); 25 29 // Initial horizontal angle : toward -Z 26 30 float horizontalAngleBase = 3.14f * 3.0f / 2.0f; // 3.14f; … … 36 40 37 41 38 void computeMatricesFromInputs( ){42 void computeMatricesFromInputs(int windowWidth, int windowHeight) { 39 43 40 44 // glfwGetTime is called only once, the first time this function is called … … 52 56 // The call has no effect the first several times it's called 53 57 if (centeredCount < 100) { 54 glfwSetCursorPos(window, 1024/2, 768/2);58 glfwSetCursorPos(window, windowWidth/2, windowHeight/2); 55 59 centeredCount++; 56 60 } … … 58 62 // Compute new orientation 59 63 /* STOP ROTATION FOR NOW */ 60 float horizontalAngle = horizontalAngleBase + mouseSpeed * float( 1024/2 - xpos );61 // verticalAngle += mouseSpeed * float( 768/2 - ypos );64 float horizontalAngle = horizontalAngleBase + mouseSpeed * float(windowWidth/2 - xpos ); 65 // verticalAngle += mouseSpeed * float( windowHeight/2 - ypos ); 62 66 63 67 // Direction : Spherical coordinates to Cartesian coordinates conversion … … 67 71 cos(verticalAngle) * cos(horizontalAngle) 68 72 ); 73 glm::vec3 lookDirection( 74 0.0f, 75 -3.0f, 76 0.0f 77 ); 78 lookDirection = lookDirection + 3.0f * direction; 69 79 70 80 // Right vector … … 105 115 // and looks here : at the same position, plus "direction" 106 116 // position+glm::vec3(-4,0,0), // position+glm::vec3(-4,-3,3), 107 position+ direction,117 position+lookDirection, 108 118 glm::vec3(0,1,0) //up // Head is up (set to 0,-1,0 to look upside-down) 109 119 ); -
common/controls.cpp
r8a6d19d r92bc4fe 35 35 36 36 37 void computeMatricesFromInputs( ){37 void computeMatricesFromInputs(int windowWidth, int windowHeight) { 38 38 39 39 // glfwGetTime is called only once, the first time this function is called -
common/controls.hpp
r8a6d19d r92bc4fe 2 2 #define CONTROLS_HPP 3 3 4 void computeMatricesFromInputs( );4 void computeMatricesFromInputs(int windowWidth, int windowHeight); 5 5 glm::mat4 getViewMatrix(); 6 6 glm::mat4 getProjectionMatrix(); -
mygame.cpp
r8a6d19d r92bc4fe 2 2 #include <stdio.h> 3 3 #include <stdlib.h> 4 5 #include <iostream> 6 using namespace std; 4 7 5 8 // Include GLEW … … 34 37 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 35 38 36 // Open a window and create its OpenGL context 37 window = glfwCreateWindow( 1024, 768, "Tutorial 04 - Colored Cube", NULL, NULL); 39 const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); 40 41 // Open a window and create its OpenGL context 42 window = glfwCreateWindow(mode->width, mode->height, "My Space Game", glfwGetPrimaryMonitor(), NULL); 38 43 if( window == NULL ){ 39 44 fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); … … 43 48 } 44 49 glfwMakeContextCurrent(window); 50 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); 45 51 46 52 // Initialize GLEW … … 213 219 glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); 214 220 215 216 217 218 219 220 221 222 223 computeMatricesFromInputs();221 do { 222 223 // Clear the screen 224 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 225 226 // Use our shader 227 glUseProgram(programID); 228 229 computeMatricesFromInputs(mode->width, mode->height); 224 230 225 231 // Projection matrix : 45� Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units … … 239 245 240 246 // Remember, matrix multiplication is the other way around 241 247 glm::mat4 MVP = Projection * View * Model; 242 248 243 249 // Send our transformation to the currently bound shader, 244 // in the "MVP" uniform 245 glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 246 247 // 1rst attribute buffer : vertices 248 glEnableVertexAttribArray(0); 249 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 250 glVertexAttribPointer( 251 0, // attribute. No particular reason for 0, but must match the layout in the shader. 252 3, // size 253 GL_FLOAT, // type 254 GL_FALSE, // normalized? 255 0, // stride 256 (void*)0 // array buffer offset 257 ); 258 259 // 2nd attribute buffer : colors 260 glEnableVertexAttribArray(1); 261 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 262 glVertexAttribPointer( 263 1, // attribute. No particular reason for 1, but must match the layout in the shader. 264 3, // size 265 GL_FLOAT, // type 266 GL_FALSE, // normalized? 267 0, // stride 268 (void*)0 // array buffer offset 269 ); 270 271 // Draw the triangle ! 272 glDrawArrays(GL_TRIANGLES, 0, 12*3+18); // 12*3 indices starting at 0 -> 12 triangles 273 274 glDisableVertexAttribArray(0); 275 glDisableVertexAttribArray(1); 276 277 // Swap buffers 278 glfwSwapBuffers(window); 279 glfwPollEvents(); 280 281 } // Check if the ESC key was pressed or the window was closed 282 while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 283 glfwWindowShouldClose(window) == 0 ); 250 // in the "MVP" uniform 251 glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 252 253 // 1rst attribute buffer : vertices 254 glEnableVertexAttribArray(0); 255 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 256 glVertexAttribPointer( 257 0, // attribute. No particular reason for 0, but must match the layout in the shader. 258 3, // size 259 GL_FLOAT, // type 260 GL_FALSE, // normalized? 261 0, // stride 262 (void*)0 // array buffer offset 263 ); 264 265 // 2nd attribute buffer : colors 266 glEnableVertexAttribArray(1); 267 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 268 glVertexAttribPointer( 269 1, // attribute. No particular reason for 1, but must match the layout in the shader. 270 3, // size 271 GL_FLOAT, // type 272 GL_FALSE, // normalized? 273 0, // stride 274 (void*)0 // array buffer offset 275 ); 276 277 // Draw the triangle ! 278 glDrawArrays(GL_TRIANGLES, 0, 12*3+18); // 12*3 indices starting at 0 -> 12 triangles 279 280 glDisableVertexAttribArray(0); 281 glDisableVertexAttribArray(1); 282 283 // Swap buffers 284 glfwSwapBuffers(window); 285 glfwPollEvents(); 286 287 // Check if the ESC key was pressed or the window was closed 288 } while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 ); 284 289 285 290 // Cleanup VBO and shader
Note:
See TracChangeset
for help on using the changeset viewer.