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.

10x, adi.
LVL 2
AdihAsked:
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.

wytzeCommented:
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);
glLoadIdentity();
gluLookAt(fx,fy,fz,ax,ay,az,0.0,0.0,1.0);
draw3D_cube();
}
0
AdihAuthor Commented:
Adjusted points to 250
0
AdihAuthor 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.

adi.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

wytzeCommented:
Your question:
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
y-depth (in to your screen)
z-up-down.



      
0
YECommented:
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);
glLoadIdentity();
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);
  glLoadIdentity();
  glTranslate3f(vCubeCoords.x, vCubeCoords.y, vCubeCoords.z);
}

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

DrawCube() {
  glBegin(GL_QUADS);
  ... // 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
AdihAuthor Commented:
Adjusted points to 300
0
AdihAuthor Commented:
i requested a comment so i may evaluate the answers without locking the question...

regarding the rejected answer...

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].

10x, adi.
0
YECommented:
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

Your issues matter to us.

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

Start your 7-day free trial
wytzeCommented:
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
wytzeCommented:
0
yairpelCommented:
Hello Adih

Try this link, I think it will do the trick:
http://www-classic.be.com/developers/developer_library/opengl.html
0
wytzeCommented:
0
ntdragonCommented:
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
ianBCommented:
Community Support has reduced points from 300 to 50
0
ianBCommented:
Adjusted points to 300
0
AdihAuthor 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
ianBCommented:
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.