gluPerspective在OpenGL 3.1中删除,任何替换? [英] gluPerspective was removed in OpenGL 3.1, any replacements?

查看:113
本文介绍了gluPerspective在OpenGL 3.1中删除,任何替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网上阅读一些OpenGL教程。问题是我发现一些旧的使用 gluPerspective()。 gluPerspective在OpenGL 3.0中已弃用,并在3.1中删除。



我可以使用什么函数?



m使用C ++安装最新的FreeGlut。

解决方案

你必须手动计算矩阵,然后传递给OpenGL。 >

计算矩阵



这段代码从 geeks3d via

  void BuildPerspProjMat(float * m,float fov,float方面,
float znear,float zfar)
{
float ymax = znear * tan(fov * PI_OVER_360);
float ymin = -ymax;
float xmax = ymax * aspect;
float xmin = ymin * aspect;

float width = xymax-xmin;
float height = xymax - ymin;

float depth = zfar - znear;
float q = - (zfar + znear)/ depth;
float qn = -2 *(zfar * znear)/ depth;

float w = 2 * znear / width;
w = w / aspect;
float h = 2 * znear / height;

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;
}

有一个C ++库叫做

在OpenGL 3.1中加载矩阵



我仍然是OpenGL 3.1 API的新手,但是你需要在GPU上更新矩阵,然后在顶点着色器中使用它来获得正确的透视效果。以下代码只是使用 glUniform4fv 加载到视频卡上的矩阵。

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

一个来自随机的简单顶点着色器博客(通过堆栈溢出发现)。

 属性vec4一个位置; 
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;
}


I'm trying to read some OpenGL tutorials on the net. the problem is that I found some old ones that use gluPerspective(). gluPerspective was deprecated in OpenGL 3.0 and removed in 3.1.

What function can I use instead?

I'm using C++ with latest FreeGlut installed.

解决方案

You have to compute the matrix manually and then pass it to OpenGL.

Computing the matrix

This snippet of code was lifted from geeks3d via opengl.org.

 void BuildPerspProjMat(float *m, float fov, float aspect,
 float znear, float zfar)
 {
  float ymax = znear * tan(fov * PI_OVER_360);
  float ymin = -ymax;
  float xmax = ymax * aspect;
  float xmin = ymin * aspect;

  float width = xymax - xmin;
  float height = xymax - ymin;

  float depth = zfar - znear;
  float q = -(zfar + znear) / depth;
  float qn = -2 * (zfar * znear) / depth;

  float w = 2 * znear / width;
  w = w / aspect;
  float h = 2 * znear / height;

  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;
 }

There is a C++ library called OpenGL Mathematics that may be useful.

Loading the Matrix in OpenGL 3.1

I am still new to the OpenGL 3.1 API, but you need to update a matrix on the GPU and then make use of it in your vertex shader to get the proper perspective. The following code just loads the matrix using glUniform4fv onto the video card.

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

A simple vertex shader from a random blog (found through stack overflow).

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;
}

这篇关于gluPerspective在OpenGL 3.1中删除,任何替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆