-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.c
432 lines (370 loc) · 9.79 KB
/
primitives.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Copyright (C) 2016 Richard Jansson
*
* This file is part of Veta.
*
* Veta is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Veta is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Veta. If not, see <http://www.gnu.org/licenses/>.
*/
#include <float.h>
#include "swan.h"
#include "3dmath.h"
#include"primitives.h"
double z_buffer[WIDTH*HEIGHT],zeroed_buffer[WIDTH*HEIGHT];
rgb red=(rgb){255,0,0};
rgb green=(rgb){0,255,0};
rgb blue=(rgb){0,0,255};
rgb black=(rgb){0,0,0};
rgb white=(rgb){ 255, 255, 255};
#define MAX_DIST 5000.0
void putpixel_3d(int x,int y,double z,rgb c){
if(x<=0||x>=WIDTH) return;
if(y<=0||y>=HEIGHT) return;
if(z_buffer[y*WIDTH+x] > z ){
putpixel_c(x,y,c);
z_buffer[y*WIDTH+x]=z;
}
}
float clipfloat(float f,float min,float max){
if( f < min ) f = 0.0;
if( f > max ) f = max;
return f;
}
void render_zbuffer(){
double max=-DBL_MAX,min=DBL_MAX;
for(int i=0;i<HEIGHT*WIDTH;i++){
if(z_buffer[i]==DBL_MAX) continue;
if(z_buffer[i]>max) max=z_buffer[i];
if(z_buffer[i]<min) min=z_buffer[i];
}
printf("[%f,%f]\n",min,max);
for(int y=0;y<HEIGHT;y++){
for(int x=0;x<WIDTH;x++){
double v=z_buffer[y*WIDTH+x];
rgb c;
if( v == DBL_MAX) c = red;
else{
v=255*(v-min)/(max-min);
// printf("%f\n",v);
// c.g=v > 255 ? 255 : v;
c.b=c.r=c.g= c.g=v > 255 ? 255 : v;
}
putpixel_c(x,y,c);
}
}
}
// Doing this instead of running a loop increased frame rate from 16 fps to 22 fps!
// that is memcpying the correct buffer instead of looping trough it!
void init_zbuffer(){
for(int i=0;i<WIDTH*HEIGHT;i++) zeroed_buffer[i]=DBL_MAX;
}
void clear_zbuffer(){
mempcpy(z_buffer,zeroed_buffer,sizeof(double)*WIDTH*HEIGHT);
}
int max(int a,int b){
if(a>b) return a;
return b;
}
int min(int a,int b){
if(a<b) return a;
return b;
}
void draw_line_color(int x0,int y0,int x1,int y1,rgb c){
int xstart=min(x0,x1),xend=max(x0,x1);
int ystart=min(y0,y1),yend=max(y0,y1);
if( x1 == x0 ){
for(int y=max(ystart,0);y<min(yend,HEIGHT);y++) putpixel_c(x0,y,c);
return;
}
if( y1 == y0 ){
for(int x=max(xstart,0);x<min(xend,WIDTH);x++) putpixel_c(x,y0,c);
return;
}
double k=(y1-y0)/(float)(x1-x0);
int d=y1-k*x1;
if( k < 1.0 && k > -1.0 ){
for(int x=max(xstart,0);x<min(xend,WIDTH);x++){
int y = k*x + d;
putpixel_c(x,y,c);
}
}else{
for(int y=max(ystart,0);y<min(yend,HEIGHT);y++){
int x = (y-d)/k;
putpixel_c(x,y,c);
}
}
}
void draw_line(int x0,int y0,int x1,int y1){
int xstart=min(x0,x1),xend=max(x0,x1);
int ystart=min(y0,y1),yend=max(y0,y1);
if( x1 == x0 ){
for(int y=max(ystart,0);y<min(yend,HEIGHT);y++) putpixel(x0,y);
return;
}
if( y1 == y0 ){
for(int x=max(xstart,0);x<min(xend,WIDTH);x++) putpixel(x,y0);
return;
}
double k=(y1-y0)/(float)(x1-x0);
int d=y1-k*x1;
if( k < 1.0 && k > -1.0 ){
for(int x=max(xstart,0);x<min(xend,WIDTH);x++){
int y = k*x + d;
putpixel(x,y);
}
}else{
for(int y=max(ystart,0);y<min(yend,HEIGHT);y++){
int x = (y-d)/k;
putpixel(x,y);
}
}
}
#define A 1
#define B 2
//#define Z 1000.0
#define Z 0
void draw_triangle(int *a,int *b,int *c,vect_t pos,rgb color){
int x0,y0,x1,y1,x2,y2;
vect_t x={a[0],a[1],a[2]+Z};
vect_t y={b[0],b[1],b[2]+Z};
vect_t z={c[0],c[1],c[2]+Z};
x=v_add(x,pos);
y=v_add(y,pos);
z=v_add(z,pos);
int q,w,e;
q=project_coord(x,&x0,&y0);
w=project_coord(y,&x1,&y1);
e=project_coord(z,&x2,&y2);
// if( !(q && w && e)) return;
draw_line_color(x0,y0,x1,y1,color);
draw_line_color(x1,y1,x2,y2,color);
draw_line_color(x2,y2,x0,y0,color);
}
void draw_triangle2(vect_t a,vect_t b,vect_t c,vect_t pos,rgb color){
int x0,y0,x1,y1,x2,y2;
a=v_add(a,pos);
b=v_add(b,pos);
c=v_add(c,pos);
project_coord(a,&x0,&y0);
project_coord(b,&x1,&y1);
project_coord(c,&x2,&y2);
draw_line_color(x0,y0,x1,y1,color);
draw_line_color(x1,y1,x2,y2,color);
draw_line_color(x2,y2,x0,y0,color);
}
void draw_triangle_gradient_new(point p[],vect_t zv[],double zs[],rgb c[]){
double minx=DBL_MAX,miny=DBL_MAX,maxx=-DBL_MAX,maxy=-DBL_MAX;
for(int i=0;i<3;i++){
if( p[i].x < minx) minx=p[i].x;
if( p[i].y < miny) miny=p[i].y;
if( p[i].x > maxx) maxx=p[i].x;
if( p[i].y > maxy) maxy=p[i].y;
}
vect_t t[3];
// printf("%f %f %f %f\n",minx,miny,maxx,maxy);
for(int i=0;i<3;i++){
t[i].x=p[i].x;
t[i].y=p[i].y;
t[i].z=0;
}
#if BARY_INLINE
// barycentric part one!
vect_t u=v_sub(t[1],t[0]);
vect_t v=v_sub(t[2],t[0]);
double d=u.x*v.y-v.x*u.y,a,bb,cc;
a=(u.y-v.y);
bb=(v.x-u.x);
cc=(u.x*v.y-v.x*u.y);
#endif
for(int y=miny;y<maxy;y++){
for(int x=minx;x<maxx;x++){
#if BARY_INLINE
vect_t pt=v_sub((vect_t){(double)x,(double)y,0},t[0]);
vect_t b;
b.x=(pt.x*a+pt.y*bb+cc)/d;
b.y=(pt.x*v.y-pt.y*v.x)/d;
b.z=(pt.y*u.x-pt.x*u.y)/d;
#else
vect_t b=barycentric(t,(vect_t){(double)x,(double)y,0.0});
#endif
// Fun effect if you put this at 0
#if 1
if(b.x < 0 || b.y < 0 || b.z < 0) continue;
if(b.x > 1.0 || b.y > 1.0 || b.z > 1.0 ) continue;
#else
putpixel_c(x,y,c[0]);
continue;
#endif
double z=b.x*zs[0] + b.y*zs[1] + b.z*zs[2];
rgb cout;
cout.r=b.x*c[0].r + b.y*c[1].r + b.z*c[2].r;
cout.g=b.x*c[0].g + b.y*c[1].g + b.z*c[2].g;
cout.b=b.x*c[0].b + b.y*c[1].b + b.z*c[2].b;
putpixel_3d(x,y,z,cout);
// putpixel_c(x,y,c[0])
}
}
}
void draw_triangle_gradient(point p[],vect_t zv[],double zs[],rgb c[]){
int min_i=0,max_i=0,mid_i=-1;
// for(int i=0;i<3;i++) print_point(p[i]);
for(int i=1;i<3;i++){
if( p[i].x < p[min_i].x ) min_i=i;
if( p[i].x > p[max_i].x ) max_i=i;
}
/* printf("min: ");
print_point(p[min_i]);
printf("max: ");
print_point(p[max_i]);
*/
for(int i=0;i<3;i++) if( i != min_i && i != max_i ) mid_i = i;
int dx0=p[min_i].x-p[max_i].x;
int dy0=p[min_i].y-p[max_i].y;
double k0=dy0/(float)dx0;
int d0=p[min_i].y-k0*p[min_i].x;
int dx1=p[min_i].x-p[mid_i].x;
int dy1=p[min_i].y-p[mid_i].y;
double k1=dy1/(float)dx1;
int d1=p[min_i].y-k1*p[min_i].x;
int dx2=p[mid_i].x-p[max_i].x;
int dy2=p[mid_i].y-p[max_i].y;
double k2=dy2/(float)dx2;
int d2=p[mid_i].y-k2*p[mid_i].x;
for(int x=p[min_i].x ;x<p[mid_i].x;x++){
int y0 = k0*x + d0;
int y1 = k1*x + d1;
for(int y=min(y0,y1);y<=max(y0,y1);y++){
point pc={x,y};
// color cc=_tri_calc_color(p,pc,c);
vect_t b=barycentric(zv,(vect_t){x,y,0});
double z=b.x*zs[0] + b.y*zs[1] + b.z*zs[2];
if( x < 0 || x > WIDTH) continue;
if( y < 0 || y > HEIGHT) continue;
// printf("%f\n",z);
//
if((y>=0 && y<HEIGHT) && (x>0&&x<WIDTH)){
if(z_buffer[y*WIDTH+x] > z ){
putpixel_c(x,y,c[0]);
z_buffer[y*WIDTH+x]=z;
}
}
}
}
for(int x=p[mid_i].x ;x<p[max_i].x;x++){
int y0 = k0*x + d0;
int y2 = k2*x + d2;
for(int y=min(y0,y2);y<=max(y0,y2);y++){
point pc={x,y};
// color cc=_tri_calc_color(p,pc,c);
// putpixel_c(x,y,c[0]);
vect_t b=barycentric(zv,(vect_t){x,y,0});
//j double z=zv[0].z/b.x + zv[1].z/b.y + zv[2].z/b.z;
double z = b.x*zs[0] + b.y*zs[1] + b.z*zs[2];
if( x < 0 || x > WIDTH) continue;
if( y < 0 || y > HEIGHT) continue;
if((y>=0 && y<HEIGHT) && (x>0&&x<WIDTH)){
if(z_buffer[y*WIDTH+x] > z){
putpixel_c(x,y,c[0]);
z_buffer[y*WIDTH+x]=z;
}
}
}
}
}
int outside_of_screen(point p[]){
int c=0;
for(int i=0;i<3;i++){
c+= p[i].x < 0 || p[i].x > WIDTH;
c+= p[i].y < 0 || p[i].y > HEIGHT;
}
return c==3;
}
int clip(int a){
if(a>255) return 255;
if( a < 0 ) return 0;
}
/* Third version of draw triangle uses a z_buffer
And we're using flat shading
clear_z_buffer needs to be run in the main loop before every frame. */
void draw_triangle3(int *a,int *b,int *c,vect_t pos,vect_t rot,rgb color){
int x0,y0,x1,y1,x2,y2;
// this is is the format that the triangles should be passed to us already
vect_t x={a[0],a[1],a[2]+Z};
vect_t y={b[0],b[1],b[2]+Z};
vect_t z={c[0],c[1],c[2]+Z};
// Lighting
vect_t n=v_norm(v_cross(x,y));
// vect_t l=v_sub(cam.pos,x);
vect_t l=cam.pos;
double lum=1.0;
// if( lum < 0 ) return 0;
rgb colors[3];
point p[3];
x=v_rot(x,rot);
y=v_rot(y,rot);
z=v_rot(z,rot);
// should we do the translation here as well does this make sense?
x=v_add(x,pos);
y=v_add(y,pos);
z=v_add(z,pos);
double lums[3];
lums[0]=v_dot(l,x)/(v_len(l)*v_len(x));
lums[1]=v_dot(l,y)/(v_len(l)*v_len(y));
lums[2]=v_dot(l,z)/(v_len(l)*v_len(z));
#if 1
for(int i=0;i<3;i++){
#if 0
lum=lums[i];
if( lum < 0 ) lum=0;
#else
lum=1.0;
#endif
colors[i].r=clip(color.r*lum);
colors[i].g=clip(color.g*lum);
colors[i].b=clip(color.b*lum);
}
#else
for(int i=0;i<3;i++){
colors[i].r=color.r;
colors[i].g=color.g;
colors[i].b=color.b;
}
#endif
int q,w,e;
q=project_coord(x,&p[0].x,&p[0].y);
w=project_coord(y,&p[1].x,&p[1].y);
e=project_coord(z,&p[2].x,&p[2].y);
if(outside_of_screen(p)){
return;
}
rgb ca[3];
ca[0]=ca[1]=ca[2]=color;
vect_t v3[3];
v3[0]=x; v3[1]=y; v3[2]=z;
double distances[3];
// introduce max distance of 5000
distances[0]=clipfloat(v_len(v_sub(x,cam.pos)),0,MAX_DIST)/MAX_DIST;
distances[1]=clipfloat(v_len(v_sub(y,cam.pos)),0,MAX_DIST)/MAX_DIST;
distances[2]=clipfloat(v_len(v_sub(z,cam.pos)),0,MAX_DIST)/MAX_DIST;
/*
distances[0]=v_len(v_sub(z,cam.pos));
distances[1]=v_len(v_sub(y,cam.pos));
distances[2]=v_len(v_sub(x,cam.pos));
*/
draw_triangle_gradient_new(p,v3,distances,colors);
#if 0
draw_line_color(p[0].x,p[0].y,p[1].x,p[1].y,black);
draw_line_color(p[1].x,p[1].y,p[2].x,p[2].y,black);
draw_line_color(p[1].x,p[1].y,p[0].x,p[0].y,black);
#endif
}