deepstone
diff src/mglgen.c @ 0:f04884489bad
dos3d initial import
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 21 Nov 2011 06:14:01 +0200 |
parents | |
children | 0e781cc43178 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/mglgen.c Mon Nov 21 06:14:01 2011 +0200 1.3 @@ -0,0 +1,167 @@ 1.4 +/* 1.5 +256-color 3D graphics hack for real-mode DOS. 1.6 +Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU General Public License as published by 1.10 +the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 +#include <math.h> 1.22 +#include <assert.h> 1.23 +#include "mingl.h" 1.24 + 1.25 + 1.26 +void mgl_cube(float sz) 1.27 +{ 1.28 + float hsz = sz * 0.5; 1.29 + 1.30 + mgl_begin(MGL_QUADS); 1.31 + /* front */ 1.32 + mgl_normal(0, 0, 1); 1.33 + mgl_vertex3f(-hsz, hsz, hsz); 1.34 + mgl_vertex3f(-hsz, -hsz, hsz); 1.35 + mgl_vertex3f(hsz, -hsz, hsz); 1.36 + mgl_vertex3f(hsz, hsz, hsz); 1.37 + /* back */ 1.38 + mgl_normal(0, 0, -1); 1.39 + mgl_vertex3f(hsz, hsz, -hsz); 1.40 + mgl_vertex3f(hsz, -hsz, -hsz); 1.41 + mgl_vertex3f(-hsz, -hsz, -hsz); 1.42 + mgl_vertex3f(-hsz, hsz, -hsz); 1.43 + /* right */ 1.44 + mgl_normal(1, 0, 0); 1.45 + mgl_vertex3f(hsz, hsz, hsz); 1.46 + mgl_vertex3f(hsz, -hsz, hsz); 1.47 + mgl_vertex3f(hsz, -hsz, -hsz); 1.48 + mgl_vertex3f(hsz, hsz, -hsz); 1.49 + /* left */ 1.50 + mgl_normal(-1, 0, 0); 1.51 + mgl_vertex3f(-hsz, hsz, -hsz); 1.52 + mgl_vertex3f(-hsz, -hsz, -hsz); 1.53 + mgl_vertex3f(-hsz, -hsz, hsz); 1.54 + mgl_vertex3f(-hsz, hsz, hsz); 1.55 + /* top */ 1.56 + mgl_normal(0, 1, 0); 1.57 + mgl_vertex3f(-hsz, hsz, -hsz); 1.58 + mgl_vertex3f(-hsz, hsz, hsz); 1.59 + mgl_vertex3f(hsz, hsz, hsz); 1.60 + mgl_vertex3f(hsz, hsz, -hsz); 1.61 + /* bottom */ 1.62 + mgl_normal(0, -1, 0); 1.63 + mgl_vertex3f(hsz, -hsz, -hsz); 1.64 + mgl_vertex3f(hsz, -hsz, hsz); 1.65 + mgl_vertex3f(-hsz, -hsz, hsz); 1.66 + mgl_vertex3f(-hsz, -hsz, -hsz); 1.67 + mgl_end(); 1.68 +} 1.69 + 1.70 +void mgl_sphere(float rad, int usub, int vsub) 1.71 +{ 1.72 + mgl_sphere_part(rad, usub, vsub, 1.0, 1.0); 1.73 +} 1.74 + 1.75 +#define sphere_vertex(u, v) \ 1.76 + do { \ 1.77 + float x, y, z, theta, phi; \ 1.78 + float costheta, sinphi; \ 1.79 + theta = (u) * 2.0 * M_PI; \ 1.80 + phi = (v) * M_PI; \ 1.81 + costheta = cos(theta); \ 1.82 + sinphi = sin(phi); \ 1.83 + x = costheta * sinphi; \ 1.84 + y = cos(phi); \ 1.85 + z = sin(theta) * sinphi; \ 1.86 + mgl_normal(x, y, z); \ 1.87 + mgl_texcoord2f(u, v); \ 1.88 + mgl_vertex3f(rad * x, rad * y, rad * z); \ 1.89 + } while(0) 1.90 + 1.91 +void mgl_sphere_part(float rad, int usub, int vsub, float umax, float vmax) 1.92 +{ 1.93 + int i, j; 1.94 + float u, v, du, dv; 1.95 + 1.96 + assert(usub > 2); 1.97 + assert(vsub > 2); 1.98 + 1.99 + du = umax / (float)usub; 1.100 + dv = vmax / (float)vsub; 1.101 + 1.102 + mgl_begin(MGL_QUADS); 1.103 + 1.104 + u = 0.0; 1.105 + for(i=0; i<usub; i++) { 1.106 + v = 0.0; 1.107 + for(j=0; j<vsub; j++) { 1.108 + sphere_vertex(u, v); 1.109 + sphere_vertex(u + du, v); 1.110 + sphere_vertex(u + du, v + dv); 1.111 + sphere_vertex(u, v + dv); 1.112 + v += dv; 1.113 + } 1.114 + u += du; 1.115 + } 1.116 + mgl_end(); 1.117 +} 1.118 + 1.119 +void mgl_torus(float inner, float outer, int usub, int vsub) 1.120 +{ 1.121 + mgl_torus_part(inner, outer, usub, vsub, 1.0, 0.0, 1.0); 1.122 +} 1.123 + 1.124 +#define torus_vertex(u, v) \ 1.125 + do { \ 1.126 + float rx, ry, rz, cx, cy, cz, theta, phi; \ 1.127 + float costheta, sintheta, sinphi; \ 1.128 + theta = (u) * 2.0 * M_PI; \ 1.129 + phi = (v) * 2.0 * M_PI; \ 1.130 + costheta = cos(theta); \ 1.131 + sintheta = sin(theta); \ 1.132 + sinphi = sin(phi); \ 1.133 + cx = costheta * inner; \ 1.134 + cy = 0.0f; \ 1.135 + cz = sintheta * inner; \ 1.136 + rx = costheta * sinphi; \ 1.137 + ry = cos(phi); \ 1.138 + rz = sintheta * sinphi; \ 1.139 + mgl_normal(rx, ry, rz); \ 1.140 + mgl_texcoord2f(u, v); \ 1.141 + mgl_vertex3f(outer * rx + cx, outer * ry + cy, outer * rz + cz); \ 1.142 + } while(0) 1.143 + 1.144 +void mgl_torus_part(float inner, float outer, int usub, int vsub, float umax, float vmin, float vmax) 1.145 +{ 1.146 + int i, j; 1.147 + float u, v, du, dv; 1.148 + 1.149 + assert(usub > 2); 1.150 + assert(vsub > 2); 1.151 + 1.152 + du = umax / (float)usub; 1.153 + dv = (vmax - vmin) / (float)vsub; 1.154 + 1.155 + mgl_begin(MGL_QUADS); 1.156 + 1.157 + u = 0.0; 1.158 + for(i=0; i<usub; i++) { 1.159 + v = vmin; 1.160 + for(j=0; j<vsub; j++) { 1.161 + torus_vertex(u, v); 1.162 + torus_vertex(u + du, v); 1.163 + torus_vertex(u + du, v + dv); 1.164 + torus_vertex(u, v + dv); 1.165 + v += dv; 1.166 + } 1.167 + u += du; 1.168 + } 1.169 + mgl_end(); 1.170 +}