rev |
line source |
nuclear@3
|
1 #ifndef __APPLE__
|
nuclear@3
|
2 #include <GL/gl.h>
|
nuclear@3
|
3 #else
|
nuclear@3
|
4 #include <OpenGL/gl.h>
|
nuclear@3
|
5 #endif
|
nuclear@3
|
6
|
nuclear@3
|
7 #include "teapot.h"
|
nuclear@3
|
8 #include "teapot_data.h"
|
nuclear@3
|
9
|
nuclear@3
|
10 static void draw_patch(struct vec3 *bez_cp, int *index, int flip, int useg, int vseg, float scale);
|
nuclear@3
|
11
|
nuclear@3
|
12 int patch_subdivision = 6;
|
nuclear@3
|
13
|
nuclear@3
|
14 void bezier_teapot(float scale)
|
nuclear@3
|
15 {
|
nuclear@3
|
16 int i;
|
nuclear@3
|
17
|
nuclear@3
|
18 scale /= 2.0;
|
nuclear@3
|
19
|
nuclear@3
|
20 for(i=0; i<NUM_TEAPOT_PATCHES; i++) {
|
nuclear@3
|
21 float flip = teapot_part_flip[i];
|
nuclear@3
|
22 float rot = teapot_part_rot[i];
|
nuclear@3
|
23
|
nuclear@3
|
24 glMatrixMode(GL_MODELVIEW);
|
nuclear@3
|
25 glPushMatrix();
|
nuclear@3
|
26 glTranslatef(0, -3.15 * scale * 0.5, 0);
|
nuclear@3
|
27 glRotatef(rot, 0, 1, 0);
|
nuclear@3
|
28 glScalef(1, 1, flip);
|
nuclear@3
|
29 glRotatef(-90, 1, 0, 0);
|
nuclear@3
|
30
|
nuclear@3
|
31 draw_patch(teapot_verts, teapot_index + i * 16, flip < 0.0 ? 1 : 0, patch_subdivision, patch_subdivision, scale);
|
nuclear@3
|
32
|
nuclear@3
|
33 glPopMatrix();
|
nuclear@3
|
34 }
|
nuclear@3
|
35 }
|
nuclear@3
|
36
|
nuclear@3
|
37 static void draw_patch(struct vec3 *bez_cp, int *index, int flip, int useg, int vseg, float scale)
|
nuclear@3
|
38 {
|
nuclear@3
|
39 static const float uoffs[2][4] = {{0, 0, 1, 1}, {1, 1, 0, 0}};
|
nuclear@3
|
40 static const float voffs[4] = {0, 1, 1, 0};
|
nuclear@3
|
41
|
nuclear@3
|
42 int i, j, k;
|
nuclear@3
|
43 struct vec3 cp[16];
|
nuclear@3
|
44 struct vec3 pt, n;
|
nuclear@3
|
45 float u, v;
|
nuclear@3
|
46 float du = 1.0 / useg;
|
nuclear@3
|
47 float dv = 1.0 / vseg;
|
nuclear@3
|
48
|
nuclear@3
|
49 /* collect control points */
|
nuclear@3
|
50 for(i=0; i<16; i++) {
|
nuclear@3
|
51 cp[i] = bez_cp[index[i]];
|
nuclear@3
|
52 }
|
nuclear@3
|
53
|
nuclear@3
|
54 glBegin(GL_QUADS);
|
nuclear@3
|
55 glColor3f(1, 1, 1);
|
nuclear@3
|
56
|
nuclear@3
|
57 u = 0;
|
nuclear@3
|
58 for(i=0; i<useg; i++) {
|
nuclear@3
|
59 v = 0;
|
nuclear@3
|
60 for(j=0; j<vseg; j++) {
|
nuclear@3
|
61
|
nuclear@3
|
62 for(k=0; k<4; k++) {
|
nuclear@3
|
63 pt = bezier_patch(cp, u + uoffs[flip][k] * du, v + voffs[k] * dv);
|
nuclear@3
|
64
|
nuclear@3
|
65 /* top/bottom normal hack */
|
nuclear@3
|
66 if(pt.z > 3.14) {
|
nuclear@3
|
67 n.x = n.y = 0.0f;
|
nuclear@3
|
68 n.z = 1.0f;
|
nuclear@3
|
69 } else if(pt.z < 0.00001) {
|
nuclear@3
|
70 n.x = n.y = 0.0f;
|
nuclear@3
|
71 n.z = -1.0f;
|
nuclear@3
|
72 } else {
|
nuclear@3
|
73 n = bezier_patch_norm(cp, u + uoffs[flip][k] * du, v + voffs[k] * dv);
|
nuclear@3
|
74 }
|
nuclear@3
|
75
|
nuclear@3
|
76 glTexCoord2f(u, v);
|
nuclear@3
|
77 glNormal3f(n.x, n.y, n.z);
|
nuclear@3
|
78 glVertex3f(pt.x * scale, pt.y * scale, pt.z * scale);
|
nuclear@3
|
79 }
|
nuclear@3
|
80
|
nuclear@3
|
81 v += dv;
|
nuclear@3
|
82 }
|
nuclear@3
|
83 u += du;
|
nuclear@3
|
84 }
|
nuclear@3
|
85
|
nuclear@3
|
86 glEnd();
|
nuclear@3
|
87 }
|