Android 3D engine research 2

Source: Internet
Author: User

In the previous article, we can see that a cube can be displayed on the screen through a small amount of code, which is equivalent to the hello World Program in min3d. However, for OpenGL ES programming, the data we usually need to process is a triangle. Unfortunately, these details are hidden using engines like min3d. Here, we manually generate a cube placed on the origin, giving vertices, triangles, and normal vectors, which are then displayed by the min3d engine. This gives us a better understanding of how OpenGL ES works. The specific implementation code is as follows:

Public mgnavobject (Boolean useuvs, Boolean usenormals, Boolean usevertexcolors ){
Super (4*6, 2*6, useuvs, usenormals, usevertexcolors );
Float width = 1.0f/2;
Float Height = 1.0f/2;
Float deepth = 1.0f/2;
Float textureu = 0.0f;
Float texturev = 0.0f;
Float normalx = 0.0f;
Float normaly = 0.0f;
Float normalz = 0.0f;
Short colorr = 0;
Short colorg = 0;
Short colorb = 0;
Short colora = 0;
// Display the right Cube:
// Area number: Starting from the front, the area number is 1 (Before), 2 (right), 3 (after), 4 (left), 5 (top), 6 (lower)
// Vertex number: Start from the top left corner, clockwise 1, 2, 3, 4; followed by four to five, two to six, three to seven, four to eight
// For example, if vertex 1 is in front of f1p1 and f5p1 is in front of vertex 1
// 1. front
NormalX = 0f;
NormalY = 0f;
NormalZ = 1.0f;
// 1.1. Add the front right top triangle
Short f1p1Idx = vertices (). addVertex (-width, height, deepth,
TextureU, textureV,
NormalX, normalY, normalZ,
ColorR, colorG, colorB, colorA );
Short f1p3Idx = vertices (). addVertex (width,-height, deepth,
TextureU, textureV,
NormalX, normalY, normalZ,
ColorR, colorG, colorB, colorA );
Short f1p2Idx = vertices (). addVertex (width, height, deepth,
TextureU, textureV,
NormalX, normalY, normalZ,
ColorR, colorG, colorB, colorA );
Faces (). add (f1p1Idx, f1p3Idx, f1p2Idx );
// 1.2. Add the Lower Left triangle to the front
Short f1p4Idx = vertices (). addVertex (-width,-height, deepth,
TextureU, textureV,
NormalX, normalY, normalZ,
ColorR, colorG, colorB, colorA );
Faces (). add (f1p1Idx, f1p4Idx, f1p3Idx );
// 2. Add the right side
NormalX = 1.0f;
NormalY = 0.0f;
NormalZ = 0.0f;
// 2.1. Add triangle slices 2, 3, and 7
Short f2p2Idx = vertices (). addVertex (width, height, deepth,
TextureU, textureV,
NormalX, normalY, normalZ,
ColorR, colorG, colorB, colorA );
Short f2p3Idx = vertices (). addVertex (width,-height, deepth,
TextureU, textureV,
NormalX, normalY, normalZ,
ColorR, colorG, colorB, colorA );
Short f2p7Idx = vertices (). addVertex (width,-height,-deepth,
TextureU, textureV,
NormalX, normalY, normalZ,
ColorR, colorG, colorB, colorA );
Faces (). Add (f2p2idx, f2p3idx, f2p7idx );
// 2.2. Add a triangle, 5
Short f2p5idx = vertices (). addvertex (width, height,-deepth,
Textureu, texturev,
Normalx, normaly, normalz,
Colorr, colorg, colorb, colora );
Faces (). Add (f2p2idx, f2p7idx, f2p5idx );
// 3. Add the following
Normalx = 0.0f;
Normaly = 0.0f;
Normalz =-1.0f;
// 3.1. Add a triangle, 6, 7, and 8
Short f3p6idx = vertices (). addvertex (width, height,-deepth,
Textureu, texturev,
Normalx, normaly, normalz,
Colorr, colorg, colorb, colora );
Short f3p7idx = vertices (). addvertex (width,-height,-deepth,
Textureu, texturev,
Normalx, normaly, normalz,
Colorr, colorg, colorb, colora );
Short f3p8Idx = vertices (). addVertex (-width,-height,-deepth,
TextureU, textureV,
NormalX, normalY, normalZ,
ColorR, colorG, colorB, colorA );
Faces (). add (f3p6Idx, f3p7Idx, f3p8Idx );
// 3.2. Add triangle film 6, 8, 5
Short f3p5Idx = vertices (). addVertex (-width, height,-deepth,
TextureU, textureV,
NormalX, normalY, normalZ,

Related Article

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.