0

I want to obtain the view vector. Thereafter I want to calculate the angle between the normal and the view vector, to determine which faces are fully visible, partially visible and not visible.

A similar question sight vector in opengl might shed some light on this topic. Please correct me if I am wrong, the line of sight presented in 1 is the same as the view vector. If so, the fundamentals are not clearly presented and discussing it might clarify things for a new beginner.

The problem is linking the theory into code and understanding what are ProjectionMat, ModelView, Vetexpos and how to obtain it? I assume it can obtained as such:

glGetFloatv(GL_PROJECTION_MATRIX, projection);//returns the matrix of projectionview
    glGetFloatv(GL_MODELVIEW_MATRIX, modelview); //returns the identity matrix of modelview`

However, how does one get the vertexpos? Furthermore, do you find the cross product of the entire matrix or just for the x,y,z coordinates?

If this is not the correct way to calculate the view vector, how does one achieve?

Community
  • 1
  • 1
Sade
  • 450
  • 7
  • 27
  • I am confused. You need to have the view vector to set up your camera matrix (view matrix) using gluLookAt. – Richard Critten Aug 11 '16 at 14:21
  • No my camera is setup . The gluLookAt values are (30,20,35, 0,0,0, 0,1,0). To provide a better understanding of the direction I am going towards [link] (http://forum.devmaster.net/t/which-view-vector-to-use-for-back-face-culling/6319). The view vector can be used in the back face culling method. I do not want to perform back culling. I want to determine the faces that are visible, partially visible and non visible. This is obtained by first getting the view vector. I understand the theory but i am struggling to implement it. – Sade Aug 11 '16 at 14:32
  • (0,0,0) - (30,20,35) ? – Mars Aug 11 '16 at 14:38
  • Is it that simple? You are taking the eye position coordinates and subtracting it with the centre. Are there any other factors that come into play? What about rotation glRotatef(-30,30,-100,-30)? – Sade Aug 11 '16 at 14:43
  • A similar topic that will also clarify what I am trying to achieve in opengl (http://stackoverflow.com/questions/28794883/back-face-culling-for-linestrips). – Sade Aug 11 '16 at 14:47

1 Answers1

0

To get better understanding of the matrices usage see

If you got the final camera matrix ... then its inverse (or transpose) contains the coordinate system of camera. so just extract its Z axis vector and that is it.

You can obtain matrices from standard OpenGL matrix stack like this:

float ProjectionMatrix[16];
float ModelViewMatrix[16];
glGetFloatv(GL_PROJECTION_MATRIX, ProjectionMatrix);
glGetFloatv(GL_MODELVIEW_MATRIX, ModelViewMatrix);

However the problem is that you do not have separate object and camera matrices instead they are combined into single matrix GL_MODELVIEW_MATRIX. The other matrix GL_PROJECTION_MATRIX is used to store camera parameters like FOV ... And must not contain the view matrix at all as that will broke the functionality of some advanced light/fog/etc capabilities of fixed OpenGL pipeline/function (that is called Projection matrix abuse).

Anyway in any decent rendering code you should have separate matrix for each object and camera directly in form of variables representing their coordinate systems (not using gluLookAt... ).

This way you can easily make camera effects like follow object etc ...

camera_follow = Inverse(object * view_displacement)

where view_displacement is just actual translation and rotation of the view relative to followed object.

If you got the final camera matrix then the view direction is its Z-axis (as you correctly assumed). You should take into account if the matrix is direct or inverse of the camera space so either

// direct
z[0] = camera[ 8]
z[1] = camera[ 9]
z[2] = camera[10]

or:

// inverse
z[0] = camera[ 2]
z[1] = camera[ 6]
z[2] = camera[10]
Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Assuming that I have one object, in this case my camera matrix is my ModelViewMatrix? – Sade Aug 12 '16 at 11:10
  • 1
    @Kribz ModelViewMatrix is cumulation of all the transformations like : `Inverse(camera)*object` so no unless your object matrix is unit. That is why you should have matrix for each object as separate variable. Most people use GLM for this. – Spektre Aug 12 '16 at 11:13
  • Thanks I have included glm. I just want to clarify that glm is just a library handling the matrices and that functions like glm::perspective or lookAt etc just builds the matrix. How does it link back to gluPerspective? I am currently using gluLookAt or gluPerspective. The matrix generated by gluPerspective is multipled by the current matrix, just as if glMultMatrix were called with the generated matrix. To load the perspective matrix onto the current matrix stack instead, precede the call to gluPerspective with a call to glLoadIdentity. – Sade Aug 15 '16 at 12:31
  • with glm functions do we need to still use gluLookAt?Sorry I am really stuck with understanding the basic, I am totally new to it. – Sade Aug 15 '16 at 12:50
  • @Kribz I do not use GLM as I use my own reper class for any matrix manipulation but yes the OpenGL interaction is with `glMulMatrix`. Beware that `gluPerspective` has inaccurate `tangens` therms making troubles if accurate projection is needed for example if merging more frustrums together to achieve better depth buffer precision see: [Is it possible to make realistic n-body solar system simulation in matter of size and mass?](http://stackoverflow.com/a/28020934/2521214). You do not need to use `gluLookAt` it is just a way how to set the matrix. (I never used it in my life) – Spektre Aug 15 '16 at 13:07
  • I just have one object and I am simply analyzing points on the object, eg: which faces are at the edge of a human face, eyes, ears etc. I found a link http://www.gamedev.net/topic/397751-how-to-get-camera-position/ . – Sade Aug 15 '16 at 13:29