110 likes | 244 Vues
Programming Concepts. Programming Concepts. Derive a new class from Activity of the framework Prepare the data beforehand, e.g., vertices, colours, normal vectors, texture coordinates, etc. Create your Renderer Check GPU version Prepare vertex shader program(s)
E N D
Programming Concepts • Derive a new class from Activity of the framework • Prepare the data beforehand, e.g., vertices, colours, normal vectors, texture coordinates, etc. • Create your Renderer • Check GPU version • Prepare vertex shader program(s) • Prepare fragment shader program(s) • Compile and link your GPU program(s) • Implement methods: onSurfaceCreated, onSurfaceChanged, onDrawFrame, etc.
Create a class derived from Activity public class MainActivity extends Activity { \\body }
Define OnCreate Method (Pseudo code) public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); create GLSurfaceView supportsEs2 = Check if the system supports OpenGL ES 2.0. if (supportsEs2) { Set GLSurfaceView.setEGLContextClientVersion for OpenGL ES 2.0. Set GLSurfaceView.setRenderer for rendering } else { // This is where you could create an OpenGL ES 1.x compatible // renderer if you wanted to support both ES 1 and ES 2. return; } setContentView(mGLSurfaceView); }
Define onCreateOptionsMenu // Inflate the menu resource (defined in XML) into the Menu provided in the callback. @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }
Renderer public class MyRenderer implements GLSurfaceView.Renderer { // three matrices: projection, view, model // vertices // handles for passing information: // matrix, position, color }
Initialization of Data final float[] myVerticesData = { X0, Y0, Z0, // position in 3Dimensional Space R0, G0, B0, A0 //red,green,blue,alpha X1, Y1, Z1, R1, G1, B1, A1 X2, Y2, Z2, R2, G2, B2, A2 //more if more vertices };
Initialization of a Buffer mMyVerticesBuffer = ByteBuffer.allocateDirect( myVerticesData.length*mBytesPerFloat ) .order( ByteOrder.nativeOrder() ).asFloatBuffer(); mMyVerticesBuffer.put( myVerticesData ).position(0);
onSurfaceCreated method // called at the start of rendering // or the OpenGL ES drawing context has to be recreated public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { • set the LookAtMatrix of the Camera • prepare a vertexShaderProgram • prepare a fragmentShaderProgram • Check vertexShader( vertexShaderProgram ) • Check fragmentShader( fragmentShaderProgram ) • Create a GPU program for the vertex and fragment programs • Link GPU program(s) • Get the GPU program handles of the parameters • Use the GPU program }
onSurfaceChanged method • The onSurfaceChanged() method is called when the surface changes size. • Possible functions: • set the OpenGL viewport • set the camera
onDrawFrame method • The onDrawFrame() method is called every frame. It is responsible for drawing the scene. • Usually clear the framebuffer and then call functions to draw the current scene.