现代 OpenGL:VBO、GLM 和矩阵堆栈
在搜索和阅读有关 Modern OpenGL 以升级我现有的项目后,我有点困惑,因为我的 3D 框架基于 OpenGL 2.1.
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.
所以,据我所知...
我们需要从顶点、索引、法线、颜色、紫外线等生成我们的顶点缓冲区对象.
We need to generate our Vertex-Buffer-Objects from vertices, indices, normals, colors, uvs, etc.
然后我们可以使用 GLM 进行矩阵变换,我们只使用 VBO 来创建或操作网格,最后我们像这样将所有内容传递到 GLSL 顶点着色器中...
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 和 C++ 实现与 pushMatrix/popMatrix 相同的结果
堆栈 > 库?
If not possible, then how to achieve same result as pushMatrix/popMatrix by using GLM and C++ < stack > library?
假设我需要这样的东西:
Lets say I need something like this:
> 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
推荐答案
- 我们需要从顶点、索引、法线、颜色、紫外线等生成我们的顶点缓冲区对象.
实际上没有必要使用 VBO,客户端顶点阵列也可以工作.然而,强烈建议使用 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).
问题:我们如何在没有 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 的矩阵堆栈,那么如何进行层次转换就变得显而易见了:在层次结构中的每个分支上制作一个转换矩阵的副本并对其进行操作.您将获得一个分层的变换树,在每个节点处存储了相应的矩阵.然后将这些矩阵作为统一体传递给顶点着色器;或者如果您正在绘制一个只有一个变换的刚性对象,则只需一个矩阵.多个矩阵,您通常只需要像这样的角色的骨骼动画之类的可变形
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.
相关文章