943,948 Members | Top Members by Rank

Ad:
May 18th, 2009
0

OpenGL - Problems with zoom in/zoom out

Expand Post »
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;
}
Last edited by GDICommander; May 18th, 2009 at 10:30 am.
Similar Threads
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
May 18th, 2009
0

Re: OpenGL - Problems with zoom in/zoom out

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
Last edited by MattEvans; May 18th, 2009 at 4:10 pm.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: RPG Battle Algorithms
Next Thread in Game Development Forum Timeline: Need Help About ArrayList of Sprites ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC