tesspot

annotate src/vmath.c @ 1:befe01bbd27f

tessellated the teapot
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Dec 2012 17:16:32 +0200
parents
children
rev   line source
nuclear@1 1 #include <math.h>
nuclear@1 2 #include "vmath.h"
nuclear@1 3
nuclear@1 4 static float bernstein(int i, float x);
nuclear@1 5
nuclear@1 6
nuclear@1 7 struct vec3 v3_add(struct vec3 a, struct vec3 b)
nuclear@1 8 {
nuclear@1 9 a.x += b.x;
nuclear@1 10 a.y += b.y;
nuclear@1 11 a.z += b.z;
nuclear@1 12 return a;
nuclear@1 13 }
nuclear@1 14
nuclear@1 15 struct vec3 v3_sub(struct vec3 a, struct vec3 b)
nuclear@1 16 {
nuclear@1 17 a.x -= b.x;
nuclear@1 18 a.y -= b.y;
nuclear@1 19 a.z -= b.z;
nuclear@1 20 return a;
nuclear@1 21 }
nuclear@1 22
nuclear@1 23 struct vec3 v3_cross(struct vec3 a, struct vec3 b)
nuclear@1 24 {
nuclear@1 25 struct vec3 res;
nuclear@1 26 res.x = a.y * b.z - a.z * b.y;
nuclear@1 27 res.y = a.z * b.x - a.x * b.z;
nuclear@1 28 res.z = a.x * b.y - a.y * b.x;
nuclear@1 29 return res;
nuclear@1 30 }
nuclear@1 31
nuclear@1 32 struct vec3 v3_normalize(struct vec3 v)
nuclear@1 33 {
nuclear@1 34 float len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
nuclear@1 35 v.x /= len;
nuclear@1 36 v.y /= len;
nuclear@1 37 v.z /= len;
nuclear@1 38 return v;
nuclear@1 39 }
nuclear@1 40
nuclear@1 41 struct vec3 bezier_patch(struct vec3 *cp, float u, float v)
nuclear@1 42 {
nuclear@1 43 int i, j;
nuclear@1 44 struct vec3 res = {0, 0, 0};
nuclear@1 45
nuclear@1 46 for(j=0; j<4; j++) {
nuclear@1 47 for(i=0; i<4; i++) {
nuclear@1 48 float bu = bernstein(i, u);
nuclear@1 49 float bv = bernstein(j, v);
nuclear@1 50
nuclear@1 51 res.x += cp->x * bu * bv;
nuclear@1 52 res.y += cp->y * bu * bv;
nuclear@1 53 res.z += cp->z * bu * bv;
nuclear@1 54
nuclear@1 55 cp++;
nuclear@1 56 }
nuclear@1 57 }
nuclear@1 58 return res;
nuclear@1 59 }
nuclear@1 60
nuclear@1 61 #define DT 0.001
nuclear@1 62
nuclear@1 63 struct vec3 bezier_patch_norm(struct vec3 *cp, float u, float v)
nuclear@1 64 {
nuclear@1 65 struct vec3 tang, bitan, norm;
nuclear@1 66
nuclear@1 67 tang = v3_sub(bezier_patch(cp, u + DT, v), bezier_patch(cp, u - DT, v));
nuclear@1 68 bitan = v3_sub(bezier_patch(cp, u, v + DT), bezier_patch(cp, u, v - DT));
nuclear@1 69 norm = v3_cross(tang, bitan);
nuclear@1 70
nuclear@1 71 return v3_normalize(norm);
nuclear@1 72 }
nuclear@1 73
nuclear@1 74
nuclear@1 75
nuclear@1 76 static float bernstein(int i, float x)
nuclear@1 77 {
nuclear@1 78 float invx = 1.0 - x;
nuclear@1 79
nuclear@1 80 switch(i) {
nuclear@1 81 case 0:
nuclear@1 82 return invx * invx * invx;
nuclear@1 83 case 1:
nuclear@1 84 return 3 * x * invx * invx;
nuclear@1 85 case 2:
nuclear@1 86 return 3 * x * x * invx;
nuclear@1 87 case 3:
nuclear@1 88 return x * x * x;
nuclear@1 89 default:
nuclear@1 90 break;
nuclear@1 91 }
nuclear@1 92 return 0;
nuclear@1 93 }