3D draw a cylindrical

Source: Internet
Author: User

CodeSimilar to the circle above, only the drawcylinder. Java code has changed;

 

What do you think is the topic? Let's talk about it directly. You can think of it as the effect of the circle moving along the vertical direction in a parallel horizontal plane. In this way, we get the two bottom surfaces of the cylinder, you can draw a triangle between the vertices of the upper and lower circles on the side. Of course, there will also be errors, so the more points of the circle, the smaller the error, the circle is like a cylindrical. Let's take a look at the code.

 

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 )));

Float X2 = (float) (J * collength-length/2 );
Float y2 = (float) (circle_radius * Math. Sin (math. toradians (circle_degree-degreespan )));
Float Z2 = (float) (circle_radius * Math. Cos (math. toradians (circle_degree-degreespan )));

Float X3 = (float) (J + 1) * collength-length/2 );
Float Y3 = (float) (circle_radius * Math. Sin (math. toradians (circle_degree-degreespan )));
Float Z3 = (float) (circle_radius * Math. Cos (math. toradians (circle_degree-degreespan )));

Float X4 = (float) (J + 1) * collength-length/2 );
Float Y4 = (float) (circle_radius * Math. Sin (math. toradians (circle_degree )));
Float Z4 = (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.
Val. Add (X2); Val. Add (Y2); Val. Add (Z2 );

Val. Add (X2); Val. Add (Y2); Val. Add (Z2 );
Val. Add (X4); Val. Add (Y4); Val. Add (Z4 );

Val. Add (X4); Val. Add (Y4); Val. Add (Z4 );
Val. Add (X1); Val. Add (Y1); Val. Add (Z1 );

Val. Add (X2); Val. Add (Y2); Val. Add (Z2 );
Val. Add (X3); Val. Add (Y3); Val. Add (Z3 );

Val. Add (X3); Val. Add (Y3); Val. Add (Z3 );
Val. Add (X4); Val. Add (Y4); Val. Add (Z4 );

Val. Add (X4); Val. Add (Y4); Val. Add (Z4 );
Val. Add (X2); Val. Add (Y2); Val. Add (Z2 );
}
}

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.