# HG changeset patch # User John Tsiombikas # Date 1382848278 -7200 # Node ID 9a973ef0e2a3e2cd7af332b643b0f49e123b863b # Parent cedf581048c7c0548c131d2e9293013ac1b0a3bd fixed the performance issue under MacOSX by replacing glutSolidTeapot (which uses glEvalMesh) with my own teapot generator. diff -r cedf581048c7 -r 9a973ef0e2a3 src/bezmath.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bezmath.c Sun Oct 27 06:31:18 2013 +0200 @@ -0,0 +1,93 @@ +#include +#include "bezmath.h" + +static float bernstein(int i, float x); + + +struct vec3 v3_add(struct vec3 a, struct vec3 b) +{ + a.x += b.x; + a.y += b.y; + a.z += b.z; + return a; +} + +struct vec3 v3_sub(struct vec3 a, struct vec3 b) +{ + a.x -= b.x; + a.y -= b.y; + a.z -= b.z; + return a; +} + +struct vec3 v3_cross(struct vec3 a, struct vec3 b) +{ + struct vec3 res; + res.x = a.y * b.z - a.z * b.y; + res.y = a.z * b.x - a.x * b.z; + res.z = a.x * b.y - a.y * b.x; + return res; +} + +struct vec3 v3_normalize(struct vec3 v) +{ + float len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z); + v.x /= len; + v.y /= len; + v.z /= len; + return v; +} + +struct vec3 bezier_patch(struct vec3 *cp, float u, float v) +{ + int i, j; + struct vec3 res = {0, 0, 0}; + + for(j=0; j<4; j++) { + for(i=0; i<4; i++) { + float bu = bernstein(i, u); + float bv = bernstein(j, v); + + res.x += cp->x * bu * bv; + res.y += cp->y * bu * bv; + res.z += cp->z * bu * bv; + + cp++; + } + } + return res; +} + +#define DT 0.001 + +struct vec3 bezier_patch_norm(struct vec3 *cp, float u, float v) +{ + struct vec3 tang, bitan, norm; + + tang = v3_sub(bezier_patch(cp, u + DT, v), bezier_patch(cp, u - DT, v)); + bitan = v3_sub(bezier_patch(cp, u, v + DT), bezier_patch(cp, u, v - DT)); + norm = v3_cross(tang, bitan); + + return v3_normalize(norm); +} + + + +static float bernstein(int i, float x) +{ + float invx = 1.0 - x; + + switch(i) { + case 0: + return invx * invx * invx; + case 1: + return 3 * x * invx * invx; + case 2: + return 3 * x * x * invx; + case 3: + return x * x * x; + default: + break; + } + return 0; +} diff -r cedf581048c7 -r 9a973ef0e2a3 src/bezmath.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bezmath.h Sun Oct 27 06:31:18 2013 +0200 @@ -0,0 +1,24 @@ +#ifndef BEZMATH_H_ +#define BEZMATH_H_ + +struct vec3 { + float x, y, z; +}; + +#ifdef __cplusplus +extern "C" +#endif + +struct vec3 v3_add(struct vec3 a, struct vec3 b); +struct vec3 v3_sub(struct vec3 a, struct vec3 b); +struct vec3 v3_cross(struct vec3 a, struct vec3 b); +struct vec3 v3_normalize(struct vec3 v); + +struct vec3 bezier_patch(struct vec3 *cp, float u, float v); +struct vec3 bezier_patch_norm(struct vec3 *cp, float u, float v); + +#ifdef __cplusplus +} +#endif + +#endif /* BEZMATH_H_ */ diff -r cedf581048c7 -r 9a973ef0e2a3 src/main.cc --- a/src/main.cc Sat Oct 26 04:24:16 2013 +0300 +++ b/src/main.cc Sun Oct 27 06:31:18 2013 +0200 @@ -6,6 +6,7 @@ #include "vr.h" #include "camera.h" #include "sdr.h" +#include "teapot.h" #ifdef __APPLE__ #include @@ -330,7 +331,8 @@ if(!tealist) { tealist = glGenLists(1); glNewList(tealist, GL_COMPILE); - glutSolidTeapot(1.0); + //glutSolidTeapot(1.0); + bezier_teapot(1.0); glEndList(); } @@ -341,6 +343,7 @@ glFrontFace(GL_CW); glCallList(tealist); + //bezier_teapot(1.0); glFrontFace(GL_CCW); glPopMatrix(); diff -r cedf581048c7 -r 9a973ef0e2a3 src/teapot.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/teapot.c Sun Oct 27 06:31:18 2013 +0200 @@ -0,0 +1,87 @@ +#ifndef __APPLE__ +#include +#else +#include +#endif + +#include "teapot.h" +#include "teapot_data.h" + +static void draw_patch(struct vec3 *bez_cp, int *index, int flip, int useg, int vseg, float scale); + +int patch_subdivision = 6; + +void bezier_teapot(float scale) +{ + int i; + + scale /= 2.0; + + for(i=0; i 3.14) { + n.x = n.y = 0.0f; + n.z = 1.0f; + } else if(pt.z < 0.00001) { + n.x = n.y = 0.0f; + n.z = -1.0f; + } else { + n = bezier_patch_norm(cp, u + uoffs[flip][k] * du, v + voffs[k] * dv); + } + + glTexCoord2f(u, v); + glNormal3f(n.x, n.y, n.z); + glVertex3f(pt.x * scale, pt.y * scale, pt.z * scale); + } + + v += dv; + } + u += du; + } + + glEnd(); +} diff -r cedf581048c7 -r 9a973ef0e2a3 src/teapot.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/teapot.h Sun Oct 27 06:31:18 2013 +0200 @@ -0,0 +1,16 @@ +#ifndef TEAPOT_H_ +#define TEAPOT_H_ + +extern int patch_subdivision; + +#ifdef __cplusplus +extern "C" { +#endif + +void bezier_teapot(float scale); + +#ifdef __cplusplus +} +#endif + +#endif /* TEAPOT_H_ */ diff -r cedf581048c7 -r 9a973ef0e2a3 src/teapot_data.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/teapot_data.h Sun Oct 27 06:31:18 2013 +0200 @@ -0,0 +1,214 @@ +#ifndef TEAPOT_DATA_H_ +#define TEAPOT_DATA_H_ + +#include "bezmath.h" + +#define NUM_TEAPOT_INDICES (sizeof teapot_index / sizeof *teapot_index) +#define NUM_TEAPOT_VERTS (sizeof teapot_verts / sizeof *teapot_verts) + +#define NUM_TEAPOT_PATCHES (NUM_TEAPOT_INDICES / 16) + +static float teapot_part_flip[] = { + 1, 1, 1, 1, /* rim flip */ + 1, 1, 1, 1, /* body1 flip */ + 1, 1, 1, 1, /* body2 flip */ + 1, 1, 1, 1, /* lid patch 1 flip */ + 1, 1, 1, 1, /* lid patch 2 flip */ + 1, -1, /* handle 1 flip */ + 1, -1, /* handle 2 flip */ + 1, -1, /* spout 1 flip */ + 1, -1, /* spout 2 flip */ + 1, 1, 1, 1 /* bottom flip */ +}; + +static float teapot_part_rot[] = { + 0, 90, 180, 270, /* rim rotations */ + 0, 90, 180, 270, /* body patch 1 rotations */ + 0, 90, 180, 270, /* body patch 2 rotations */ + 0, 90, 180, 270, /* lid patch 1 rotations */ + 0, 90, 180, 270, /* lid patch 2 rotations */ + 0, 0, /* handle 1 rotations */ + 0, 0, /* handle 2 rotations */ + 0, 0, /* spout 1 rotations */ + 0, 0, /* spout 2 rotations */ + 0, 90, 180, 270 /* bottom rotations */ +}; + + +static int teapot_index[] = { + /* rim */ + 102, 103, 104, 105, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, + + 102, 103, 104, 105, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, + + 102, 103, 104, 105, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, + + 102, 103, 104, 105, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, + + /* body1 */ + 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, + + 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, + + 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, + + 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, + + /* body 2 */ + 24, 25, 26, 27, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + + 24, 25, 26, 27, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + + 24, 25, 26, 27, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + + 24, 25, 26, 27, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + + /* lid 1 */ + 96, 96, 96, 96, 97, 98, 99, 100, + 101, 101, 101, 101, 0, 1, 2, 3, + + 96, 96, 96, 96, 97, 98, 99, 100, + 101, 101, 101, 101, 0, 1, 2, 3, + + 96, 96, 96, 96, 97, 98, 99, 100, + 101, 101, 101, 101, 0, 1, 2, 3, + + 96, 96, 96, 96, 97, 98, 99, 100, + 101, 101, 101, 101, 0, 1, 2, 3, + + /* lid 2 */ + 0, 1, 2, 3, 106, 107, 108, 109, + 110, 111, 112, 113, 114, 115, 116, 117, + + 0, 1, 2, 3, 106, 107, 108, 109, + 110, 111, 112, 113, 114, 115, 116, 117, + + 0, 1, 2, 3, 106, 107, 108, 109, + 110, 111, 112, 113, 114, 115, 116, 117, + + 0, 1, 2, 3, 106, 107, 108, 109, + 110, 111, 112, 113, 114, 115, 116, 117, + + /* handle 1 */ + 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 52, 53, 54, 55, 56, + + 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 52, 53, 54, 55, 56, + + /* handle 2 */ + 53, 54, 55, 56, 57, 58, 59, 60, + 61, 62, 63, 64, 28, 65, 66, 67, + + 53, 54, 55, 56, 57, 58, 59, 60, + 61, 62, 63, 64, 28, 65, 66, 67, + + /* spout 1 */ + 68, 69, 70, 71, 72, 73, 74, 75, + 76, 77, 78, 79, 80, 81, 82, 83, + + 68, 69, 70, 71, 72, 73, 74, 75, + 76, 77, 78, 79, 80, 81, 82, 83, + + /* spout 2 */ + 80, 81, 82, 83, 84, 85, 86, 87, + 88, 89, 90, 91, 92, 93, 94, 95, + + 80, 81, 82, 83, 84, 85, 86, 87, + 88, 89, 90, 91, 92, 93, 94, 95, + + /* bottom */ + 118, 118, 118, 118, 124, 122, 119, 121, + 123, 126, 125, 120, 40, 39, 38, 37, + + 118, 118, 118, 118, 124, 122, 119, 121, + 123, 126, 125, 120, 40, 39, 38, 37, + + 118, 118, 118, 118, 124, 122, 119, 121, + 123, 126, 125, 120, 40, 39, 38, 37, + + 118, 118, 118, 118, 124, 122, 119, 121, + 123, 126, 125, 120, 40, 39, 38, 37 +}; + + +static struct vec3 teapot_verts[] = { + { 0.2000, 0.0000, 2.70000 }, { 0.2000, -0.1120, 2.70000 }, + { 0.1120, -0.2000, 2.70000 }, { 0.0000, -0.2000, 2.70000 }, + { 1.3375, 0.0000, 2.53125 }, { 1.3375, -0.7490, 2.53125 }, + { 0.7490, -1.3375, 2.53125 }, { 0.0000, -1.3375, 2.53125 }, + { 1.4375, 0.0000, 2.53125 }, { 1.4375, -0.8050, 2.53125 }, + { 0.8050, -1.4375, 2.53125 }, { 0.0000, -1.4375, 2.53125 }, + { 1.5000, 0.0000, 2.40000 }, { 1.5000, -0.8400, 2.40000 }, + { 0.8400, -1.5000, 2.40000 }, { 0.0000, -1.5000, 2.40000 }, + { 1.7500, 0.0000, 1.87500 }, { 1.7500, -0.9800, 1.87500 }, + { 0.9800, -1.7500, 1.87500 }, { 0.0000, -1.7500, 1.87500 }, + { 2.0000, 0.0000, 1.35000 }, { 2.0000, -1.1200, 1.35000 }, + { 1.1200, -2.0000, 1.35000 }, { 0.0000, -2.0000, 1.35000 }, + { 2.0000, 0.0000, 0.90000 }, { 2.0000, -1.1200, 0.90000 }, + { 1.1200, -2.0000, 0.90000 }, { 0.0000, -2.0000, 0.90000 }, + { -2.0000, 0.0000, 0.90000 }, { 2.0000, 0.0000, 0.45000 }, + { 2.0000, -1.1200, 0.45000 }, { 1.1200, -2.0000, 0.45000 }, + { 0.0000, -2.0000, 0.45000 }, { 1.5000, 0.0000, 0.22500 }, + { 1.5000, -0.8400, 0.22500 }, { 0.8400, -1.5000, 0.22500 }, + { 0.0000, -1.5000, 0.22500 }, { 1.5000, 0.0000, 0.15000 }, + { 1.5000, -0.8400, 0.15000 }, { 0.8400, -1.5000, 0.15000 }, + { 0.0000, -1.5000, 0.15000 }, { -1.6000, 0.0000, 2.02500 }, + { -1.6000, -0.3000, 2.02500 }, { -1.5000, -0.3000, 2.25000 }, + { -1.5000, 0.0000, 2.25000 }, { -2.3000, 0.0000, 2.02500 }, + { -2.3000, -0.3000, 2.02500 }, { -2.5000, -0.3000, 2.25000 }, + { -2.5000, 0.0000, 2.25000 }, { -2.7000, 0.0000, 2.02500 }, + { -2.7000, -0.3000, 2.02500 }, { -3.0000, -0.3000, 2.25000 }, + { -3.0000, 0.0000, 2.25000 }, { -2.7000, 0.0000, 1.80000 }, + { -2.7000, -0.3000, 1.80000 }, { -3.0000, -0.3000, 1.80000 }, + { -3.0000, 0.0000, 1.80000 }, { -2.7000, 0.0000, 1.57500 }, + { -2.7000, -0.3000, 1.57500 }, { -3.0000, -0.3000, 1.35000 }, + { -3.0000, 0.0000, 1.35000 }, { -2.5000, 0.0000, 1.12500 }, + { -2.5000, -0.3000, 1.12500 }, { -2.6500, -0.3000, 0.93750 }, + { -2.6500, 0.0000, 0.93750 }, { -2.0000, -0.3000, 0.90000 }, + { -1.9000, -0.3000, 0.60000 }, { -1.9000, 0.0000, 0.60000 }, + { 1.7000, 0.0000, 1.42500 }, { 1.7000, -0.6600, 1.42500 }, + { 1.7000, -0.6600, 0.60000 }, { 1.7000, 0.0000, 0.60000 }, + { 2.6000, 0.0000, 1.42500 }, { 2.6000, -0.6600, 1.42500 }, + { 3.1000, -0.6600, 0.82500 }, { 3.1000, 0.0000, 0.82500 }, + { 2.3000, 0.0000, 2.10000 }, { 2.3000, -0.2500, 2.10000 }, + { 2.4000, -0.2500, 2.02500 }, { 2.4000, 0.0000, 2.02500 }, + { 2.7000, 0.0000, 2.40000 }, { 2.7000, -0.2500, 2.40000 }, + { 3.3000, -0.2500, 2.40000 }, { 3.3000, 0.0000, 2.40000 }, + { 2.8000, 0.0000, 2.47500 }, { 2.8000, -0.2500, 2.47500 }, + { 3.5250, -0.2500, 2.49375 }, { 3.5250, 0.0000, 2.49375 }, + { 2.9000, 0.0000, 2.47500 }, { 2.9000, -0.1500, 2.47500 }, + { 3.4500, -0.1500, 2.51250 }, { 3.4500, 0.0000, 2.51250 }, + { 2.8000, 0.0000, 2.40000 }, { 2.8000, -0.1500, 2.40000 }, + { 3.2000, -0.1500, 2.40000 }, { 3.2000, 0.0000, 2.40000 }, + { 0.0000, 0.0000, 3.15000 }, { 0.8000, 0.0000, 3.15000 }, + { 0.8000, -0.4500, 3.15000 }, { 0.4500, -0.8000, 3.15000 }, + { 0.0000, -0.8000, 3.15000 }, { 0.0000, 0.0000, 2.85000 }, + { 1.4000, 0.0000, 2.40000 }, { 1.4000, -0.7840, 2.40000 }, + { 0.7840, -1.4000, 2.40000 }, { 0.0000, -1.4000, 2.40000 }, + { 0.4000, 0.0000, 2.55000 }, { 0.4000, -0.2240, 2.55000 }, + { 0.2240, -0.4000, 2.55000 }, { 0.0000, -0.4000, 2.55000 }, + { 1.3000, 0.0000, 2.55000 }, { 1.3000, -0.7280, 2.55000 }, + { 0.7280, -1.3000, 2.55000 }, { 0.0000, -1.3000, 2.55000 }, + { 1.3000, 0.0000, 2.40000 }, { 1.3000, -0.7280, 2.40000 }, + { 0.7280, -1.3000, 2.40000 }, { 0.0000, -1.3000, 2.40000 }, + { 0.0000, 0.0000, 0.00000 }, { 1.4250, -0.7980, 0.00000 }, + { 1.5000, 0.0000, 0.07500 }, { 1.4250, 0.0000, 0.00000 }, + { 0.7980, -1.4250, 0.00000 }, { 0.0000, -1.5000, 0.07500 }, + { 0.0000, -1.4250, 0.00000 }, { 1.5000, -0.8400, 0.07500 }, + { 0.8400, -1.5000, 0.07500 } +}; + +#endif /* TEAPOT_DATA_H_ */