Annotation of OpenXM/src/cfep/glicosa.c, Revision 1.1.1.1
1.1 takayama 1: #include <stdio.h>
2: #include <math.h>
3: #include "myenv.h"
4: #include "mygl.h"
5:
6: #ifdef COCOA
7: #import <OpenGL/OpenGL.h>
8: #import <OpenGL/gl.h>
9: #import <OpenGL/glu.h>
10: #else
11: #include <GL/gl.h>
12: #include <GL/glu.h>
13: #include <GL/glut.h>
14: #endif
15:
16:
17: #ifdef MYTEST
18: main(int argc,char **argv) {
19: test_main(argc,argv);
20: }
21: void ourDisplay() {
22: glib3_std_scene0(0.0, 0.0, 3.0);
23: glib3_icosahedron(0.0, 0.0, -3.0, 1.0);
24: printf("ourDisplay is called.\n");
25: }
26: int test_main(int argc,char **argv) {
27: glutInit(&argc, argv);
28: glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
29: glutInitWindowSize(400,400);
30: glutInitWindowPosition(100,100);
31: glutCreateWindow("glicosahedron");
32:
33: glib3_std_scene0(0.0, 0.0, 3.0);
34: glutDisplayFunc(ourDisplay);
35: glutReshapeFunc(ourDisplay);
36: glutMainLoop();
37: return 0;
38: }
39: #endif
40:
41: void glib3_std_scene0_aux(int frustum, int usebox,
42: float xnear, float ynear, float znear,
43: float left,float right,float bottom,float top,float near,float far) {
44: GLfloat light_position0[] = { 10.0, 10.0, 10.0, 1.0 };
45: GLfloat light_position1[] = { -10.0, 10.0, 10.0, 1.0 };
46: GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
47: GLfloat diffuse[] = { 0.1, 0.8, 0.8, 1.0 };
48: GLfloat specular[] = { 0.9, 0.9, 0.9, 1.0 };
49: GLfloat shininess[] = { 70.0 };
50: GLfloat std_light_param0[] = { 1.0, 1.0, 1.0, 1.0 };
51: GLfloat std_light_param[] = { 0.5, 0.5, 0.5, 1.0 };
52:
53: glShadeModel(GL_SMOOTH); // GL_SMOOTH or GL_FLAT
54: glDepthFunc(GL_LEQUAL);
55: glEnable(GL_DEPTH_TEST);
56:
57: glClearColor(1.0, 1.0, 1.0, 1.0);
58: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
59: glMatrixMode(GL_PROJECTION);
60: glLoadIdentity();
61: if (frustum) {
62: if (znear >= 2.0) gluPerspective(40.0,1.0,1.0,200.0);
63: else gluPerspective(40.0,1.0,znear/2,20.0);
64: }else{
65: glOrtho(left,right,bottom,top,near,far);
66: }
67:
68: glMatrixMode(GL_MODELVIEW);
69: glLoadIdentity();
70: gluLookAt(xnear,ynear,znear,0.0,0.0,0.0,0.0,1.0,0.0);
71:
72:
73: glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
74: /* glLightfv( GL_LIGHT0, GL_AMBIENT, std_light_param ); */
75: glLightfv( GL_LIGHT0, GL_DIFFUSE, std_light_param );
76: glLightfv( GL_LIGHT0, GL_SPECULAR, std_light_param );
77: glLightfv( GL_LIGHT0, GL_POSITION, light_position0 );
78:
79: glLightfv( GL_LIGHT1, GL_DIFFUSE, std_light_param );
80: glLightfv( GL_LIGHT1, GL_SPECULAR, std_light_param );
81:
82: glLightfv( GL_LIGHT1, GL_POSITION, light_position1 );
83: glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuse );
84: glMaterialfv( GL_FRONT, GL_SPECULAR, specular );
85: glMaterialfv( GL_FRONT, GL_SHININESS, shininess );
86:
87: glEnable( GL_LIGHTING );
88: glEnable( GL_LIGHT0 );
89: glEnable( GL_LIGHT1 );
90:
91: glEnable(GL_COLOR_MATERIAL);
92:
93: if (usebox > 0) glib3_bounding_box((float) usebox);
94:
95: }
96: void glib3_std_scene0(float xnear, float ynear, float znear) {
97: glib3_std_scene0_aux(1,0,xnear, ynear, znear, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
98: }
99: void glib3_std_scene0_ortho(float xnear, float ynear,float znear,float size) {
100: glib3_std_scene0_aux(0,0, xnear, ynear, znear,
101: (-1.0+xnear)*size, (1.0+xnear)*size,
102: (-1.0+ynear)*size, (1.0+ynear)*size, -100.0*size, 100.0*size);
103: }
104:
105: #define X 0.525731112119133606
106: #define Z 0.850650808352039932
107: static void normalize(float v[3]) {
108: float norm;
109: norm = sqrt((double) (v[0]*v[0]+v[1]*v[1]+v[2]*v[2]));
110: if (norm > 0.0) {
111: v[0] = v[0]/norm;
112: v[1] = v[1]/norm;
113: v[2] = v[2]/norm;
114: }
115: }
116: static void outerProd(float v1[3], float v2[3], float ans[3]) {
117: ans[0] = v1[1]*v2[2]-v1[2]*v2[1];
118: ans[1] = v1[2]*v2[0]-v1[0]*v2[2];
119: ans[2] = v1[0]*v2[1]-v1[1]*v2[0];
120: normalize(ans);
121: }
122: static int icosaFace[20][3] = {
123: {0,4,1},{0,9,4}, {9,5,4},{4,5,8}, {4,8,1},
124: {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
125: {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
126: {6,1,10},{9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
127: };
128: void glib3_icosahedron(float x,float y,float z, float size) {
129: float vv[12][3];
130: int i,j;
131: float e1[3], e2[3], normal[3];
132: static int nnn=0;
133: nnn++; if (nnn > 20) nnn=1;
134: for (i=0; i<12; i++) {vv[i][0] = x; vv[i][1] = y; vv[i][2] = z; }
135: vv[0][0] += -X*size, vv[0][1] += 0.0; vv[0][2] += Z*size;
136: vv[1][0] += X*size, vv[1][1] += 0.0; vv[1][2] += Z*size;
137: vv[2][0] += -X*size, vv[2][1] += 0.0; vv[2][2] += -Z*size;
138: vv[3][0] += X*size, vv[3][1] += 0.0; vv[3][2] += -Z*size;
139: vv[4][0] += 0.0, vv[4][1] += Z*size; vv[4][2] += X*size;
140: vv[5][0] += 0.0, vv[5][1] += Z*size; vv[5][2] += -X*size;
141: vv[6][0] += 0.0, vv[6][1] += -Z*size; vv[6][2] += X*size;
142: vv[7][0] += 0.0, vv[7][1] += -Z*size; vv[7][2] += -X*size;
143: vv[8][0] += Z*size, vv[8][1] += X*size; vv[8][2] += 0.0;
144: vv[9][0] += -Z*size, vv[9][1] += X*size; vv[9][2] += 0.0;
145: vv[10][0] += Z*size, vv[10][1] += -X*size; vv[10][2] += 0.0;
146: vv[11][0] += -Z*size, vv[11][1] += -X*size; vv[11][2] += 0.0;
147:
148: // glMaterialf(GL_FRONT,GL_SHININESS,80.0);
149: glColor3f(1.0,0.0,0.0);
150: // glEnable(GL_AUTO_NORMAL);
151: for (i=0; i<20; i++) { // use 20 or use "nnn"
152: if (i%3 == 0) glColor3f(1.0,0.0,0.0);
153: else if (i%3 == 1) glColor3f((i%10)/10.0,1.0,0.0);
154: else glColor3f((i%10)/10.0, 0.0, 1.0);
155: for (j=0; j<3; j++) {
156: e1[j] = vv[icosaFace[i][0]][j] - vv[icosaFace[i][1]][j];
157: e2[j] = vv[icosaFace[i][1]][j] - vv[icosaFace[i][2]][j];
158: }
159: outerProd(e1,e2,normal);
160: glNormal3fv(normal);
161:
162: // NSLog(@"icosaface=(%d,%d,%d)\n",icosaFace[i][0],icosaFace[i][1],icosaFace[i][2]);
163: glBegin(GL_TRIANGLES);
164: glVertex3fv(&(vv[icosaFace[i][0]][0]));
165: glVertex3fv(&(vv[icosaFace[i][1]][0]));
166: glVertex3fv(&(vv[icosaFace[i][2]][0]));
167: glEnd();
168: }
169:
170: }
171: // Draw a wireflame of [-box,box]^3.
172: void glib3_bounding_box(float box) {
173: glPolygonMode(GL_FRONT,GL_LINE);
174: glPolygonMode(GL_BACK,GL_LINE);
175:
176: // fix z back
177: glBegin(GL_POLYGON);
178: glColor3f(0,1.0,0);
179: glNormal3f(0,0,1);
180: glVertex3f(-box,-box,-box); glVertex3f(box,-box,-box); glVertex3f(box,box,-box);
181: glVertex3f(-box,box,-box); glVertex3f(-box,-box,-box);
182: glEnd();
183:
184: // fix z, front
185: glBegin(GL_POLYGON);
186: glNormal3f(0,0,1); // 0,0,-1 : invisible
187: glVertex3f(-box,-box,box); glVertex3f(box,-box,box); glVertex3f(box,box,box);
188: glVertex3f(-box,box,box); glVertex3f(-box,-box,box);
189: glEnd();
190:
191: // fix x left box
192: glBegin(GL_POLYGON);
193: glNormal3f(1,0,0);
194: glVertex3f(-box,-box,-box); glVertex3f(-box,box,-box); glVertex3f(-box,box,box);
195: glVertex3f(-box,-box,box); glVertex3f(-box,-box,-box);
196: glEnd();
197:
198: // right box
199: glBegin(GL_POLYGON);
200: glNormal3f(1,0,0);
201: glVertex3f(box,-box,-box); glVertex3f(box,box,-box); glVertex3f(box,box,box);
202: glVertex3f(box,-box,box); glVertex3f(box,-box,-box);
203: glEnd();
204:
205: glPolygonMode(GL_FRONT,GL_FILL);
206: glPolygonMode(GL_BACK,GL_FILL);
207:
208: }
209:
210: void glib3_test_depth() {
211: int i,j;
212: float vv0[3];
213: float vv1[3];
214: float vv2[3];
215:
216: vv0[0] = 0.0; vv0[1] = 0.0; vv0[2] = 0.0;
217: vv1[0] = 0.0; vv1[1] = 1.0; vv1[2] = 0.0;
218: vv2[0] = 1.0; vv2[1] = 0.0; vv2[2] = 0.0;
219: for (i=0; i<20; i++) { // use 20 or use "nnn"
220: if (i%3 == 0) glColor3f(1.0,0.0,0.0);
221: else if (i%3 == 1) glColor3f(0.0,1.0,0.0);
222: else glColor3f(0.0, 0.0, 1.0);
223:
224: vv1[1] += i/10.0;
225: vv0[2] = vv1[2] = vv2[2] = -i/10.0;
226: glBegin(GL_TRIANGLES);
227: glVertex3fv(vv0);
228: glVertex3fv(vv1);
229: glVertex3fv(vv2);
230: glEnd();
231: }
232: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>