oculus1

view src/teapot.c @ 29:9a973ef0e2a3

fixed the performance issue under MacOSX by replacing glutSolidTeapot (which uses glEvalMesh) with my own teapot generator.
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 27 Oct 2013 06:31:18 +0200
parents
children
line source
1 #ifndef __APPLE__
2 #include <GL/gl.h>
3 #else
4 #include <OpenGL/gl.h>
5 #endif
7 #include "teapot.h"
8 #include "teapot_data.h"
10 static void draw_patch(struct vec3 *bez_cp, int *index, int flip, int useg, int vseg, float scale);
12 int patch_subdivision = 6;
14 void bezier_teapot(float scale)
15 {
16 int i;
18 scale /= 2.0;
20 for(i=0; i<NUM_TEAPOT_PATCHES; i++) {
21 float flip = teapot_part_flip[i];
22 float rot = teapot_part_rot[i];
24 glMatrixMode(GL_MODELVIEW);
25 glPushMatrix();
26 glTranslatef(0, -3.15 * scale * 0.5, 0);
27 glRotatef(rot, 0, 1, 0);
28 glScalef(1, 1, flip);
29 glRotatef(-90, 1, 0, 0);
31 draw_patch(teapot_verts, teapot_index + i * 16, flip < 0.0 ? 1 : 0, patch_subdivision, patch_subdivision, scale);
33 glPopMatrix();
34 }
35 }
37 static void draw_patch(struct vec3 *bez_cp, int *index, int flip, int useg, int vseg, float scale)
38 {
39 static const float uoffs[2][4] = {{0, 0, 1, 1}, {1, 1, 0, 0}};
40 static const float voffs[4] = {0, 1, 1, 0};
42 int i, j, k;
43 struct vec3 cp[16];
44 struct vec3 pt, n;
45 float u, v;
46 float du = 1.0 / useg;
47 float dv = 1.0 / vseg;
49 /* collect control points */
50 for(i=0; i<16; i++) {
51 cp[i] = bez_cp[index[i]];
52 }
54 glBegin(GL_QUADS);
55 glColor3f(1, 1, 1);
57 u = 0;
58 for(i=0; i<useg; i++) {
59 v = 0;
60 for(j=0; j<vseg; j++) {
62 for(k=0; k<4; k++) {
63 pt = bezier_patch(cp, u + uoffs[flip][k] * du, v + voffs[k] * dv);
65 /* top/bottom normal hack */
66 if(pt.z > 3.14) {
67 n.x = n.y = 0.0f;
68 n.z = 1.0f;
69 } else if(pt.z < 0.00001) {
70 n.x = n.y = 0.0f;
71 n.z = -1.0f;
72 } else {
73 n = bezier_patch_norm(cp, u + uoffs[flip][k] * du, v + voffs[k] * dv);
74 }
76 glTexCoord2f(u, v);
77 glNormal3f(n.x, n.y, n.z);
78 glVertex3f(pt.x * scale, pt.y * scale, pt.z * scale);
79 }
81 v += dv;
82 }
83 u += du;
84 }
86 glEnd();
87 }