-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_controller.cc
More file actions
192 lines (155 loc) · 4.87 KB
/
scene_controller.cc
File metadata and controls
192 lines (155 loc) · 4.87 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
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
#include "scene_controller.h"
#include "utils.h"
#include <stb_image.h>
#include <glm/matrix.hpp>
#include <glm/ext/matrix_common.hpp>
#include <glm/ext/matrix_clip_space.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/ext/matrix_transform.hpp>
// clang-format off
constexpr float vertexList[] = {
// back
1, 0, 0, 0, 1, 0, 0, 0, -1,
1, 1, 0, 0, 0, 0, 0, 0, -1,
0, 1, 0, 1, 0, 0, 0, 0, -1,
0, 0, 0, 1, 1, 0, 0, 0, -1,
// left
0, 0, 0, 0, 1, 0, -1, 0, 0,
0, 1, 0, 0, 0, 0, -1, 0, 0,
0, 1, 1, 1, 0, 0, -1, 0, 0,
0, 0, 1, 1, 1, 0, -1, 0, 0,
// front
0, 0, 1, 0, 1, 0, 0, 0, 1,
0, 1, 1, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 0, 0, 0, 1,
// right
1, 0, 1, 0, 1, 0, 1, 0, 0,
1, 1, 1, 0, 0, 0, 1, 0, 0,
1, 1, 0, 1, 0, 0, 1, 0, 0,
1, 0, 0, 1, 1, 0, 1, 0, 0,
// bottom
0, 0, 0, 0, 1, 0, 0, -1, 0,
0, 0, 1, 0, 0, 0, 0, -1, 0,
1, 0, 1, 1, 0, 0, 0, -1, 0,
1, 0, 0, 1, 1, 0, 0, -1, 0,
// top
0, 1, 1, 0, 1, 0, 0, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 0, 1, 0,
1, 1, 1, 1, 1, 0, 0, 1, 0,
};
// clang-format on
constexpr int indexList[] = {
// front
0, 1, 2,
0, 2, 3,
// left
4, 5, 6,
4, 6, 7,
// back
8, 9, 10,
8, 10, 11,
// right
12, 13, 14,
12, 14, 15,
// bottom
16, 17, 18,
16, 18, 19,
// top
20, 21, 22,
20, 22, 23};
SceneController::SceneController(CanvasType&& canvas) : width(canvas.Width()), height(canvas.Height()), renderer(std::move(canvas)), textureData(nullptr, stbi_image_free)
{
float fov = 55.f;
float aspectRatio = (float)width / (float)height;
float zNear = 0.01;
float zFar = 1000;
renderer.SetMultiSampleLevel(2);
projectionMatrix = glm::perspective(glm::radians(fov), aspectRatio, zNear, zFar);
renderer.ProjectionMatrix(projectionMatrix);
textureData.reset(stbi_load("sample_640x426.jpeg", &textureW, &textureH, &textureChannels, STBI_rgb_alpha));
if (!textureData)
{
throw std::runtime_error(stbi_failure_reason());
}
modelData = CudaNewManaged<RendererType::ModelDataType>();
modelData->SetIndexList({std::cbegin(indexList), std::cend(indexList)});
modelData->SetVertexList({std::cbegin(vertexList), std::cend(vertexList)});
modelData->SetVertexDescriptor({
{VertexAttributes::Position, VertexAttributeTypes::Vec3},
{VertexAttributes::TextureCoordinate, VertexAttributeTypes::Vec3},
{VertexAttributes::Normal, VertexAttributeTypes::Vec3},
});
pixelProgram = CudaNewManaged<PBRMaterial>();
pixelProgram->SetDiffuseMap(CudaUploadData<std::uint8_t>(textureData.get(), textureW * textureH * 4), textureW, textureH);
programCtx = renderer.LinkProgram(pixelProgram.get());
}
void SceneController::Render(float timeElapsed)
{
glm::vec3 cameraPos{0, 0, cameraDistance};
cameraPos = glm::eulerAngleYXZ(cubeRotation.x, cubeRotation.y, cubeRotation.z) * glm::vec4{cameraPos, 1};
auto viewTransform = (glm::lookAt(cameraPos, glm::vec3{0, 0, 0}, glm::vec3{0, 1, 0}));
auto scaleMatrix = glm::scale(glm::identity<glm::mat4>(), glm::vec3{1, 1, 1});
auto translateOriginMatrix = glm::translate(glm::identity<glm::mat4>(), glm::vec3{-0.5, -0.5, -0.5});
auto translateBackMatrix = glm::identity<glm::mat4>();
auto translateMatrix = glm::translate(glm::identity<glm::mat4>(), glm::vec3{0, 0, 0});
auto rotationMatrix = glm::eulerAngleXYZ(0.f, 0.f, 0.f);
auto modelTransform = translateMatrix * translateBackMatrix * rotationMatrix * scaleMatrix * translateOriginMatrix;
renderer.AddMesh(modelData.get(), programCtx);
renderer.ClearColorBuffer(argb(0xFF, 0xFF, 0xFF, 0xFF));
renderer.ClearZBuffer();
renderer.Canvas().SwapBuffer();
renderer.Draw(timeElapsed);
renderer.Canvas().AddText(0, 24, 12, std::format("x: {}, y: {}", mouseX, mouseY), 0xFFFFFFFF);
}
void SceneController::SetHWND(NativeWindowHandle hwnd)
{
this->hwnd = hwnd;
}
void SceneController::MouseDown()
{
if (hwnd != 0)
{
// SetCapture(hwnd);
}
mouseCaptured = true;
}
void SceneController::MouseUp()
{
if (hwnd != 0)
{
// ReleaseCapture();
}
mouseCaptured = false;
lastMouseX = -1;
lastMouseY = -1;
}
void SceneController::MouseWheel(int val)
{
cameraDistance += val / 100.f;
}
void SceneController::MouseMove(int x, int y)
{
mouseX = x;
mouseY = y;
if (!mouseCaptured)
{
return;
}
if (lastMouseX == -1)
{
lastMouseX = x;
}
if (lastMouseY == -1)
{
lastMouseY = y;
}
auto deltaX = x - lastMouseX;
auto deltaY = y - lastMouseY;
cubeRotation.x -= (float)deltaX * 2.0 / (float)width;
cubeRotation.y -= (float)deltaY * 2.0 / (float)height;
cubeRotation.y = std::clamp(cubeRotation.y, -89.99f, 89.99f);
lastMouseX = x;
lastMouseY = y;
}