0

I am currently using this: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-7-model-loading/

All I've done so far is replace the model, changed up the camera so that it is isometric, and made the cursor show up again. So nothing major.

My new model shows up perfectly along with its textures. I was wondering how I can make it so that by pressing W, A, S, & D, I can move my model around?

I'm planning on making a very simple "game" where I just move my model around, and collide with other objects scattered around the map.

Any help is appreciated! Thanks everyone!

// Clear the screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Use our shader
    glUseProgram(programID);

    // Send our transformation to the currently bound shader, 
    // in the "MVP" uniform
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

    // Bind our texture in Texture Unit 0
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, Texture);
    // Set our "myTextureSampler" sampler to user Texture Unit 0
    glUniform1i(TextureID, 0);

    // 1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(
        0,                  // attribute
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        (void*)0            // array buffer offset
    );

    // 2nd attribute buffer : UVs
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
    glVertexAttribPointer(
        1,                                // attribute
        2,                                // size
        GL_FLOAT,                         // type
        GL_FALSE,                         // normalized?
        0,                                // stride
        (void*)0                          // array buffer offset
    );

    // Draw the triangle !
    glDrawArrays(GL_TRIANGLES, 0, vertices.size() );

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();
  • Keep a vector for current position. Change position vector when you press a key. Every frame, before drawing a model, apply translation by position vector to the model's vertices. – Ivan Aksamentov - Drop May 16 '17 at 07:27
  • see [Understanding 4x4 homogenous transform matrices](http://stackoverflow.com/a/28084380/2521214) especially the last links with player&camera control examples ... – Spektre May 16 '17 at 07:46

1 Answers1

2

the MVP matricies encode the translation, rotation and scaling of the model. It is the product of the Projection * View * Modelmatrix. If you apply the translation the the Modelmatrix and recalculate mvp, then you should see the object moving.

OutOfBound
  • 1,914
  • 14
  • 31
  • Thank you very much for your reply! If I make other models, do I have to recalculate the mvp so that the other models can stay static and not move? – Russell Leong May 16 '17 at 08:07
  • 2
    each model should have it's own modelmatrix (rotation scaling translation) which you later multiply with the camera matricies (view/projectionmatrix). you can use glm to use matrices and vectors in c++ – Thomas May 16 '17 at 08:24