Hello.
I am porting my game to android using SDL and NDK, and I am trying to make the game hardware accelerated with openGL.
In the game I need to draw colored rectangles and to draw an arbitrary bitmap.
void DrawRectangleGL( ScreenBuffer* buffer, Vec2 min, Vec2 max, Vec4 color )
{
GLfloat matrix[] = {
2.0f / buffer->width, 0, 0, 0,
0, 2.0f / buffer->height, 0, 0,
0, 0, 1, 0,
-1, -1, 0, 1
};
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
glMultMatrixf( matrix );
GLfloat triangles[] = {
min.X, min.Y,
max.X, min.Y,
max.X, max.Y,
min.X, min.Y,
min.X, max.Y,
max.X, max.Y
};
GLfloat colors[] = {
color.X, color.Y, color.Z, color.W,
color.X, color.Y, color.Z, color.W,
color.X, color.Y, color.Z, color.W,
color.X, color.Y, color.Z, color.W,
color.X, color.Y, color.Z, color.W,
color.X, color.Y, color.Z, color.W
};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, triangles);
glEnableClientState( GL_COLOR_ARRAY );
glColorPointer( 4, GL_FLOAT, 0, colors );
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void DrawBitmap( ScreenBuffer* buffer, Bitmap* bitmap, Vec2 position, real32 alpha )
{
if( bitmap->textureMap == -1 )
{
glGenTextures(1, ( GLuint* ) &bitmap->textureMap);
}
glBindTexture(GL_TEXTURE_2D, bitmap->textureMap );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, bitmap->width, bitmap->height,
0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, bitmap->pixels );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glEnable( GL_TEXTURE_2D );
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode( GL_TEXTURE );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
GLfloat matrix[] = {
2.0f / buffer->width, 0, 0, 0,
0, 2.0f / buffer->height, 0, 0,
0, 0, 1, 0,
-1, -1, 0, 1
};
glLoadIdentity();
glMultMatrixf( matrix );
Vec2 min = position;
Vec2 max = min + V2( bitmap->width, bitmap->height );
GLfloat triangles[] = {
min.X, min.Y,
max.X, min.Y,
max.X, max.Y,
min.X, min.Y,
min.X, max.Y,
max.X, max.Y,
};
GLfloat textureVertices[] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, triangles);
glEnableClientState( GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer( 2, GL_FLOAT, 0, textureVertices );
glDrawArrays(GL_TRIANGLES, 0, 6);
}
glViewport(0, 0, globalScreenBuffer.width, globalScreenBuffer.height );
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);