Android OpenGL ES (vii) Basic geometry definition.

Source: Internet
Author: User
Tags int size

In front of Android OpenGL ES (vi): Create an instance to apply the Opengldemos program framework we created the basic framework for the sample program and provided a "Hello world" example to display the screen in red.

This example introduces several basic geometries supported by the OpenGL ES 3D Graphics library, usually a two-dimensional graphics library that can draw points, lines, polygons, arcs, paths, and so on. OpenGL ES supports the drawing of basic geometries into three categories: points, segments, triangles. This means that OpenGL ES can only draw these three basic geometries. Any complex 2D or 3D figure is constructed from these three geometries.

Complex 3D graphics, for example, are made up of dividing them into tiny triangular faces. The 3D effect is then constructed by coloring (color), adding a material (Texture), and then adding light (lighting):

Points, segments, and triangles are defined by vertices, that is, vertex arrays. Corresponding to a series of vertices on the plane, you can see the isolated points (point), you can also connect the two two lines (line Segment), can also be three three to join a triangle (Triangle). These different interpretations of a set of vertices define the basic geometry that Android OpenGL ES can draw, which defines several modes of OpenGL ES definition:

Gl_points

Draws an independent point.

Gl_line_strip

Draws a series of segments.

Gl_line_loop

Similar, but the end-to-end connection, forming a closed curve.

Gl_lines

Vertex 22 joins, consisting of multiple line segments.

Gl_triangles

Each of the three vertices constitutes a triangle, consisting of multiple triangles.

Gl_triangle_strip

Each adjacent three vertices form a triangle, which is composed of a series of triangle triangles.

Gl_triangle_fan

A point is a triangular public vertex, forming a series of adjacent triangles.

The above mode corresponds to the Android rendering method:

OpenGL ES provides two types of methods to draw a spatial geometry:

    • Public abstract Void gldrawarrays (int mode, int first, int count) Using Vetexbuffer to draw, the order of vertices is specified in the order of VertexBuffer.
    • public abstract Void gldrawelements (int mode, int count, int type, Buffer indices), the order of vertices can be redefined, and the order of vertices is specified by indices Buffer.

Where mode is the above-explained vertex mode.

As I said before, vertices are typically defined using arrays, and buffer is used to store them to improve drawing performance

Define three vertex coordinates as below and store them in Floatbuffer:

float[] Vertexarray =New float[]{ -0.8f, -0.4f*1.732f,0.0f , 0.8f, -0.4f*1.732f,0.0f , 0.0f,0.4f*1.732f,0.0f , }; Bytebuffer VBB= Bytebuffer.allocatedirect (vertexarray.length*4); Vbb.order (Byteorder.nativeorder ()); Floatbuffer Vertex=Vbb.asfloatbuffer (); Vertex.put (Vertexarray); Vertex.position (0);

With the definition of vertices, you can pass the vertex parameters to the OpenGL library by opening the appropriate switches for the Android OpenGL ES (ii): OpenGL ES Pipeline (Pipeline):

Here's how to open the vertex switch and close the vertex switch:

Gl.glenableclientstate (Gl10.gl_vertex_array); ... gl.gldisableclientstate (Gl10.gl_vertex_array);

When the vertex switch is turned on, the vertex coordinates are passed to the OpenGL pipeline by: Glvertexpointer:

public void glvertexpointer (int size,int type,int stride,buffer pointer)

    • Size: The coordinate dimension of each vertex, which can be 2,3,4.
    • type: The data type of the vertex, which can be gl_byte, Gl_short, gl_fixed, or gl_float, the default is floating-point type gl_float.
    • stride: The interval (in bytes) between each adjacent vertex in the array, which defaults to 0, indicating that there are no gaps between vertex stores.
    • pointer: Stores an array of vertices.

Apply the color values of the vertex can be stored behind the corresponding vertex, for example, RGB in 4 byte representation, at this time the adjacent vertices are not continuous storage, the STRIDE value is 4

The corresponding vertex can also specify color, material, normal (for lighting processing), and so on, in addition to the coordinates for which it can be defined.

Glenableclientstate and Gldisableclientstate can control the pipeline switch can have: gl_color_array (color), Gl_normal_array (normal), gl_texture_ Coord_array (material), Gl_vertex_array (vertex), gl_point_size_array_oes, etc.

The corresponding methods for passing in colors, vertices, materials, and normals are as follows:

Glcolorpointer (int size,int type,int  stride,buffer pointer) glvertexpointer ( int int int stride, buffer pointer) gltexcoordpointer (intintint  stride, buffer Pointer) glnormalpointer (intint stride, Buffer pointer)

If you need to use triangles to construct complex graphics, you can use Gl_triangle_strip or Gl_triangle_fan mode, and the other is by defining a sequence of vertices:

As defined by a square:

The corresponding vertex and buffer definition code:

Private  Short[] Indices = {0,1,2,0,2,3 };//To gain some performance we also put the this ones in a byte buffer.//2 bytes, therefore we multiply the number if vertices with 2.Bytebuffer IBB = Bytebuffer.allocatedirect (Indices.length *2); Ibb.order (Byteorder.nativeorder ()); Shortbuffer IndexBuffer=Ibb.asshortbuffer (); Indexbuffer.put (indices); Indexbuffer.position (0);

The order in which you define the vertices of a triangle is important . The order in which vertices are used to define polygons is important when stitching surfaces, because the order of vertices defines the orientation (forward or back) of the polygon, and in order to get the high performance of the drawing, the front and back of the polygon are not drawn, only the front of the polygon is drawn. Although the definition of "front" and "back" can be human and easy, the uniform vertex order (clockwise or counterclockwise) is generally defined for all "front".

The following code sets the counterclockwise method to "front" of the polygon:

Gl.glfrontface (GL10.GL_CCW);

Open the Ignore "back" setting:

Gl.glenable (Gl10.gl_cull_face);


The code that explicitly indicates which face to ignore is the following:

Gl.glcullface (Gl10.gl_back);  

Android OpenGL ES (vii) Basic geometry definition.

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.