C++ gluPerspective 在 OpenGL 3.1 中被移除,有什么替代品吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2417697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gluPerspective was removed in OpenGL 3.1, any replacements?
提问by ufk
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.
我正在尝试在网上阅读一些 OpenGL 教程。问题是我发现了一些使用gluPerspective()
. gluPerspective 在 OpenGL 3.0 中被弃用并在 3.1 中被移除。
What function can I use instead?
我可以改用什么功能?
I'm using C++ with latest FreeGlut installed.
我正在使用安装了最新 FreeGlut 的 C++。
采纳答案by Dan
You have to compute the matrix manually and then pass it to OpenGL.
您必须手动计算矩阵,然后将其传递给 OpenGL。
Computing the matrix
计算矩阵
This snippet of code is based on the gluPerspective documentation.
这段代码基于gluPerspective 文档。
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;
}
There is a C++ library called OpenGL Mathematicsthat may be useful.
有一个名为OpenGL Mathematics的 C++ 库可能很有用。
Loading the Matrix in OpenGL 3.1
在 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 glUniform4fvonto the video card.
我还是 OpenGL 3.1 API 的新手,但您需要更新 GPU 上的矩阵,然后在顶点着色器中使用它来获得正确的视角。以下代码仅使用glUniform4fv将矩阵加载到视频卡上。
{
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;
}
回答by Jherico
Use GLM.
使用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));
Note, if you have GLM_FORCE_RADIANS
defined then you should use radians in the perspective function, not degrees...
请注意,如果您已经GLM_FORCE_RADIANS
定义,那么您应该在透视函数中使用弧度,而不是度数...
glm::mat4 projection = glm::perspective(
// FOV & aspect
PI / 3.0f, 16.0f / 10.0f,
// Near and far planes
0.001f, 1000f);
回答by Toji
Copied from one of my older projects:
从我的旧项目之一复制:
// 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 );
回答by shade4159
This is a modified version of Dan's function, with simplified calculations from Unspecified Behavior.
这是 Dan 函数的修改版本,使用来自Unspecified Behavior 的简化计算。
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;
}
回答by bobobobo
The formula for various perspective matrices is documented in the Direct3D documentation.
各种透视矩阵的公式记录在Direct3D 文档中。
Here is the formulation for D3DXMatrixPerspectiveFovRH
, the right-handed perspective projection matrix:
这是D3DXMatrixPerspectiveFovRH
右手透视投影矩阵的公式:
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