【发布时间】:2013-08-06 14:13:17
【问题描述】:
我已经为我正在开发的一个小型 openGL 2.0 应用程序编写了自己的顶点和片段着色器。 一切似乎都很好,除了一件事,每个顶点的纹理坐标似乎是 (0, 0)。
当我不使用自己的着色器时,即当我使用openGL的默认着色器时,那么一切都被绘制出来了 很好。
当我激活我自己的着色器时,形状仍然可以,顶点位置是正确的。但是纹理坐标都变成了(0, 0)。
这是顶点着色器的代码:
in vec2 position;
in vec2 texcoord;
out vec2 coord;
void main(void) {
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 0, 1);
coord = vec2(gl_TextureMatrix[0] * vec4(texcoord, 1, 1));
}
这是片段着色器:
uniform sampler2D texture;
uniform float redFactor;
uniform float greenFactor;
uniform float blueFactor;
in vec2 coord;
void main(void) {
vec4 color = texture2D(texture, coord);
float grey = (color.r * redFactor + color.g * greenFactor + color.b * blueFactor);
gl_FragColor = vec4(grey, grey, grey, color.a);
}
同样,它可以正常工作没有我自己的着色器。所以 VBO 的设置是正确的。
顺便说一句,texcoords 正确地从顶点着色器传递到片段着色器。 例如,当我更改行时
coord = vec2(gl_TextureMatrix[0] * vec4(texcoord, 1, 1));
在顶点着色器中这样做:
coord = vec2(0.5, 0.5);
我得到了正确的结果。
这是我的 VBO 内容的样子:
[0.0, 0.0,
0.0, 0.0,
0.0, 1.0,
0.0, 1.0,
1.0, 1.0,
1.0, 1.0,
1.0, 0.0,
1.0, 0.0]
这是绘图的 IBO:
[0, 1, 2, 2, 3, 0]
这里是指针:
VERTEX_ARRAY_POINTER(size = 2, stride = 16, offset = 0),
TEXCOORD_ARRAY_POINTER(size = 2, stride = 16, offset = 8)
编辑:顺便问一下,这个编辑器的换行有什么问题?
【问题讨论】:
-
换行符用两个空格完成,如果你是这个意思的话。
-
您的
#version指令在哪里?in/out在110中无效。着色器编译/链接日志中有任何内容吗?
标签: opengl glsl shader fragment-shader