deepstone

view src/mglgen.c @ 21:00d84ab1ef26

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