OpenGL Drawing Line---OpenGL Learning notes (iii) __OPENGL

Source: Internet
Author: User
Tags clear screen cos sin truncated

There are three kinds of entities in OpenGL: dots, lines, triangles.

For Lines, OpenGL provides us with three ways to draw lines:

① Draw Segment (lines): The so-called line segment is not connected to some of the set of lines.


② Draw Line (Line_strip): A line that is connected but not closed.


③ Draw line Ring (Line_loop): The end-connected and closed line.

The first step is to implement the line segment, where we draw a divergent ray shape, and the effect is as follows:


render implementation Class rewrite three methods, the first two identical, the key to look at the Ondrawframe () method:

again, the preparation is to clear the color buffer, set the drawing color, set the matrix mode, load the unit matrix, place the eyeball position, and set the rotation angle if necessary.

so get ready for work and you can officially paint the rays, first of all to give the line segment a length of R, and then define the coordinates of the points on the circle, using the list to store the coordinates of each point, found that too many points can not be manually specified, so with a for loop the coordinates of the points on the circle and put into the list, Before placing this point in the list, remember to put the center point (0,0,0) first into the list, this allows a line to be determined by two points (the points on the center point and the circular circle), and finally, set the vertex pointer and the array, and when you draw the array, be aware that type is gl10.gl_lines, The number one or 0,count is divided by three.


next to achieve the draw line, this time with the way of drawing lines to draw a spiral, the effect of the picture is as follows:

The implementation is basically similar to a line segment, except that the origin is not required, and then the type parameter in the Gldrawarrays () method is set to Gl_line_strip and OK.

Last attached code

Draw segment:

public class Mylinerenderer extends Abstractrenderer {public void Ondrawframe (GL10 gl) {//Clear color buffer
        Gl.glclear (Gl10.gl_color_buffer_bit);
        Sets the drawing color gl.glcolor4f (1f,1f,1f,1f);
        Set the Model View Matrix Gl.glmatrixmode (Gl10.gl_modelview);
        Load Unit matrix gl.glloadidentity ();
        Place Eyeball Position Glu.glulookat (GL, 0f, 0f, 5f, 0f, 0f, 0f, 0f, 1f, 0f); Rotation angle for more intuitive viewing//Gl.glrotatef ( -90,1,0,0);//round X axis rotation (opengl specified, clockwise rotation to negative)//Gl.glrotatef (yrotate,1,0,0);//Around Y axis
         Rotation/** * Coordinates of the calculated point * @param r radius * @param coordslist coordinate set * @param x,y,z coordinates of each point
        * @param Alpha Angle * * */Float R = 0.5f;//radius float x = 0f,y = 0f,z = 0f;//Point coordinates

        list<float> coordslist = new arraylist<float> (); Loop draw point for (float alpha = 0f; Alpha < Math.PI * 6;alpha = (float) (ALPHA+MATH.PI/16)) {x = (flo at) (Math.Cos (Alpha) * r);
            y = (float) (Math.sin (Alpha) * R);
            Add the Origin point Coordslist.add (0f);
            Coordslist.add (0f);
            Coordslist.add (0f);
            Add the current point Coordslist.add (x);
            Coordslist.add (y);
        Coordslist.add (z);
        Gl.glvertexpointer (3, gl10.gl_float, 0, Bufferutils.list2bytebuffer (coordslist));
    Gl.gldrawarrays (Gl10.gl_lines,0,coordslist.size ()/3); }
}
Draw the line belt:

public class Mylinestriprenderer extends abstractrenderer{public void Ondrawframe (GL10 gl) {//Clear color buffer
        Gl.glclear (Gl10.gl_color_buffer_bit);
        Set Drawing color gl.glcolor4f (1f, 1f, 1f, 1f);
        Set the Model View Matrix Gl.glmatrixmode (Gl10.gl_modelview);
        Load Unit matrix gl.glloadidentity ();
        Place Eyeball Position Glu.glulookat (GL, 0f, 0f, 5f, 0f, 0f, 0f, 0f, 1f, 0f); Rotation angle for more intuitive viewing of Gl.glrotatef (-90, 1, 0, 0);//round X axis rotation (opengl specified, clockwise rotation to negative) Gl.glrotatef (yrotate,1,0,0);/Around Y axis
         /** * Coordinates of the calculated point * @param r radius * @param coordslist coordinate set * @param x,y,z coordinates of each point * @param alpha Angle */list<float> coordslist = new arraylist<float> ();
        Use list to store each point float R = 0.5f;//radius float x = 0f,y = 0f,z = 1.5f;//point coordinate float zstep = step length of 0.005f;//z axis
        float lsize = 1.0f;
        float lstep = 0.5f; To loop out the coordinates for each point (Float alpha = 0f;
            Alpha < Math.PI * 12;alpha = (float) (ALPHA+MATH.PI/32)) {x = (float) (Math.Cos (Alpha) * R);
            y = (float) (Math.sin (Alpha) * R);
            z = z-zstep;
            Gl.gllinewidth (lsize = lstep+lsize);
            Coordslist.add (x);
            Coordslist.add (y);
        Coordslist.add (z);  } gl.glvertexpointer (3,gl10.gl_float,0, Bufferutils.list2bytebuffer (coordslist));//specify vertex pointer/** * Draw Array * Because the coordslist uses 3 dots to save one coordinate, * so the number of points is divided by 3/gl.gldrawarrays (Gl10.gl_line_strip,0,co


    Ordslist.size ()/3); }
}
Tool classes to be required

Abstactrenderer:

Public abstract class Abstractrenderer implements glsurfaceview.renderer{private float ratio;//viewport ratio public float Xrotate = 0f;//rotation around the x axis public float yrotate = 0f;//rotation around the y-axis @Override public void onsurfacecreated (GL10 gl,
         Eglconfig config) {/** * steps: * The first step is to set the screen color, and the second step is to enable the vertex buffer array.
        ///Set Clear screen color Gl.glclearcolor (0f, 0f, 0f, 1f);

    Enables vertex buffer array gl.glenableclientstate (Gl10.gl_vertex_array); @Override public void onsurfacechanged (GL10 gl, int width, int height) {/** Step: * The first step is to set the viewport, and then set the
         Flat truncated head body, and to set the flat truncated head body must first specify the matrix type for the projection matrix * Set the matrix type, immediately to load the unit matrix.
         * * Set the flat head body: * In order to see the display of the image is not distorted, the general setting of the proportion of the flat head and the ratio of the viewport.
         * So it is necessary to calculate the proportion of the viewport, and then applied to the flat truncated head body.
        *//Set the viewport Gl.glviewport (0,0,width,height);
        Calculate the viewport ratio ratio = (float) width/(float) height;
        Set matrix mode Gl.glmatrixmode (gl10.gl_projection); Load Unit matrix Gl.glloadidentiTy (); Set the flat truncated head body gl.glfrustumf (ratio,-ratio,-1f,1f,3f,7f);//ratio is with/height, so height is, width is ratio} @Override pub
Lic abstract void Ondrawframe (GL10 gl); }
Bufferutils:

public class Bufferutils {

    /**
     *
     *
     

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.