着色器破坏相机

Posted

技术标签:

【中文标题】着色器破坏相机【英文标题】:Shader breaks camera 【发布时间】:2014-05-02 12:06:32 【问题描述】:

我为我的 VBO 立方体(着色)制作了一个着色器,但遇到了一些问题。

启用着色器时,我无法移动相机。

启用着色器后我的相机坏了。

Cube.vert:

#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;

// Notice that the "1" here equals the "1" in glVertexAttribPointer
layout(location = 1) in vec3 vertexColor;

// Output data ; will be interpolated for each fragment.
out vec3 fragmentColor;

void main()

    gl_Position.xyz = vertexPosition_modelspace;
    gl_Position.w = 1.0;

    // The color of each vertex will be interpolated
    // to produce the color of each fragment
    fragmentColor = vertexColor;

Cube.frag:

#version 330 core

// Interpolated values from the vertex shaders
in vec3 fragmentColor;

// Ouput data
out vec3 color;

void main()
    // Output color = color specified in the vertex shader,
    // interpolated between all 3 surrounding vertices
    color = fragmentColor;

【问题讨论】:

你在说什么相机? 您可能会“移动”相机,但因为您没有在着色器中进行任何世界/投影计算,所以它没有任何效果。如果vertexPosition_modelspace 不包含预投影矩阵,您只需使用它的模型数据将立方体绘制到屏幕上。 gl_Position 应该是 projection * view * model,而不仅仅是 model 我对着色器很陌生,我该怎么做? 阅读http://open.gl/transformations。不要被它背后的所有数学运算所拖延,如果你需要,你可以跳过那部分并让它工作,而无需完全了解幕后实际发生的事情。但要知道,“相机”基本上是您将模型数据乘以的视图/投影制服。 我知道相机是什么,但不知道如何将其应用于着色器:) 【参考方案1】:

尝试:投影*视图*模型

它们都是 3 Mat4 模型将是:1

然后将这些东西推回着色器。

【讨论】:

以上是关于着色器破坏相机的主要内容,如果未能解决你的问题,请参考以下文章

数学上相同的变量 - 一个破坏着色器而不是

着色器中的 OpenGL 法线变换

如何避免使用着色器在 OpenGL 中消失线?

OpenGL gluLookat 不适用于着色器

无法从 OpenGL 中的着色器访问先前渲染的纹理

着色器材质透明度