Search code examples
c++openglglut

c++ opengl camera movement


I am having issues figuring out why the camera wont move! I think i setup the code correct. Any pointers?

Screenshot!

When I launch it the triangle comes up but i cannot move the camera, most likely its my whole method behind the camera movement that is to blame due to inexperience. Could somebody point me in the right direction?

include "stdafx.h"
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

//THESE ARE MY VARIABLES
//===========
int x = 0; //|
int y = 0; //|
int z = 0; //|
//===========

//Camera Movement

void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
{
    if (WM_KEYDOWN == VK_UP) {
        std::cout << "User pressed UP!";
        ++z;
    }
    if (WM_KEYDOWN == VK_DOWN) {
        std::cout << "User pressed DOWN!";
        --z;
    }
    if (WM_KEYDOWN == VK_LEFT) {
        std::cout << "User pressed LEFT!";
        --x;
    }
    if (WM_KEYDOWN == VK_RIGHT) {
        std::cout << "User pressed RIGHT!";
        ++x;
    }
}

void display()
{
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);
    glColor3f(1.0, 0, 0);
    glVertex3f(0.5, 0.5, 1);
    glColor3f(0, 1.0, 0);
    glVertex3f(0.5, 0, 1);
    glColor3f(0, 0, 1.0);
    glVertex3f(0, 0.5, 1);
    glVertex3f(0.5, 0.5, 1);

    glEnd();

    glFlush();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(10, 10);
    glutCreateWindow("McDank");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Solution

    • You need to register a keyboard callback with GLUT via glutKeyboardFunc() or glutSpecialFunc() to handle keystrokes.
    • Your glViewport() function is...bizarre. Not sure what you were trying to achieve here with comparing random Windows enums against other enums.
    • You need to actually use your x/y/z variables in your display function, perhaps in a glTranslate().

    All together:

    #include <GL/glut.h>
    
    float x = 0;
    float y = 0;
    float z = 0;
    void special( int key, int, int )
    {
        const float step = 0.1;
        if( GLUT_KEY_LEFT == key )
            x -= step;
        if( GLUT_KEY_RIGHT == key )
            x += step;
        if( GLUT_KEY_UP == key )
            y += step;
        if( GLUT_KEY_DOWN == key )
            y -= step;
        glutPostRedisplay();
    }
    
    void display()
    {
        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
    
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glOrtho( -2, 2, -2, 2, -1, 1 );
    
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        glTranslatef( x, y, z );
    
        glBegin( GL_TRIANGLES );
        glColor3ub( 255, 0, 0 );
        glVertex2f( -1, -1 );
        glColor3ub( 0, 255, 0 );
        glVertex2f(  1, -1 );
        glColor3ub( 0, 0, 255 );
        glVertex2f(  0,  1 );
        glEnd();
    
        glutSwapBuffers();
    }
    
    int main(int argc, char* argv[])
    {
        glutInit(&argc, argv);
        glutInitWindowSize(600, 600);
        glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
        glutCreateWindow("McDank");
        glutSpecialFunc( special );
        glutDisplayFunc(display);
        glutMainLoop();
        return 0;
    }