多纹理问题(新手)

时间:2011-01-15 19:09:37

标签: opengl-es textures

我是OpenGL和移动编程的新手。

我正在尝试加载2个纹理并将它们显示在一个具有不同坐标的对象(我的意思是一组分支)上。

我的方法:

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

我做错了什么?接下来我该怎么办?

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glDrawArrays(GL_TRIANGLE_STRIP, 0, BYTES_PER_PIXEL);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

结果我得到了白色物体(根本没有纹理)。

2 个答案:

答案 0 :(得分:2)

如果您要使用GL_TEXTURE_MIN_FILTER的{​​{1}} GL_NEAREST_MIPMAP_LINEAR,请尝试提供一整套mipmap。

或将GL_TEXTURE_MIN_FILTER设为GL_NEAREST / GL_LINEAR

答案 1 :(得分:0)

初始化时,请尝试:

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

根据您的特定需要修改上述内容,并重复第二个纹理。

要绘图,请尝试:

// 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

注意:不要通过传递GL_TEXTURE1GL_TEXTURE2等来做您正在做的事情,而是将GL_TEXTURE0+i传递给 i 是介于0和GL_MAX_TEXTURE_COORDS的特定于实现的结果之间的值。

相关问题