dos3d
view src/mglgen.c @ 24:698c4d07702d
fixed the linux makefile
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 20 Jun 2017 21:55:49 +0300 |
parents | 00d84ab1ef26 |
children |
line source
1 /*
2 256-color 3D graphics hack for real-mode DOS.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <math.h>
19 #include <assert.h>
20 #include "mingl.h"
23 void mgl_cube(float sz)
24 {
25 float hsz = sz * 0.5;
27 mgl_begin(MGL_QUADS);
28 /* front */
29 mgl_normal(0, 0, 1);
30 mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, hsz);
31 mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, -hsz, hsz);
32 mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, -hsz, hsz);
33 mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, hsz);
34 /* back */
35 mgl_normal(0, 0, -1);
36 mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, hsz, -hsz);
37 mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, -hsz);
38 mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, -hsz);
39 mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, hsz, -hsz);
40 /* right */
41 mgl_normal(1, 0, 0);
42 mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, hsz, hsz);
43 mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, hsz);
44 mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, -hsz, -hsz);
45 mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, -hsz);
46 /* left */
47 mgl_normal(-1, 0, 0);
48 mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, -hsz);
49 mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, -hsz, -hsz);
50 mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, hsz);
51 mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, hsz, hsz);
52 /* top */
53 mgl_normal(0, 1, 0);
54 mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, -hsz);
55 mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, hsz, hsz);
56 mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, hsz, hsz);
57 mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, -hsz);
58 /* bottom */
59 mgl_normal(0, -1, 0);
60 mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, -hsz, -hsz);
61 mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, hsz);
62 mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, hsz);
63 mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, -hsz, -hsz);
64 mgl_end();
65 }
67 void mgl_sphere(float rad, int usub, int vsub)
68 {
69 mgl_sphere_part(rad, usub, vsub, 1.0, 1.0);
70 }
72 #define sphere_vertex(u, v) \
73 do { \
74 float x, y, z, theta, phi; \
75 float costheta, sinphi; \
76 theta = (u) * 2.0 * M_PI; \
77 phi = (v) * M_PI; \
78 costheta = cos(theta); \
79 sinphi = sin(phi); \
80 x = costheta * sinphi; \
81 y = cos(phi); \
82 z = sin(theta) * sinphi; \
83 mgl_normal(x, y, z); \
84 mgl_texcoord2f(u, v); \
85 mgl_vertex3f(rad * x, rad * y, rad * z); \
86 } while(0)
88 void mgl_sphere_part(float rad, int usub, int vsub, float umax, float vmax)
89 {
90 int i, j;
91 float u, v, du, dv;
93 assert(usub > 2);
94 assert(vsub > 2);
96 du = umax / (float)usub;
97 dv = vmax / (float)vsub;
99 mgl_begin(MGL_QUADS);
101 u = 0.0;
102 for(i=0; i<usub; i++) {
103 v = 0.0;
104 for(j=0; j<vsub; j++) {
105 sphere_vertex(u, v);
106 sphere_vertex(u + du, v);
107 sphere_vertex(u + du, v + dv);
108 sphere_vertex(u, v + dv);
109 v += dv;
110 }
111 u += du;
112 }
113 mgl_end();
114 }
116 void mgl_torus(float inner, float outer, int usub, int vsub)
117 {
118 mgl_torus_part(inner, outer, usub, vsub, 1.0, 0.0, 1.0);
119 }
121 #define torus_vertex(u, v) \
122 do { \
123 float rx, ry, rz, cx, cy, cz, theta, phi; \
124 float costheta, sintheta, sinphi; \
125 theta = (u) * 2.0 * M_PI; \
126 phi = (v) * 2.0 * M_PI; \
127 costheta = cos(theta); \
128 sintheta = sin(theta); \
129 sinphi = sin(phi); \
130 cx = costheta * inner; \
131 cy = 0.0f; \
132 cz = sintheta * inner; \
133 rx = costheta * sinphi; \
134 ry = cos(phi); \
135 rz = sintheta * sinphi; \
136 mgl_normal(rx, ry, rz); \
137 mgl_texcoord2f(u, v); \
138 mgl_vertex3f(outer * rx + cx, outer * ry + cy, outer * rz + cz); \
139 } while(0)
141 void mgl_torus_part(float inner, float outer, int usub, int vsub, float umax, float vmin, float vmax)
142 {
143 int i, j;
144 float u, v, du, dv;
146 assert(usub > 2);
147 assert(vsub > 2);
149 du = umax / (float)usub;
150 dv = (vmax - vmin) / (float)vsub;
152 mgl_begin(MGL_QUADS);
154 u = 0.0;
155 for(i=0; i<usub; i++) {
156 v = vmin;
157 for(j=0; j<vsub; j++) {
158 torus_vertex(u, v);
159 torus_vertex(u + du, v);
160 torus_vertex(u + du, v + dv);
161 torus_vertex(u, v + dv);
162 v += dv;
163 }
164 u += du;
165 }
166 mgl_end();
167 }