OpenGL ES 2.0 texture looks black

I am working on an application using OpenGL ES 2.0 for an embedded device.

This is my fragment shader:

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

I set the textures correctly. For some reason, the call glTexImage2Ddoes not lead to the result I'm looking for. The texture is completely black, and not filled with the data that I provide it.

This is how I create the texture:

   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 );

Then I snap the texture like this:

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

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

I confirmed that the vertex coordinates and textures are connected and passed to the shaders when debugging. So this should be a problem with the function s_textureor glTexImage2D.

+2
source share
2 answers

CLAMP_TO_EDGE U V, .

+6

, - , 2 x 2 3 (= 4 x 3 = 12 ) ; 6 " GLubyte [6 * 3]" ?

6 * 3 18 != 2

// glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 3, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, );

: glTexImage2D (GLenum target, GLint level, GLint internalFormat, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid * );

vars a ^ 2...

, :

// glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, );

2 , "255"; , , .

:

GLubyte pixels [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);
};
+2

All Articles