2013-04-11 159 views
1

我一直试图做简单的纹理映射与土壤,并在加载PNG纹理我一直有奇怪的输出..离奇GLSL纹理坐标

输出仅显示。 (SOIL_load_OGL_texture) 其他纹理显示为灰色或白色。

顶点通过为:

struct Vertex { 
    float position[3]; 
    float texturepos[2]; 
}; 

Vertex vertices[] = { // w and h are width and height of square. 
    0,0,0,0,0, 
    w,0,0,1,0, 
    0,h,0,0,1, 
    w,0,0,1,0, 
    w,h,0,1,1, 
    0,h,0,0,1 
}; 

顶点着色器:

attribute vec2 texpos; 
attribute vec3 position; 
uniform mat4 transform; 
varying vec2 pixel_texcoord; 
void main(void) { 
    gl_Position=transform * vec4(position,1.0); 
    pixel_texcoord=texpos; 
} 

片段着色器:

varying vec2 pixel_texcoord; 
uniform sampler2D texture; 
void main(void) { 
    gl_FragColor=texture2D(texture,pixel_texcoord); 
} 

制服的所有和验证属性。

纹理试图呈现:

image for render (为128x128,2的幂)

输出[正常着色器]:

output1

然而,我想问题完全在于我尝试调试时发生的奇怪事情。

我改变了片段着色器:

varying vec2 pixel_texcoord; 
uniform sampler2D texture; 
void main(void) { 
    gl_FragColor=vec4(pixel_texcoord.x,0,pixel_texcoord.y,1); 
} 

,并得到结果: output2 东西是非常错误的纹理坐标,如根据着色器,Y现在是X和X不复存在。 任何人都可以解释这一点吗?

如果我的纹理坐标正确定位,然后我就开始寻找另一个图像库..

[编辑]我想通过原始GIMP生成的数据加载图像,并有同样的问题。 就好像纹理坐标是一维的。

+2

你应该添加你设置顶点流的方式。 glvertexattribpointer等调用。 – starmole 2013-04-11 06:00:43

+0

从描绘调试着色器中的纹理坐标可以清楚地看出,它根本不可能与纹理图像有任何关系。我第二* starmole *,在显示实际顶点格式设置(和绘图)的情况下,对于解决问题至关重要。 – 2013-04-11 07:53:52

+0

你的'#版本'指令在哪里? – genpfault 2013-04-11 16:46:05

回答

0

发现问题了!由于starmole的意见,我又看看glVertexAttribPointer调用,被格式化成这样:

glVertexAttribPointer(attribute_vertex,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),0); 
glVertexAttribPointer(attribute_texture_coordinate,2,GL_FLOAT,GL_FALSE,sizeof(Vertex),(void*) (sizeof(GLfloat) * 2)); 

的2 (void*) (sizeof(GLfloat) * 2));应该是一个3,因为有3顶点坐标。

现在一切正常。

令人惊讶的是,这样一个小错字如何破坏它如此糟糕。