dos3d

view src/mglrast.c @ 17:1e9f0b3616fa

fixed the matrix multiplication order
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 30 Nov 2011 00:04:16 +0200
parents 0e781cc43178
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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <assert.h>
23 #include "mingl.h"
24 #include "mglimpl.h"
27 static struct vertex *vleft, *vright;
28 static struct framebuffer *fb;
29 static struct state *st;
32 #define SCAN_EDGE scan_edge_flat
33 #define SCAN_LINE scan_line_flat
34 #undef INTERP_DEPTH
35 #undef INTERP_ENERGY
36 #undef INTERP_TEX
37 #include "scantmpl.h"
38 #undef SCAN_EDGE
39 #undef SCAN_LINE
41 #define SCAN_EDGE scan_edge_z
42 #define SCAN_LINE scan_line_z
43 #define INTERP_DEPTH
44 #undef INTERP_ENERGY
45 #undef INTERP_TEX
46 #include "scantmpl.h"
47 #undef SCAN_EDGE
48 #undef SCAN_LINE
50 #define SCAN_EDGE scan_edge_e
51 #define SCAN_LINE scan_line_e
52 #undef INTERP_DEPTH
53 #define INTERP_ENERGY
54 #undef INTERP_TEX
55 #include "scantmpl.h"
56 #undef SCAN_EDGE
57 #undef SCAN_LINE
59 #define SCAN_EDGE scan_edge_ze
60 #define SCAN_LINE scan_line_ze
61 #define INTERP_DEPTH
62 #define INTERP_ENERGY
63 #undef INTERP_TEX
64 #include "scantmpl.h"
65 #undef SCAN_EDGE
66 #undef SCAN_LINE
68 #define SCAN_EDGE scan_edge_t
69 #define SCAN_LINE scan_line_t
70 #undef INTERP_DEPTH
71 #undef INTERP_ENERGY
72 #define INTERP_TEX
73 #include "scantmpl.h"
74 #undef SCAN_EDGE
75 #undef SCAN_LINE
77 #define SCAN_EDGE scan_edge_zt
78 #define SCAN_LINE scan_line_zt
79 #define INTERP_DEPTH
80 #undef INTERP_ENERGY
81 #define INTERP_TEX
82 #include "scantmpl.h"
83 #undef SCAN_EDGE
84 #undef SCAN_LINE
86 #define SCAN_EDGE scan_edge_et
87 #define SCAN_LINE scan_line_et
88 #undef INTERP_DEPTH
89 #define INTERP_ENERGY
90 #define INTERP_TEX
91 #include "scantmpl.h"
92 #undef SCAN_EDGE
93 #undef SCAN_LINE
95 #define SCAN_EDGE scan_edge_zet
96 #define SCAN_LINE scan_line_zet
97 #define INTERP_DEPTH
98 #define INTERP_ENERGY
99 #define INTERP_TEX
100 #include "scantmpl.h"
101 #undef SCAN_EDGE
102 #undef SCAN_LINE
105 static void (*scan_edge)(struct vertex*, struct vertex*);
106 static void (*scan_line)(int, unsigned char*);
108 int mgl_rast_init(struct state *state, struct framebuffer *fbuf)
109 {
110 fb = fbuf;
111 st = state;
113 if(!(vleft = malloc(fb->height * sizeof *vleft))) {
114 return -1;
115 }
116 if(!(vright = malloc(fb->height * sizeof *vright))) {
117 free(vleft);
118 return -1;
119 }
121 scan_edge = scan_edge_flat;
122 scan_line = scan_line_flat;
124 return 0;
125 }
127 void mgl_rast_cleanup(void)
128 {
129 free(vleft);
130 free(vright);
131 }
133 void mgl_rast_prepare(void)
134 {
135 static void (*sedge[])(struct vertex*, struct vertex*) = {
136 /* tez */
137 scan_edge_flat, /* 000 */
138 scan_edge_z, /* 001 */
139 scan_edge_e, /* 010 */
140 scan_edge_ze, /* 011 */
141 scan_edge_t, /* 100 */
142 scan_edge_zt, /* 101 */
143 scan_edge_et, /* 110 */
144 scan_edge_zet /* 111 */
145 };
146 static void (*sline[])(int, unsigned char*) = {
147 /* tez */
148 scan_line_flat, /* 000 */
149 scan_line_z, /* 001 */
150 scan_line_e, /* 010 */
151 scan_line_ze, /* 011 */
152 scan_line_t, /* 100 */
153 scan_line_zt, /* 101 */
154 scan_line_et, /* 110 */
155 scan_line_zet /* 111 */
156 };
157 int bits = 0;
159 if((st->flags & MGL_TEXTURE_2D) && st->tex.pixels) {
160 bits |= 4;
161 }
162 if(st->flags & MGL_SMOOTH) {
163 bits |= 2;
164 }
165 if((st->flags & MGL_DEPTH_TEST) && fb->zbuf) {
166 bits |= 1;
167 }
169 scan_edge = sedge[bits];
170 scan_line = sline[bits];
171 }
173 void mgl_draw_point(struct vertex *v)
174 {
175 int x = (int)ROUND(v->pos.x);
176 int y = (int)ROUND(v->pos.y);
178 if(x >= 0 && x < fb->width && y >= 0 && y < fb->height) {
179 int cidx = v->cidx + v->energy * st->col_range;
180 fb->pixels[y * fb->width + x] = cidx;
181 }
182 }
184 void mgl_draw_line(struct vertex *v0, struct vertex *v1)
185 {
186 /* TODO */
187 fprintf(stderr, "draw_line unimplemented\n");
188 abort();
189 }
191 void mgl_draw_poly(struct vertex *v, int numv)
192 {
193 int ybeg, yend, i;
194 unsigned char *sline;
196 ybeg = fb->height;
197 yend = 0;
199 for(i=0; i<numv; i++) {
200 struct vertex *v0 = v + i;
201 struct vertex *v1 = v + (i + 1) % numv;
202 int y = (int)ROUND(v0->pos.y);
204 scan_edge(v0, v1);
206 if(y > yend) yend = y;
207 if(y < ybeg) ybeg = y;
208 }
210 if(ybeg < 0) ybeg = 0;
211 if(yend >= fb->height) yend = fb->height - 1;
213 sline = fb->pixels + ybeg * fb->width;
214 for(i=ybeg; i<yend; i++) {
215 scan_line(i, sline);
216 sline += fb->width;
217 }
218 }