OpenGL Draw Helix

Source: Internet
Author: User

1. Figure:

2. Code

/** * @ description use OpenGL to draw Helix * @ Project Name APP_OPENGL * @ Package Name Com.android.opengl * @ class name spiralmainactivity * @author Chenlin * @da TE May 12, 2014 PM 10:57:20 * @version 1.0 */public class Spiralmainactivity extends Activity {private static float
    ANGLE = -90f;
    private static float pos_x = 1f;
    private static float pos_y = 0f;

    private static float pos_z = 0f;
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
        Myglsurfaceview Surfaceview = new Myglsurfaceview (this);
        Surfaceview.setrenderer (New Myrender ());
    Setcontentview (Surfaceview); } private class Myglsurfaceview extends Glsurfaceview {public Myglsurfaceview (context context, AttributeSet
        Attrs) {Super (context, attrs);
        } public Myglsurfaceview (context context) {super (context); }}/** * Change position according to key */@Override public boolean onKeyDown (int keycode,KeyEvent event) {float step = 0.5f;
        Determine the key if (keycode = = keyevent.keycode_dpad_up) {pos_x = Pos_x-step;

        } else if (keycode = = Keyevent.keycode_dpad_down) {pos_x = pos_x + step;

        } else if (keycode = = keyevent.keycode_dpad_left) {pos_y = Pos_y-step;

        } else if (keycode = = keyevent.keycode_dpad_right) {pos_y = pos_y + step;
    } return Super.onkeydown (KeyCode, event);

        } Private class Myrender implements Renderer {private float mratio; The surface is created @Override public void onsurfacecreated (GL10 gl, EGLConfig config) {//R,g,b,alph
            A) Gl.glclearcolor (0f, 0f, 0f, 1f);
        Enable array of vertex buffers gl.glenableclientstate (Gl10.gl_vertex_array);
            }//Surface size @Override public void onsurfacechanged (GL10 gl, int width, int height) { Gl.glviewport (0, 0, width, height);
            Mratio = width * 1.0f/height;
            Gl.glmatrixmode (gl10.gl_projection);
            Gl.glloadidentity ();
        GL.GLFRUSTUMF ( -1f, 1f,-mratio, Mratio, 3f, 7f); }//Drawing @Override public void Ondrawframe (GL10 gl) {//Clear color buffers Gl.glclea
            R (Gl10.gl_color_buffer_bit);
            Set the drawing color gl.glcolor4f (1f, 0f, 0f, 1f);
            Operation Model Matrix Gl.glmatrixmode (Gl10.gl_modelview);
            Gl.glloadidentity ();

            Set the eye observation point Glu.glulookat (GL, 0f, 0f, 5f, 0f, 0f, 0f, 0f, 1f, 0f);
            Rotary//Gl.glrotatef ( -90f, 1, 0, 0);

            Gl.glrotatef (ANGLE, pos_x, pos_y, pos_z);
            Draw Circular Float R = 0.5f;//radius list<float> coordslist = new arraylist<float> ();
            Defines the center coordinate of float x = 0f, y = 0f, z = 1.5f;
            float zstep = 0.01f; for (Float i = 0f; I < Math.PI * 6;
                i = (float) (i + math.pi/32)) {//get rotated at coordinates x = (float) (R * Math.Cos (i));
                y = (float) (R * Math.sin (i));
                z = z-zstep;
                Coordslist.add (x);
                Coordslist.add (y);
            Coordslist.add (z);
            } bytebuffer bb = Bytebuffer.allocatedirect (Coordslist.size () * 4);
            Bb.order (Byteorder.nativeorder ());
            Floatbuffer fb = Bb.asfloatbuffer ();
            for (float f:coordslist) {fb.put (f);
            } bb.position (0);
            Specifies the vertex pointer gl.glvertexpointer (3, gl10.gl_float, 0, BB);
        Plot the number of points gl.gldrawarrays (gl10.gl_points, 0, Coordslist.size ()/3); }

    }
}

3. Drawing a picture

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.