• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

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.
0
Adih
Asked:
Adih
  • 5
  • 5
  • 3
  • +3
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now