deepstone

annotate src/mglgen.c @ 32:7c8402e27b7c

renamed Makefile.dos to Makefile
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Sep 2013 18:37:46 +0300
parents bce78aaafc68
children
rev   line source
nuclear@0 1 #include <math.h>
nuclear@0 2 #include <assert.h>
nuclear@0 3 #include "mingl.h"
nuclear@21 4 #include "vmath.h"
nuclear@0 5
nuclear@0 6
nuclear@0 7 void mgl_cube(float sz)
nuclear@0 8 {
nuclear@0 9 float hsz = sz * 0.5;
nuclear@0 10
nuclear@0 11 mgl_begin(MGL_QUADS);
nuclear@0 12 /* front */
nuclear@0 13 mgl_normal(0, 0, 1);
nuclear@9 14 mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, hsz);
nuclear@9 15 mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, -hsz, hsz);
nuclear@9 16 mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, -hsz, hsz);
nuclear@9 17 mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, hsz);
nuclear@0 18 /* back */
nuclear@0 19 mgl_normal(0, 0, -1);
nuclear@9 20 mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, hsz, -hsz);
nuclear@9 21 mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, -hsz);
nuclear@9 22 mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, -hsz);
nuclear@9 23 mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, hsz, -hsz);
nuclear@0 24 /* right */
nuclear@0 25 mgl_normal(1, 0, 0);
nuclear@9 26 mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, hsz, hsz);
nuclear@9 27 mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, hsz);
nuclear@9 28 mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, -hsz, -hsz);
nuclear@9 29 mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, -hsz);
nuclear@0 30 /* left */
nuclear@0 31 mgl_normal(-1, 0, 0);
nuclear@9 32 mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, -hsz);
nuclear@9 33 mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, -hsz, -hsz);
nuclear@9 34 mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, hsz);
nuclear@9 35 mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, hsz, hsz);
nuclear@0 36 /* top */
nuclear@0 37 mgl_normal(0, 1, 0);
nuclear@9 38 mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, -hsz);
nuclear@9 39 mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, hsz, hsz);
nuclear@9 40 mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, hsz, hsz);
nuclear@9 41 mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, -hsz);
nuclear@0 42 /* bottom */
nuclear@0 43 mgl_normal(0, -1, 0);
nuclear@9 44 mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, -hsz, -hsz);
nuclear@9 45 mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, hsz);
nuclear@9 46 mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, hsz);
nuclear@9 47 mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, -hsz, -hsz);
nuclear@0 48 mgl_end();
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 void mgl_sphere(float rad, int usub, int vsub)
nuclear@0 52 {
nuclear@0 53 mgl_sphere_part(rad, usub, vsub, 1.0, 1.0);
nuclear@0 54 }
nuclear@0 55
nuclear@0 56 #define sphere_vertex(u, v) \
nuclear@0 57 do { \
nuclear@0 58 float x, y, z, theta, phi; \
nuclear@0 59 float costheta, sinphi; \
nuclear@0 60 theta = (u) * 2.0 * M_PI; \
nuclear@0 61 phi = (v) * M_PI; \
nuclear@0 62 costheta = cos(theta); \
nuclear@0 63 sinphi = sin(phi); \
nuclear@0 64 x = costheta * sinphi; \
nuclear@0 65 y = cos(phi); \
nuclear@0 66 z = sin(theta) * sinphi; \
nuclear@0 67 mgl_normal(x, y, z); \
nuclear@0 68 mgl_texcoord2f(u, v); \
nuclear@0 69 mgl_vertex3f(rad * x, rad * y, rad * z); \
nuclear@0 70 } while(0)
nuclear@0 71
nuclear@0 72 void mgl_sphere_part(float rad, int usub, int vsub, float umax, float vmax)
nuclear@0 73 {
nuclear@0 74 int i, j;
nuclear@0 75 float u, v, du, dv;
nuclear@0 76
nuclear@0 77 assert(usub > 2);
nuclear@0 78 assert(vsub > 2);
nuclear@0 79
nuclear@0 80 du = umax / (float)usub;
nuclear@0 81 dv = vmax / (float)vsub;
nuclear@0 82
nuclear@0 83 mgl_begin(MGL_QUADS);
nuclear@0 84
nuclear@0 85 u = 0.0;
nuclear@0 86 for(i=0; i<usub; i++) {
nuclear@0 87 v = 0.0;
nuclear@0 88 for(j=0; j<vsub; j++) {
nuclear@0 89 sphere_vertex(u, v);
nuclear@0 90 sphere_vertex(u + du, v);
nuclear@0 91 sphere_vertex(u + du, v + dv);
nuclear@0 92 sphere_vertex(u, v + dv);
nuclear@0 93 v += dv;
nuclear@0 94 }
nuclear@0 95 u += du;
nuclear@0 96 }
nuclear@0 97 mgl_end();
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 void mgl_torus(float inner, float outer, int usub, int vsub)
nuclear@0 101 {
nuclear@0 102 mgl_torus_part(inner, outer, usub, vsub, 1.0, 0.0, 1.0);
nuclear@0 103 }
nuclear@0 104
nuclear@0 105 #define torus_vertex(u, v) \
nuclear@0 106 do { \
nuclear@0 107 float rx, ry, rz, cx, cy, cz, theta, phi; \
nuclear@0 108 float costheta, sintheta, sinphi; \
nuclear@0 109 theta = (u) * 2.0 * M_PI; \
nuclear@0 110 phi = (v) * 2.0 * M_PI; \
nuclear@0 111 costheta = cos(theta); \
nuclear@0 112 sintheta = sin(theta); \
nuclear@0 113 sinphi = sin(phi); \
nuclear@0 114 cx = costheta * inner; \
nuclear@0 115 cy = 0.0f; \
nuclear@0 116 cz = sintheta * inner; \
nuclear@0 117 rx = costheta * sinphi; \
nuclear@0 118 ry = cos(phi); \
nuclear@0 119 rz = sintheta * sinphi; \
nuclear@0 120 mgl_normal(rx, ry, rz); \
nuclear@0 121 mgl_texcoord2f(u, v); \
nuclear@0 122 mgl_vertex3f(outer * rx + cx, outer * ry + cy, outer * rz + cz); \
nuclear@0 123 } while(0)
nuclear@0 124
nuclear@0 125 void mgl_torus_part(float inner, float outer, int usub, int vsub, float umax, float vmin, float vmax)
nuclear@0 126 {
nuclear@0 127 int i, j;
nuclear@0 128 float u, v, du, dv;
nuclear@0 129
nuclear@0 130 assert(usub > 2);
nuclear@0 131 assert(vsub > 2);
nuclear@0 132
nuclear@0 133 du = umax / (float)usub;
nuclear@0 134 dv = (vmax - vmin) / (float)vsub;
nuclear@0 135
nuclear@0 136 mgl_begin(MGL_QUADS);
nuclear@0 137
nuclear@0 138 u = 0.0;
nuclear@0 139 for(i=0; i<usub; i++) {
nuclear@0 140 v = vmin;
nuclear@0 141 for(j=0; j<vsub; j++) {
nuclear@0 142 torus_vertex(u, v);
nuclear@0 143 torus_vertex(u + du, v);
nuclear@0 144 torus_vertex(u + du, v + dv);
nuclear@0 145 torus_vertex(u, v + dv);
nuclear@0 146 v += dv;
nuclear@0 147 }
nuclear@0 148 u += du;
nuclear@0 149 }
nuclear@0 150 mgl_end();
nuclear@0 151 }