0

Hi, everyone!

I'm having problems with the implantation of a free camera in a 3D world. For now, I'm using the A/D/W/S keys to make the camera go right, left, forward and backward. The problem is that I don't see the effects of the forward and backward movements. Even with high translation values, the effect (being visually close of far of the object) doesn't appear.

Can you help me with this problem? I tried to solve this for hours, but I am so blindfooled by my code so I can't see the cause of the problem. The transformation matrix looks good...

N.B: You may need some additional includes to make the code work.

#include <gl/glut.h>
#include <stdio.h>

//Facteurs de translation et de rotation.
GLdouble translationFactor = 25.0;
GLdouble rotationFactor = 0.5;

GLdouble objectTransformationMatrix[16];

GLint windowX = 500;
GLint windowY = 500;

void init(void) 
{
	glClearColor (0.0, 0.0, 0.0, 0.0);

	GLfloat materialSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat materialShininess[] = { 50.0 };
	GLfloat lightPosition[] = { 5.0, 5.0, 0.0, 0.0 };
	GLfloat lightAmbiant[] = {0.0, 0.0, 1.0, 0.0 };		//Couleur d'ambiance bleue.

	//Préparation et activation de la source lumineuse.
	glShadeModel(GL_SMOOTH);

	glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular);
	glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess);

	glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
	glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbiant);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);

	glDepthFunc( GL_LESS );
	glEnable(GL_DEPTH_TEST);

	//Préparation du système de coordonnées...
	glViewport(0, 0, 500, 500);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, 500.0, 0.0, 500.0, -5000.0, 5000.0);

	//On met la perspective de vue ici.
	//gluPerspective(90, (GLdouble)windowX / (GLdouble)windowY, 1, 300);
	//gluPerspective(90, 1, 1, 300);

	//Matrice de transformation de la sphère chargée.
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslated(250.0, 250.0, 0.0);		//Position initiale de la sphère.

	glGetDoublev(GL_MODELVIEW_MATRIX, objectTransformationMatrix);
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glColor3f(1.0, 1.0, 1.0);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//On dessine la sphère
	glPushMatrix();
		glLoadMatrixd(objectTransformationMatrix);
		glutSolidSphere(50.0, 50, 50);
	glPopMatrix();

	//Application de la matrice de transformation.
	glFlush();
}

void keyboard(unsigned char key, int x, int y)
{
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//On va chercher la matrice de transformation.
	glLoadMatrixd(objectTransformationMatrix);

	switch (key)
	{
		case 'a':	//On se tasse à gauche -- tous les objets se tassent à droite.
		{
			glTranslated(translationFactor, 0.0, 0.0);
			break;
		}
		case 'd':	//On se tasse à droite -- tous les objets se tassent à gauche.
		{
			glTranslated(-translationFactor, 0.0, 0.0);
			break;
		}
		case 'w':	//On va en avant. -- les objets "avancent" vers la caméra.
		{
			glTranslated(0.0, 0.0, translationFactor);
			break;
		}
		case 's':	//On va en arrière. -- les objets reculent vers la caméra.
		{
			glTranslated(0.0, 0.0, -translationFactor);
			break;
		}
		case '4':	//On tourne la caméra vers la gauche -- TODO
		{
			break;
		}
		case '6':	//On tourne la caméra vers la droite
			break;
		case '2':	//On tourne la caméra vers le haut. (vue inversée)
			break;
		case '8':	//On tourne la caméra vers le bas. (vue inversée)
			break;
		case 'z':	//For debugging purposes
		{
			printf("%f %f %f %f\n",objectTransformationMatrix[0],objectTransformationMatrix[4],objectTransformationMatrix[8],objectTransformationMatrix[12]);
            printf("%f %f %f %f\n",objectTransformationMatrix[1],objectTransformationMatrix[5],objectTransformationMatrix[9],objectTransformationMatrix[13]);
            printf("%f %f %f %f\n",objectTransformationMatrix[2],objectTransformationMatrix[6],objectTransformationMatrix[10],objectTransformationMatrix[14]);
            printf("%f %f %f %f\n",objectTransformationMatrix[3],objectTransformationMatrix[7],objectTransformationMatrix[11],objectTransformationMatrix[15]);
			printf("---------------------------------------------------------\n");
			break;
		}
	}
	
	//On sauvegarde la matrice de transformation des objets.
	glGetDoublev(GL_MODELVIEW_MATRIX, objectTransformationMatrix);

	glutPostRedisplay();
}

void reshape(GLint x, GLint y)
{
	windowX = x;
	windowY = y;
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (500, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow ("A legendary camera!");
   init();

   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by MattEvans
0

It's because you are using an orthographic projection, in an orthographic projection, a higher depth doesn't make objects appear smaller.

See : http://www.songho.ca/opengl/gl_projectionmatrix.html

So, if you want this effect, use gluPerspective, with a FOV (first parameter) of about 45.. (90 is waaay too wide a field-of-view, it will make the 'viewer' seem infinitly small). You may well need to scale your objects, to make them 'the right size' under this new projection

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.