OpenGL中的矩阵堆栈已弃用? [英] Matrix stacks in OpenGL deprecated?

查看:139
本文介绍了OpenGL中的矩阵堆栈已弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读过这篇文章:

" OpenGL提供了支持 使用标准矩阵堆栈管理坐标转换和投影 (GL_MODELVIEW和GL_PROJECTION).但是,在核心OpenGL 4.0中,所有功能 支撑矩阵堆栈的支撑架已被移除.因此,我们有责任提供自己的 支持通常的变换和投影矩阵,然后将它们传递给我们 着色器."

"OpenGL provided support for managing coordinate transformations and projections using the standard matrix stacks (GL_MODELVIEW and GL_PROJECTION). In core OpenGL 4.0, however, all of the functionality supporting the matrix stacks has been removed. Therefore, it is up to us to provide our own support for the usual transformation and projection matrices, and then to pass them into our shaders."

这很奇怪,所以现在如何设置模型视图和投影矩阵?我应该创造 它们在opengl应用程序中,然后将顶点着色器中的顶点与矩阵相乘?

This is strange, so how do I set the modelview and projection matrices now? I should create them in the opengl app and then multiply the vertices in the vertex shader with the matrices?

推荐答案

这很奇怪

不.固定功能已由可编程管线取代,可让您根据需要设计转换.

Nope. Fixed function was replaced by programmable pipeline that lets you design your transformations however you want.

我应该在opengl应用程序中创建它们,然后将顶点着色器中的顶点与矩阵相乘?

I should create them in the opengl app and then multiply the vertices in the vertex shader with the matrices?

如果您想要具有与旧的OpenGL矩阵堆栈对一样的功能,那么您希望使顶点着色器看起来像这样,

If you want to have something that would work just like the old OpenGL pair of matrix stacks, then you'd want to make your vertex shader look, for instance, like:

in vec4 vertexPosition; 
// ...

uniform mat4 ModelView, Projection;

void main() {
    gl_Position = Projection * ModelView * vertexPosition;
    // ...
}

(当然,您可以对其进行一些优化)

相应的客户端大小代码(此处显示为C ++)将是:

And the corresponding client-size code (shown as C++ here) would be like:

std::stack<Matrix4x4> modelViewStack;
std::stack<Matrix4x4> projectionStack;

// Initialize them by
modelViewStack.push(Matrix4x4.Identity());
projectionStack.push(Matrix4x4.Identity());

// glPushMatrix:
stack.push(stack.top());
    // `stack` is either one stack or the other;
    // in old OpenGL you switched the affected stack by glMatrixMode

// glPopMatrix:
stack.pop();

// glTranslate and family:
stack.top().translate(1,0,0);

// And in order to pass the topmost ModelView matrix to a shader program:
GLint modelViewLocation = glGetUniformLocation(aLinkedProgramObject,
                                               "ModelView");
glUniformMatrix4fv(modelViewLocation, 1, false, &modelViewStack.top());

我在这里假设您有一个Matrix4x4类,该类支持类似.translate()的操作.像 GLM 之类的库可以为您提供行为类似于相应GLSL类型的矩阵和向量的客户端实现,以及实现gluPerspective之类的功能.

I've assumed here that you have a Matrix4x4 class that supports operations like .translate(). A library like GLM can provide you with client-side implementations of matrices and vectors that behave like corresponding GLSL types, as well as implementations of functions like gluPerspective.

您还可以通过OpenGL兼容性配置文件继续使用OpenGL 1功能,但是不建议这样做(那么您将不会使用OpenGL的全部功能).

You can also keep using the OpenGL 1 functionality through the OpenGL compatibility profile, but that's not recommended (you won't be using OpenGL's full potential then).

OpenGL 3(和4)的界面比OpenGL 1更底层.如果您认为以上代码太多,那么最好使用Irrlicht之类的渲染引擎.

OpenGL 3 (and 4)'s interface is more low level than OpenGL 1; If you consider the above to be too much code, then chances are you're better off with a rendering engine, like Irrlicht.

这篇关于OpenGL中的矩阵堆栈已弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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