【问题标题】:bizarre GLSL texture coordinates奇怪的 GLSL 纹理坐标
【发布时间】:2013-04-03 04:37:09
【问题描述】:

我一直在尝试使用 SOIL 进行简单的纹理映射,但输出结果很奇怪..

仅在加载 PNG 纹理时显示输出。 (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);
}

所有制服和属性都经过验证。

尝试渲染的纹理:

(是 128x128,2 的幂。)

输出[使用普通着色器]:

但是,我认为问题完全在于我尝试调试它时发生的一些非常奇怪的事情。

我将片段着色器更改为:

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

得到这个结果: 纹理坐标出了点问题,因为根据着色器,Y 现在是 X,而 X 不再存在。 谁能解释一下?

如果我的纹理坐标定位正确,那么我将开始查看另一个图像库..

[编辑] 我尝试通过原始 gimp 生成的数据加载图像,但它遇到了同样的问题。 就好像纹理坐标是一维的..

【问题讨论】:

  • 您应该添加设置顶点流的方式。 glvertexattribpointer 等调用。
  • 通过在调试着色器中描绘纹理坐标,很明显它与纹理图像根本没有任何关系。我第二个 starmole 在显示实际顶点格式设置(和绘图)的情况下对于解决问题至关重要。
  • 你的#version指令在哪里?

标签: opengl coordinates textures


【解决方案1】:

发现问题了!感谢 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));

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

现在一切正常。

这么小的错别字竟然能把它弄得这么糟糕,真是令人惊讶。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 2015-09-30
    • 2012-03-05
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多