Android 3D Rotation triangle (1)

Source: Internet
Author: User

Package com. Sunny;

Import Android. App. activity;
Import Android. OS. Bundle;

Public class mainactivity extends activity {
Private Static final string log_tag = mainactivity. Class. getsimplename ();
Private vortexview _ vortexview;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
_ Vortexview = new vortexview (this );
Setcontentview (_ vortexview );
}
}

Vortexview

Package com. Sunny;

Import Android. content. context;
Import Android. OpenGL. glsurfaceview;
Import Android. View. motionevent;

public class vortexview extends glsurfaceview {// inherits glsurfaceview because it will help us draw 3D images
Private Static final string log_tag = vortexview. class. getsimplename ();
private vortexrenderer _ Renderer; // an Renderer contains everything necessary to draw a frame.
Public vortexview (context) {
super (context);
// todo auto-generated constructor stub
_ Renderer = new vortexrenderer ();
setrenderer (_ Renderer );
}< BR >@ override
Public Boolean ontouchevent (final motionevent event) {
// todo auto-generated method stub
queueevent (New runnable () {

@ Override
Public void run () {// call the setcolor method in Renderer
// Todo auto-generated method stub
_ Renderer. setcolor (event. getx ()/getwidth (), event. Gety ()/getheight (), 1.0f );
_ Renderer. setangle (event. getx ()/10 );
}

});
Return true;
}

}

Vortexrenderer

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 floatbuffer _ vertexbuffer; // coordinates of the triangle
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;
}
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 ();

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)
};
_ Vertexbuffer. Put (coords );
_ Indexbuffer. Put (_ indicesarray );

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


@ 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
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
// Todo auto-generated method stub
// Set Rotation
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.

// Define the color we want to be displayed as the "clipping wall"
Gl. glclearcolor (_ Red, _ Green, _ Blue, 1.0f );
// Clear the color buffer to show the clearcolor we called abve...
Gl. glclear (gl10.gl _ color_buffer_bit );
// 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. gldrawelements (gl10.gl _ triangles, _ nrofvertices, gl10.gl _ unsigned_short, _ indexbuffer );

}

}

 

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.