Domanda

I'm trying to adapt some code from using the legacy fixed function pipeline to the modern programable pipeline and am struggling a little.

I currently have code that looks like this:

procedure TGLCamera.SetModelViewMatrix;
begin
  //calculate new look at point
  lookAt.x := self.eye.x - forwardVect.x;
  lookAt.y := self.eye.y - forwardVect.y;
  lookAt.z := self.eye.z - forwardVect.z;
  //set modelview matrix mode as current OpenGL matrix mode
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity; //load identity as a modelview matrix
  //set view parameters using OpenGL command
  gluLookAt(self.eye.x, self.eye.y, self.eye.z,
            lookAt.x, lookAt.y, lookAt.z,
            upVect.x, upVect.y, upVect.z);
end;

Would I still be using glMatrixMode, glLoadIdentity & gluLookAt ?

Do any of you know of some examples of before and after version 120?

È stato utile?

Soluzione

In modern core OpenGL with deprecated functionality removed (i.e. core 3.1 or higher), the matrix stack, matrix manipulation functions and functionality layered on-top of said features (like GLU) is no longer available. For more info on legacy OpenGL, see this.

Therefore, the functions you mention will not be available to you when using either OpenGL 3.1 with a forward-compatible context or an implementation that does not expose GL_ARB_compatibility (as is the case, for instance, with Intel Sandy Bridge and higher CPUs on Linux) or, beginning with OpenGL 3.2, when using a core profile context. In the latter case, you can stick with the old stuff (although for new code I would strongly advise against it) by using a compatibility profile context. For more information on profiles and the rationale behind them, have a look at this.

The modern way to use matrices is to simply use whatever linear algebra library you have at hand and simply pass results from your calculations to a shader. A good library is groovounet's GLM. It also provides rewrites of old GLU functions, such as glmProjection or glmLookAt and handles all operations using column-major matrices, i.e. the format OpenGL expects.

For instance, the following shader contains a uniform 4x4 matrix representing the model-view-projection matrix:

#version 440 // ... or whatever modern version you're targeting
uniform mat4 ModelViewProjection;

layout (location = 0) in vec3 Position; // the attribute storage class is also gone in
                                          modern GLSL, layouts are very handy, e.g. to 
                                          avoid application-side location queries

void main()
{
  gl_Position = ModelViewProjection * vec4(Position, 1.0);
}

On the API level (i.e. in your application), you will need to pass the vertex attributes to you shader in the assorted ways modern OpenGL offers, the most obvious being to use a vertex buffer object as data store and (mandatory since OpenGL 3.2) a vertex array object describing how to pull attributes from said data store. And of course, you need to compute all the necessary data, like the MVP matrix, and pass them to a shader using either a uniform buffer object or the classic glUniform*() family of functions.

I general, for a good load of information on modern OpenGL, check the official wiki and assorted tutorials on the web, like this one I tend to recommend ('cause I know it's good).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top