# opengl question

hi.

i have a 3-d cube.

i need to move in the screen so when i press the appropriate arrow on the keyboard i will move in perspective to the cube.

so when i move forward the cube gets bigger and getting closer to me, and so for the other direction [back,left,right].

i need the functions that allow me to perform these activities.

LVL 2
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
A very simple solution is to use the gluLookAt function

gluLookAt ( from_x,from_y,from_z,
at_x,  at_y,  at_z,
up_x,  up_y,  up_z);

from_*=> the position where you are
at_*  => the position whre you look at
in your case the center of the 3D-cube
up_*  => the up vector most (0,0,1)

so:

{
if (kbhit()){
if (arrow_left) fx-=0.1;
if (arrow_right) fx +=0.1;
...etc.
}
glMatrixMode(GL_MODELVIEW);
gluLookAt(fx,fy,fz,ax,ay,az,0.0,0.0,1.0);
draw3D_cube();
}
0
Author Commented:
0
Author Commented:
im rejecting the answer to give a chance to other experts to handle with the problem.

i did not understand what is the up vector, and what is it used for.

also i did not understood how to make the movement... what is the fx?

also how do i know my coordinates for the center of the cube?

last, please elaborate of the if(kbhit) - whats the exact function? and the arrow_keys...ascii values or what?

this is more then i started so im raising question points to 250.

0
Commented:
i need to move in the screen so when i press the appropriate arrow on the keyboard i will move in perspective to the cube.

if (kbhit()){
if (arrow_left) fx-=0.1;
if (arrow_right) fx +=0.1;
...etc.
}

This is pseudo-code. If you hit the keyboard (some compilers use the kbhit function to test this). If it is the left-arrow key which is defined different on many computers decremnt the x-position ==> fx

If you don't know the center of the cube how can you draw it?

The up vector is used for rotation of the eye-point. UP vector 0,0,1 means that the coordinate system is like
x-left right
z-up-down.

0
Commented:
I'd do it like this:

1. Set up a viewing transformation. The camera is at (0, 0, 0), looking at the negative Z axis. Say, we'll allow the cube to move from z=-5 (near) to z=-100 (far).
Make a function RenderSetup, call it in the beginning of the program (after OpenGL is initialized, of course), and set the frustum like that:

glMatrixMode(GL_PROJECTION);
glFrustum(-2, -2, 2, 2, 5, 100);

2. Store the coordinates of the cube in a vector3 (a structure with 3 floats, x, y, z):

vector3 vCubeCoords = { 0, 0, -20 };

3. For each frame set the modelview transform matrix to a translation from (0, 0, 0) to the cube coords. Place this in a function FrameSetup() called once per frame:

FrameSetup() {
... // clear screen etc
glMatrixMode(GL_MODELVIEW);
glTranslate3f(vCubeCoords.x, vCubeCoords.y, vCubeCoords.z);
}

4. After that, draw the cube as if it were at (0, 0, 0):

DrawCube() {
... // feed vertices in an appropriate order
glEnd();
}

5. in the keypress handler (say, your windows message function) adjust the cube coords:

case KEY_UP: vCubeCoords.y += 0.1f; break;
case KEY_DOWN: vCubeCoords.y -= 0.1f; break;
case KEY_NEARER: vCubeCoords.z -= 0.1f; break;
.... // etc, all directions

To handle things like OpenGL initialization, etc., go to www.longbowdigitalarts.com, click on Programming, and look for BackBuffer and GLBuffer - these are simple template programs which handle these mundane tasks and let you concentrate on important things - like drawing cubes :-)
0
Author Commented:
0
Author Commented:
i requested a comment so i may evaluate the answers without locking the question...

i followed ur explanation and failed to gained movement.

u must understand that i am learning oprn gl from the book "the opengl superbible" and im a total newbie.

i m trying to gain movement in my window but i cant!

i know how to draw polygons, but i cant seem to achive movement...

if u can write me a sample code, refer me to a sample code, or SEND ME VIA EMAIL i would gladly award the points.

so in conclusion, i need a code of movement ,using kbd keys [or mouse or whatever but controlable].

0
Commented:
check out this:
http://glpexeso.webjump.com

This is a very simple OpenGL game with heavily commented sources available.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
I have a very good OpenGl lessons file from NeHe. He had a website with a whole tutorial on OpenGl with examples for all kinds of operating systems. I checked the site but its not up anymore. I have the zip-file including examples and so on for you. Texturing, motion etc. with runnable code for Windows 95/98.  I could email you this but whats your email address?
0
Commented:
0
Commented:

Try this link, I think it will do the trick:
http://www-classic.be.com/developers/developer_library/opengl.html
0
Commented:
0
Commented:
in OpenGL there is two mode one is ortho
and the second perspective when you will
use the perspective mode and u"ll put a look point with glLookAt(...) func if the object will get near the look point it will be bigger and the oposit
0
Commented:
Community Support has reduced points from 300 to 50
0
Commented:
0
Author Commented:
since no1 managed to solve my problem i am going to delete this question.

However since effort was made in trying to help me out, i decided to award 50 pts for YE who gave an honest effort.

10x for all of you, adi.
0
Commented:
Community Support has reduced points from 300 to 50
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.