-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphics.cpp
311 lines (229 loc) · 6.78 KB
/
graphics.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
#include <SDL.h>
#include <list>
#include <cmath>
#include "player.h"
#include "graphics.h"
#define PI 3.14159265358
//-----Point Class-----
Point::Point(float inputX, float inputY, float inputZ) {
x = inputX;
y = inputY;
z = inputZ;
}
Point::Point(int inputX, int inputY) {
screenX = inputX;
screenY = inputY;
}
//-----Triangle Class-----
void Triangle::addPoint(int x, int y) {
pointList.push_back(Point(x, y));
}
void Triangle::drawLineTriangle(SDL_Renderer* gRenderer) {
SDL_SetRenderDrawColor(gRenderer, color[0], color[1], color[2], alpha);
int x[3];
int y[3];
int i = 0;
for (auto &point : pointList){
x[i] = point.screenX;
y[i] = point.screenY;
i++;
}
if (!solid) {
//Outline triangle
SDL_Point points[4];
int i = 0;
for (auto &point : pointList){
if (i == 0) {
points[0] = {point.screenX, point.screenY};
points[3] = {point.screenX, point.screenY};
} else {
points[i] = {point.screenX, point.screenY};
}
i++;
}
SDL_RenderDrawLines(gRenderer, points, 4);
} else {
//Fill triangle
int numOfPoints = 0;
//Calculate angle between point A and B and between A and C
float angleA = atan2(y[0] - y[1], x[0] - x[1]);
float angleB = atan2(y[0] - y[2], x[0] - x[2]);
//Calculate angles
float cAngleA = cos(angleA);
float sAngleA = sin(angleA);
float cAngleB = cos(angleB);
float sAngleB = sin(angleB);
//Flags to toggle when reaching target coordinates
int doneA = 0;
int doneB = 0;
float xA, yA, xB, yB;
//Distance between C and A AND C and B
float distA = sqrt(pow(x[0] - x[1], 2) + pow(y[0] - y[1], 2));
float distB = sqrt(pow(x[0] - x[2], 2) + pow(y[0] - y[2], 2));
int buffSize = 7500;
SDL_Point linePoints[buffSize];
float radiusA = 0.0f;
float radiusB = 0.0f;
float range = 1;
while (!doneA || !doneB) {
if (numOfPoints > buffSize) break;
//Calculate coordinates if job not done
if (!doneA) {
xA = radiusA * cAngleA + x[1];
yA = radiusA * sAngleA + y[1];
//If target is not reached, calculate coordinate and add to list
if ((distA - radiusA) < range) {
doneA = 1;
} else {
linePoints[numOfPoints++] = {(int) xA, (int) yA};
radiusA += 0.5f;
}
}
if (!doneB) {
xB = radiusB * cAngleB + x[2];
yB = radiusB * sAngleB + y[2];
//If target is not reached, calculate coordinate and add to list
if ((distB - radiusB) < range) {
doneB = 1;
} else {
linePoints[numOfPoints++] = {(int) xB, (int) yB};
radiusB += 0.5f;
}
}
}
SDL_RenderDrawLines(gRenderer, linePoints, numOfPoints);
}
}
//-----Vertex Class-----
Vertex::Vertex(std::initializer_list<float> pointA, std::initializer_list<float> pointB, std::initializer_list<float> pointC) {
pointList.push_back(Point(pointA.begin()[0], pointA.begin()[1], pointA.begin()[2]));
pointList.push_back(Point(pointB.begin()[0], pointB.begin()[1], pointB.begin()[2]));
pointList.push_back(Point(pointC.begin()[0], pointC.begin()[1], pointC.begin()[2]));
}
//-----Object Class-----
Object::Object() {
x = 0.0f;
y = 0.0f;
z = 0.0f;
phiAngle = 0.0f;
thetaAngle = 0.0f;
colorR = 255;
colorG = 255;
colorB = 255;
alpha = 255;
solid = 1;
visible = 1;
objectPointCount = 0;
cX = 0;
cY = 0;
cZ = 0;
}
//Retrieves three 3d points in form of array lists
void Object::addVertex(std::initializer_list<float> pointA, std::initializer_list<float> pointB, std::initializer_list<float> pointC) {
vertexList.push_back(Vertex(pointA, pointB, pointC));
int pointCount = 0;
//Find center of object
for (auto &vertex : vertexList) {
//To hold phi and angle relative to origo for each point
float x[3], y[3], z[3];
int index = 0;
//Three points for each vertex
std::list<Point> &pointList = vertex.pointList;
for (auto &point : pointList) {
x[index] = point.x;
y[index] = point.y;
z[index] = point.z;
cX += point.x;
cY += point.y;
cZ += point.z;
objectPointCount++;
pointCount++;
index++;
}
//Calculate phi and theta between A and B and between A and C relative to A
float phiAB = atan2(y[1] - y[0], x[1] - x[0]);
float thetaAB = acos((z[1] - z[0])/sqrt(pow(x[1] - x[0], 2) + pow(y[1] - y[0], 2) + pow(z[1] - z[0], 2)));
float phiAC = atan2(y[2] - y[0], x[2] - x[0]);
float thetaAC = acos((z[2] - z[0])/sqrt(pow(x[2] - x[0], 2) + pow(y[2] - y[0], 2) + pow(z[2] - z[0], 2)));
//Treat these as 2D coordinates on the spherical plane. Determine angle and rotate 90 degrees to find parallel angle
float angle = atan2(thetaAB - thetaAC, phiAB - phiAC);
float radius = sqrt(pow(thetaAB - thetaAC, 2) + pow(phiAB - phiAC, 2));
//Rotate angle 90 degrees
angle += PI/4;
//Calculate parallel phi and theta
float phiParallel = phiAB + radius * cos(angle);
float thetaParallel = thetaAB + radius * sin(angle);
//Assign values to vertex
vertex.phi = phiParallel;
vertex.theta = thetaParallel;
}
centerX = cX / objectPointCount;
centerY = cY / objectPointCount;
centerZ = cZ / objectPointCount;
float maxDist = 0;
//Calculate maximum radius
for (auto &vertex : vertexList) {
//Three points for each vertex
std::list<Point> &pointList = vertex.pointList;
for (auto &point : pointList) {
float pointDist = sqrt(pow(centerX - point.x, 2) + pow(centerY - point.y, 2) + pow(centerZ - point.z, 2));
if (pointDist > maxDist) {
maxDist = pointDist;
}
}
}
}
//Rotate an object
void Object::rotateObject(float x, float y, float z, float thetaAdd, float phiAdd) {
//Iterate through each vertex in object
for (auto &vertex : vertexList) {
//Three points for each vertex
std::list<Point> &pointList = vertex.pointList;
for (auto &point : pointList) {
//Calculate phi angle
//Calculate diff values for xy plane
float diffX = point.x - x;
float diffY = point.y - y;
//Radius
float r = sqrt(pow(diffX, 2) + pow(diffY, 2));
//Degrees
float phi = atan2(diffY, diffX);
//Add rotation
phi += phiAdd;
//Rotate
point.x = x + r * cos(phi);
point.y = y + r * sin(phi);
//Calculate theta angle
//Calculate diff values for xy plane
diffX = point.x - x;
float diffZ = point.z - z;
//New radius
r = sqrt(pow(diffX, 2) + pow(diffZ, 2));
//Degrees
float theta = atan2(diffX, diffZ);
//Add rotation
theta += thetaAdd;
//Rotate
point.x = x + r * sin(theta);
point.z = z + r * cos(theta);
}
}
}
//Update object color
void Object::setColor(int inputR, int inputG, int inputB) {
colorR = inputR;
colorG = inputG;
colorB = inputB;
}
//Update object color
void Object::setAlpha(int inputA) {
alpha = inputA;
}
//Update object color
void Object::setSolid(int inputVal) {
solid = inputVal;
}
//Update object color
void Object::setVisible(int inputVal) {
visible = inputVal;
}