Frage

Ich versuche, einige OpenGL Tutorials im Netz zu lesen. das Problem ist, dass ich einige alte gefunden gluPerspective() verwenden. gluPerspective wurde in OpenGL veraltet 3.0 und entfernt in 3.1.

Welche Funktion kann ich stattdessen?

Ich bin mit C ++ mit dem neuesten freeglut installiert.

War es hilfreich?

Lösung

Sie haben die Matrix manuell zu berechnen und gibt sie dann zu OpenGL.

Computer die Matrix

Dieser Code-Snippet auf der gluPerspective Dokumentation .

 void BuildPerspProjMat(float *m, float fov, float aspect,
 float znear, float zfar)
 {
  float f = 1/tan(fov * PI_OVER_360);

  m[0]  = f/aspect;
  m[1]  = 0;
  m[2]  = 0;
  m[3]  = 0;

  m[4]  = 0;
  m[5]  = f;
  m[6]  = 0;
  m[7]  = 0;

  m[8]  = 0;
  m[9]  = 0;
  m[10] = (zfar + znear) / (znear - zfar);
  m[11] = -1;

  m[12] = 0;
  m[13] = 0;
  m[14] = 2*zfar*znear / (znear - zfar);
  m[15] = 0;
 }

Es ist eine C ++ Bibliothek namens OpenGL Mathematik die nützlich sein können.

Laden der Matrix in OpenGL 3.1

Ich bin noch neu in dem OpenGL 3.1 API, aber Sie müssen eine Matrix auf der GPU zu aktualisieren und dann nutzen Sie es in Ihren Vertex-Shader, um die richtige Perspektive zu bekommen. Der folgende Code lädt nur die Matrix unter Verwendung von glUniform4fv auf das Video Karte.

{
  glUseProgram(shaderId);
  glUniformMatrix4fv(glGetUniformLocation(shaderId, "u_proj_matrix"),
                     1, GL_FALSE, theProjectionMatrix);
  RenderObject();
  glUseProgram(0);
}

Ein einfacher Vertex-Shader aus einem zufälligen Blog rel="nofollow (durch Stapelüberlauf gefunden).

attribute vec4      a_position;
attribute vec4      a_color;

varying vec4        v_color;

uniform mat4 u_proj_matrix;
uniform mat4 u_model_matrix;

void main() {
  mat4 mvp_matrix = u_proj_matrix * u_model_matrix;
  v_color = a_color;
  gl_Position = mvp_matrix * a_position;
}

Andere Tipps

Verwenden Sie GLM .

glm::mat4 projection = glm::perspective(
  // FOV & aspect
  60.0f, 16.0f / 10.0f, 
  // Near and far planes
  0.001f, 1000f);

// If you're using the now deprecated matrix stacks
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(projection));

// if you're using the new shader based pipelines
GLint projectionUniformLocation = ...;
glUniformMatrix4fv(projectionUniformLocation, 1, GL_FALSE, 
  glm::value_ptr(projection));

Beachten Sie, wenn Sie festgelegt haben GLM_FORCE_RADIANS dann sollten Sie Radiant in der Perspektive Funktion verwenden, nicht Grad ...

glm::mat4 projection = glm::perspective(
  // FOV & aspect
  PI / 3.0f, 16.0f / 10.0f, 
  // Near and far planes
  0.001f, 1000f);

Kopiert von einem meiner älteren Projekten:

// The following code is a fancy bit of math that is eqivilant to calling:
// gluPerspective( fieldOfView/2.0f, width/height , 0.1f, 255.0f )
// We do it this way simply to avoid requiring glu.h
GLfloat zNear = 0.1f;
GLfloat zFar = 255.0f;
GLfloat aspect = float(width)/float(height);
GLfloat fH = tan( float(fieldOfView / 360.0f * 3.14159f) ) * zNear;
GLfloat fW = fH * aspect;
glFrustum( -fW, fW, -fH, fH, zNear, zFar );

Dies ist eine modifizierte Version von Dans Funktion, mit vereinfachten Berechnungen von Keine Angabe Verhalten .

void buildPerspProjMat(GLfloat *m, GLfloat fov, GLfloat aspect,
GLfloat znear, GLfloat zfar){

  GLfloat h = tan(fov);
  GLfloat w = h / aspect;
  GLfloat depth = znear - zfar;
  GLfloat q = (zfar + znear) / depth;
  GLfloat qn = 2 * zfar * znear / depth;

  m[0]  = w;  m[1]  = 0;  m[2]  = 0;  m[3]  = 0;

  m[4]  = 0;  m[5]  = h;  m[6]  = 0;  m[7]  = 0;

  m[8]  = 0;  m[9]  = 0;  m[10] = q;  m[11] = -1;

  m[12] = 0;  m[13] = 0;  m[14] = qn;  m[15] = 0;
}

Die Formel für verschiedene perspektivische Matrizen wird dokumentiert in die Direct3D Dokumentation .

Hier ist die Formulierung für D3DXMatrixPerspectiveFovRH die rechtshändige perspektivische Projektions matrix:

yScale = cot(fovY/2)
xScale = yScale / aspect ratio

xScale     0          0              0
0        yScale       0              0
0        0        zf/(zn-zf)        -1
0        0        zn*zf/(zn-zf)      0
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top