I am new to the OpenGL and mobile programming in general.
I am trying to load 2 textures and show them on one object (I mean one set of vercicles) with different coordinates.
My approach:
glGenTextures(2, &textures[0]);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glActiveTexture(GL_TEXTURE0);
glClientActiveTexture( GL_TEXTURE0 );
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexCoordPointer(2, GL_SHORT, 0, mapTextCoords);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, map.width, map.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, [map getByteData]);
glActiveTexture(GL_TEXTURE1);
glClientActiveTexture( GL_TEXTURE1 );
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glTexCoordPointer(2, GL_SHORT, 0, b开发者_Python百科ackgroundTextCoords);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, background.width, background.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, [background getByteData]);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, mapVertices);
What am I doing wrong? And what should I do next?
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glDrawArrays(GL_TRIANGLE_STRIP, 0, BYTES_PER_PIXEL);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
In result i got white object (no textures at all).
Try providing a complete set of mipmaps if you're going to use GL_TEXTURE_MIN_FILTER
's default of GL_NEAREST_MIPMAP_LINEAR
.
Or set GL_TEXTURE_MIN_FILTER
to GL_NEAREST
/GL_LINEAR
.
For initializing, try:
glGenTextures(1, &texA);
glBindTexture(GL_TEXTURE_2D, texA);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_t, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, lod, GL_RGB, pixWidth, pixHeight, 0, GL_RGB, GL_FLOAT, pixelData);
glGenerateMipmap(GL_TEXTURE_2D);
Modify the above as needed for your particular needs, and repeat for the 2nd texture.
For drawing, try:
// Bind textures
glActiveTexture(GL_TEXTURE0+i); // set which texture unit will be affected by subsequent glBindTexture
glBindTexture(GL_TEXTURE_2D, texA); // bind a 2D texture
glActiveTexture(GL_TEXTURE0+i); // set a different texture unit than above
glBindTexture(GL_TEXTURE_2D, texB); // bind another 2D texture
// Set texcoords for first texture
glClientActiveTexture(GL_TEXTURE0+i); // set which texture unit will be affected by subsequent
// call to glTexCoordPointer (should match what you
// gave glActiveTexture for texA earlier)
glEnableClientState(GL_TEXTURE_COORD_ARRAY); // enable texcoord attributes
glTexCoordPointer(...); // pass the texcoord array for texA
// Set texcoords for second texture
glClientActiveTexture(GL_TEXTURE0+i); // set which texture unit will be affected by subsequent
// call to glTexCoord pointer (should match what you
// gave glActiveTexture for texB earlier)
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(...); // pass the texcoord array for texB
Note: Don't do what you're doing by passing GL_TEXTURE1
, GL_TEXTURE2
, etc., but rather pass GL_TEXTURE0+i
where i is a value between 0 and the implementation-specific result of GL_MAX_TEXTURE_COORDS
.
精彩评论