OpenGL ES 2.0纹理显示为黑色

时间:2012-01-27 21:49:49

标签: c graphics opengl-es opengl-es-2.0

我正在使用OpenGL ES 2.0为嵌入式设备开发应用程序。

这是我的片段着色器:

varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main() {
  gl_FragColor = texture2D(s_texture, v_texCoord);
}

我正确设置了纹理。出于某种原因,调用glTexImage2D并不会产生我正在寻找的结果。纹理完全是黑色的,而不是填充我提供的数据。

这就是我创建纹理的方法:

   GLuint textureId;

   // 2x2 Image, 3 bytes per pixel (R, G, B)
   GLubyte pixels[6 * 3] =
   {  
      255,   0,   0, // Red
        0, 255,   0, // Green
        0,   0, 255, // Blue
      255, 255,   0,  // Yellow
        0, 255, 255,
        255, 0, 255
   };

   // Use tightly packed data
   glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );

   // Generate a texture object
   glGenTextures ( 1, &textureId );

   // Bind the texture object
   glBindTexture ( GL_TEXTURE_2D, textureId );

   // Load the texture
   glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, 3, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels );


   // Set the filtering mode
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

之后,我像这样绑定纹理:

samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
glActiveTexture ( GL_TEXTURE0 );
glBindTexture ( GL_TEXTURE_2D, textureId );

// Set the sampler texture unit to 0
glUniform1i ( samplerLoc, 0 );

我确认顶点和纹理坐标是绑定的,并在调试时传递给着色器。因此,它必须是s_textureglTexImage2D函数本身的问题。

2 个答案:

答案 0 :(得分:7)

对于U和V尺寸,您需要将钳制模式设置为CLAMP_TO_EDGE,否则纹理不完整并将采样为黑色。

答案 1 :(得分:2)

也许我错过了一些明显的东西但如果你只存储2 x 2像素,每种颜色3个字节(= 4 x 3字节= 12个字节)作为纹理;那么“GLubyte像素[6 * 3]”中的6来自哪里?

6 * 3等于18和!= 2的幂

  

//加载纹理glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, 3,,0,   GL_RGB,GL_UNSIGNED_BYTE,pixels);

从规格: glTexImage2D(GLenum target,GLint level,GLint internalFormat, GLsizei width,GLsizei height ,GLint border,GLenum format,GLenum type,const GLvoid * data);

Bolded vars最好是^ 2 ......

所以,试试这个:

  

//加载纹理glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, 2,2 ,0,   GL_RGB,GL_UNSIGNED_BYTE,pixels);

从像素数组中删除最后2行,然后将所有值更改为“255”;如果你得到一个白色的纹理,这意味着它正在工作。

另一个例子:

  

GLubyte像素[4 * 3] = {

     

255,0,0,//红色//绿色//蓝色//像素0

     

255,0,0,//红色//绿色//蓝色//像素1

     

255,0,0,//红色//绿色//蓝色//像素2

     

255,0,0 //红色//绿色//蓝色//像素3

     

};

一切都显示为红色。

如果不这样做,也许我的代码会有所帮助:

void CMesh::renderMesh(GLuint program, glm::mat4 *mvp){

glUseProgram(program);

int mvpLocation = glGetUniformLocation(program, "matViewProjection");
int texLocation = glGetUniformLocation(program, "baseMap");
glUniformMatrix4fv( mvpLocation, 1, GL_FALSE, glm::value_ptr(*mvp));

int vertexAttribLocation = glGetAttribLocation(program, "vertex");
int uvAttribLocation = glGetAttribLocation(program, "texturecoordinate");

// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureIndex);
glUniform1i(texLocation, 0);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(
   vertexAttribLocation,            
   3,                               // size
   GL_FLOAT,                        // type
   GL_FALSE,                        // normalized?
   0,                               // stride
   (void*)0                         // array buffer offset
);

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
glVertexAttribPointer(
   uvAttribLocation,                  
   2,                  // size
   GL_FLOAT,           // type
   GL_FALSE,           // normalized?
   0,                  // stride
   (void*)0            // array buffer offset
);

// Draw the triangles !
glDrawArrays(GL_TRIANGLES, 0, vertices.size() );

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
};
相关问题