Projection and camera view in OpenGL ES 1.0
Projection matrix - Create a projection matrix using the geometry of the device screen in order to recalculate object coordinates so they are drawn with correct proportions. The following example code demonstrates how to modify the
onSurfaceChanged()
method of a GLSurfaceView.Renderer
implementation to create a projection matrix based on the screen's aspect ratio and apply it to the OpenGL rendering environment. public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); // make adjustments for screen ratio float ratio = (float) width / height; gl.glMatrixMode(GL10.GL_PROJECTION); // set matrix to projection mode gl.glLoadIdentity(); // reset the matrix to its default state gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7); // apply the projection matrix }
Camera transformation matrix - Once you have adjusted the coordinate system using a projection matrix, you must also apply a camera view. The following example code shows how to modify the
onDrawFrame()
method of a GLSurfaceView.Renderer
implementation to apply a model view and use the GLU.gluLookAt()
utility to create a viewing tranformation which simulates a camera position. public void onDrawFrame(GL10 gl) { ... // Set GL_MODELVIEW transformation mode gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); // reset the matrix to its default state // When using GL_MODELVIEW, you must set the camera view GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f); ... }
How to create a simple Android application that uses the OpenGL ES 1.0 API to perform some basic graphics operations:
1.Create an activity using
GLSurfaceView
and GLSurfaceView.Renderer
public class HelloOpenGLES10 extends Activity
{
private GLSurfaceView mGLView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
mGLView = new HelloOpenGLES10SurfaceView(this);
setContentView(mGLView);
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
mGLView.onPause();
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
mGLView.onResume();
}
}
public class HelloOpenGLES10SurfaceView extends GLSurfaceView
{
public HelloOpenGLES10SurfaceView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
setRenderer(new HelloOpenGLES10Renderer());
}
}
public class HelloOpenGLES10Renderer implements GLSurfaceView.Renderer { // onDrawFrame() is called for each redraw of the GLSurfaceView. @Override public void onDrawFrame(GL10 gl) { // TODO Auto-generated method stub gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); }
// onSurfaceChanged() is called if the geometry of the GLSurfaceView changes, // for example when the device's screen orientation changes. @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO Auto-generated method stub gl.glViewport(0, 0, width, height); }
// onSurfaceCreated() is called once to set up the GLSurfaceView environment. @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO Auto-generated method stub gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); } }
2.Create and draw a graphic object
By default, OpenGL ES assumes a coordinate system where [0,0,0] (X,Y,Z) specifies the center of the GLSurfaceView
frame, [1,1,0] is the top right corner of the frame and [-1,-1,0] is bottom left corner of the frame.
3.Define a projection to correct for screen geometry
4.Define a camera view
5.Rotate a graphic object
6.Make graphics touch-interactive