OpenGL顶点着色器转换和输出
我有一个简单的顶点着色器
#version 330 core
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
in vec3 in_Position;
out vec3 pass_Color;
void main(void)
{
//gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
gl_Position = vec4(in_Position, 1.0);
pass_Color = vec3(1,1,1);
}
在我的客户端代码中
glm::vec4 vec1(-1,-1,0,1);//first
glm::vec4 vec2(0,1,0,1);//second
glm::vec4 vec3(1,-1,0,1);//third
glm::mat4 m = projectionMatrix * viewMatrix * modelMatrix;
//translate on client side
vec1 = m * vec1;
vec2 = m * vec2;
vec3 = m * vec3;
//first vertex
vertices[0] = vec1.x;
vertices[1] = vec1.y;
vertices[2] = vec1.z;
//second
vertices[3] = vec2.x;
vertices[4] = vec2.y;
vertices[5] = vec2.z;
//third
vertices[6] = vec3.x;
vertices[7] = vec3.y;
vertices[8] = vec3.z;
现在我的问题是,如果我在着色器中不使用矩阵乘法并且在客户端代码中不使用矩阵乘法,这将呈现一个漂亮的三角形,它延伸整个屏幕,所以我将其作为顶点着色器映射在具有 x=-1..1
和 y=-1..1
的坐标系中将其给予屏幕的坐标
如果我在着色器中进行矩阵乘法,则一切效果很好。但是,如果我像所示注释掉着色器中的代码并在客户端上执行此操作,我会得到奇怪的结果。它不应该产生相同的结果吗?
我是否错误地认为顶点着色器 gl_Position
的输出是 2D 坐标,尽管它是 vec4
?
感谢您的任何帮助。我真的很想了解顶点着色器的输出在顶点位置方面到底是什么。
I have a simple vertex shader
#version 330 core
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
in vec3 in_Position;
out vec3 pass_Color;
void main(void)
{
//gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
gl_Position = vec4(in_Position, 1.0);
pass_Color = vec3(1,1,1);
}
In my client code i have
glm::vec4 vec1(-1,-1,0,1);//first
glm::vec4 vec2(0,1,0,1);//second
glm::vec4 vec3(1,-1,0,1);//third
glm::mat4 m = projectionMatrix * viewMatrix * modelMatrix;
//translate on client side
vec1 = m * vec1;
vec2 = m * vec2;
vec3 = m * vec3;
//first vertex
vertices[0] = vec1.x;
vertices[1] = vec1.y;
vertices[2] = vec1.z;
//second
vertices[3] = vec2.x;
vertices[4] = vec2.y;
vertices[5] = vec2.z;
//third
vertices[6] = vec3.x;
vertices[7] = vec3.y;
vertices[8] = vec3.z;
Now my question if i use no matrix multiplication in the shader and none in client code this will render me a nice triangle which strectch the whole screen, so i take it the vertex shader maps cordinates its given to the screen in a cordinate system with x=-1..1
and y=-1..1
If i do the matrix multiplication in the shader everything works nice. But if i comment out the code in the shader like shown and do it on the client i get odd results. Shouldnt it yield the same result?
Have i gotten it wrong thinking the output of the vertex shader gl_Position
is 2D cordinates despite being a vec4
?
Thanks for any help. I really like to understand what exactly the output of the vertex shader is in terms of vertex position.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在你的着色器中,因为它只接受 3 个位置分量。如果第四个坐标尚未位于投影空间中,则可以将第四个坐标设置为 1(就像您所做的那样)。
当您在客户端空间中进行转换时,结果是正确的 4 分量齐次向量。您只需在顶点着色器中
按原样
使用它们即可:The problem is in your shader as it accepts only 3 components of position. It is OK to set the forth coordinate to 1 (like you do it) if the coordinate is not in projection space yet.
When you are doing the transformation in client space, the results are correct 4-component homogeneous vectors. You just need to use them
as is
in your vertex shader: