-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
158 lines (148 loc) · 5.71 KB
/
main.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
/* ---------------------- Includes ---------------------- */
#include <vector>
#include "RasterSurface.h"
#include "RasterUtil.h"
#include "camera.h"
#include "fwd.h"
#include "geometry.h"
#include "greendragon.h"
#include "shaders.h"
#include "vecs.h"
/* ----------------------- Globals ---------------------- */
#pragma region Globals
const unsigned long rasterWidth = 400;
const unsigned long rasterHeight = 200;
RASTER *onlyRaster = new RASTER(&rasterWidth, &rasterHeight);
float ZeroMatrix[4][4]{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
float IdentityMatrix[4][4]{
{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
#pragma endregion
/*************
* MAIN BODY *
*************/
int main() {
RS_Initialize(rasterWidth, rasterHeight);
/* ------------------- Initialize Grid ------------------ */
#pragma region Grid
VECTOR_3 gridVerts[44] = {
VECTOR_3{0, 0, 0, 0, 0},
};
std::vector<EDGE *> gridEdges;
for (int i = 0; i < 11; i++) {
// Left edge.
gridVerts[i].x = -0.5f;
gridVerts[i].z = float(i) / 10.0f - 0.5f;
gridVerts[i].w = 1;
gridVerts[i].col = 0xFFFFFFFF;
// Right edge.
gridVerts[i + 22].x = 0.5f;
gridVerts[i + 22].z = float(i) / 10.0f - 0.5f;
gridVerts[i + 22].w = 1;
gridEdges.push_back(new EDGE(&gridVerts[i], &gridVerts[i + 22]));
gridVerts[i + 22].col = 0xFFFFFFFF;
// Top edge.
gridVerts[i + 11].x = float(i) / 10.0f - 0.5f;
gridVerts[i + 11].z = 0.5f;
gridVerts[i + 11].w = 1;
gridVerts[i + 11].col = 0xFFFFFFFF;
// Bottom edge.
gridVerts[i + 33].x = float(i) / 10.0f - 0.5f;
gridVerts[i + 33].z = -0.5f;
gridVerts[i + 33].w = 1;
gridVerts[i + 33].col = 0xFFFFFFFF;
gridEdges.push_back(new EDGE(&gridVerts[i + 11], &gridVerts[i + 33]));
}
MESH *gridMesh = new MESH(gridEdges, &IdentityMatrix[0][0]);
gridMesh->SetWorldMatrix(&IdentityMatrix[0][0]);
#pragma endregion;
/* ------------------- Initialize Cube ------------------ */
#pragma region Cube
VECTOR_3 cubeVerts[8];
std::vector<EDGE *> cubeEdges;
unsigned int red = 0xFFFF0000;
unsigned int green = 0xFF00FF00;
unsigned int blue = 0xFF0000FF;
unsigned int yellow = 0xFFFFFF00;
// Bottom verts.
cubeVerts[0] = VECTOR_3{-0.25, -0.25, -0.25, 1, 0.0f, 1.0f, green};
cubeVerts[1] = VECTOR_3{0.25, -0.25, -0.25, 1, 1.0f, 1.0f, green};
cubeVerts[2] = VECTOR_3{-0.25, -0.25, 0.25, 1, 1.0f, 1.0f, green};
cubeVerts[3] = VECTOR_3{0.25, -0.25, 0.25, 1, 0.0f, 1.0f, green};
cubeEdges.push_back(new EDGE(&cubeVerts[0], &cubeVerts[1]));
cubeEdges.push_back(new EDGE(&cubeVerts[0], &cubeVerts[2]));
cubeEdges.push_back(new EDGE(&cubeVerts[3], &cubeVerts[1]));
cubeEdges.push_back(new EDGE(&cubeVerts[3], &cubeVerts[2]));
// Top verts.
cubeVerts[4] = VECTOR_3{-0.25, 0.25, -0.25, 1, 0.0f, 0.0f, green};
cubeVerts[5] = VECTOR_3{0.25, 0.25, -0.25, 1, 1.0f, 0.0f, green};
cubeVerts[6] = VECTOR_3{-0.25, 0.25, 0.25, 1, 1.0f, 0.0f, green};
cubeVerts[7] = VECTOR_3{0.25, 0.25, 0.25, 1, 0.0f, 0.0f, green};
cubeEdges.push_back(new EDGE(&cubeVerts[4], &cubeVerts[5]));
cubeEdges.push_back(new EDGE(&cubeVerts[4], &cubeVerts[6]));
cubeEdges.push_back(new EDGE(&cubeVerts[7], &cubeVerts[5]));
cubeEdges.push_back(new EDGE(&cubeVerts[7], &cubeVerts[6]));
// Side edges.
cubeEdges.push_back(new EDGE(&cubeVerts[0], &cubeVerts[4]));
cubeEdges.push_back(new EDGE(&cubeVerts[1], &cubeVerts[5]));
cubeEdges.push_back(new EDGE(&cubeVerts[2], &cubeVerts[6]));
cubeEdges.push_back(new EDGE(&cubeVerts[3], &cubeVerts[7]));
// Compose mesh.
MESH *cubeMesh = new MESH(cubeEdges, &IdentityMatrix[0][0]);
cubeMesh->GetWorldMatrix()->Translate(0, 0.25, 0);
// Front quad.
cubeMesh->TrisFromQuad(&cubeVerts[4], &cubeVerts[5], &cubeVerts[0],
&cubeVerts[1], green);
// Back quad.
cubeMesh->TrisFromQuad(&cubeVerts[2], &cubeVerts[3], &cubeVerts[6],
&cubeVerts[7], green);
// Right quad.
cubeMesh->TrisFromQuad(&cubeVerts[5], &cubeVerts[7], &cubeVerts[1],
&cubeVerts[3], red);
// Left quad.
cubeMesh->TrisFromQuad(&cubeVerts[4], &cubeVerts[6], &cubeVerts[0],
&cubeVerts[2], blue);
// Bottom quad.
cubeMesh->TrisFromQuad(&cubeVerts[0], &cubeVerts[1], &cubeVerts[2],
&cubeVerts[3], yellow);
// Top quad.
cubeMesh->TrisFromQuad(&cubeVerts[4], &cubeVerts[5], &cubeVerts[6],
&cubeVerts[7], yellow);
cubeMesh->SetTexture(&greendragon_pixels[0], greendragon_width,
greendragon_height);
float cubeAngle = 0.0f;
#pragma endregion
/* ------------------ Initialize Camera ----------------- */
#pragma region Camera
CAMERA *camera = new CAMERA();
camera->SetViewMatrix(&IdentityMatrix[0][0]);
camera->GetViewMatrix()->DRotate(0, -18);
camera->GetViewMatrix()->Translate(0, 0.4, -1);
camera->Invert();
camera->SetAspectRatio(rasterWidth, rasterHeight);
camera->SetFOV(90);
camera->SetProjMatrix(0.1f, 10.0f);
#pragma endregion
/* ---------------------- Core Loop --------------------- */
do {
onlyRaster->CleanZ();
onlyRaster->ClearRaster(0xFF000000);
// cubeAngle += 0.165f;
cubeAngle += 10.165f;
MESH *gridCopy = new MESH(*gridMesh);
MESH *cubeCopy = new MESH(*cubeMesh);
cubeCopy->GetLocalMatrix()->DRotate(1, cubeAngle);
ShaderUtil::VS_ProjectEdges(*gridCopy, camera);
ShaderUtil::VS_ProjectFaces(*cubeCopy, camera);
gridCopy->RenderWireframe(onlyRaster);
cubeCopy->RenderFaces(onlyRaster);
delete gridCopy;
delete cubeCopy;
onlyRaster->EvaluateZ(camera);
} while (RS_Update(onlyRaster->GetSurface(), onlyRaster->GetArea()));
RS_Shutdown();
delete gridMesh;
delete cubeMesh;
delete camera;
delete onlyRaster;
return 0;
}