GLSL + OpenGL 远离状态机

发布于 2024-10-31 11:53:32 字数 1114 浏览 2 评论 0原文

我开始将我的一个项目从固定管道中移开,因此为了尝试一下,我尝试编写一个着色器,它可以简单地传递 OpenGL 矩阵并用它转换顶点,然后在我知道可行后开始计算我自己的。我认为这将是一个简单的任务,但即使这样也行不通。

我开始使用这个用于普通固定管道的着色器:

void main(void)
{
    gl_Position = gl_ModelViewProjectionMatrix  * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
}

然后将其更改为:

uniform mat4 model_matrix;
uniform mat4 projection_matrix;

void main(void)
{
    gl_Position = model_matrix * projection_matrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
}

然后我像这样检索 OpenGL 矩阵,并使用以下代码将它们传递给着色器:

 [material.shader bindShader];

 GLfloat modelmat[16];
        GLfloat projectionmat[16];
        
        glGetFloatv(GL_MODELVIEW_MATRIX, modelmat);
        glGetFloatv(GL_PROJECTION_MATRIX, projectionmat);
        
            
        glUniformMatrix4fv([material.shader getUniformLocation:"model_matrix"], 1, GL_FALSE, modelmat);
        glUniformMatrix4fv([material.shader getUniformLocation:"projection_matrix"], 1, GL_FALSE, projectionmat );
... Draw Stuff  

出于某种原因,这不会绘制任何内容(我有 95% 的肯定)在我通过它们之前这些矩阵是正确的顺便说一句)有什么想法吗?

I started moving one of my projects away from fixed pipeline, so to try things out I tried to write a shader that would simply pass the OpenGL matrices and transform the vertex with that and then start calculating my own once I knew that worked. I thought this would be a simple task but even this will not work.

I started out with this shader for normal fixed pipeline:

void main(void)
{
    gl_Position = gl_ModelViewProjectionMatrix  * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
}

I then changed it to this:

uniform mat4 model_matrix;
uniform mat4 projection_matrix;

void main(void)
{
    gl_Position = model_matrix * projection_matrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
}

I then retrieve the OpenGL matrices like this and pass them to the shader with this code:

 [material.shader bindShader];

 GLfloat modelmat[16];
        GLfloat projectionmat[16];
        
        glGetFloatv(GL_MODELVIEW_MATRIX, modelmat);
        glGetFloatv(GL_PROJECTION_MATRIX, projectionmat);
        
            
        glUniformMatrix4fv([material.shader getUniformLocation:"model_matrix"], 1, GL_FALSE, modelmat);
        glUniformMatrix4fv([material.shader getUniformLocation:"projection_matrix"], 1, GL_FALSE, projectionmat );
... Draw Stuff  

For some reason this does not draw anything (I am 95% positive those matrices are correct before I pass them btw) Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花浅忆 2024-11-07 11:53:32

问题是我的矩阵乘法顺序错误。我不知道这些操作是不可交换的。

正确的顺序应该是:

projection * modelview * vertex

感谢 ltjax 和 doug65536

The problem was that my order of matrix multiplication was wrong. I was not aware that the operations were not commutative.

The correct order should be:

projection * modelview * vertex

Thanks to ltjax and doug65536

堇年纸鸢 2024-11-07 11:53:32

对于矩阵数学,请尝试使用外部库,例如 GLM。他们还有一些关于如何创建必要矩阵的基本示例投影*视图*模型变换。

For the matrix math, try using an external library, such as GLM. They also have some basic examples on how to create the necessary matrices and do the projection * view * model transform.

东北女汉子 2024-11-07 11:53:32

使用OpenGL 3.3的着色语言。在硬件方面,OpenGL 3.3 与 DirectX10 大致相当。

不要使用已弃用的功能。第一个 void main 示例中的几乎所有内容都已被弃用。如果您希望使用驱动程序的高性能代码路径,则必须明确声明您的输入和输出。已弃用的功能也更有可能充满驱动程序错误。

使用更新、更明确的方式来声明输入和输出,并将它们设置在代码中。确实不错。我认为这会很丑陋,但实际上很简单(我希望我早点完成)。

仅供参考,我上次查看 OpenGL 的最低公分母(2012 年)时是 OpenGL 3.3。实际上,AMD 和 NVidia 的所有具有游戏功能的显卡都支持 OpenGL 3.3。他们已经有一段时间了,因此您现在为 OpenGL 3.3 编写的任何代码都可以在典型的低端或更好的 GPU 上运行。

Use OpenGL 3.3's shading language. OpenGL 3.3 is roughly comparable to DirectX10, hardware-wise.

Don't use the deprecated functionality. Almost everything in your first void main example is deprecated. You must explicity declare your inputs and outputs if you expect to use the high-performance code path of the drivers. Deprecated functionality is also far more likely to be full of driver bugs.

Use the newer, more explicit style of declaring inputs and outputs and set them in your code. It really isn't bad. I thought this would be ugly but it actually was pretty easy (I wish I had just done it earlier).

FYI, the last time I looked at a lowest common denominator for OpenGL (2012), it was OpenGL 3.3. Practically all video cards from AMD and NVidia that have any gaming capability will have OpenGL 3.3. And they have for a while, so any code you write now for OpenGL 3.3 will work on a typical low-end or better GPU.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文