简体   繁体   English

如何在顶点着色器外部将顶点与模型矩阵相乘

[英]How to multiply vertices with model matrix outside the vertex shader

I am using OpenGL ES2 to render a fairly large number of mostly 2d items and so far I have gotten away by sending a premultiplied model/view/projection matrix to the vertex shader as a uniform and then multiplying my vertices with the resulting MVP in there. 我正在使用OpenGL ES2来渲染相当多的大部分2d项,到目前为止,我已经通过将预乘模型/视图/投影矩阵统一发送到顶点着色器,然后将顶点与其中的结果MVP相乘而逃脱了。

All items are batched using texture atlases and I use one MVP per batch. 所有项目均使用纹理地图集进行批处理,我每批使用一个MVP。 So all my vertices are relative to the translation of that MVP. 所以我所有的顶点都与那个MVP的翻译有关。

Now I want to have rotation and scaling for each of the separate items, which means I need a different model for each of them. 现在,我想对每个单独的项目进行旋转和缩放,这意味着我需要为每个项目使用不同的模型。 So I modified my vertex to include the model (16 floats!) and added a mat4 attribute in my shader and it all works well. 因此,我修改了顶点以包括模型(16个浮点!),并在着色器中添加了mat4属性,并且一切正常。 But I'm kinda dissapointed with this solution since it dramatically increased the vertex size. 但是我对此解决方案有点失望,因为它极大地增加了顶点大小。

So as I was staring at my screen trying to think of a different solution I thought about transforming my vertices to world space before I send them over to the shader. 因此,当我盯着屏幕尝试尝试一种不同的解决方案时,我想到了在将顶点发送到着色器之前将其转换为世界空间的想法。 Or even to screen space if its possible. 甚至在可能的情况下筛选屏幕空间。 The vertices I use are unnormalized coordinates in pixels. 我使用的顶点是以像素为单位的非标准化坐标。

So the question is, is such a thing possible? 所以问题是,这可能吗? And if yes how do you do it? 如果是的话,您该怎么做? I can't think why it shouldn't be since its just maths but after a fairly long search on google, it doesn't look like a lot of people are actually doing this... 我不认为为什么不应该这样做,因为它只是数学运算,但是在google上搜索了相当长的时间之后,似乎并没有很多人真正地这样做过...

Strange cause if it is indeed possible, it would be quite a major optimization in cases like this one. 如果确实有奇怪的原因,那么在这种情况下,这将是一个重大的优化。

If the number of matrices per batch are limited then you can pass all those matrices as uniforms (preferably in a UBO) and expand the vertex data with an index which specifies which matrix you need to use. 如果每批矩阵的数量有限,则可以将所有这些矩阵统一传递(最好在UBO中),并用指定指定要使用的矩阵的索引扩展顶点数据。

This is similar to GPU skinning used for skeletal animation . 这类似于用于骨骼动画的 GPU蒙皮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM