I’m having trouble with this source that moves the camera with the cursor keys. I can’t get it to redraw the frame, so that there’s no movement. I’ve tried using the glutIdleFunc but the compiler gives following error:
118 C:\Program Files\c++\Dev-Cpp\ben-prog\glgrid3.cpp
invalid conversion from void (*)(int, int)' to
void (*)()’
Also, when I use glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); as oppose to GLUT_SINGLE, all I get is a blank screen.
What do I need to update frame?
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
static float angle=0.0,ratio;
static float x=0.0f,y=1.75f,z=5.0f;
static float lx=0.0f,ly=0.0f,lz=-1.0f;
static GLint snowman_display_list;
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void Draw3DSGrid(void)
{
// This function was added to give a better feeling of moving around.
// A black background just doesn’t give it to ya We just draw 100
// green lines vertical and horizontal along the X and Z axis.
// Turn the lines GREEN
glColor3ub(0, 255, 0);
// Draw a 1x1 grid along the X and Z axis'
for(float i = -50; i <= 50; i += 1)
{
// Start drawing some lines
glBegin(GL_LINES);
// Do the horizontal lines (along the X)
glVertex3f(-50, 0, i);
glVertex3f(50, 0, i);
// Do the vertical lines (along the Z)
glVertex3f(i, 0, -50);
glVertex3f(i, 0, 50);
// Stop drawing lines
glEnd();
}
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
//glPushMatrix();
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix /
/ viewing transformation */
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
//gluLookAt (0.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */
glutWireCube (1.0);
Draw3DSGrid();
//glPopMatrix();
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
glutSwapBuffers();
}
void orientMe(float ang) {
lx = sin(ang);
lz = -cos(ang);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}
void moveMeFlat(int direction) {
x = x + direction*(lx)0.1;
z = z + direction(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}
void inputKey(int key, int x, int y) {
switch (key) {
case GLUT_KEY_LEFT :
angle -= 0.01f;
orientMe(angle);break;
case GLUT_KEY_RIGHT :
angle +=0.01f;
orientMe(angle);break;
case GLUT_KEY_UP :
moveMeFlat(1);break;
case GLUT_KEY_DOWN :
moveMeFlat(-1);break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init();
glutSpecialFunc ( inputKey );
glutDisplayFunc(display);
glutIdleFunc(reshape);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}