Author Topic: Need help with OpenGL  (Read 2996 times)

Borde

  • *
  • Posts: 891
    • View Profile
Need help with OpenGL
« on: 2006-09-10 16:59:46 »
Hello everyone.
Maybe you herad some time ago that I'm redoing Kimera (mostly) from scratch due to some crappy accident. Currently I'm trying to add new features. More specifically, I'm trying to add an option to move vertices freely draging them with the mouse. My problem is that I need to get the director vector from the ModelView Matrix.
Could somebody arround here tell me how to do it?
Thanks for your time.

Alhexx

  • *
  • Posts: 1894
    • View Profile
    • http://www.alhexx.com
Re: Need help with OpenGL
« Reply #1 on: 2006-09-11 19:14:32 »
That depends on how you implement the camera under OpenGL.

Basically, there are 2 "different" ways how to implement a camera:

1. using the glRotate and glTranslate commands - this is the most popular method
2. using the gluLookAt method - the more elegant method

I'm currently working on Eve, my new 3d model editor project. I have decided to use the second option, because, in my opinion, it works better for 3d editors.

If you are using option 2, then getting the vectors to move around is quite simple:

For the gluLookAt, you need to specify 3 vectors:
  • The Center-Vector - this is the point you focus
  • The Eye-Vector - this is from where you look
  • The Up-Vector - this is the vector that stands orthogonal on the line of sight and specifies, how the camera is rotated.
For moving around you need two other (temporary) vectors, which can be calculated of the three ones above.
1) The Ray-Vector represents the line of sight, i.e. the vectors from the Eye-Vector to the Center-Vector, therefore it is:
Ray = Eye - Center
2) The Left-Vector which simply points to the left from the camera. The Left-Vector is orthogonal to the plane which is given by the Up-Vector and the Ray-Vector, therefore it can the calculated using the Cross-Product:
Left = Up × Ray

If you would want to move a vertice to the up or down, you would simply add or subtract the Up-Vector.
If you want to move a vertice left or right, then you would add or subtract the Left-Vector

I hope this helps a bit.

 - Alhexx

Micky

  • *
  • Posts: 300
    • View Profile
Re: Need help with OpenGL
« Reply #2 on: 2006-09-11 21:50:03 »
Maybe you herad some time ago that I'm redoing Kimera (mostly) from scratch due to some crappy accident. Currently I'm trying to add new features. More specifically, I'm trying to add an option to move vertices freely draging them with the mouse. My problem is that I need to get the director vector from the ModelView Matrix.
While I'm not totally sure what you're planning to do, there are some things you can look at:

You can get the current projection matrix / modelview matrix / viewport with:

   GLdouble modelViewMtx[16];
   glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMtx);
   
   GLdouble projectionMtx[16];
   glGetDoublev(GL_PROJECTION_MATRIX, projectionMtx);
   
   GLint viewport[4];
   glGetIntegerv(GL_VIEWPORT, viewport);

Then there are some GLU functions:
   gluProject - does the same transformation as OpenGL would do to a vertex for a custom world coordinate and gives you the window position
   gluUnProject - can convert view coordinates into world coordinates by doing the reverse transformation

Using these functions you could get any vector you want from view space into world space and back.

Borde

  • *
  • Posts: 891
    • View Profile
Re: Need help with OpenGL
« Reply #3 on: 2006-09-12 10:58:39 »
Thanks guys, I think now I understand it. The gluLookAt function is very neat and more intuitive for certain operations (such as panning), but I had troubles in the past using it (the camera goes mad when you reach a certain angle) so I'm sticking with the glRotate/glTranslate solution. Now I think I'll be able to finish it.