forked from DaveDisI/Battery_Barrage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterrain_renderer.cpp
More file actions
145 lines (123 loc) · 4.82 KB
/
terrain_renderer.cpp
File metadata and controls
145 lines (123 loc) · 4.82 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
#include "terrain_renderer.h"
void prepareTerrainRenderer(TerrainRenderer* terrainRenderer){
RenderDevice* device = terrainRenderer->renderDevice;
device->bindShader(&terrainRenderer->shader);
device->bindVertexBuffer(&terrainRenderer->vertexBuffer);
device->bindIndexBuffer(&terrainRenderer->indexBuffer);
device->bindVertexUniformBuffer(&terrainRenderer->uniformBuffer);
device->enableDepthTesting(true);
}
void renderTerrain(TerrainRenderer* terrainRenderer, Camera* camera){
RenderDevice* device = terrainRenderer->renderDevice;
TerrainUniforms* unis = (TerrainUniforms*)device->getPointerToBufferData(&terrainRenderer->uniformBuffer);
//TODO: get rid of hard coded numbers for width and height
Matrix4 proj = createPerpectiveProjectionMatrix(70.0, 1280.0 / 720.0, 0.001, 1000.0);
unis->lightPosition = Vector3(0, 0, 0);
Matrix4 viewMatrix = createIdentityMatrix();
viewMatrix.m[3][0] = camera->position.x;
viewMatrix.m[3][1] = camera->position.y;
viewMatrix.m[3][2] = camera->position.z;
viewMatrix = multiply(quaternionToMatrix4(camera->orientation), viewMatrix);
Vector3 newUp(viewMatrix.m[0][1], viewMatrix.m[1][1], viewMatrix.m[2][1]);
Vector3 newRight(viewMatrix.m[0][0], viewMatrix.m[1][0], viewMatrix.m[2][0]);
Vector3 newForward(-viewMatrix.m[0][2], -viewMatrix.m[1][2], -viewMatrix.m[2][2]);
normalize(&newUp);
normalize(&newRight);
normalize(&newForward);
camera->up = newUp;
camera->right = newRight;
camera->forward = newForward;
Matrix4 mvMatrix = multiply(proj, viewMatrix);
unis->projectionViewMatrix = mvMatrix;
device->drawIndices(0, terrainRenderer->totalIndices, RENDERER_INDEX_TYPE_U16, RENDER_DRAW_MODE_TRIANGLES);
}
void initializeTerrainRenderer(OSDevice* osDevice, RenderDevice* renderDevice, TerrainRenderer* terrainRenderer){
terrainRenderer->osDevice = osDevice;
terrainRenderer->renderDevice = renderDevice;
u32 totalVertices = 40000;
f32 width = sqrt(totalVertices);
u32 totalRects = (u32)(width - 1) * (u32)(width - 1);
u32 totalFloats = totalVertices * 6;
u32 totalIndices = totalRects * 6;
f32* verts = new f32[totalFloats];
u16* elms = new u16[totalIndices];
u32 ctr = 0;
f32 x = -width / 2;
f32 z = -width / 2;
f32 s = 1;
for(int i = 0; i < totalVertices; i++){
float sinX = sin(x);
float sinZ = sin(z);
float cosX = cos(x);
float cosZ = cos(z);
verts[ctr++] = x; verts[ctr++] = sinX + sinZ; verts[ctr++] = z;
x += s;
if(x >= width / 2){
x = -width / 2;
z += s;
}
Vector3 v1;
Vector3 v2;
if(cosX == 0){
v1 = Vector3(0, 1, 0);
}else{
v1 = normalize(Vector3(1, 1 / cosX, 0));
}
if(cosZ == 0){
v2 = Vector3(0, 1, 0);
}else{
v2 = normalize(Vector3(0, 1 / cosZ, 1));
}
Vector3 norm = normalize(v1 + v2);
verts[ctr++] = norm.x;
verts[ctr++] = norm.y;
verts[ctr++] = norm.z;
terrainRenderer->totalVertices++;
}
ctr = 0;
u32 elNum = 0;
int rc = 0;
for(int i = 0; i < totalRects; i++){
elms[ctr++] = elNum;
elms[ctr++] = elNum + 1;
elms[ctr++] = elNum + width;
elms[ctr++] = elNum + width;
elms[ctr++] = elNum + 1;
elms[ctr++] = elNum + width + 1;
rc++;
if(rc == (u32)width - 1){
rc = 0;
elNum += 2;
}else{
elNum++;
}
terrainRenderer->totalIndices += 6;
}
renderDevice->createBufferWithData(&terrainRenderer->vertexBuffer, verts, sizeof(f32) * totalFloats, 0);
renderDevice->createBufferWithData(&terrainRenderer->indexBuffer, elms, sizeof(u16) * totalIndices, 1);
renderDevice->createBuffer(&terrainRenderer->uniformBuffer, sizeof(TerrainUniforms), 2);
delete[] verts;
delete[] elms;
RendererVertexFormat rvf[] = {
RENDERER_VERTEX_FORMAT_F32x3, RENDERER_VERTEX_FORMAT_F32x3
};
u32 elemSizes[] = {
sizeof(float), sizeof(float)
};
u32 bufOffs[] = {
0, sizeof(float) * 3
};
VertexBufferDescriptor vbd;
vbd.totalAttributes = 2;
vbd.rendererVertexFormats = rvf;
vbd.attributeElementSizes = elemSizes;
vbd.attributeBufferOffsets = bufOffs;
s8* shaderText;
u64 fileLength;
const s8* pth = osDevice->getPathFromExecutable("terrain_shader.metal");
osDevice->readTextFile(pth, &shaderText, &fileLength);
delete[] pth;
renderDevice->createShaderFromString(&terrainRenderer->shader, (const char*)shaderText, "vertexShader", "fragmentShader", &terrainRenderer->vertexBuffer, &vbd);
renderDevice->bindShader(&terrainRenderer->shader);
renderDevice->bindVertexBuffer(&terrainRenderer->vertexBuffer);
}