Package com. Sunny;
Import java. NiO. bytebuffer;
Import java. NiO. byteorder;
Import java. NiO. floatbuffer;
Import java. NiO. Protocol buffer;
Import javax. microedition. khronos. EGL. eglconfig;
Import javax. microedition. khronos. opengles. gl10;
Import android. openGL. glsurfaceview;
public class vortexrenderer implements glsurfaceview. renderer {
Private Static final string log_tag = vortexrenderer. class. getsimplename ();
private writable buffer _ indexbuffer; // Save the index
private floatbuffer _ vertexbuffer; // Save the coordinates of the specified point
private floatbuffer _ colorbuffer;
// Private short [] _ indicesarray = {, 2 };
private int _ nrofvertices = 0; // defines the number of vertices required. for a triangle, three vertices are required.
private float _ xangle;
private float _ yangle;
public float getxangle () {
return _ xangle;
}
Public void setxangle (float angle ){
This. _ xangle = angle;
}
Public float getyangle (){
Return _ yangle;
}
Public void setyangle (float angle ){
This. _ yangle = angle;
}
@ Override
Public void onsurfacecreated (gl10 GL, eglconfig config) {// called after surface is created
// Todo auto-generated method stub
// Enable the differentiation of which side may be visible
Gl. glable (gl10.gl _ cull_face); // enable the culling surface to ensure only one side
// Which is the front? The one which is drawn counter clockwise
Gl. glfrontface (gl10.gl _ CCW); // gl_ccw indicates a counter-clockwise
// Which one shocould not be drawn
Gl. glcullface (gl10.gl _ Back); // gl_front_and_back
Gl. glableclientstate (gl10.gl _ vertex_array );
Gl. glableclientstate (gl10.gl _ color_array );
Inittriangle ();
}
@ Override
Public void onsurfacechanged (gl10 GL, int W, int h) {// called after the surface changes, for example, when switching from the portrait screen to the landscape Screen
// Todo auto-generated method stub
Gl. glviewport (0, 0, W, H );
}
@ Override
Public void ondrawframe (gl10 GL) {// when calling a drawing method at any time
// Define the color we want to be displayed as the "clipping wall"
Gl. glclearcolor (0f, 0f, 0f, 1.0f );
// Reset the matrix-Good to fix the rotation to a static angle
Gl. glloadidentity ();
// Clear the color buffer to show the clearcolor we called abve...
Gl. glclear (gl10.gl _ color_buffer_bit );
// Set Rotation
Gl. glrotatef (_ xangle, 1f, 0f, 0f );
Gl. glrotatef (_ yangle, 0f, 1f, 0f );
Gl. glvertexpointer (3, gl10.gl _ float, 0, _ vertexbuffer );
Gl. glcolorpointer (4, gl10.gl _ float, 0, _ colorbuffer );
Gl. gldrawelements (gl10.gl _ triangles, _ nrofvertices, gl10.gl _ unsigned_short, _ indexbuffer );
}
Private void inittriangle (){
Float [] coords = {// coordinates
-0.5f,-0.5f, 0.5f, // 0
0.5f,-0.5f, 0.5f, // 1
0f,-0.5f,-0.5f, // 2
0f, 0.5f, 0f, // 3
};
_ Nrofvertices = coords. length; // more dynamic
Float [] colors = {// color
1f, 0f, 0f, 1f, // point 0 red
0f, 1f, 0f, 1f, // point 1 green
0f, 0f, 1f, 1f, // point 2 blue
1f, 1f, 1f, 1f, // point 3 white
};
Short [] indices = new short [] {// count
0, 1, 3, // RWG
0, 2, 1, // rbg
0, 3, 2, // rbw
1, 2, 3, // BWG
};
// Allocate the required memory for the two buffers.
// Float has 4 bytes
Bytebuffer vBB = bytebuffer. allocatedirect (_ nrofvertices * 3*4 );
VBB. Order (byteorder. nativeorder ());
_ Vertexbuffer = vBB. asfloatbuffer ();
// Short has 2 bytes
Bytebuffer Ibb = bytebuffer. allocatedirect (_ nrofvertices * 2 );
Ibb. Order (byteorder. nativeorder ());
_ Indexbuffer = Ibb. asw.buffer ();
Bytebuffer CBB = bytebuffer. allocatedirect (4 * _ nrofvertices * 4 );
CBB. Order (byteorder. nativeorder ());
_ Colorbuffer = CBB. asfloatbuffer ();
_ Vertexbuffer. Put (coords );
_ Indexbuffer. Put (indices );
_ Colorbuffer. Put (colors );
_ Vertexbuffer. Position (0 );
_ Indexbuffer. Position (0 );
_ Colorbuffer. Position (0 );
}
}