vrheights

view src/teapot.c @ 14:25cab9e20c9c

mesh vbos
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 09 Oct 2014 01:29:28 +0300
parents 316ec8250af2
children
line source
1 #include "opengl.h"
2 #include "teapot.h"
3 #include "teapot_data.h"
5 static void draw_patch(struct vec3 *bez_cp, int *index, int flip, int useg, int vseg, float scale);
7 int patch_subdivision = 6;
9 static int dlist_sub[32];
11 void bezier_teapot(float scale)
12 {
13 int i;
15 glMatrixMode(GL_MODELVIEW);
17 if(!dlist_sub[patch_subdivision]) {
18 dlist_sub[patch_subdivision] = glGenLists(1);
19 glNewList(dlist_sub[patch_subdivision], GL_COMPILE);
21 for(i=0; i<NUM_TEAPOT_PATCHES; i++) {
22 float flip = teapot_part_flip[i];
23 float rot = teapot_part_rot[i];
25 glPushMatrix();
26 glTranslatef(0, -3.15 * 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, 1.0);
33 glPopMatrix();
34 }
36 glEndList();
37 }
39 scale /= 2.0;
40 glPushMatrix();
41 glScalef(scale, scale, scale);
42 glCallList(dlist_sub[patch_subdivision]);
43 glPopMatrix();
44 }
46 static void draw_patch(struct vec3 *bez_cp, int *index, int flip, int useg, int vseg, float scale)
47 {
48 static const float uoffs[2][4] = {{0, 0, 1, 1}, {1, 1, 0, 0}};
49 static const float voffs[4] = {0, 1, 1, 0};
51 int i, j, k;
52 struct vec3 cp[16];
53 struct vec3 pt, n;
54 float u, v;
55 float du = 1.0 / useg;
56 float dv = 1.0 / vseg;
58 /* collect control points */
59 for(i=0; i<16; i++) {
60 cp[i] = bez_cp[index[i]];
61 }
63 glBegin(GL_QUADS);
64 glColor3f(1, 1, 1);
66 u = 0;
67 for(i=0; i<useg; i++) {
68 v = 0;
69 for(j=0; j<vseg; j++) {
71 for(k=0; k<4; k++) {
72 pt = bezier_patch(cp, u + uoffs[flip][k] * du, v + voffs[k] * dv);
74 /* top/bottom normal hack */
75 if(pt.z > 3.14) {
76 n.x = n.y = 0.0f;
77 n.z = 1.0f;
78 } else if(pt.z < 0.00001) {
79 n.x = n.y = 0.0f;
80 n.z = -1.0f;
81 } else {
82 n = bezier_patch_norm(cp, u + uoffs[flip][k] * du, v + voffs[k] * dv);
83 }
85 glTexCoord2f(u, v);
86 glNormal3f(n.x, n.y, n.z);
87 glVertex3f(pt.x * scale, pt.y * scale, pt.z * scale);
88 }
90 v += dv;
91 }
92 u += du;
93 }
95 glEnd();
96 }