-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRender.cpp
212 lines (161 loc) · 5.32 KB
/
Render.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
#include <RGL.h>
#include <Render.h>
bool sorty(std::valarray<float> a, std::valarray<float> b) {
return a[1] > b[1];
}
void printv(char const *t, std::valarray<float> a) {
printf("V: %20s:", t);
for(int i = 0; i < a.size(); i++) {
printf(" %8.1f", a[i]);
}
printf(";\n");
}
void Render::fillBottomFlatTriangle(std::valarray<float> v1, std::valarray<float> v2, std::valarray<float> v3) {
/*
* v1
v2 *------------------------------------------- * v3
*/
if(v2[0] > v3[0])
{
v3.swap(v2);
}
std::valarray<float> invslope1 = (v2 - v1) / (v2[1] - v1[1]);
std::valarray<float> invslope2 = (v3 - v1) / (v3[1] - v1[1]);
std::valarray<float> curx1 = v2;
std::valarray<float> curx2 = v3;
for (; curx1[1] < v1[1]; curx1 += invslope1, curx2 += invslope2) {
std::valarray<float> xinc = (curx2 - curx1) / (curx2[0] - curx1[0]); // xsteps
for(std::valarray<float> scancolX = curx1; scancolX[0] < (curx2[0] - 1.); scancolX += xinc)
{
fragment(scancolX);
}
}
}
void Render::fillTopFlatTriangle(std::valarray<float> v1, std::valarray<float> v2, std::valarray<float> v3) {
/*
v1 *------------------------------------------- * v2
* v3
*/
if(v1[0] > v2[0]) {
v1.swap(v2);
}
std::valarray<float> invslope1 = (v1 - v3) / (v1[1] - v3[1]);
std::valarray<float> invslope2 = (v2 - v3) / (v2[1] - v3[1]);
std::valarray<float> curx1 = v3;
std::valarray<float> curx2 = v3;
for (; curx1[1] < v1[1]; curx1 += invslope1, curx2 += invslope2) {
std::valarray<float> xinc = (curx2 - curx1) / (curx2[0] - curx1[0]); // xsteps
for(std::valarray<float> scancolX = curx1; scancolX[0] < (curx2[0] - 1.); scancolX += xinc)
{
fragment(scancolX);
}
}
}
bool isEdge(std::valarray<float> v1, std::valarray<float> v2, std::valarray<float> v3){
bool isInside = (((v3[0] - v1[0] ) * (v2[1] - v1[1])) - ((v3[1] - v1[1]) * (v2[0] - v1[0])) >= 0);
return isInside;
}
void Render::drawShadedTriangle(std::valarray<float> v1, std::valarray<float> v2, std::valarray<float> v3) {
/*
After sort.
* v1
* v2 * v4
* v3
*/
int width = WIDTH;
int height = HEIGHT;
//Loop throgh each pixel on screen and then check if inside and then draw if it is
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
std::valarray<float> p = {x,y};
bool inside = true;
inside &= isEdge(v1,v2,p);
inside &= isEdge(v2,v3,p);
inside &= isEdge(v3,v1,p);
if (inside){
fragment(p);
}
}
}
/* at first sort the three glm::vec2's by y-coordinate ascending so v1 is the topmost. */
// assert(v1.size() == v2.size() && v2.size() == v3.size());
// assert(v1.size() >= 4);
// assert(v2.size() >= 4);
// assert(v3.size() >= 4);
// std::vector<std::valarray<float> > co = {v1, v2, v3};
// std::sort(co.begin(), co.end(), sorty);
// if((int) co[1][1] == (int) co[2][1] && (int) co[0][1] == (int) co[2][1]) //degenerate triangle.
// return;
// if ((int) co[1][1] == (int) co[2][1]) /* check for trivial case of bottom-flat triangle */
// {
// fillBottomFlatTriangle(co[0], co[1], co[2]);
// }
// else if ((int) co[0][1] == (int) co[1][1]) /* check for trivial case of top-flat triangle */
// {
// fillTopFlatTriangle(co[0], co[1], co[2]);
// }
// else /* general case - split the triangle in a topflat and bottom-flat one */
// {
// std::valarray<float> v4;
// v4 = co[0] + ((co[1][1] - co[0][1]) / (co[2][1] - co[0][1])) * (co[2] - co[0]);
// fillBottomFlatTriangle(co[0], co[1], v4);
// fillTopFlatTriangle(co[1], v4, co[2]);
// }
}
int Render::add(vector<glm::vec4> points) { // adds a vector of vertices
int newObjects = points.size();
for(int i = 0; i < newObjects; i++)
Render::in_vertices.push_back(points[i]);
return Render::in_vertices.size();
}
int Render::add(glm::vec4 point) { // adds a single vertices
Render::in_vertices.push_back(point);
return Render::in_vertices.size();
}
int Render::size(void) { // returns the size.
return Render::in_vertices.size();
}
int Render::remove(int n) { // removes one or more vertices
int objects = Render::in_vertices.size();
assert(n > 0);
if(n >= objects)
{
Render::in_vertices.clear();
}
else
{
for(int i = 0; i < n; i++)
Render::in_vertices.pop_back();
}
return Render::in_vertices.size();
}
void Render::vertex(glm::vec4 point) { // this constructs the geometry data
glm::vec4 t;
t = Transform * point;
t[0] = t[0] / t[3]; // homogenous normalization
t[1] = t[1] / t[3];
t[2] = t[2] / t[3];
t[3] = 1.0;
t = _workstation(t);
std::valarray<float> vv = {t[0], t[1], t[2], t[3]}; //x, y, z, w, b brings all the attributes togethrer for rendering.
Render::vertices.push_back(vv);
}
int Render::draw()
{
int objects = in_vertices.size();
/*
* Convert vertices to screen coordiates.
*/
Render::vertices.clear(); // need this otherwise draw previous values as well
for(int i = 0; i < objects; i++)
{
Render::vertex(Render::in_vertices[i]);
}
/*
* Render every 3 as a triangle..
*/
for(int i = 0; i < objects; i += 3)
{
drawShadedTriangle(Render::vertices[i], Render::vertices[i + 1], Render::vertices[i + 2]);
}
}