Changeset 92bc4fe in opengl-game


Ignore:
Timestamp:
Jun 25, 2017, 7:15:12 PM (7 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
fe70cd3
Parents:
8a6d19d
Message:

Make the game window fullscreen, hide the mouse cursor, and change the view to an overhead one as the player moves around the scene

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • common/controls-new.cpp

    r8a6d19d r92bc4fe  
    22#include <GLFW/glfw3.h>
    33extern 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
     7using namespace std;
    48
    59// Include GLM
     
    2226
    2327// Initial position : on +Z
    24 glm::vec3 position = glm::vec3(4,3,-3); //glm::vec3( 0, 0, 5 );
     28glm::vec3 position = glm::vec3(4,6,-3); //glm::vec3( 0, 0, 5 );
    2529// Initial horizontal angle : toward -Z
    2630float horizontalAngleBase = 3.14f * 3.0f / 2.0f; // 3.14f;
     
    3640
    3741
    38 void computeMatricesFromInputs(){
     42void computeMatricesFromInputs(int windowWidth, int windowHeight) {
    3943
    4044        // glfwGetTime is called only once, the first time this function is called
     
    5256  // The call has no effect the first several times it's called
    5357  if (centeredCount < 100) {
    54     glfwSetCursorPos(window, 1024/2, 768/2);
     58    glfwSetCursorPos(window, windowWidth/2, windowHeight/2);
    5559    centeredCount++;
    5660  }
     
    5862        // Compute new orientation
    5963  /* 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 );
    6266
    6367        // Direction : Spherical coordinates to Cartesian coordinates conversion
     
    6771                cos(verticalAngle) * cos(horizontalAngle)
    6872        );
     73  glm::vec3 lookDirection(
     74    0.0f,
     75    -3.0f,
     76    0.0f
     77  );
     78  lookDirection = lookDirection + 3.0f * direction;
    6979
    7080        // Right vector
     
    105115                // and looks here : at the same position, plus "direction"
    106116                                                                // position+glm::vec3(-4,0,0), // position+glm::vec3(-4,-3,3),
    107                                                                 position+direction,
     117                                                                position+lookDirection,
    108118                                                                glm::vec3(0,1,0) //up                  // Head is up (set to 0,-1,0 to look upside-down)
    109119                                                   );
  • common/controls.cpp

    r8a6d19d r92bc4fe  
    3535
    3636
    37 void computeMatricesFromInputs(){
     37void computeMatricesFromInputs(int windowWidth, int windowHeight) {
    3838
    3939        // glfwGetTime is called only once, the first time this function is called
  • common/controls.hpp

    r8a6d19d r92bc4fe  
    22#define CONTROLS_HPP
    33
    4 void computeMatricesFromInputs();
     4void computeMatricesFromInputs(int windowWidth, int windowHeight);
    55glm::mat4 getViewMatrix();
    66glm::mat4 getProjectionMatrix();
  • mygame.cpp

    r8a6d19d r92bc4fe  
    22#include <stdio.h>
    33#include <stdlib.h>
     4
     5#include <iostream>
     6using namespace std;
    47
    58// Include GLEW
     
    3437        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    3538
    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);
    3843        if( window == NULL ){
    3944                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" );
     
    4348        }
    4449        glfwMakeContextCurrent(window);
     50  glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
    4551
    4652        // Initialize GLEW
     
    213219        glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
    214220
    215         do {
    216 
    217                 // Clear the screen
    218                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    219 
    220                 // Use our shader
    221                 glUseProgram(programID);
    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);
    224230
    225231    // Projection matrix : 45� Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
     
    239245
    240246    // Remember, matrix multiplication is the other way around
    241           glm::mat4 MVP = Projection * View * Model;
     247    glm::mat4 MVP = Projection * View * Model;
    242248
    243249    // 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 );
    284289
    285290        // Cleanup VBO and shader
Note: See TracChangeset for help on using the changeset viewer.