Open GL setting an orthographic projection - opengl

i am trying to draw and a simple cube and move it along the screen on Left Arrow key and Right key down (left direction and right direction respectively). I donot have exact idea of gluOrtho2d(leftortho,rightortho,bottomortho,toportho). i have tried glOrtho(leftortho,rightortho,bottomortho,toportho,-1.0,1.0) with six parameters but the result is same. The cube moves to upper right corner and vanishes.. I need help in this regard..
double leftortho=5.0,rightortho=1.0,bottomortho=5.0,toportho=1.0;
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glutWireCube (1.0);
gluOrtho2D(leftortho,rightortho,bottomortho,toportho);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
}
float move_unit = 0.001;
void keyboardown(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_RIGHT:
{
leftortho+=move_unit;
rightortho+=move_unit;
//bottomortho+=move_unit;
//toportho+=move_unit;
break;
}
case GLUT_KEY_LEFT:
{
leftortho-=move_unit;
rightortho-=move_unit;
//bottomortho-=move_unit;
//toportho-=move_unit;
break;
}
case GLUT_KEY_UP:
//posY+=move_unit;;
break;
case GLUT_KEY_DOWN:
// posY-=move_unit;;
break;
default:
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(keyboardown);
glutMainLoop();
return 0;
}

Give this a shot:
#include <GL/glut.h>
double leftortho=-1.0;
double rightortho=1.0;
double bottomortho=-1.0;
double toportho=1.0;
float move_unit = 0.01;
void keyboardown(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_RIGHT:
leftortho-=move_unit;
rightortho-=move_unit;
break;
case GLUT_KEY_LEFT:
leftortho+=move_unit;
rightortho+=move_unit;
break;
case GLUT_KEY_UP:
toportho-=move_unit;
bottomortho-=move_unit;
break;
case GLUT_KEY_DOWN:
toportho+=move_unit;
bottomortho+=move_unit;
break;
default:
break;
}
glutPostRedisplay();
}
void display(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glShadeModel (GL_FLAT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(leftortho,rightortho,bottomortho,toportho, -10, 10);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f (1.0, 1.0, 1.0);
glutWireCube (1.0);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glutDisplayFunc(display);
glutSpecialFunc(keyboardown);
glutMainLoop();
return 0;
}

I think the problem is that you are not resetting the projection matrix before calling gluOrtho2d(), so your moves are accumulating. I think you need to do this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2d(left,right,bottom,top);
Note also that your left and right appear to be swapped, as do your bottom and top. This is legal, but it may end up causing the left and right arrow keys to seem reversed. It basically sets your transformation to be backwards and upside down.

Related

OpenGL Won't Close on Mouse Click (mouseFunc) - glutKeyboardFunc is fine?

I have my program configured to close when I press the 'q' key. It was simple enough to orchestrate after reading the glutKeyboard prototype, so I thought I would also add the option to close with the right mouse button. However, no matter what I do, I can't get this to work. I'm curious if there is some subtle difference between mouseFunc and keyboardFunc that I'm missing? Here is my code:
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
#include <GL/gl.h>
//#include <assert.h>
void init (void)
{
glClearColor (1.0, 1.0, 0.0, 0.0); /* Set background to yellow */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2d (0.0, 0.0);
glVertex2d (1.0, 0.0);
glVertex2d (0.5, 0.866);
glEnd();
glFlush (); //Display immediately
}
void keyEscape( unsigned char key, int x, int y )
{
switch ( key )
{
case 113: // 'Q' key for escape
int windowID = glutCreateWindow ("triangle");
glutDestroyWindow (windowID);
exit (0);
break;
}
glutPostRedisplay();
}
void mouseEscape( int button, int state, int x, int y )
{
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
int windowID = glutCreateWindow ("triangle");
glutDestroyWindow (windowID);
exit (0);
glutPostRedisplay();
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition ((glutGet(GLUT_SCREEN_WIDTH)-250)/2, (glutGet(GLUT_SCREEN_HEIGHT)-250)/2);
glutCreateWindow ("triangle");
init ();
glutDisplayFunc(display);
glutKeyboardFunc(keyEscape);
glutMouseFunc(mouseEscape);
glutMainLoop();
return 0;
}
I parsed out a few more keyboard shortcuts (such as z for zooming) that all work similarly, hence my use of a switch statement as opposed to an if, but that is the only real difference I can see, and attempting to use a switch case has not worked for me either. I've also tried moving the redisplay command outside the if, to no avail. Any idea why mouse closing won't cooperate, but key closing will?
Turns out I was somehow building against an old .exe that wouldn't updated even with a 'Clean Project' operation. I changed projects and now all is fine... Thanks for the assistance all the same!

Trying to Scale OpenGL Program with Width (but not height)?

I am fiddling with graphics, and trying to find a way to have the shape I am drawing (in this case, a triangle) scale with window sizing. However, I only want the width to update, and the height to remain the same.
I have done some research, and tried using glutGet(GLUT_SCREEN_WIDTH) as a multiplier or similar (e.g. (GLUT_SCREEN_WIDTH /100) - 250) to the vertices of my shape, but I feel I may be missing a key idea. Should I instead be applying the scaling operation to the viewport, not the shape's points? Whenever I scale the points, they don't seem to scale with the window. Code below.
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
#include <GL/gl.h>
//#include <assert.h>
void init (void)
{
glClearColor (1.0, 1.0, 0.0, 0.0); /* Set background to yellow */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2d (0.0, 0.0);
glVertex2d(1.0, 0.0);
glVertex2d (0.5, 0.866);
glEnd();
glFlush (); //Display immediately
}
void keyEscape( unsigned char key, int x, int y )
{
switch ( key )
{
case 113: // 'Q' key for escape
int windowID = glutCreateWindow ("triangle");
glutDestroyWindow (windowID);
exit (0);
break;
}
glutPostRedisplay();
}
void mouseEscape( int button, int state, int x, int y )
{
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
int windowID = glutCreateWindow ("triangle");
glutDestroyWindow (windowID);
exit (0);
glutPostRedisplay();
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition ((glutGet(GLUT_SCREEN_WIDTH)-250)/2, (glutGet(GLUT_SCREEN_HEIGHT)-250)/2);
glutCreateWindow ("triangle");
init ();
glutKeyboardFunc(keyEscape);
glutMouseFunc(mouseEscape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Step 1: Get rid of the Init function. Projection setup is part of the drawing process.
Step 2: Use the Window width as input for the left/right parameter of glOrtho
Like this:
void display(void)
{
int const win_width = glutGet(GLUT_WINDOW_WIDTH);
int const win_height = glutGet(GLUT_WINDOW_HEIGHT);
float const win_aspect = (float)win_width / (float)win_height;
glClearColor (1.0, 1.0, 0.0, 0.0); /* Set background to yellow */
glClear (GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, win_aspect, 0.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2d (0.0, 0.0);
glVertex2d(1.0, 0.0);
glVertex2d (0.5, 0.866);
glEnd();
glFlush (); // Tell OpenGL to process what we submitted so far
}
BTW: You should switch to a double buffered mode and use glutSwapBuffers instead of glFlush/glFinish; on some systems single buffered mode doesn't work (well). Today the only reliable method is double buffering.

OpenGL: Error moving object using keyboard

I'm new to OpenGL and I'm trying to paint a stickman and then move it using the arrow-keys from the keyboard.
My idea was to use global variables for the stickman and to change them when a certain key is pressed. Afterwards the draw-function (myDisplay()) is called again.
Unfortunately i always get the following error-message:
"Error 10 error C2371: 'myDisplay' : redefinition; different basic types "
When I replace the myDisplay()-call in the keyboard-function with glutPostRedisplay() as suggested in some tutorials I read the error message disapears and the build is successful. But the stickman doesn't move when pressing the keys.
Here's my code:
#include <GL/glut.h>
GLint x; GLint y; GLint d; //parameters for the stickman
void myKeyboard(unsigned char key, int mx, int my) {
int x1 = mx;
int y1 = 480 - my;
switch(key){
case GLUT_KEY_LEFT :
x = x-50;
myDisplay();
break;
case 'E' :
exit(-1);
break;
default:
break;
}
}
void stickman () {
glBegin(GL_LINES); //body
glVertex2i(x, y);
glVertex2i(x, y-2*d);
glVertex2i(x-d, y-d);
glVertex2i(x+d, y-d);
glVertex2i(x, y-2*d);
glVertex2i(x-d, y-3*d);
glVertex2i(x, y-2*d);
glVertex2i(x+d, y-3*d);
glEnd();
glBegin(GL_LINE_LOOP); //head
glVertex2i(x,y);
glVertex2i(x+0.5*d, y);
glVertex2i(x+0.5*d, y+0.5*d);
glVertex2i(x-0.5*d, y+0.5*d);
glVertex2i(x-0.5*d, y);
glEnd();
}
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
stickman();
glFlush();
}
void myInit() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void main(int argc, char** argv) {
x = 320;
y = 350;
d = 100;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 50);
glutCreateWindow("Stickman");
glutDisplayFunc(myDisplay);
glutKeyboardFunc(myKeyboard);
glutPostRedisplay();
myInit();
glutMainLoop();
}
As I found out (GLUT Keyboard Tutorial) the problem wasn't the repaint of the picture, but rather that the left arrow key wasn't recognized.
I needed to write another keyboard-function for my special keys such as the arrow keys.
Now my code looks like this and it works:
void processNormalKeys (unsigned char key, int mx, int my) {
if (key == 'E')
exit(-1);
}
void processSpecialKeys (int key, int mx, int my) {
switch(key){
case GLUT_KEY_LEFT :
x = x-5;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT :
x = x+5;
glutPostRedisplay();
break;
case GLUT_KEY_UP :
y = y+5;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN :
y = y-5;
glutPostRedisplay();
break;
default:
break;
}
}
I also had to adjust the main function:
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);
But thanks for your help anyways!
glutPostRedisplay() is used to tell mark that the current window needs to be redisplayed as said in the documentation. That means that you need to call this function at the end of your myKeyboard function.
I will also recommend you to use double or tripple buffer to avoid undesired results. For double buffer you need to change glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); with glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); and then at the end of your display function you need to add glutSwapBuffers();. You can find more info here http://www.swiftless.com/tutorials/opengl/smooth_rotation.html.

how to change integer values of glLookat view elements with keyboard?

I am making a room and i want to change the values of lookat because i want to move my camera up,down,right,left,forward and backward inside my room.
My code is given below with comments:
#include <iostream>
using namespace std;
#include "glut.h"
#include "GL/gl.h"
//---------------------------------------------------------------------
static int all =0;
static int allD =0;
static int a=0;
static int d=0;
static int o=0;
static float w=0;
static float s=0;
//---------------------------------------------------------------------
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}
//---------------------------------------------------------------------
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_DEPTH);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//glDisable(GL_DEPTH_TEST);
glPushMatrix();
glRotatef(a,0,1,0);
glRotatef(d,0,1,0);
//back wall glPushMatrix();
glTranslatef(0,0,-2);
glColor3f(0.5,1,0);
glScalef (50.0, 20.0, 2);
glutSolidCube (0.5);
glPopMatrix();
//left wall glPushMatrix();
glTranslatef(-12,0,4.5);
glColor3f(1,1,0);
glScalef (2, 20, 25);
glutSolidCube (0.5);
glPopMatrix();
//floor glPushMatrix();
glTranslatef(0,-5,4.5);
glColor3f(0.5,1,1);
glScalef (50, 0.2, 25);
glutSolidCube (0.5);
glPopMatrix();
//roof glPushMatrix();
glTranslatef(0,4.5,4.5);
glColor3f(0,0,1);
glScalef (50, 2, 25);
glutSolidCube (0.5);
glPopMatrix();
//right wall glPushMatrix();
glTranslatef(12,0,4.5);
glColor3f(0.8,0.2,1);
glScalef (2, 20, 25);
glutSolidCube (0.5);
glPopMatrix();
////front wall 1 //glPushMatrix();
// glTranslatef(-2,0,9);
// glColor3f(0.5,0,0);
// glScalef (12.0, 20.0, 2);
// glutSolidCube (0.5);
//glPopMatrix();
////front wall 2 //glPushMatrix();
// glTranslatef(2.5,2.5,9);
// glColor3f(0.5,0,0);
// glScalef (10.0, 10.0, 2);
// glutSolidCube (0.5);
//glPopMatrix();
////Door //glPushMatrix();
// glTranslatef(0.8,-2,9);
// glRotatef(o,0,1,0);
// glColor3f(0.5,0.5,0.5);
// glScalef (1.0, 8.0, 0.2);
// glutSolidCube (0.5);
//glPushMatrix();
// glTranslatef(1.7,0,0);
// glColor3f(0.5,0.5,0.5);
// glScalef (6.0, 1.5, 0.2);
// glutSolidCube (0.5);
//glPopMatrix();
//glPopMatrix();
//table glPopMatrix();
glutPostRedisplay();
glutSwapBuffers();
}
//---------------------------------------------------------------------
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
//glFrustum(-2,2,-2,2,2.5,20);
//glOrtho(-2,2,-2,2,2.5,20);
gluLookAt (0.0, 0.0, 25, w, s, 0.0, 0.0, 1.0, 0.0);
//i made it 'w' and 's' but its not working. glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//---------------------------------------------------------------------
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'a': a = (a - 1) % 360;
glutPostRedisplay();
break;
case 'd': d = (d + 1) % 360;
glutPostRedisplay();
break;
case 'o': o = (o + 1) % 90;
glutPostRedisplay();
break;
case 'w': w = (w + 0.1) ;
glutPostRedisplay();
break;
case 's': s = (s - 0.1);
glutPostRedisplay();
break;
default: break;
}
}
//---------------------------------------------------------------------
void SpecialKeys(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_RIGHT : all = (all + 10) % 360;
glutPostRedisplay();
break;
case GLUT_KEY_UP : allD = (allD - 10) % 360;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT : all = (all - 10) % 360;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN : allD = (allD + 10) % 360;
glutPostRedisplay();
break;
default: break;
}
}
//---------------------------------------------------------------------
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (700, 500);
glutInitWindowPosition (0,0);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutSpecialFunc(SpecialKeys);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
//
You need to re-execute the gluLookAt() function whenever you update the values you're passing to it.
A cleaner way to execute this part of your code would be:
static int W = 0;
static int H = 0;
static int S = 0;
//---------------------------------------------------------------------
void updateCamera() {
gluLookAt (0.0, 0.0, 25, W, S, 0.0, 0.0, 1.0, 0.0);
}
//---------------------------------------------------------------------
void reshape (int w, int h)
{
W = w;
H = h;
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
//glFrustum(-2,2,-2,2,2.5,20);
//glOrtho(-2,2,-2,2,2.5,20);
updateCamera();
glLoadIdentity();
}
//---------------------------------------------------------------------
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
a = (a - 1) % 360;
break;
case 'd':
d = (d + 1) % 360;
break;
case 'o':
o = (o + 1) % 90;
break;
case 'w':
W = (W + 0.1) ;
updateCamera();
break;
case 's':
S = (S - 0.1);
updateCamera();
break;
default: break;
}
glutPostRedisplay();
}
In general, it's a good idea to abstract out the calls you make often and/or from different places so that you aren't copy-pasting and making future updates or bug fixes more difficult.

glulookat() - move camera with keyboard( OpenGL)

I am trying to move the camera with the use of the keyboard. I am calling the glulookat function in the draw method as follows:
gluLookAt(posx,posy,posz,lookx,looky,lookz,upx,upy,upz);
Also I have, for example, for moving the position of the camera on the X axis the following code:
void keyboard(unsigned char key, int x, int y) {
switch(key) {
case 'w' :
break;
case 'a' :
posx-=1.0;
break;
case 's' :
break;
case 'd' :
posx+=1.0;
break;
}
}
The posx,posy,posz,lookx,looky,lookz,upx,upy,upz variables are declared as global double variables. I have tried to initialize them in two ways: when declaring them, as well as in the init() method, but with no success. The camera isn't moving, although the program receives the keyboard input properly, as I have tested this aspect separately. Any ideas of what I am doing wrong?
EDIT: I provided the main code for a better understanding:
Outer_space* space;
Light* sceneLight;
Light* sceneLight2;
Light* sceneLight3;
Satelite* satelite1;
double posx=35.0,posy=35.0,posz=35.0,lookx=0,looky=1,lookz=0,upx=0,upy=0,upz=-1;
void init(void) {
//random number generator
srand(time(NULL));
space = new Outer_space(12, 12,12, new Vector3D(0.5f, 0.7f, 0.9f));
sceneLight = new Light(0, 0);
sceneLight->setTranslation(new Vector3D(0, 20, 0));
sceneLight->setConstantAttenuation(0.09f);
sceneLight->setLinearAttenuation(0.08f);
sceneLight2 = new Light(1, 0);
sceneLight2->setTranslation(new Vector3D(20, 0, 0));
sceneLight2->setConstantAttenuation(0.09f);
sceneLight2->setLinearAttenuation(0.08f);
sceneLight3 = new Light(2, 0);
sceneLight3->setTranslation(new Vector3D(0, 0, 20));
sceneLight3->setConstantAttenuation(0.09f);
sceneLight3->setLinearAttenuation(0.08f);
satelite1 = new Satelite(2,new Vector3D(0.2f,0.3f,0.5f));
satelite1->setTranslation(new Vector3D(10,10,10));
satelite1->setRotation(new Vector3D(-90, 0, 0));
satelite1->setScale(new Vector3D(10, 10, 10));
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
}
void draw(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(0,20,0, 0, 0, 0, 0, 1, 0);
gluLookAt(posx,posy,posz,lookx,looky,lookz,upx,upy,upz);
space->draw();
sceneLight->draw();
sceneLight2->draw();
sceneLight3->draw();
satelite1->draw();
glPushMatrix();
glRasterPos3f(-8.5, 4, -6);
glutSwapBuffers();
}
void update(void){
glutPostRedisplay();
}
void resize(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
GLfloat aspect = (GLfloat)w / (GLfloat)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, aspect, 1.0, 60);
}
void keyboard(unsigned char key, int x, int y) {
switch (key)
{
case 'w' :
break;
case 'a' :
posx-=1.0;
break;
case 's' :
break;
case 'd' :
posx+=1.0;
break;
}
}
void specialKeyboard(int key, int x, int y) {
switch (key)
{
case GLUT_KEY_RIGHT:
posx+=1;
break;
case GLUT_KEY_LEFT:
posx-=1;
break;
case GLUT_KEY_UP:
break;
case GLUT_KEY_DOWN:
break;
}
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Asteroid Escape");
init();
glutIdleFunc(update);
glutDisplayFunc(draw);
glutReshapeFunc(resize);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeyboard);
glutMainLoop();
return 0;
}
EDIT2:
I have commented out the glPushMatrix(); call from the draw() method and now the camera seems to be moving. What is the explanation?
glPushMatrix is supposed to be followed by glPopMatrix.
It seems that you overflow the matrix stack, did you check glGetError result ?
Besides, jour call to glPushMatrix seems pretty useless like this, what do you expect it to do ?