oculus1
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/teapot.c Sun Oct 27 06:31:18 2013 +0200 1.3 @@ -0,0 +1,87 @@ 1.4 +#ifndef __APPLE__ 1.5 +#include <GL/gl.h> 1.6 +#else 1.7 +#include <OpenGL/gl.h> 1.8 +#endif 1.9 + 1.10 +#include "teapot.h" 1.11 +#include "teapot_data.h" 1.12 + 1.13 +static void draw_patch(struct vec3 *bez_cp, int *index, int flip, int useg, int vseg, float scale); 1.14 + 1.15 +int patch_subdivision = 6; 1.16 + 1.17 +void bezier_teapot(float scale) 1.18 +{ 1.19 + int i; 1.20 + 1.21 + scale /= 2.0; 1.22 + 1.23 + for(i=0; i<NUM_TEAPOT_PATCHES; i++) { 1.24 + float flip = teapot_part_flip[i]; 1.25 + float rot = teapot_part_rot[i]; 1.26 + 1.27 + glMatrixMode(GL_MODELVIEW); 1.28 + glPushMatrix(); 1.29 + glTranslatef(0, -3.15 * scale * 0.5, 0); 1.30 + glRotatef(rot, 0, 1, 0); 1.31 + glScalef(1, 1, flip); 1.32 + glRotatef(-90, 1, 0, 0); 1.33 + 1.34 + draw_patch(teapot_verts, teapot_index + i * 16, flip < 0.0 ? 1 : 0, patch_subdivision, patch_subdivision, scale); 1.35 + 1.36 + glPopMatrix(); 1.37 + } 1.38 +} 1.39 + 1.40 +static void draw_patch(struct vec3 *bez_cp, int *index, int flip, int useg, int vseg, float scale) 1.41 +{ 1.42 + static const float uoffs[2][4] = {{0, 0, 1, 1}, {1, 1, 0, 0}}; 1.43 + static const float voffs[4] = {0, 1, 1, 0}; 1.44 + 1.45 + int i, j, k; 1.46 + struct vec3 cp[16]; 1.47 + struct vec3 pt, n; 1.48 + float u, v; 1.49 + float du = 1.0 / useg; 1.50 + float dv = 1.0 / vseg; 1.51 + 1.52 + /* collect control points */ 1.53 + for(i=0; i<16; i++) { 1.54 + cp[i] = bez_cp[index[i]]; 1.55 + } 1.56 + 1.57 + glBegin(GL_QUADS); 1.58 + glColor3f(1, 1, 1); 1.59 + 1.60 + u = 0; 1.61 + for(i=0; i<useg; i++) { 1.62 + v = 0; 1.63 + for(j=0; j<vseg; j++) { 1.64 + 1.65 + for(k=0; k<4; k++) { 1.66 + pt = bezier_patch(cp, u + uoffs[flip][k] * du, v + voffs[k] * dv); 1.67 + 1.68 + /* top/bottom normal hack */ 1.69 + if(pt.z > 3.14) { 1.70 + n.x = n.y = 0.0f; 1.71 + n.z = 1.0f; 1.72 + } else if(pt.z < 0.00001) { 1.73 + n.x = n.y = 0.0f; 1.74 + n.z = -1.0f; 1.75 + } else { 1.76 + n = bezier_patch_norm(cp, u + uoffs[flip][k] * du, v + voffs[k] * dv); 1.77 + } 1.78 + 1.79 + glTexCoord2f(u, v); 1.80 + glNormal3f(n.x, n.y, n.z); 1.81 + glVertex3f(pt.x * scale, pt.y * scale, pt.z * scale); 1.82 + } 1.83 + 1.84 + v += dv; 1.85 + } 1.86 + u += du; 1.87 + } 1.88 + 1.89 + glEnd(); 1.90 +}