OpenGL Geometry Transformation (rendering sphere)---OpenGL learning notes (vi) __OPENGL

Source: Internet
Author: User
Tags clear screen cos sin
Transformations in OpenGL include the following:
The ① View (modeling)---Specify the Observer or camera position Glu.gllookat () by default, the Observer in perspective projection is viewed from the origin to the z-axis, or it can be set itself.
② model (Viewing)---Move objects in a scene including moving, rotating, scaling
③ Model View (Modelview)---Describe the duality of cartography and model transformations for example: The Observer is close to the object, and the object is placed close to the observer, and the effect is the same.
The ④ projection---(projection) changes the size of the viewable area and the Glfrustum () of the shape () contains orthographic projections (also called parallel projections, no near and far concepts) and perspective projections (with depth concepts).
⑤ Viewport---(viewport) pseudo transform to scale the final output on the window

 

Pipeline for vertex transformations:
Multiply the Model view matrix multiplied by the projection matrix to apply the perspective Division viewport transform

source vertex Data-----------------> transformed view coordinates-------------> trim coordinates---------------> normalized device coordinates-------------> window coordinates


we are going to draw a sphere, then we have to cut the sphere first, cut horizontally into several points, take a part of it out is a ribbon, this part of the cut will be a parallelogram belt, the following figure.

Note: Because OpenGL in the elimination of the surface is usually removed from the back, and the process of drawing triangles, using the method of drawing a triangular band, if according to 0-->1-->2-->0 1-->2-->3-->1 The way it is drawn will cause the triangle to be drawn with a counterclockwise, a clockwise, while culling, will remove one of them, so OpenGL in the drawing of triangular band, the actual practice is:0-->1-->2-->0 2-->1-- >3-->2, this ensures that all triangles are drawn in a counter-clockwise direction.

then the parallelogram can be drawn in a triangular band, so if the number of points is enough (calculus), and each part is drawn in a parallelogram, then the sphere can be realized, as in the following figure:


The first is the calculation of the coordinates:

If the center dot is the coordinate origin, then the y-coordinate of the horizontal cut parts can be determined and different. The x and z coordinates of each round surface can be determined and different in the inner loop, so given the radius r of the circle, the coordinates of each point of the sphere can be plotted in two loops.

Float R = 0.7f;//radius int statck = 20;//STATCK: Slice----cut the sphere horizontally into several separate float statckstep = (float) (MATH.PI/STATCK);/unit angle Value int Slice = 50;//longitudinal cut several separate float slicestep = (float) (math.pi/slice);//horizontal Circle increment angle float r0,r1,x0,x1,y0,y1,z0,z1;
R0, R1 for the center of the two lines (x0,y0,z0) and (X1,Y1,Z1) toward the surface of the two adjacent slices are points near two sections. float alpha0 = 0,ALPHA1 = 0; Before and after two angles float beta = 0;
Angle on the slice plane list<float> coordslist = new arraylist<float> ();
    Outer loop for (int i = 0;i < statck;i++) {alpha0 = (float) (-MATH.PI/2 + (i*statckstep));
    Alpha1 = (float) (-MATH.PI/2 + ((i+1) *statckstep));
    y0 = (float) (R * Math.sin (ALPHA0));
    R0 = (float) (R * Math.Cos (ALPHA0));
    Y1 = (float) (R * Math.sin (ALPHA1));

    R1 = (float) (R * Math.Cos (ALPHA1));
        Loops for each layer of the circle for (int j = 0;j <= (slice * 2); J + +) {beta = j * SLICESTEP;
        x0 = (float) (R0 * Math.Cos (Beta));
        Z0 =-(float) (R0 * Math.sin (Beta));
        X1 = (float) (R1 * Math.Cos (Beta)); Z1 =-(float) (R1 * Math.sin (Beta));
        Coordslist.add (x0);
        Coordslist.add (y0);
        Coordslist.add (Z0);
        Coordslist.add (x1);
        Coordslist.add (y1);

    Coordslist.add (z1); }
After the coordinates are determined, the rest is simple, the same, first to set the screen color, set the drawing color, and then specify the Model view matrix, load the unit matrix, place the eyeball position, set the rotation angle. Finally, the vertex pointer is specified and the triangle band is drawn.

Run Effect chart:

Note: For the convenience of observation, this diagram is drawn in the form of a line band, and it is the method of drawing a triangular band to draw the sphere.

last attached code:

public class Mysphererenderer extends abstractrenderer{@Override public void Ondrawframe (GL10 gl) {GL.G Lclear (Gl10.gl_color_buffer_bit)//Set Clear screen color gl.glcolor4f (1f, 1f, 1f, 1f);//Set Drawing color Gl.glmatrixmode (gl10.gl_mod
        Elview);//Model View Matrix gl.glloadidentity ()//load Unit matrix Glu.glulookat (GL, 0, 0, 5, 0, 0, 0, 0, 1, 0);/Place Eyeball position Gl.glrotatef (xrotate, 1, 0, 0),//x axis rotation angle Gl.glrotatef (yrotate,0,1,0),//y axis rotation angle/*********************** calculation
        Sphere coordinates ****************************/float R = 0.7f;//sphere radius int statck = 20;//STATCK: Slice----cut the sphere crosswise into several parts float statckstep = (float) (MATH.PI/STATCK),//unit angle value int slice = 50;//longitudinal cut several separate float slicestep = (float) ( Math.pi/slice);//horizontal Circle increment angle float r0,r1,x0,x1,y0,y1,z0,z1;
        R0, R1 for the center of the two lines (x0,y0,z0) and (X1,Y1,Z1) toward the surface of the two adjacent slices are points near two sections. float alpha0 = 0,ALPHA1 = 0; Before and after two angles float beta = 0; Angle on slice plane list<float> coordslist = new Arraylist<floaT> ();
            Outer loop for (int i = 0;i < statck;i++) {alpha0 = (float) (-MATH.PI/2 + (i*statckstep));
            Alpha1 = (float) (-MATH.PI/2 + ((i+1) *statckstep));
            y0 = (float) (R * Math.sin (ALPHA0));
            R0 = (float) (R * Math.Cos (ALPHA0));
            Y1 = (float) (R * Math.sin (ALPHA1));

            R1 = (float) (R * Math.Cos (ALPHA1));
                Loops for each layer of the circle for (int j = 0;j <= (slice * 2); J + +) {beta = j * SLICESTEP;
                x0 = (float) (R0 * Math.Cos (Beta));
                Z0 =-(float) (R0 * Math.sin (Beta));
                X1 = (float) (R1 * Math.Cos (Beta));
                Z1 =-(float) (R1 * Math.sin (Beta));
                Coordslist.add (x0);
                Coordslist.add (y0);
                Coordslist.add (Z0);
                Coordslist.add (x1);
                Coordslist.add (y1);

            Coordslist.add (z1); }//Specify vertex pointer gl.glvertexpointeR (3,gl10.gl_float,0, Bufferutils.list2floatbuffer (coordslist));
        To draw a sphere, it should be drawn in the form of a triangular band, for the convenience of observation, where Gl.gldrawarrays (Gl10.gl_line_strip,0,coordslist.size ()/3) is used to draw the line band; }

    }
}

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.