I am workiong on the Android platform. I have just started with OpenGL and am trying to put an image on a rectangle. This is my code:
public class IntroActivity extends Activity {
private GLSurfaceView glsv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glsv = new GLSurfaceView(this);
glsv.setRenderer(new IntroRenderer());
setContentView(glsv);
}
@Override
protected void onResume() {
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onResume();
glsv.onResume();
}
@Override
protected void onPause() {
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onPause();
glsv.onPause();
}
Here IntroRenderer is an inner class.
class IntroRenderer implements GLSurfaceView.Renderer{
@Override
public void onDrawFrame(GL10 gl) {
gl.glClearColor(0,1.0f,1.0f,1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
float positions[] = {
0.0f,1.0f,0.0f,
0.0f, 0.0f, 0.0f,
1.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,
1.0f,1.0f, 0.0f,
1.0f,0.0f,0.0f
};
ByteBuffer bb = ByteBuffer.allocateDirect(positions.length*4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(positions);
fb.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3,GL10.GL_FLOAT, 0, fb);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3*2);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.rect2985);
Bitmap bmp256 = Bitmap.createScaledBitmap(bmp, 256,256,false);
gl.glEnable(GL10.GL_TEXTURE_2D);
int[] buffers = new int[1];
gl.glGenTextures(1,buffers,0);
int texture = buffers[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D,0,bmp256,0);
//gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);
//gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST);
bmp.recycle();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
}
}
}
On running this activity, the background rectangle is rendered but ther eis no image. What am I doing wrong?