I’ve tried a simple program that dynamically generates a texture and map it to a quad using NDK. Everything is fine on an emulator but failed on real devices.
Here is my code:
private static class Renderer implements GLSurfaceView.Renderer
{
@Override
public void onDrawFrame(GL10 gl)
{
nativeDrawFrame();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
nativeInit(width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
// do nothing...
}
}
and the native code:
const static GLfloat vertices_f[4][2] =
{
{ 0.0f, 0.0f },
{ 100.0f, 0.0f },
{ 100.0f, 100.0f },
{ 0.0f, 100.0f }
};
const static GLfloat texCoords_f[4][2] =
{
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f }
};
JNIEXPORT void JNICALL Java_com_sangfor_gltest_GLView_nativeInit(JNIEnv * env, jobject obj, jint width, jint height)
{
if (!bitmap)
{
// allocate dynamic texture memory
bitmap = memalign(16, 1024*1024);
if (!bitmap)
{
__android_log_print(ANDROID_LOG_ERROR, "native-render", "failed allocation.");
return;
}
}
glViewport(0, 0, width, height);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//glOrthox(0, 0x10000, 0, 0x10000, 0x10000, -0x10000);
//glClearColorx(0, 0, 0, 0);
glGenTextures(1, &texture);
__android_log_print(ANDROID_LOG_INFO, "native-render", "texture = %d", texture);
// glVertexPointer(2, GL_FIXED, 0, vertices);
// glTexCoordPointer(2, GL_FIXED, 0, texCoords);
//glEnableClientState(GL_COLOR_ARRAY);
glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
JNIEXPORT void JNICALL Java_com_sangfor_gltest_GLView_nativeDrawFrame(JNIEnv * env, jobject obj)
{
struct timeval tv;
unsigned char color_value;
glClear(GL_COLOR_BUFFER_BIT);
// fill texture according to current timestamp
gettimeofday(&tv, NULL);
color_value = (unsigned char)(tv.tv_usec * 0.000255f);
memset(bitmap, color_value, 1024*1024);
__android_log_print(ANDROID_LOG_INFO, "native-render", "color_value = %d", color_value);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap);
glBindTexture(GL_TEXTURE_2D, texture);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords_f);
glVertexPointer(2, GL_FLOAT, 0, vertices_f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glFlush();
}
On an emulator it just works as I expected: an area on the screen keeps changing its color.
However, when I run it on real devices (Samsung Galaxy S 2.3.3 and Asus Transformer TF101 3.2.1), it just show a white block and texture mapping seems not working.
I tried add and comment out projection transform, enable texture mapping by calling glEnable(GL_TEXTURE_2D), disable alpha blending and testing by calling glDisable(...), move glBindTexture and glTexImage2D to init function, change texture size to 32 x 32 but none of these work.
Can anyone help me figure out why texture mapping fails just on real devices? Is there a GPU limitation or something?
EDIT: I’ve tried Spoon’s suggestion and found the real problem. No matter which texture I bind, the device uses texture named 0 to render quads, so glBindTexture(GL_TEXTURE_2D, 0) works fine but glBindTexture(GL_TEXTURE_2D, 1) and anything returned by glGenTextures(...) fails. This means I can save only one texture, but I have to use 2 or more.
White block means texturing is working but the textures haven’t loaded properly.
If the actual mapping were a problem, you would see the textures but all the colours warped or messed up.
Check the actual values given by
glGenTextures(). I found that when using openGL-es 1.x on later versions of android (2.2+) thatglGenTextures()threw out some really random numbers rather than giving id’s of 0,1,2,3 etc. It may be you have to manually supply your own ID’s and not useglGenTextures()Alternatively, check the pixel data is being correctly loaded from file. Blank white textures may appear if you have a filename wrong or file missing but have put in some error handling that allows the app to continue. And/or don’t forget to refresh res folders or even clean the project when you add new textures
UPDATE:
This is how I did it:
Then when drawing:
This however is all in java and uses a weird mix of opengl-es 2.0 for texture loading and opengl-es 1.1 for drawing