Spherical parametric equations of mathematical foundations
The parametric curve of the spherical surface can be represented by the spherical coordinates, and the parameter u,v is introduced, where V is the angle between the line of the ball point and the origin and the z-axis forward, and u represents the angle between the projection of the XY plane and the x-axis forward, as shown in:
The spherical parametric equation can be expressed as:
Spherical normal vector
After the parametric equations of the spherical surface are known, it is easy to obtain a normal vector of the given point, respectively, for the partial derivative of the U and V directions, and then the cross product of the two derived vectors:
Implementation Details
After the known parametric equation, it is necessary to discrete, set the steps of U and V respectively: Ustep, Vstep. then through the different U and V, the actual coordinates of the midpoint of the coordinate system (x, Y, z) are obtained, one thing to note in the implementation is:
U=0 and U=v These two lines point is the sphere's two upper and lower poles, so the rendering needs to distinguish, wherein the middle segment of the discrete points can be rendered according to the quadrilateral, and the upper and lower sections need to be rendered according to the triangle.
Code Description:
This is just a spherical surface, and if you want to draw a sphere, just use the normal vector of points when rendering.
Data structure of the point:
class Point{public: Point(){}; Point(double a,double b,double c):x(a),y(b),z(c){};public: double x; double y; double z;};
Parameter coordinates (U,V) are converted into timing coordinates (x, y, z) functions
Point getPoint(double u,double v){ doublesin(PI*v)*cos(PI2*u); doublesin(PI*v)*sin(PI2*u); doublecos(PI*v); return Point(x,y,z);}
Draw spherical
void Drawwire () {Double ustep =1/(double) ustepsnum, Vstep =1/(Double) vstepnum;Double U =0, V =0;Draw the lower triangle Group for (int i =0; i<ustepsnum;i++){Glbegin (Gl_line_loop);Point A = GetPoint (0,0);Glvertex3d (A. xA. YA. Z);Point B = GetPoint (U,vstep);Glvertex3d (b. xB. YB. Z);Point C = GetPoint (U+ustep,vstep);Glvertex3d (c. xC. YC. Z);U + = Ustep;Glend ();}//Draw Intermediate quadrilateral Group U =0, V = vstep;for (int i=1; i<vstepnum-1;i++){for (int j=0; j<ustepsnum;j++){Glbegin (Gl_line_loop);Point A = GetPoint (u,v);Point B = GetPoint (u+ustep,v);Point C = GetPoint (U+ustep,v+vstep);Point d = GetPoint (U,v+vstep);Glvertex3d (A. xA. YA. Z);Glvertex3d (b. xB. YB. Z);Glvertex3d (c. xC. YC. Z);Glvertex3d (d. xD. YD. Z);U + = Ustep;Glend ();} v + = Vstep;}//Draw lower triangle group U =0;for (int i=0; i<ustepsnum;i++){Glbegin (Gl_line_loop);Point A = GetPoint (0,1);Point B = getpoint (U,1-vstep);Point C = GetPoint (U+ustep,1-vstep);Glvertex3d (A. xA. YA. Z);Glvertex3d (b. xB. YB. Z);Glvertex3d (c. xC. YC. Z);Glend ();}}
Initializing settings and drawing functions
#define PI 3.14159265358979323846#define PI2 6.28318530717958647692600600;int5050;
voidInit () {Glclearcolor (0,1,1,1); Glcleardepth (1.0); Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); Glclearcolor (0,1,1,1); Glcleardepth (1.0); Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); Glfloat Light_position [] = {1.0f,1.0f,1.0f,0.0f}; Glfloat Light_ambient [] = {0.2f,0.2f,0.2f,0.2f}; Glfloat Light_diffuse [] = {0.5f,0.5f,0.5f,0.2f}; Glfloat Light_specular [] = {0.5f,0.5f,0.5f,0.2f}; GLLIGHTFV (Gl_light0, gl_position, light_position); GLLIGHTFV (Gl_light0, gl_ambient, light_ambient); GLLIGHTFV (Gl_light0, Gl_diffuse, Light_diffuse); GLLIGHTFV (Gl_light0, Gl_specular, light_specular); Glenable (gl_color_material); Glenable (gl_lighting); Glenable (GL_LIGHT0); Glenable (Gl_auto_normal); Glenable (gl_normalize); Glenable (gl_depth_test); Gldepthfunc (gl_less);}
voidDisplayfunc (){Glmatrixmode (Gl_modelview);glloadidentity ();glclear (gl_color_buffer_bit|gl_depth_buffer_bit);glcolor3f (1.0,0.0,0.0);glpointsize (1.0);glrotated (1,0,0);glrotated (0,1,0);glrotated (0,0,1);Drawwire ();glutswapbuffers ();}
Main function
main(int argc,char* argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowPosition(100,100); glutInitWindowSize(width,height); glutCreateWindow("Sphere"); init(); glutDisplayFunc(displayFunc); glutMainLoop();}
Effect Show
OpenGL: Drawing a Sphere