Camera and view matrix

Hello guys ! I need some features for the jME camera :



As you know, openGL don't separate Model and View matrices, but sometimes when you write shaders you need to manipulate only the view matrix…

For my needs, I wrote a function which compute the view matrix from the camera parameters.


   public Matrix3f getViewMatrixInverse() {
      Matrix3f viewMatrix = new Matrix3f();
      
      Vector3f f  = getDirection().normalize();
      Vector3f up = getUp().normalize();
      Vector3f s  = f.cross(up);
      Vector3f u  = s.cross(f);
      
      viewMatrix.setColumn(0, s);
      viewMatrix.setColumn(1, u);
      viewMatrix.setColumn(2, f.negateLocal());
      return viewMatrix;
   }

   public getViewMatrixInverseTranspose() {
      return getViewMatrixInverse().transpose();
   }



Can you add it to the com.jme.renderer.AbstractCamera please ?

model and projection matrices are separate in OpenGL.  Abstract camera already grabs the Projection matrix.  I haven't really studied what you are proposing, but is this matrix not enough?  (Maybe we have the method misnamed.)

I think he refers to the fact that DirectX uses three matrixes:


  1. World Matrix - Transforms 3D data from Model Space into World Space. You need to set this before rendering every entity in your world.
  2. View Matrix - Transforms from World Space into View Space. You need to set this each time your camera changes position
  3. Projection Matrix - Transforms from View Space into Screen Space. You normally set this just once during initialization.



    (source: http://www.toymaker.info/Games/html/matrices.html)


You are right jjmontes !!!



In DirectX, you have 3 matrices :


  • The model matrix, to send data (vertices for example) from the object space to the world space;

  • The view matrix, to send data from the world space to the view space;

  • The projection matrix, to send vertices from the view space to the screen space.



In OpenGL, the model matrix and the view matrix are automatically merged.
So, in a GLSL shader program, you can only use 2 matrices :

  • The model/view matrix, to send data from the object space to the view space;

  • The projection Matrix, to send data from the view space to the screen space.



If you only use the fixed graphic pipeline, don't need to use separatly the model and the view matrix,.
But sometimes, when you use shaders you need them separately.

In jme we could add functions to get the model matrix and the view matrix separatly, by adding getViewMatrixInverse() to the camera and another function getModelMatrixInverse() to the com.jme.scene.Spatial class.

I have not much knowledge on all this but I always thought that the two matrices that are combined in OpenGL are the view and the projection matrix. The model matrix is still alone in both 3D APIs. I'm unsure anyway.



Anyway, I don't understand the solution you propose and you seem to know what you're talking about better than me. I hope that someone can help you with this.

I always thought that the two matrices that are combined in OpenGL are the view and the projection matrix


It is strange, but you are not the first person that tell that the projection and and the view matrix are combine in OpenGL.

In fact, you can combine them as you say : the result would be the same because (Mat_projection * Mat_view) * Mat_model == Mat_projection * (Mat_view * Mat_model).
But I think your way is not the right way because OpenGL provides matrix stacks which are named GL_PROJECTION and GL_MODELVIEW. (see the glMatrixMode specification http://pyopengl.sourceforge.net/documentation/manual/glMatrixMode.3G.html).

I think it is bad to store projection/view matrix inside the GL_PROJECTION stack and the model matrix inside the GL_MODELVIEW stack...

I don't understand the solution you propose


I want to create some GLSL standard Fx like a per pixel lighting or a real time paralax dot3 bump mapping (like in TestFragmentProgramState demo http://www.jmonkeyengine.com/webstart/jmedemo.php?renderer.state.TestFragmentProgramState) in an entire scene. To do that, I need to process separatly the model matrix and the view matrix, and that is why I'm going to develop some usefull functions to get these matrices.

i think targ is right…the viewmatrix is much needed in shaders, and is not supplied by opengl automatically(why i have no clue). fortunately it's very easy to extract…just multiply the inverse of the modelmatrix with the modelviewmatrix and you are there…

i think targ is right...

Year, I rule !  :mrgreen:

just multiply the inverse of the modelmatrix with the modelviewmatrix and you are there...

I'm agree with you, but you cannot get the model/view matrix from jme ! The only place to get it is the vertex/pixel shader. Then , if you want to compute the view matrix by this way, you must recompute it in the shader and then recompute it for each vertex/pixel. I think that it is hightly unoptimized  :-o !!!

My prefered way is to precompute the view matrix in the java program by using the function that I wrote in my first post, and then to send it to the shader program like an uniform variable.

PS : My view matrix computation comes from the gluLookAt documentation : http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html.