-
Notifications
You must be signed in to change notification settings - Fork 8
/
trirender.cpp
365 lines (329 loc) · 11.4 KB
/
trirender.cpp
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
#include "trirender.h"
#include <math.h>
#include <string.h>//for memset
#include <iostream>
using namespace std;
typedef struct
{
long long x;
long long y;
long long z;
}Vertice;
const int int_scale=1024;
static unsigned char * image_buffer;
static int image_width;
static int image_height;
int sabs(int a)
{
if (a<0)
return -a;
return a;
}
static void putPixel(long long x,long long y, long long value)
{
if (x<image_width*int_scale && x>=0 && y<image_height*int_scale && y>=0)
{
if (value>(256*int_scale-1)) return;
//value=(256*int_scale-1);
else if (value<0)
value=0;
if (image_buffer[(x/int_scale)+(y/int_scale)*image_width]<(unsigned char)(value/int_scale))
image_buffer[(x/int_scale)+(y/int_scale)*image_width]=(unsigned char)(value/int_scale);
}
}
static void drawScanLine(long long fromx, long long tox,long long fromc,long long toc, long long y)
{
long long x,c=fromc;
x=(fromx+int_scale-1);
x=x-(x%int_scale);
for (;x<tox;x+=int_scale)
{
c=fromc+(toc-fromc)*(x-fromx)/(tox-fromx);
putPixel(x,y, c);
}
}
static void fillBottomFlatTriangle(const Vertice& v1, const Vertice& v2, const Vertice& v3)
{
long long curx1;
long long curx2;
long long curc1;
long long curc2;
long long scanlineY= v1.y;
scanlineY=(scanlineY+int_scale-1);
scanlineY=scanlineY-(scanlineY%int_scale);
if (v1.x>=v3.x)//rounding is toward zero...
{
for (; scanlineY < v2.y; scanlineY+=int_scale)
{
curx1 = v1.x-((-v2.x + v1.x)*(scanlineY-v1.y) / (v2.y - v1.y));
curx2 = v1.x-((-v3.x + v1.x)*(scanlineY-v1.y) / (v3.y - v1.y));
curc1 = v1.z+(v2.z - v1.z)*(scanlineY-v1.y) / (v2.y - v1.y);
curc2 = v1.z+(v3.z - v1.z)*(scanlineY-v1.y) / (v3.y - v1.y);
drawScanLine(curx1-1, curx2,curc1, curc2, scanlineY);
}
}
else if (v1.x<=v2.x)
{
for (; scanlineY < v2.y; scanlineY+=int_scale)
{
curx1 = v1.x+((v2.x - v1.x)*(scanlineY-v1.y) / (v2.y - v1.y));
curx2 = v1.x+((v3.x - v1.x)*(scanlineY-v1.y) / (v3.y - v1.y));
curc1 = v1.z+(v2.z - v1.z)*(scanlineY-v1.y) / (v2.y - v1.y);
curc2 = v1.z+(v3.z - v1.z)*(scanlineY-v1.y) / (v3.y - v1.y);
drawScanLine(curx1, curx2+1,curc1, curc2, scanlineY);
}
}
else
for (; scanlineY < v2.y; scanlineY+=int_scale)
{
curx1 = v1.x-((-v2.x + v1.x)*(scanlineY-v1.y) / (v2.y - v1.y));
curx2 = v1.x+((v3.x - v1.x)*(scanlineY-v1.y) / (v3.y - v1.y));
curc1 = v1.z+(v2.z - v1.z)*(scanlineY-v1.y) / (v2.y - v1.y);
curc2 = v1.z+(v3.z - v1.z)*(scanlineY-v1.y) / (v3.y - v1.y);
drawScanLine(curx1-1, curx2+1,curc1, curc2, scanlineY);
}
}
static void fillTopFlatTriangle(const Vertice& v1, const Vertice& v2, const Vertice& v3)
{
long long curx1;
long long curx2;
long long curc1;
long long curc2;
long long scanlineY = v3.y-1;
scanlineY=scanlineY-(scanlineY%int_scale);
if (v3.x>=v2.x)
{
for (; scanlineY >= v1.y; scanlineY-=int_scale)
{
curx1 = v3.x-((v3.x - v1.x)*(v3.y-scanlineY) / (v3.y - v1.y));
curx2 = v3.x-((v3.x - v2.x)*(v3.y-scanlineY) / (v3.y - v2.y));
curc1 = v3.z-(v3.z - v1.z)*(v3.y-scanlineY) / (v3.y - v1.y);
curc2 = v3.z-(v3.z - v2.z)*(v3.y-scanlineY) / (v3.y - v2.y);
drawScanLine(curx1-1, curx2,curc1, curc2, scanlineY);
}
}
else if (v3.x<=v1.x)
{
for (; scanlineY >= v1.y; scanlineY-=int_scale)
{
curx1 = v3.x+((-v3.x + v1.x)*(v3.y-scanlineY) / (v3.y - v1.y));
curx2 = v3.x+((-v3.x + v2.x)*(v3.y-scanlineY) / (v3.y - v2.y));
curc1 = v3.z-(v3.z - v1.z)*(v3.y-scanlineY) / (v3.y - v1.y);
curc2 = v3.z-(v3.z - v2.z)*(v3.y-scanlineY) / (v3.y - v2.y);
drawScanLine(curx1, curx2+1,curc1, curc2, scanlineY);
}
}
else
for (; scanlineY >= v1.y; scanlineY-=int_scale)
{
curx1 = v3.x-((v3.x - v1.x)*(v3.y-scanlineY) / (v3.y - v1.y));
curx2 = v3.x+((-v3.x + v2.x)*(v3.y-scanlineY) / (v3.y - v2.y));
curc1 = v3.z-(v3.z - v1.z)*(v3.y-scanlineY) / (v3.y - v1.y);
curc2 = v3.z-(v3.z - v2.z)*(v3.y-scanlineY) / (v3.y - v2.y);
drawScanLine(curx1-1, curx2+1,curc1, curc2, scanlineY);
}
}
#define SWAPON(V1,V2,C) do{if(V1->C > V2->C) {temp=V1;V1=V2;V2=temp;}}while(0);
static void drawTriangle(const Vertice* v1,const Vertice* v2,const Vertice* v3)
{
const Vertice* temp;
SWAPON(v1,v2,y);
SWAPON(v1,v3,y);
SWAPON(v2,v3,y);
/* here we know that v1->y <= v2->y <= v3->y */
/* check for trivial case of bottom-flat triangle */
if (v1->y != v3->y)
{
if ( v2->y == v3->y)
{
SWAPON(v2,v3,x);
fillBottomFlatTriangle(*v1, *v2, *v3);
}
// check for trivial case of top-flat triangle
else if (v1->y == v2->y)
{
SWAPON(v1,v2,x);
fillTopFlatTriangle(*v1, *v2, *v3);
}
else
{
// general case - split the triangle in a topflat and bottom-flat one
Vertice v4;
v4.x=(v1->x + ((v3->x - v1->x)*(v2->y - v1->y))/ (v3->y - v1->y));
v4.y=v2->y;
v4.z=(v1->z + ((v3->z - v1->z)*(v2->y - v1->y))/ (v3->y - v1->y));
drawTriangle(v1, v2, &v4);
drawTriangle(v2, &v4, v3);
}
}
else// all y are equal
{
/*SWAPON(v1,v2,x);
SWAPON(v1,v3,x);
SWAPON(v2,v3,x);
drawScanLine(v2->x, v3->x,v2->z, v3->z, v2->y);
drawScanLine(v1->x, v3->x,v1->z, v3->z, v1->y);
drawScanLine(v1->x, v2->x,v1->z, v2->z, v1->y);*/
}
}
static void drawTri(/*unsigned char *buffer, int w, int h,*/const long long *points,const unsigned int *triangles, int tri_ind)
{
unsigned int t[3];
for (int i=0;i<3;++i)
t[i]=triangles[tri_ind*3+i];
const Vertice *v[3];
for (int i=0;i<3;++i)
{
v[i]=(const Vertice*)&points[t[i]*3];
}
drawTriangle(v[0],v[1],v[2]);
}
#define DEG2RAD (M_PI/180.0)
#define MAP_MATRIX(I,J) (J*4+I)
static void my_PerspectiveFOV(float fov, float aspect, float near, float far, float* mret) {
float yScale = 1.0 / t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 ]*m2[ 10 ]+m1[ 14 ]*m2[ 11 ],
m1[ 3 ]*m2[ 8 ]+m1[ 7 ]*m2[ 9 ]+m1[ 11 ]*m2[ 10 ]+m1[ 15 ]*m2[ 11 ],
m1[ 0 ]*m2[ 12 ]+m1[ 4 ]*m2[ 13 ]+m1[ 8 ]*m2[ 14 ]+m1[ 12 ]*m2[ 15 ],
m1[ 1 ]*m2[ 12 ]+m1[ 5 ]*m2[ 13 ]+m1[ 9 ]*m2[ 14 ]+m1[ 13 ]*m2[ 15 ],
m1[ 2 ]*m2[ 12 ]+m1[ 6 ]*m2[ 13 ]+m1[ 10 ]*m2[ 14 ]+m1[ 14 ]*m2[ 15 ],
m1[ 3 ]*m2[ 12 ]+m1[ 7 ]*m2[ 13 ]+m1[ 11 ]*m2[ 14 ]+m1[ 15 ]*m2[ 15 ]
};
memcpy(res, m, sizeof(float)*16);
}
static void xrotation_mat(float xrot, float* mret) {
float cxrot=cosf(xrot*DEG2RAD);
float sxrot=sinf(xrot*DEG2RAD);
float m[16] = {
1, 0, 0, 0,
0, cxrot, sxrot, 0,
0, -sxrot, cxrot, 0,
0, 0, 0, 1
};
memcpy(mret, m, sizeof(float)*16);
}
static void yrotation_mat(float yrot, float* mret) {
float cyrot=cosf(yrot*DEG2RAD);
float syrot=sinf(yrot*DEG2RAD);
float m[16] = {
cyrot, 0, -syrot, 0,
0 , 1, 0 , 0,
syrot, 0, cyrot , 0,
0, 0, 0, 1
};
memcpy(mret, m, sizeof(float)*16);
}
static void zrotation_mat(float zrot, float* mret) {
float czrot=cosf(zrot*DEG2RAD);
float szrot=sinf(zrot*DEG2RAD);
float m[16] = {
czrot, szrot, 0, 0,
-szrot , czrot, 0 , 0,
0, 0, 1, 0,
0, 0, 0, 1
};
memcpy(mret, m, sizeof(float)*16);
}
static void translation_mat(float x,float y,float z, float* mret) {
float m[16] = {
1, 0, 0, 0,
0 , 1, 0 , 0,
0, 0, 1, 0,
x, y, z, 1
};
memcpy(mret, m, sizeof(float)*16);
}
static void scale_mat(float sx,float sy,float sz, float* mret) {
float m[16] = {
sx, 0, 0, 0,
0 , sy, 0 , 0,
0, 0, sz, 0,
0, 0, 0, 1
};
memcpy(mret, m, sizeof(float)*16);
}
static void mult_mat_vec(float* m , float *v,float *out_v)
{
float res[4] = {
m[ 0 ]*v[ 0 ]+m[ 4 ]*v[ 1 ]+m[ 8 ]*v[ 2 ]+m[ 12 ]*v[ 3 ],
m[ 1 ]*v[ 0 ]+m[ 5 ]*v[ 1 ]+m[ 9 ]*v[ 2 ]+m[ 13 ]*v[ 3 ],
m[ 2 ]*v[ 0 ]+m[ 6 ]*v[ 1 ]+m[ 10 ]*v[ 2 ]+m[ 14 ]*v[ 3 ],
m[ 3 ]*v[ 0 ]+m[ 7 ]*v[ 1 ]+m[ 11 ]*v[ 2 ]+m[ 15 ]*v[ 3 ]};
memcpy(out_v, res, sizeof(float)*4);
}
extern float projMatrix[16];
extern float modelMatrix[16];
void trirender(unsigned char *buffer, int width, int height, const float *points, int np,const unsigned int *triangles, int nt, float xrot, float yrot, float zrot,
float zoom,float contrast,float xoff,float yoff,float scale)
{
image_buffer=buffer;
image_width=width;
image_height=height;
memset(buffer,0,width*height);
long long *i_points=new long long[np*3];
/*float cxrot=cosf(xrot*DEG2RAD);
float sxrot=sinf(xrot*DEG2RAD);
float cyrot=cosf(yrot*DEG2RAD);
float syrot=sinf(yrot*DEG2RAD);
float czrot=cosf(zrot*DEG2RAD);
float szrot=sinf(zrot*DEG2RAD);*/
float prjmat[16];
float temp_mat[16];
float model_mat[16]={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
my_PerspectiveFOV(20,(float)width/height,(zoom-contrast),(zoom+contrast),prjmat);
//float _w,old_val_x;
//float x,y,z;
translation_mat(xoff,yoff,-zoom,temp_mat);
mult_mat(model_mat,temp_mat,model_mat);
scale_mat(scale,scale,1,temp_mat);
mult_mat(model_mat,temp_mat,model_mat);
xrotation_mat(xrot,temp_mat);
mult_mat(model_mat,temp_mat,model_mat);
yrotation_mat(yrot,temp_mat);
mult_mat(model_mat,temp_mat,model_mat);
zrotation_mat(zrot,temp_mat);
mult_mat(model_mat,temp_mat,model_mat);
for (int i=0;i<np;++i)
{
float trans_vec[4]={points[i*3+0],points[i*3+1],points[i*3+2],1};
mult_mat_vec(model_mat,trans_vec,trans_vec);
mult_mat_vec(prjmat,trans_vec,trans_vec);
trans_vec[2]=(trans_vec[2]*-128)/trans_vec[3];
trans_vec[1]=(trans_vec[1]*(height/2))/trans_vec[3];
trans_vec[0]=(trans_vec[0]*(width/2))/trans_vec[3];
i_points[i*3]=(long long)(int_scale*(double)trans_vec[0])+width*int_scale/2;
i_points[i*3+1]=(long long)(int_scale*(double)trans_vec[1])+height*int_scale/2;
i_points[i*3+2]=(long long)(int_scale*(double)trans_vec[2])+128*int_scale;
}
for (int i=0;i<nt;++i)
{
drawTri(/*buffer,width,height,*/i_points,triangles,i);
}
delete [] i_points;
}