现代OpenGL:VBO,GLM和矩阵堆栈 [英] Modern OpenGL: VBO, GLM and Matrix Stacks

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

问题描述

在搜索和阅读Modern OpenGL以升级现有项目之后,我有点困惑,因为我的基于OpenGL 2.1的3D框架。

After searching and reading about Modern OpenGL in order to upgrade my existing project, I'm a bit confused, since my 3D framework based on OpenGL 2.1.


  • 我们需要从顶点,索引,法线生成顶点缓冲区对象,颜色,uvs等。

  • We need to generate our Vertex-Buffer-Objects from vertices, indices, normals, colors, uvs, etc.

然后我们可以使用GLM进行矩阵转换,我们只使用VBO来创建或操作网格, GLSL vertex shader like this ...

then we can use GLM for matrix transformation, and we only use VBO to create or manipulate meshes, finally we pass everything into GLSL vertex shader like this...

glm::mat4 MVP = projection * view * model;
glUniformMatrix4fv(glGetUniformLocation(shaderProgramID, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); //or &MVP[0][0]

// uniform mat4 MVP;
// in vec3 Vertex;
// void main(void)
// {
//    gl_Position = MVP * vec4(Vertex, 1.0); //instead of ftransform();
// }


我们如何做没有pushMatrix / popMatrix的层次转换? (或者我们可以通过使用我们的VBO进行分层转换,是可能吗?)

QUESTION: How we do hierarchical transformations without pushMatrix/popMatrix? (or maybe we do hierarchical transformation by using our VBOs, is it possible?)

如果不可能,那么如何使用GLM和pushMatrix / popMatrix C ++& stack> library?

If not possible, then how to achieve same result as pushMatrix/popMatrix by using GLM and C++ < stack > library?

假设我需要这样的:

> Set identity
> Translate to X, Y, Z
> Draw Mesh 1
> Rotate 0.5 by X axis
> Draw Mesh 2
> Scale down to 0.1
> Draw Mesh 3


推荐答案



  • 我们需要从顶点,索引,法线,颜色,uvs等生成顶点缓冲区对象。

这不是真的需要使用VBOs,客户端顶点数组也可以工作。但是强烈建议使用VBO,因为它使得驱动程序的生命更容易,并且从长远来看也是你的必须要操作数据的人。代码开销是可以忽略的(它与生成和上传纹理数据大致相同),性能只会增加。

It's not really necessary to use VBOs, client side Vertex Arrays do work as well. However it's strongly recommended to use VBO, because it makes the life of the driver easier and in the long run also your's as the one who has to juggle the data. The code overhead is neglectible (it's about the same as generating and uploading texture data) and performance will only increase.



  • 然后我们可以使用GLM进行矩阵转换,我们只使用VBO创建或操作网格,最后我们将所有东西传递到GLSL顶点着色器中,像这样...

您不限于GLM。任何矩阵数学库都会做。如果你正在寻找可以在C99中使用的东西,请查看我的(仍然不完整) linmath.h https://github.com/datenwolf/linmath.h ,它只是一个带有 static inline 函数的头文件。如果代码重复对性能有负面影响(代码大小产生L1缓存压力),我还没有进行基准测试。

You're not limited to GLM. Any matrix math library will do. If you're looking for something you can use in C99, have a look at my (still incomplete) linmath.h https://github.com/datenwolf/linmath.h which is just a header file with static inline functions. I've yet to benchmark if the code duplication has a negative impact on performance (code size creates L1 cache pressure).


QUESTION:我们如何在没有pushMatrix / popMatrix的情况下执行层次化转换? (或者我们可以通过使用我们的VBO进行层次转换,是可能的吗?)

QUESTION: How we do hierarchical transformations without pushMatrix/popMatrix? (or maybe we do hierarchical transformation by using our VBOs, is it possible?)

VBO与此无关。给大多数老式OpenGL用户的麻烦是那些矩阵堆栈函数,这使得OpenGL看起来有点像一个场景图。但它不是。

The VBOs have nothing to do with this. What gives most users of old fashioned OpenGL trouble are those matrix stack functions, which make OpenGL look a bit like a scene graph. But it is not.

如果你忘记了旧的OpenGL的矩阵堆栈,它变得明显如何做层次化转型:在层次结构中的每个分支,转换矩阵并对其进行运算。你得到一个变换的分层树,在每个节点存储相应的矩阵。然后你将这些矩阵作为uniforms传递给顶点着色器;或者只是一个矩阵,如果你画一个刚性的对象,只有一个转换。多个矩阵你通常只需要变形,像一个字符的骨骼动画像这样

If you forget about the matrix stack of old OpenGL, it becomes obvious how to do hierarchical tranformations: At each branch in the hierarchy make a copy of the transformation matrix and operate on that. You get a hierarchical tree of transformations, at each node the corresponding matrix stored. Then you pass those matrices as uniforms to the vertex shader; or just one matrix if you're drawing a rigid object that has only one transformation. Multiple matrices you normally only need for deformables like skeletal animation of a character like this

worldtransform -> 
    pelvis ->
        left upper leg -> left lower leg -> left foot
        right upper leg -> right lower leg -> right foot
    torso ->
        neck -> head ->
             left eye
             right eye
             facial deformation // this is a whole chapter of it's own
        left upper arm -> left lower arm -> left hand
        right upper arm -> right lower arm -> right hand

每次你加入一个 - > 在这样的层次,你制作一个矩阵的副本,并继续工作在那一个。当回到树的更高层时,你再次从该矩阵开始工作。

Everytime you enounter a -> in such a hierachy you make a copy of the matrix and proceed working on that one. When falling back to a higher level of the tree you start working from that matrix again.

这篇关于现代OpenGL:VBO,GLM和矩阵堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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