Labels: Graphics OpenGL
I haven't updated my blog for a long time. I recently studied OpenGL graphics programming and wrote some interesting programs. Let's talk about it.
The sphere is widely used as a basic geometric image in game programs. The most well-known is that it can be used as a sky dome to simulate the sky, which is more detailed than the sky box, even with other special effects, it is not easy to wear.
We should have learned sphere parameter equations in our middle school mathematics class.
X = r * sin (angz) * Cos (angxy)
Y = r * sin (angz) * sin (angxy)
Z = r * Cos (angz)
Angz is the vertical angle, angxy is the horizontal angle, and r is the radius. Remember, use this equation to represent the points on the sphere surface, and use this equation to draw a sphere model.
Let's take a look at the variables and functions to be defined:
# Ifndef PI # define PI 3.1415926 // This does not need to be explained # endif # ifndef Pi2 # define Pi2 6.2831853 // 2PI # endifclass sphere {PRIVATE: gluint * vboid; gluint vert, texcoord; glfloat * Verts; // the pointer glfloat * texcoords for storing vertices and normal vectors; // the pointer int vertnum for storing texture coordinates; public: sphere (int m, int N ); // m is the vertical subdivision degree, and N is the horizontal subdivision degree ~ Sphere (); void render (); // render the Sphere !};
M and N in the constructor represent the vertical and horizontal subdivisions, respectively. The larger the value, the finer the sphere looks. gluint, glfloat is the variable type of OpenGL, which is equivalent to the unsigned int and float in C ++.
Next let's take a look at the most important part of constructing a sphere:
Vertnum = m * n * 4; // Number of Verts = new glfloat [vertnum * 3]; // each vertex has three parts: XYZ, therefore, * 3 texcoords = new glfloat [vertnum * 2]; // The texture coordinate of each vertex has two UV components. Therefore, * 2 float stepangz = PI/m; // float stepangxy = Pi2/N; // float angz = 0.0; // float angxy = 0.0; // The initial transverse angle int Index = 0; int indextex = 0; For (INT I = 0; I <m; I ++) {for (Int J = 0; j <n; j ++) {// construct a vertex float X1 = sin (angz) * Cos (angxy); float Y1 = sin (angz) * sin (angxy ); float z1 = cos (angz); Verts [Index] = x1; index ++; Verts [Index] = Y1; index ++; Verts [Index] = Z1; index ++; float V1 = angz/PI; float U1 = angxy/Pi2; texcoords [indextex] = U1; indextex ++; texcoords [indextex] = V1; indextex ++; float X2 = sin (angz + stepangz) * Cos (angxy); float y2 = sin (angz + stepangz) * sin (angxy); float Z2 = cos (angz + stepangz ); verts [Index] = x2; index ++; Verts [Index] = Y2; index ++; Verts [Index] = Z2; index ++; float v2 = (angz + stepangz)/PI; float U2 = angxy/Pi2; texcoords [indextex] = u2; indextex ++; texcoords [indextex] = V2; indextex ++; float X3 = sin (angz + stepangz) * Cos (angxy + stepangxy); float Y3 = sin (angz + stepangz) * sin (angxy + stepangxy ); float Z3 = cos (angz + stepangz); Verts [Index] = X3; index ++; Verts [Index] = Y3; index ++; Verts [Index] = Z3; index ++; float V3 = (angz + stepangz)/PI; float U3 = (angxy + stepangxy)/Pi2; texcoords [indextex] = U3; indextex ++; texcoords [indextex] = V3; indextex ++; float X4 = sin (angz) * Cos (angxy + stepangxy); float Y4 = sin (angz) * sin (angxy + stepangxy); float Z4 = cos (angz); Verts [Index] = X4; index ++; Verts [Index] = Y4; index ++; verts [Index] = z4; index ++; float V4 = angz/PI; float U4 = (angxy + stepangxy)/Pi2; texcoords [indextex] = U4; indextex ++; texcoords [indextex] = V4; indextex ++; angxy + = stepangxy;} angxy = 0.0; // The horizontal angle is 0 angz + = stepangz when the horizontal angle reaches 2 pi each time ;}
By adding a fixed angle to construct the sphere mesh, as to why the Sphere's normal vector is consistent with the vertex coordinate of the sphere surface, see the following hand-drawn:
See, the normal vector on the P point is the vector from the origin point to the P point. next we will calculate the texture coordinates. Since the horizontal angle ranges from 0 to 2 Pi, the vertical angle coordinates are [0, Pi], and the texture coordinate ranges from 0 to 1, then, the texture coordinates of each vertex can be calculated by changing the value of the horizontal and vertical angle to [0, 1] proportionally. well, since vertices, normal vectors, and texture coordinates are all available, submit the data to the video memory and then render the image.
Well, that's the result. Circle? The above results are rendered using OpenGL, and DirectX can achieve the same effect.
Draw a sphere from scratch