1

据我所知,我第一次尝试在三角形上绘制纹理的设置正确,但它显示为全黑。

我将图像发送到 OpenGL,如下所示:

   GLuint gridTexture;
    glGenTextures(1, &gridTexture);
    glBindTexture(GL_TEXTURE_2D, gridTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x,
                 size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

虽然我不确定如何测试“像素”是否符合我的预期,但我确实知道我正在使用的 PNG 的size.xsize.y变量正在正确记录,所以我假设像素也运行良好,因为它们都是在我的资源加载器中一起提取

我的着色器很简单:

attribute vec4 Position;
attribute vec4 SourceColor;
attribute vec2 TextureCoordinate;

varying vec4 DestinationColor;
varying vec2 TextureCoordOut;

uniform mat4 Projection;
uniform mat4 Modelview;

void main(void)
{
    DestinationColor = SourceColor;

    gl_Position=Projection*Modelview*Position;
    TextureCoordOut = TextureCoordinate;
}

分段:

varying lowp vec4 DestinationColor;
varying mediump vec2 TextureCoordOut;

uniform sampler2D Sampler;

void main(void)
{
    gl_FragColor = texture2D(Sampler, TextureCoordOut) * DestinationColor;
//        gl_FragColor = DestinationColor; //this works and I see varied colors fine
}

我从客户端内存中发送纹理坐标,如下所示:

glEnableVertexAttribArray(textCoordAttribute));    
glVertexAttribPointer(textCoordAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(vec2),&texs[0]);

带有纹理坐标的三角形及其顶点是这样的;我知道坐标没有抛光,我只想在屏幕上看到一些东西:

//omitting structures that I use to hold my vertex data, but you can at least see the vertices and the coordinates I am associating with them. The triangle draws fine, and if I disable the texture2D() function in the frag shader I can see the colors of the vertices so everything appears to be working except the texture itself.

    top.Color=vec4(1,0,0,1);
    top.Position=vec3(0,300,0);
    texs.push_back(vec2(0,1));

    right.Color=vec4(0,1,0,1);
    right.Position=vec3(300,0,0);
    texs.push_back(vec2(1,0));

    left.Color=vec4(0,0,1,1);
    left.Position=vec3(-300,0,0);
    texs.push_back(vec2(0,0));

    verts.push_back(top);
    verts.push_back(right);
    verts.push_back(left);

为了更好地衡量,我尝试在绘制之前再次使用 glBindTexture 绑定纹理以使其处于“活动状态”,但这没有任何区别。

我认为可能有一个非常简单的步骤我没有在某个地方做,但我在任何地方都找不到它。

4

3 回答 3

3

对于纹理显示为黑色的人,我发现的另一个原因是,如果您在创建纹理时(在 glTexImage2D 之前)没有设置这些简单的参数,则纹理显示为黑色

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
于 2019-02-07T21:27:12.490 回答
0

通过使我的纹理尺寸的长度和宽度为 2 的幂来解决该问题。

于 2013-04-18T08:11:22.487 回答
-3

在设置过滤器之前,您必须将数据绑定到纹理。您必须先调用 glTexImage2D

GLuint gridTexture;
glGenTextures(1, &gridTexture);
glBindTexture(GL_TEXTURE_2D, gridTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x,
                 size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);  
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
于 2013-04-18T08:32:36.127 回答