Archived

This topic is now archived and is closed to further replies.

Moving around . . .

Recommended Posts

Hello to everybody ! It''s about 1 week now that i started learning OpenGL so i am quite new in OpenGL. My problem right now is that i cannot move in 3D space. Ok to make myself clearer i post my code below (use up,down arrow keys and ''a'' and ''z'').

#include <GL/glut.h>
#include <math.h>

int window;

double	posX = 0.0 ,
posY = 0.0 ,
posZ = 0.0 ,

lookX = 0.0 ,
lookY = 0.0 ,
lookZ = 0.0 ,

upX = 1.0 ,
upY = 0.0 ,
upZ = 0.0;

double i = 0.0;

double angle = 360.0;

int triangles;

int rotating_speed = 2;

double speed = 0.03;

double b = 0.5 , n = 0.5 , f = 0.5;

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

glEnable( GL_DEPTH_TEST );

gluPerspective ( 90.0 , 1.0 , 1 , 5000.0 );

gluLookAt( posX , posY , posZ , lookX , lookY , lookZ , upX , upY , upZ );

}

void display()
{

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

angle = 360 * sin(i);
i += (double)rotating_speed / 100.0;

if ( i >= 360.0 )
i = 0.0;

glLoadIdentity();

glTranslatef( -posX , -posY , -posZ );

glPushMatrix();

//   Rotate the triangles
//	glRotatef( angle , 1.0, 1.0, 0.0);

glTranslatef( -1.0 , -1.0 , 0.0 );

for ( triangles = 1 ; triangles <= 2 ; triangles++ )
{
glBegin(GL_POLYGON);

glColor3f( 1.0 , 0.0 , 0.0 );
//			glVertex3f( 250.0 , 500.0 , 0.0 );
glVertex3f( 1.0 , 1.5 , 0.0 );

glColor3f( 0.0 , 1.0 , 0.0);
//			glVertex3f( 125.0 , 0.0 , 125.0 );
glVertex3f( 0.0 , 0.5 , 0.0 );

glColor3f( 0.0 , 0.0 , 1.0 );
//			glVertex3f( 250 + 125.0 , 0.0 , 0 );
glVertex3f( 2.0 , 0.5 , 0.0 );

glEnd();

glTranslatef( b , n , f);
}

glPopMatrix();

glutSwapBuffers();

}

void move_camera(int direction)
{

posZ += direction * speed;

//glutPostRedisplay();
}

void handle_arrows(int key,int x , int y)
{
switch( key )
{
case GLUT_KEY_UP:
move_camera(1);
display();
break;

case GLUT_KEY_DOWN:
move_camera(-1);
display();
break;

case GLUT_KEY_RIGHT:
rotating_speed++;
break;

case GLUT_KEY_LEFT:
rotating_speed--;
break;

default:
break;
}
}

void hande_simple_keys (unsigned char key , int x , int y)
{
switch( key )
{
case 27:
glutDestroyWindow( window );
exit( 0 );
break;

case int(''a''):
posY += speed;
display();
break;

case int(''z''):
posY -= speed;
display();
break;

default:
break;
}
}

void main(int argc , char **argv)
{

glutInit(&argc , argv);

glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GL_DOUBLE );

glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
window = glutCreateWindow("move it");

init();

glutDisplayFunc( display );
glutIdleFunc( display );

glutSpecialFunc( handle_arrows );
glutKeyboardFunc( hande_simple_keys );

glutMainLoop();
}

[/CODE]

Please somebody answer me cause that thing is driving me crazy. I have studied some tutorials and according to them my code should work (at least that''s what i think so).

Oh and sth else. why should i use gluLookAt in order to move in 3D space when i can use glTranslate and glRotate (which personaly i find much easier) bofore drawing anything. Besides that''s what gluLookAt does (but whithout telling it to you)

Thanks in advance.   
What is "real"? How do you define "real"? If you''''re talking about what you can feel. . . what you can smell, taste and see. . . then "real" is simply electrical signals interpreted by your brain.

Share this post

Share on other sites
I use glTranslatef and glRotatef and find it much easier, but think about it, there is much math involved (search the forums before you post a question, more than likely it has been asked before)

-"watch out for the flying bullets!"

-"Its not the birds but the bees that I''m worried about"

Share this post

Share on other sites
First of all I dont see the defined "mainloop()." Make sure you call ever function as it needs to be called

The camera has 3 sets of cords. The posistion of the camera, where its looking and the up vector (used to roll the camera).

In order to rotate, you will need to change the view cords. To straff you need to change both. www.gametutorials.com has some good camera movement to get you started.

Share this post

Share on other sites
Thanks for your concern gyus!

My problem is (sorry for not making it clear before) that i can move up and down but i cannot move backward or forward.

Try runnig the code. After pressing some times the down or up key the triangle will dissapear(but it wont move as it does when you move up or down,it just desapears).

I would be really glad if someone could modify my code and make it run correctly.

Thanks again.

What is "real"?
How do you define "real"?
If you''''re talking about
what you can feel. . . what you can smell, taste and see. . . then "real" is simply electrical
signals interpreted by your brain.

Share this post

Share on other sites
You dont understand movement, read up on it before asking for help.

• Forum Statistics

• Total Topics
628365
• Total Posts
2982273

• 10
• 9
• 13
• 24
• 11