质地是全黑的

问题描述 投票:0回答:3

至于我可以告诉大家,我的第一次尝试画上一个三角形正在设置正确的纹理,但它显示为全黑。

我的图像发送到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);

虽然我不知道如何测试“像素”保持我的期望,我不知道size.xsize.y变量对我使用这样PNG正确登录我假设的像素也运作良好,因为他们在我的资源加载器都一起提取

我的着色器很简单:

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再结合质感但并没有区别。

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

opengl-es opengl-es-2.0
3个回答
0
投票

这个问题是通过使我的纹理尺寸在长度和宽度的2的幂解决。


0
投票

对于人患的纹理显示出来的黑色,另一个原因是我发现的是,如果创建纹理(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);

-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);
© www.soinside.com 2019 - 2024. All rights reserved.