1

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;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 2
    Welcome to SO. Please, take a look at how to do a question. Specially, [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). You have not shown how you try to move the camera. – Ripi2 Mar 15 '18 at 18:01
  • 1
    I would answer, but there are so many things that is lacking, that instead, I will suggest you to spend some time on tutorials, like, for example: http://www.learnopengl.com – Amadeus Mar 15 '18 at 18:02
  • 3
    You are never using `x`, `y` or `z` in your rendering code? – UnholySheep Mar 15 '18 at 18:02
  • 2
    You have no code for the camera movement yet. Suggesting to move from legacy GLUT to newest GLFW. Check this: https://stackoverflow.com/questions/24040982/c-opengl-glfw-drawing-a-simple-cube) – Victor Gubin Mar 15 '18 at 18:02
  • 2
    Are you sure that this is the acutal code? You have the definition `void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)` that contains the movement code for your camera, and that can't be correct. – t.niese Mar 15 '18 at 18:13
  • @Amadeus thanks ik there is a ton missing i just need to be pointed in the right direction! –  Mar 15 '18 at 18:49
  • The right direction is following a good tutorial in modern OpenGL, like [this](https://learnopengl.com/) – Ripi2 Mar 15 '18 at 18:56
  • read [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) and that look into last 3 links in there. There are some C++ examples of player and camera control ... – Spektre Mar 16 '18 at 09:57

1 Answers1

1
  • 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;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139