-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideo.c
275 lines (255 loc) · 6.39 KB
/
video.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "alloc.h"
#include "video.h"
#include "sandersboard.h"
#include "sandersio.h"
#include "boopt.h"
#include "sleep.h"
#include "matrixlib.h"
#include "mathlib.h"
#include <math.h>
int vid_x;
int vid_y;
extern char * vidmem;
//no bounds checks
void video_fill_rect(unsigned char color, int mx, int my, int lx, int ly){
int t;
if(mx > lx){
t = mx;
mx = lx;
lx = t;
}
if(my > ly){
t = my;
my = ly;
ly = t;
}
int y, x;
for(y = my; y < ly; y++){
unsigned char * line = vidmem + y * vid_x;
for(x = mx; x < lx; x++){
line[x] = color;
}
}
}
void video_fill_texture(texture_t t, int mx, int my, int lx, int ly){
int y, x, tx, ty;
for(ty = 0, y = my; y < ly; y++, ty++){
unsigned char * line = vidmem + y * vid_x;
unsigned char * tline = t.data + ty * t.res[0];
for(tx = 0, x = mx; x < lx; x++, tx++){
line[x] = tline[tx];
}
}
}
void video_draw_line(unsigned char color, int sx, int sy, int ex, int ey){
if(sy > ey){
int t = sy;
sy = ey;
ey = t;
}
float dx = sx - ex;
if(dx != 0.0){
float dy = sy - ey;
float err = 0;
float derr = (dy / dx);
if(derr < 0.0) derr = - derr;
int x, y = sy;
for(x = sx; x < ex; x++){
vidmem[y*vid_x + x] = color;
err += derr;
while(err >= 0.5f){
vidmem[y*vid_x + x] = color;
y++;
err-=1.0;
}
}
} else { //vertical
int y;
for(y = sy; y < ey; y++){
vidmem[y*vid_x + sx] = color;
}
}
}
void line(unsigned char color, int x0, int y0, int x1, int y1) {
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
int err = (dx>dy ? dx : -dy)/2, e2;
for(;;){
vidmem[y0 * vid_x + x0] = color;
if (x0==x1 && y0==y1) break;
e2 = err;
if (e2 >-dx) { err -= dy; x0 += sx; }
if (e2 < dy) { err += dx; y0 += sy; }
}
}static inline float orient2d(vec2_t a, vec2_t b, vec2_t c){
return (b[0]-a[0])*(c[1]-a[1]) - (b[1]-a[1])*(c[0]-a[0]);
}
static inline float min3(float a, float b, float c){
float ret = a;
if(b < a) ret = b;
if(c < a) ret = c;
return ret;
}
static inline float max3(float a, float b, float c){
float ret = a;
if(b > a) ret = b;
if(c > a) ret = c;
return ret;
}
static inline float max(float a, float b){
return a > b ? a : b;
}
static inline float min(float a, float b){
return a < b ? a : b;
}
static inline int iorient2d(ivec2_t a, ivec2_t b, ivec2_t c){
return (b[0]-a[0])*(c[1]-a[1]) - (b[1]-a[1])*(c[0]-a[0]);
}
static inline int imin3(int a, int b, int c){
int ret = a;
if(b < a) ret = b;
if(c < a) ret = c;
return ret;
}
static inline int imax3(int a, int b, int c){
int ret = a;
if(b > a) ret = b;
if(c > a) ret = c;
return ret;
}
static inline int imax(int a, int b){
return a > b ? a : b;
}
static inline int imin(int a, int b){
return a < b ? a : b;
}
int screenHeight;
int screenWidth;
int (*renderPixel)(ivec2_t pos, vec3_t weights, void * tridata);
void video_draw_triangle(unsigned int color, ivec_t *verts, void * tridata){
ivec_t *v0 = verts;
ivec_t *v1 = verts+2;
ivec_t *v2 = verts+4;
// Compute triangle bounding box
int minX = imin3(v0[0], v1[0], v2[0]);
int minY = imin3(v0[1], v1[1], v2[1]);
int maxX = imax3(v0[0], v1[0], v2[0]);
int maxY = imax3(v0[1], v1[1], v2[1]);
// Clip against screen bounds
minX = imax(minX, 0);
minY = imax(minY, 0);
maxX = imin(maxX, screenWidth - 1);
maxY = imin(maxY, screenHeight - 1);
// Rasterize
ivec2_t p;
for(p[1] = minY; p[1] <= maxY; p[1]++){
for(p[0] = minX; p[0] <= maxX; p[0]++){
// Determine barycentric coordinates
int w0 = iorient2d(v1, v2, p);
int w1 = iorient2d(v2, v0, p);
int w2 = iorient2d(v0, v1, p);
// If p is on or inside all edges, render pixel.
if(w0 >= 0 && w1 >= 0 && w2 >= 0){
vec3_t weights;
weights[0] = 0.33;
weights[1] = 0.33;
weights[2] = 0.33;
renderPixel(p, weights, tridata);
}
}
}
}
void video_key_handler(char keycodde){
return;
}
matrix4x4_t proj;
vec_t verts[24] = {
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0 };
int indices[24] = { 0, 1, 0, 2, 1, 3, 2, 3, 4, 5, 4, 6, 5, 7, 6, 7,
0, 4, 1, 5, 2, 6, 3, 7};
int oldc1[48] = {0};
int newc1[48] ={0};
int oldc2[48] = {0};
int newc2[48] ={0};
void calc_cube(int * buf, float spinsx, float spinsy, float spinsz, int centx, int centy, float scaler){
matrix4x4_t meet;
matrix4x4_t mvp;
Matrix4x4_CreateFromQuakeEntity(&meet, 0.0, 0.0, 3.0, spinsx, spinsy, spinsy, 1.0);
Matrix4x4_Concat(&mvp, &proj, &meet);
int i;
for( i = 0; i < 12; i++){
vec_t * pp = &verts[indices[i*2] * 3];
vec_t * ps = &verts[indices[i*2+1] * 3];
vec4_t in1, out1, in2, out2;
in1[0] = pp[0];
in1[1] = pp[1];
in1[2] = pp[2];
in1[3] = 1.0;
in2[0] = ps[0];
in2[1] = ps[1];
in2[2] = ps[2];
in2[3] = 1.0;
Matrix4x4_Transform4(&mvp, in1, out1);
Matrix4x4_Transform4(&mvp, in2, out2);
out1[0] /= out1[3];
out1[1] /= out1[3];
out2[0] /= out2[3];
out2[1] /= out2[3];
//now "clip" to screen coords
int x1, x2, y1, y2;
buf[i * 4] = out1[0] * scaler+centx;
buf[i * 4+1] = out1[1] * scaler+centy;
buf[i * 4+2] = out2[0] * scaler+centx;
buf[i * 4+3] = out2[1] * scaler+centy;
}
}
void draw_cube(char color, int * old, int * new){
int i = 0;
for(i = 0; i < 12; i++){
line(0, old[i*4], old[i*4+1], old[i*4+2], old[i*4+3]);
}
for(i = 0; i < 12; i++){
line(color,new[i*4], new[i*4+1], new[i*4+2], new[i*4+3]);
}
}
extern int init_vga(int blah);
int videorun(int argc, char * argv[]){
set_keyboard_handler(&video_key_handler);
init_vga(0);
vid_x = 320;
vid_y = 200;
float dz = 10.0 - 0.1;
proj.m[0][0] = 1.0 / ((float)vid_x / (float)vid_y);
proj.m[1][1] = 1.0;
proj.m[2][2] = -(10.0 + 0.1) / dz;
proj.m[2][3] = -1.0;
proj.m[3][2] = -2.0 * 0.1 * 10.0 / dz;
int i,z;
int jj = 0;
for(i = 0; 1; i++){
int x = i % 160;
int y = 30;
float xoff = sin(i * 0.001 + M_PI * 0.5);
float yoff = sin(i * 0.001);
float cent = sin(i * 0.00131 + 0.00313) * 20.0;
line(i/ 1000 % 39, 160 + xoff * cent, 100 + yoff * cent, 160 + xoff * (40.0 + cent * 0.1), 100 + yoff * (40 + cent * 0.1));
if(!(i % 10)){
jj = !jj;
calc_cube( jj ? newc1 : oldc1, i * 0.01, sin(i * 0.00011) * 500.0,0.0, 70, 100, 100.0);
calc_cube( jj ? newc2 : oldc2, i * 0.01 + 30.0, sin(i * 0.000051) * 1000.0,0.0, 250, 100, 100.0);
if(jj) draw_cube(6, oldc1, newc1);
else draw_cube(6, newc1, oldc1);
if(jj) draw_cube(10, oldc2, newc2);
else draw_cube(10, newc2, oldc2);
}
sleepy_sanders(10000);
}
return 1;
}