deepstone

view src/mglrast.c @ 3:0e781cc43178

adding textures
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 10:16:09 +0200
parents f04884489bad
children bce78aaafc68
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 <assert.h>
22 #include "mingl.h"
23 #include "mglimpl.h"
26 static struct vertex *vleft, *vright;
27 static struct framebuffer *fb;
28 static struct state *st;
31 #define SCAN_EDGE scan_edge_flat
32 #define SCAN_LINE scan_line_flat
33 #undef INTERP_DEPTH
34 #undef INTERP_ENERGY
35 #undef INTERP_TEX
36 #include "scantmpl.h"
37 #undef SCAN_EDGE
38 #undef SCAN_LINE
40 #define SCAN_EDGE scan_edge_z
41 #define SCAN_LINE scan_line_z
42 #define INTERP_DEPTH
43 #undef INTERP_ENERGY
44 #undef INTERP_TEX
45 #include "scantmpl.h"
46 #undef SCAN_EDGE
47 #undef SCAN_LINE
49 #define SCAN_EDGE scan_edge_e
50 #define SCAN_LINE scan_line_e
51 #undef INTERP_DEPTH
52 #define INTERP_ENERGY
53 #undef INTERP_TEX
54 #include "scantmpl.h"
55 #undef SCAN_EDGE
56 #undef SCAN_LINE
58 #define SCAN_EDGE scan_edge_ze
59 #define SCAN_LINE scan_line_ze
60 #define INTERP_DEPTH
61 #define INTERP_ENERGY
62 #undef INTERP_TEX
63 #include "scantmpl.h"
64 #undef SCAN_EDGE
65 #undef SCAN_LINE
67 #define SCAN_EDGE scan_edge_t
68 #define SCAN_LINE scan_line_t
69 #undef INTERP_DEPTH
70 #undef INTERP_ENERGY
71 #define INTERP_TEX
72 #include "scantmpl.h"
73 #undef SCAN_EDGE
74 #undef SCAN_LINE
76 #define SCAN_EDGE scan_edge_zt
77 #define SCAN_LINE scan_line_zt
78 #define INTERP_DEPTH
79 #undef INTERP_ENERGY
80 #define INTERP_TEX
81 #include "scantmpl.h"
82 #undef SCAN_EDGE
83 #undef SCAN_LINE
85 #define SCAN_EDGE scan_edge_et
86 #define SCAN_LINE scan_line_et
87 #undef INTERP_DEPTH
88 #define INTERP_ENERGY
89 #define INTERP_TEX
90 #include "scantmpl.h"
91 #undef SCAN_EDGE
92 #undef SCAN_LINE
94 #define SCAN_EDGE scan_edge_zet
95 #define SCAN_LINE scan_line_zet
96 #define INTERP_DEPTH
97 #define INTERP_ENERGY
98 #define INTERP_TEX
99 #include "scantmpl.h"
100 #undef SCAN_EDGE
101 #undef SCAN_LINE
104 static void (*scan_edge)(struct vertex*, struct vertex*);
105 static void (*scan_line)(int, unsigned char*);
107 int mgl_rast_init(struct state *state, struct framebuffer *fbuf)
108 {
109 fb = fbuf;
110 st = state;
112 if(!(vleft = malloc(fb->height * sizeof *vleft))) {
113 return -1;
114 }
115 if(!(vright = malloc(fb->height * sizeof *vright))) {
116 free(vleft);
117 return -1;
118 }
120 scan_edge = scan_edge_flat;
121 scan_line = scan_line_flat;
123 return 0;
124 }
126 void mgl_rast_cleanup(void)
127 {
128 free(vleft);
129 free(vright);
130 }
132 void mgl_rast_prepare(void)
133 {
134 static void (*sedge[])(struct vertex*, struct vertex*) = {
135 /* tez */
136 scan_edge_flat, /* 000 */
137 scan_edge_z, /* 001 */
138 scan_edge_e, /* 010 */
139 scan_edge_ze, /* 011 */
140 scan_edge_t, /* 100 */
141 scan_edge_zt, /* 101 */
142 scan_edge_et, /* 110 */
143 scan_edge_zet /* 111 */
144 };
145 static void (*sline[])(int, unsigned char*) = {
146 /* tez */
147 scan_line_flat, /* 000 */
148 scan_line_z, /* 001 */
149 scan_line_e, /* 010 */
150 scan_line_ze, /* 011 */
151 scan_line_t, /* 100 */
152 scan_line_zt, /* 101 */
153 scan_line_et, /* 110 */
154 scan_line_zet /* 111 */
155 };
156 int bits = 0;
158 if((st->flags & MGL_TEXTURE_2D) && st->tex.pixels) {
159 bits |= 4;
160 }
161 if(st->flags & MGL_SMOOTH) {
162 bits |= 2;
163 }
164 if((st->flags & MGL_DEPTH_TEST) && fb->zbuf) {
165 bits |= 1;
166 }
168 scan_edge = sedge[bits];
169 scan_line = sline[bits];
170 }
172 void mgl_draw_point(struct vertex *v)
173 {
174 int x = (int)ROUND(v->pos.x);
175 int y = (int)ROUND(v->pos.y);
177 if(x >= 0 && x < fb->width && y >= 0 && y < fb->height) {
178 int cidx = v->cidx + v->energy * st->col_range;
179 fb->pixels[y * fb->width + x] = cidx;
180 }
181 }
183 void mgl_draw_line(struct vertex *v0, struct vertex *v1)
184 {
185 /* TODO */
186 fprintf(stderr, "draw_line unimplemented\n");
187 abort();
188 }
190 void mgl_draw_poly(struct vertex *v, int numv)
191 {
192 int ybeg, yend, i;
193 unsigned char *sline;
195 ybeg = fb->height;
196 yend = 0;
198 for(i=0; i<numv; i++) {
199 struct vertex *v0 = v + i;
200 struct vertex *v1 = v + (i + 1) % numv;
201 int y = (int)ROUND(v0->pos.y);
203 scan_edge(v0, v1);
205 if(y > yend) yend = y;
206 if(y < ybeg) ybeg = y;
207 }
209 if(ybeg < 0) ybeg = 0;
210 if(yend >= fb->height) yend = fb->height - 1;
212 sline = fb->pixels + ybeg * fb->width;
213 for(i=ybeg; i<yend; i++) {
214 scan_line(i, sline);
215 sline += fb->width;
216 }
217 }