-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.cpp
More file actions
executable file
·149 lines (140 loc) · 4.4 KB
/
render.cpp
File metadata and controls
executable file
·149 lines (140 loc) · 4.4 KB
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
#include <QColor>
#include <QImage>
#include "render.h"
const int depth = 64000;
v3df m2v(mtrx m)
{
return v3df(m[0][0]/m[3][0], m[1][0]/m[3][0], m[2][0]/m[3][0]);
}
mtrx viewport(int x, int y, int w, int h) {
mtrx m = mtrx::ident(4);
m[0][3] = x+w/2.f;
m[1][3] = y+h/2.f;
m[2][3] = depth/2.f;
m[0][0] = w/2.f;
m[1][1] = h/2.f;
m[2][2] = depth/2.f;
return m;
}
mtrx lookat(v3df eye, v3df center, v3df up) {
v3df z = (eye-center).normalize();
v3df x = (up^z).normalize();
v3df y = (z^x).normalize();
mtrx res = mtrx::ident(4);
for (int i=0; i<3; i++)
{
res[0][i] = x[i];
res[1][i] = y[i];
res[2][i] = z[i];
res[i][3] = -center[i];
}
return res;
}
v3df viewat(v3df eye, v3df center, v3df up) {
v3df k;
v3df z = (eye-center).normalize();
v3df x = (up^z).normalize();
v3df y = (z^x).normalize();
mtrx res = mtrx::ident(4);
for (int i=0; i<3; i++)
{
res[0][i] = x[i];
res[1][i] = y[i];
res[2][i] = z[i];
res[i][3] = -center[i];
}
return res;
}
void triangle_nondiffuse(v3df t0, v3df t1, v3df t2, QImage &image, QColor color, int *zbuffer)
{
if (t0.y==t1.y && t0.y==t2.y) return; // i dont care about degenerate triangles
if (t0.y>t1.y) std::swap(t0, t1);
if (t0.y>t2.y) std::swap(t0, t2);
if (t1.y>t2.y) std::swap(t1, t2);
int total_height = t2.y-t0.y;
for (int i=0; i<total_height; i++)
{
bool second_half = i>t1.y-t0.y || t1.y==t0.y;
float segment_height = second_half ? t2.y-t1.y : t1.y-t0.y;
float alpha = (float)i/total_height;
float beta = (float)(i-(second_half ? t1.y-t0.y : 0))/segment_height; // be careful: with above conditions no division by zero here
v3di A = v3di(t0) + v3df(t2-t0)*alpha;
v3di B = second_half ? v3di(t1) + v3df(t2-t1)*beta : v3di(t0) + v3df(t1-t0)*beta;
if (A.x>B.x) std::swap(A, B);
for (int j=A.x; j<=B.x; j++)
{
float phi = B.x==A.x ? 1. : (float)(j-A.x)/(float)(B.x-A.x);
v3di P = v3df(A) + v3df(B-A)*phi;
if (P.x > 0 && P.x < image.width() && P.y > 0 && P.y < image.height())
{
unsigned long int idx = P.y+P.x*image.height();
//int idx = P.x+P.y*image.width();
if (idx >= 0 && idx <= image.width()*image.height())
{
int curzbf = zbuffer[idx];
if ((curzbf<P.z))
{
zbuffer[idx] = P.z;
{
image.setPixelColor(P.x, P.y, color);
}
}
}
}
//else (printf("idx out of bounds\n"));
}
}
}
void triangle_diffuse(v3df t0, v3df t1, v3df t2, v2di uv0, v2di uv1, v2di uv2, QImage &image, Model &mdl, QRgb color, float intensity, int *zbuffer)
{
if (t0.y==t1.y && t0.y==t2.y) return; // i dont care about degenerate triangles
if (t0.y>t1.y) std::swap(t0, t1);
if (t0.y>t2.y) std::swap(t0, t2);
if (t1.y>t2.y) std::swap(t1, t2);
int total_height = t2.y-t0.y;
for (int i=0; i<total_height; i++)
{
bool second_half = i>t1.y-t0.y || t1.y==t0.y;
float segment_height = second_half ? t2.y-t1.y : t1.y-t0.y;
float alpha = (float)i/total_height;
float beta = (float)(i-(second_half ? t1.y-t0.y : 0))/segment_height; // be careful: with above conditions no division by zero here
v3df A = v3di(t0) + v3df(t2-t0)*alpha;
v3df B = second_half ? v3di(t1) + v3df(t2-t1)*beta : v3di(t0) + v3df(t1-t0)*beta;
v2di uvA = uv0 + (uv2-uv0)*alpha;
v2di uvB = second_half ? uv1 + (uv2-uv1)*beta : uv0 + (uv1-uv0)*beta;
if (A.x>B.x)
{
std::swap(A, B);
std::swap(uvA, uvB);
}
for (int j=A.x; j<=B.x; j++)
{
float phi = B.x==A.x ? 1. : (float)(j-A.x)/(float)(B.x-A.x);
v3di P = v3df(A) + v3df(B-A)*phi;
v2di uvP = uvA + (uvB-uvA)*phi;
if (P.x > 0 && P.x < image.width() && P.y > 0 && P.y < image.height())
{
unsigned long int idx = P.y+P.x*image.height();
//int idx = P.x+P.y*image.width();
if (idx >= 0 && idx <= image.width()*image.height())
{
//int curzbf = zbuffer[idx];
if ((zbuffer[idx]<P.z))
{
zbuffer[idx] = P.z;
//QRgb tcolor = qRgb(255,255,255);
QRgb tcolor = mdl.diffuse(uvP);
QRgb tc = qRgb(((qRed(tcolor)-qRed(color))*intensity),
((qGreen(tcolor)-qGreen(color))*intensity),
((qBlue(tcolor)-qBlue(color))*intensity));
QRgb *rowData = (QRgb*)image.scanLine(P.y);
QRgb *pixelData = &rowData[P.x];
rowData+=P.x;
*rowData = tc;
}
}
}
//else (printf("idx out of bounds\n"));
}
}
}