[OpenGL] Case 13: Subdivision approximation program of Sphere

Source: Internet
Author: User
// A.13 subdivision approximation program for Sphere/* recursive subdivision of tetrahedron (Chapter 6 ). three displaymodes: wire frame, constant, and interpolative shading * // * program also inclustrates defining materials and light sourcesint Init () * // * Mode 0 = wire frame, mode 1 = constant shading, Mode 3 = interpolative shading */# include <stdlib. h> # include <math. h> # include <Gl/glut. h> typedef float point [3];/* Initial tetrahedron */Point V [] = {0.0, 0.0, 1.0}, {0.0, 0.942809,-0.33333}, {-0.816497,-0.471405,-0.333333}, {0.816497, -0.471405,-0.333333 }}; static glfloat Theta [] = {0.0, 0.0, 0.0}; int N; int mode; void triangle (point a, point B, point C) /* display one triangle using a line loop for wire frame, a singlenormal for constant shading, or three normals for interpolative shading */{If (mode = 0) glbegin (gl_line_loop ); else Glbegin (gl_polygon); If (mode = 1) glnormal3fv (a); If (mode = 2) glnormal3fv (a); glvertex3fv (); if (mode = 2) glnormal3fv (B); glvertex3fv (B); If (mode = 2) glnormal3fv (c); glvertex3fv (c); glend ();} void normal (point P) {/* normalize a vector */float d = 0.0; int I; for (I = 0; I <3; I ++) d + = P [I] * P [I]; D = SQRT (d); If (D> 0.0) for (I = 0; I <3; I ++) P [I]/= D;} void divide_triangle (point a, point B, point C, int m) {/* triangle subdi Vision using Vertex numbersrighthand rule applied to create outward pointing faces */point V1, V2, V3; Int J; If (M> 0) {for (j = 0; j <3; j ++) V1 [J] = A [J] + B [J]; normal (V1); For (j = 0; j <3; j ++) v2 [J] = A [J] + C [J]; normal (V2); For (j = 0; j <3; j ++) v3 [J] = B [J] + C [J]; normal (V3); divide_triangle (A, V1, V2 m-1); divide_triangle (C, V2, V3, m-1 |, B, C);/* draw triangle at end of recursion */} void tetrahedron (INT m) {/* apply triangle subdivision to faces of tetrahedron */divide_triangle (V [0], V [1], V [2], M); divide_triangle (V [3], V [2], V [1], M); divide_triangle (V [0], V [3], V [1], M); divide_triangle (V [0], V [2], V [3], M);} void display (void) {/* displays all three modes, side by side */glclear (gl_color_buffer_bit | gl_depth_buffer_bit); glloadide Ntity (); mode = 0; tetrahedron (n); mode = 1; gltranslatef (-2.0, 0.0, 0.0); tetrahedron (n); mode = 2; gltranslatef (4.0, 0.0, 0.0); tetrahedron (n); glflush ();} void myreshape (int w, int h) {glviewport (0, 0, W, H ); glmatrixmode (gl_projection); glloadidentity (); If (W <= h) glortho (-4.0, 4.0,-4.0 * (glfloat) h/(glfloat) W, 4.0 * (glfloat) h/(glfloat) W,-10.0, 10.0); elseglortho (-4.0 * (glfloat) W/(glfloat) h, 4.0 * (GL Float) W/(glfloat) h,-4.0, 4.0,-10.0, 10.0); glmatrixmode (gl_modelview); display ();} void myinit () {glfloat mat_specular [] = {1.0, 1.0, 1.0, 1.0}; glfloat mat_diffuse [] = {1.0, 1.0, 1.0, 1.0}; glfloat mat_ambient [] = {1.0, 1.0, 1.0, 1.0}; glfloat mat_shininess = {100.0}; glfloat light_ambient [] = {0.0, 0.0, 0.0, 1.0}; glfloat light_diffuse [] = {1.0, 1.0, 1.0, 1.0}; glfloat light_specular [] = {1.0, 1.0, 1.0, 1.0 };/* Set up ambient, diffuse, and specular components for Light 0 */gllightfv (gl_light0, latency, latency); gllightfv (gl_light0, gl_diffuse, light_diffuse); gllightfv (gl_light0, gl_specular, light_specular);/* define material proerties for front face of all polygons */trim (gl_front, gl_specular, mat_specular); trim (gl_front, callback, callback); glmaterialfv (gl_front, gl_diffu Se, mat_diffuse); glmaterialf (gl_front, gl_shininess, mat_shininess); glshademodel (gl_smooth);/* enable smooth shading */gl_lighting ); /* enable lighting */glable (gl_light0);/* enable Light 0 */glable (gl_depth_test);/* enable Z buffer */glclearcolor (1.0, 1.0, 1.0, 1.0); glcolor3f (0.0, 0.0, 0.0);} void main (INT argc, char ** argv) {n = 3; // n = ...... // N = atoi (argv [1]); // an error may occur when running the program directly. For details, refer to the source code Processing Solution of bookcode: gluinit (& argc, argv ); gluinitdisplaymode (glu_single | glu_rgb | glu_depth); gluinitwindowsize (500,500); glucreatewindow ("sphere"); myinit (); glureshapefunc (myreshape); gludisplayfunc (Display); glumainloop ();}

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.