Android 3D Rotation triangle (4)

Source: Internet
Author: User

Perspective

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 fixed point coordinates

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


Private float _ xangle;
Private float _ yangle;

Private float _ width = 320f;
Private float _ Height = 480f;

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
Gl. glmatrixmode (gl10.gl _ projection); // sets the projection.
Float size = 0.1f * (float) math. Tan (math. toradians (45.0)/2 );

Float ratio = _ width/_ height; // calculate the same screen ratio.
// Gl. glorthof (-1, 1,-1/ratio, 1/ratio, 0.01f, 100366f); // orthogonal orthographic: orthographic View
// Perspective: glfrustumf ()
Gl. glfrustumf (-size, size,-size/ratio, size/ratio, 0.01f, 100366f );

Gl. glviewport (0, 0, (INT) _ width, (INT) _ height); // set the viewpoint
Gl. glmatrixmode (gl10.gl _ modelview );
Gl. glable (gl10.gl _ depth_test); // even if the object should have been overwritten by a larger object, we can still see it.

Gl. glclearcolor (0f, 0f, 0f, 1.0f );
// 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 the clockwise direction and CW indicates the clockwise direction.
// 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
_ Width = W;
_ Height = h;
Gl. glviewport (0, 0, W, H );
}

@ Override
Public void ondrawframe (gl10 GL) {// when calling a drawing method at any time
Gl. glloadidentity ();
// 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 | gl10.gl _ depth_buffer_bit );




Gl. glvertexpointer (3, gl10.gl _ float, 0, _ vertexbuffer );
Gl. glcolorpointer (4, gl10.gl _ float, 0, _ colorbuffer );

For (INT I = 0; I <= 10; I ++ ){
Gl. glloadidentity ();
Gl. gltranslatef (0.0f,-1f,-1.0f +-1.5f * I );
// Set Rotation
Gl. glrotatef (_ xangle, 1f, 0f, 0f );
Gl. glrotatef (_ yangle, 0f, 1f, 0f );
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 );
}

}

 

Original article: http://blog.csdn.net/lixinso/article/details/5272495

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.