Draw a circle in 3D

Source: Internet
Author: User

You need to know that there is no drawcircle (X, Y, R, paint); function in glsurfaceview in 2D, so you have to think of other methods.

Circle is based on R * r = r * rcos ^ 2 + R * rsin ^ 2; loop N times, so that we can get n points, each point is a straight line, the more points, the more circles. Let's take a look at the code.

 

 

Package WYF. lgz;

Import Android. App. activity;
Import Android. content. PM. activityinfo;
Import Android. OS. Bundle;
Import Android. View. window;
Import Android. View. windowmanager;

Public class activity_gl_cylinder extends activity {
Private myglsurfaceview mglsurfaceview;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

Requestwindowfeature (window. feature_no_title );
Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen );
Setrequestedorientation (activityinfo. screen_orientation_landscape );

Mglsurfaceview = new myglsurfaceview (this );
Setcontentview (mglsurfaceview );
Mglsurfaceview. setfocusableintouchmode (true); // you can set it to touch.
Mglsurfaceview. requestfocus (); // obtain the focus
}

@ Override
Protected void onresume (){
Super. onresume ();
Mglsurfaceview. onresume ();
}

@ Override
Protected void onpause (){
Super. onpause ();
Mglsurfaceview. onpause ();
}
}

 

 

 

Package WYF. lgz;
Import Android. OpenGL. glsurfaceview;
Import javax. microedition. khronos. EGL. eglconfig;
Import javax. microedition. khronos. opengles. gl10;
Import Android. content. context;
Import Android. View. motionevent;

Public class myglsurfaceview extends glsurfaceview {
Private Final float touch_scale_factor = 180.0f/320; // angle scaling ratio
Private scenerenderer mrenderer; // scene Renderer
Private float mpreviousy; // y coordinate of the last touch position
Private float mpreviousx; // y coordinate of the last touch position
 
Public myglsurfaceview (context ){
Super (context );
Mrenderer = new scenerenderer (); // creates a scene Renderer.
Setrenderer (mrenderer); // sets the Renderer
Setrendermode (glsurfaceview. rendermode_continuously); // set the rendering mode to active rendering.
}

// Touch Event Callback Method
@ Override
Public Boolean ontouchevent (motionevent e ){
Float y = E. Gety ();
Float x = E. getx ();
Switch (E. getaction ()){
Case motionevent. action_move:
Float DY = Y-mpreviousy; // calculates the y displacement of the touch pen.
Float dx = x-mpreviousx; // calculates the Y-shift of the touch pen.
Mrenderer. cylinder. manglex + = Dy * touch_scale_factor; // you can specify the Rotation Angle along the X axis.
Mrenderer. cylinder. manglez + = DX * touch_scale_factor; // you can specify the Rotation Angle Along the Z axis.
Requestrender (); // re-painting surface
}
Mpreviousy = y; // record the pen position
Mpreviousx = x; // record the pen position
Return true;
}

Private class scenerenderer implements glsurfaceview. Renderer
{
Drawcylinder cylinder; // create a cylinder

Public void ondrawframe (gl10 GL ){
// Clear the color Cache
Gl. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit );
// Set the current matrix as the mode Matrix
Gl. glmatrixmode (gl10.gl _ modelview );
// Set the current matrix as the unit matrix
Gl. glloadidentity ();

Gl. glpushmatrix (); // protection transformation matrix field
Gl. gltranslatef (0, 0,-8f); // pan
Cylinder. drawself (GL); // draw
Gl. glpopmatrix (); // restore transformation matrix field
}

Public void onsurfacechanged (gl10 GL, int width, int height ){
// Set the window size and position
Gl. glviewport (0, 0, width, height );
// Set the current matrix to a projection matrix
Gl. glmatrixmode (gl10.gl _ projection );
// Set the current matrix as the unit matrix
Gl. glloadidentity ();
// Calculate the percentage of Perspective Projection
Float ratio = (float) width/height;
// Call this method to calculate the Perspective Projection Matrix
Gl. glfrustumf (-ratio, ratio,-1, 1, 1,100 );
}

Public void onsurfacecreated (gl10 GL, eglconfig config ){
// Disable anti-Jitter
Gl. gldisable (gl10.gl _ dither );
// Set the mode of a specific hint project, which is set to use Quick Mode.
Gl. glhint (gl10.gl _ perspective_correction_hint, gl10.gl _ fastest );
// Set the screen background color, white rgba
Gl. glclearcolor (1, 1, 1 );
// Set the coloring model to smooth coloring
Gl. glshademodel (gl10.gl _ smooth );
// Enable deep Test
Gl. glable (gl10.gl _ depth_test );

Cylinder = new drawcylinder (10f, 2f, 1f, 5); // create a cylinder

}
}
}

 

 

 

Package WYF. lgz;

Import java. NiO. bytebuffer;
Import java. NiO. byteorder;
Import java. NiO. floatbuffer;
Import java. util. arraylist;

Import javax. microedition. khronos. opengles. gl10;

Public class drawcylinder
{
Private floatbuffer myvertexbuffer; // vertex coordinate Buffer
 
Int vcount; // Number of vertices
 
Float length; // The length of the column.
Float circle_radius; // circle radius
Float degreespan; // The degree of each part of the circular truncation Ring
Int Col; // Number of cylindrical Blocks
 
Public float manglex;
Public float mangley;
Public float manglez;
 
Public drawcylinder (float length, float circle_radius, float degreespan, int col)
{
This. circle_radius = circle_radius;
This. Length = length;
This. Col = Col;
This. degreespan = degreespan;

Float collength = (float) length/COL; // The length of each column

Arraylist <float> val = new arraylist <float> (); // vertex storage list

For (float circle_degree = 360.0f; circle_degree> 0.0f; circle_degree-= degreespan) // loop rows
{
Int J = 0;
// For (Int J = 0; j <Col; j ++) // loop Column
{
Float X1 = (float) (J * collength-length/2 );
Float Y1 = (float) (circle_radius * Math. Sin (math. toradians (circle_degree )));
Float z1 = (float) (circle_radius * Math. Cos (math. toradians (circle_degree )));

 

Val. Add (X1); Val. Add (Y1); Val. Add (Z1); // two vertices of each line are determined. There are 6 lines and 12 vertices in total.

}
}

Vcount = Val. Size ()/3; // determine the number of vertices

// Vertex
Float [] vertexs = new float [vcount * 3];
For (INT I = 0; I <vcount * 3; I ++)
{
Vertexs [I] = Val. Get (I );
}
Bytebuffer vBB = bytebuffer. allocatedirect (vertexs. length * 4 );
VBB. Order (byteorder. nativeorder ());
Myvertexbuffer = vBB. asfloatbuffer ();
Myvertexbuffer. Put (vertexs );
Myvertexbuffer. Position (0 );
}
 
Public void drawself (gl10 GL)
{
Gl. glrotatef (manglex, 1, 0, 0); // rotate
Gl. glrotatef (mangley, 0, 1, 0 );
Gl. glrotatef (manglez, 0, 0, 1 );

Gl. glableclientstate (gl10.gl _ vertex_array); // enable the vertex buffer.
Gl. glvertexpointer (3, gl10.gl _ float, 0, myvertexbuffer); // specify the vertex buffer

Gl. glcolor4f (0, 0, 0, 0); // set the draw line to black.
Gl. gldrawarrays (gl10.gl _ line_loop, 0, vcount); // draw an image

Gl. gldisableclientstate (gl10.gl _ vertex_array );
}
}

 

 

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.