Android 3D Rotation triangle (2)

Source: Internet
Author: User

Add color mode

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 float _ Red = 0.9f; // use floating point numbers to define each color in the RGB Color System
Private float _ Green = 0.2f;
Private float _ Blue = 0.2f;

Private writable buffer _ indexbuffer; // Save the index
Private writable buffer _ indexbufferstatic;

Private floatbuffer _ vertexbuffer; // coordinates of the triangle
Private floatbuffer _ vertexbufferstatic;

Private floatbuffer _ colorbuffer;
Private short [] _ indicesarray = {0, 1, 2 };
Private int _ nrofvertices = 3; // defines the number of vertices required. For a triangle, three vertices are required.

Private float _ angle;
Public void setangle (float angle ){
_ Angle = angle;
}


@ Override
Public void onsurfacecreated (gl10 GL, eglconfig config) {// called after surface is created
// Todo auto-generated method stub
Gl. glableclientstate (gl10.gl _ vertex_array); // sets OpenGL to use the vertex array to draw
Gl. glableclientstate (gl10.gl _ color_array );
Inittriangle ();
// Initstatictriangle ();
}

@ 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 (_ Red, _ Green, _ Blue, 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 );

Gl. glrotatef (_ angle, 0f, 1f, 0f); // rotate around the Y axis. The value in the parameter represents a vector, marking the coordinate axis in which the triangle rotates.
// Set the color of our element to set the triangle to dark red
// Gl. glcolor4f (0.5f, 0f, 0f, 0.5f );
// Define the vertices we want to draw
/* Use glvertexpointer () to initialize vertex pointer.
* The first parameter is the size, which is also the vertex dimension. We use 3D coordinates x, y, and z.
* The second parameter, gl_float, defines the data type used in the buffer.
* The third variable is 0, because our coordinates are compact in the array and no offset is used.
* The last and fourth parameter vertex buffering.
*/
Gl. glvertexpointer (3, gl10.gl _ float, 0, _ vertexbuffer );
// Finally draw the vertices
/* Gldrawelements () draws all these elements.
* The first parameter defines the elements to be drawn.
* The second parameter defines the number of elements,
* The third is the data type used by indices.
* The last one is the index buffer used to draw the vertex.
*/
Gl. glcolorpointer (4, gl10.gl _ float, 0, _ colorbuffer );

Gl. gldrawelements (gl10.gl _ triangles, _ nrofvertices, gl10.gl _ unsigned_short, _ indexbuffer );

}
Public void setcolor (float R, float g, float B ){
_ Red = R;
_ Green = g;
_ Blue = B;
}
Private void inittriangle (){
// 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 ();

Float [] coords = {
-0.5f,-0.5f, 0f, // (x1, Y1, Z1)
0.5f,-0.5f, 0f, // (X2, Y2, Z2)
0f, 0.5f, 0f // (X3, Y3, Z3)
};
Float [] colors = {// rgba (red, green, blue, alpha)
1f, 0f, 0f, 1f,
0f, 1f, 0f, 1f,
0f, 0f, 1f, 1f
};
_ Vertexbuffer. Put (coords );
_ Indexbuffer. Put (_ indicesarray );
_ Colorbuffer. Put (colors );

_ Vertexbuffer. Position (0 );
_ Indexbuffer. Position (0 );
_ Colorbuffer. Position (0 );
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.