-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cube.h
81 lines (73 loc) · 3.25 KB
/
Cube.h
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
#pragma once
#include "Vector.h"
#include "Point.h"
#include "Rotate.h"
#define PI 3.14159265
double cubeSideLength = 5;
double distanceFromViewer = 12;
double pointSpacing = 0.15;
int screenWidthInPixels = 30;
int screenHeightInPixels = 25;
double distanceOfScreenFromViewer = 3;
Vector lightSourceVector = Vector(0,-1,-0.5).normalize();
void render(double angleX, double angleY, double angleZ) {
std::vector<std::vector<double>> zDistances(screenHeightInPixels, std::vector<double>(screenWidthInPixels, 0));
std::vector<std::vector<char>> screen(screenHeightInPixels, std::vector<char>(screenWidthInPixels, ' '));
for(double i = -cubeSideLength; i < cubeSideLength; i += pointSpacing) {
for(double j = -cubeSideLength; j < cubeSideLength; j += pointSpacing) {
Points points;
points.emplace_back(i, j, -cubeSideLength);
points.emplace_back(i, j, cubeSideLength);
points.emplace_back(-cubeSideLength, i, j);
points.emplace_back(cubeSideLength, i, j);
points.emplace_back(i, -cubeSideLength, j);
points.emplace_back(i, cubeSideLength, j);
Vectors luminances = {
Vector(0,0,-1),
Vector(0,0,1),
Vector(-1,0,0),
Vector(1,0,0),
Vector(0,-1,0),
Vector(0,1,0),
};
for(int k = 0; k < 6; k++) {
Point current = points[k];
Point finalPoint = rotateOnAll3Axes(current, angleX, angleY, angleZ) + Point(0,0,distanceFromViewer);
double ooz = 1 / finalPoint.z;
// int projectedX = (int) (screenWidthInPixels / 2 + finalPoint.x * distanceOfScreenFromViewer * ooz);
// int projectedY = (int) (screenHeightInPixels / 2 - finalPoint.y * distanceOfScreenFromViewer * ooz);
int projectedX = (int)(screenWidthInPixels / 2 + finalPoint.x);
int projectedY = (int)(screenWidthInPixels / 2 + finalPoint.y);
Point luminance = Point(luminances[k].x,luminances[k].y,luminances[k].z);
luminance = rotateOnAll3Axes(luminance, angleX, angleY, angleZ);
Vector luminanceVector = Vector(luminance.x, luminance.y, luminance.z).normalize();
double luminanceProduct = dot(luminanceVector, lightSourceVector)+1;
if (luminanceProduct > 0) {
if (ooz > zDistances[projectedY][projectedX]) {
int luminanceIndex = (luminanceProduct * 6);
if(luminanceIndex == 12) luminanceIndex = 11;
zDistances[projectedY][projectedX] = ooz;
screen[projectedY][projectedX] = ".,-~:;=!*#$@"[luminanceIndex];
}
}
}
}
}
system("cls");
for (int i = 0; i < screenHeightInPixels; i++) {
for (int j = 0; j < screenWidthInPixels; j++) {
std::cout << screen[i][j];
}
putchar('\n');
}
}
void Cube() {
double X = 0, Y = 0, Z = 0;
double xIncrement=0.05, yIncrement=0.03, zIncrement=0.1;
while(true) {
render(X,Y,Z);
X += xIncrement;
Y += yIncrement;
Z += zIncrement;
}
}