-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
307 lines (258 loc) · 9.98 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
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
301
302
303
304
305
306
307
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtx/euler_angles.hpp>
#include "AttributeInfo.h"
#include "ShaderProgram.h"
#include "BaseApp.h"
#include "Texture.h"
#include "VertexBuffer.h"
#if __EMSCRIPTEN__
#include <emscripten.h>
#endif
struct DemoVertex {
glm::vec3 position;
glm::vec2 texCoord;
};
struct DemoApp : public BaseApp
{
ShaderProgram *defaultProgram = NULL;
Texture *backgroundTex = NULL;
VertexBuffer *backgroundVBO = NULL;
Texture *mikeTex = NULL;
VertexBuffer *mikeVBO = NULL;
bool useOrtho = false;
bool useFrontToBack = true;
float fieldOfView = 45.0f;
glm::vec2 vanishPoint = glm::vec3(0.0f);
glm::vec3 mikePosition = glm::vec3(0.0f);
glm::vec3 mikeRotation = glm::vec3(0.0f);
glm::vec3 mikeScale = glm::vec3(1.0f);
glm::vec3 mikeCenterPoint = glm::vec3(0.0f);
std::vector<DemoVertex> backgroundVertices = {
//{ X Y Z } { S T }
{ { 0.0f, 0.0f, 0.0f}, {0.0f, 0.0f} },
{ {0.0f, 600.0f, 0.0f}, {0.0f, 1.0f} },
{ {800.0f, 0.0f, 0.0f}, {1.0f, 0.0f} },
{ {800.0f, 600.0f, 0.0f}, {1.0f, 1.0f} },
};
std::vector<DemoVertex> mikeVertices = {
//{ X Y Z } { S T }
{ { 0.0f, 0.0f, 0.0f}, {0.0f, 0.0f} },
{ {0.0f, 512.0f, 0.0f}, {0.0f, 1.0f} },
{ {512.0f, 0.0f, 0.0f}, {1.0f, 0.0f} },
{ {512.0f, 512.0f, 0.0f}, {1.0f, 1.0f} },
};
glm::mat4 projectionMatrix;
glm::mat4 modelMatrix;
GLint u_MVP = 0;
GLint u_texture0 = 0;
virtual bool userInit() override
{
Shader defaultVert("assets/default.vert");
if (defaultVert.compile() != 0)
{
return false;
}
Shader defaultFrag("assets/default.frag");
if (defaultFrag.compile() != 0)
{
return false;
}
std::vector<AttributeInfo> defaultAttributes = {
{"a_position", AttributeInfo::Float, 3},
{"a_texCoord0", AttributeInfo::Float, 2},
};
defaultProgram = new ShaderProgram(defaultAttributes);
defaultProgram->attach(&defaultVert);
defaultProgram->attach(&defaultFrag);
if (defaultProgram->link() != 0)
{
return false;
}
u_MVP = defaultProgram->getUniformLocation("u_MVP");
u_texture0 = defaultProgram->getUniformLocation("u_texture0");
backgroundTex = new Texture("assets/background.jpg");
if (backgroundTex->decode() != 0)
{
return false;
}
backgroundVBO = new VertexBuffer();
backgroundVBO->upload(backgroundVertices, VertexBuffer::Static);
mikeTex = new Texture("assets/mike.png");
if (mikeTex->decode() != 0)
{
return false;
}
mikeVBO = new VertexBuffer();
mikeVBO->upload(mikeVertices, VertexBuffer::Static);
// Vanish point initially the center of the screen
vanishPoint.x = displayWidth * 0.5f;
vanishPoint.y = displayHeight * 0.5f;
// center initially at the center of mike
mikeCenterPoint.x = 256.0f;
mikeCenterPoint.y = 256.0f;
// mike initially at the top left corer of the screen
mikePosition.x = 256.0f;
mikePosition.y = 256.0f;
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
return true;
}
virtual void userShutdown() override
{
delete defaultProgram;
defaultProgram = NULL;
delete backgroundTex;
backgroundTex = NULL;
delete backgroundVBO;
backgroundVBO = NULL;
delete mikeTex;
mikeTex = NULL;
delete mikeVBO;
mikeVBO = NULL;
}
void drawMike()
{
// Draw mike
// set vanishing point
if (!useOrtho) {
float vpx = vanishPoint.x;
float vpy = displayHeight - vanishPoint.y;
projectionMatrix[2][0] = (2.0f * vpx / displayWidth) - 1.0f;
projectionMatrix[2][1] = (2.0f * vpy / displayHeight) - 1.0f;
}
defaultProgram->setUniform(u_MVP, projectionMatrix * modelMatrix);
mikeVBO->bind(defaultProgram);
mikeTex->bind();
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)mikeVertices.size());
}
void drawBackground()
{
// Draw the background
defaultProgram->setUniform(u_MVP, projectionMatrix); // No Model transforms for the background.
backgroundVBO->bind(defaultProgram);
backgroundTex->bind();
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)backgroundVertices.size());
}
virtual void userRender() override
{
if (useOrtho)
{
projectionMatrix = glm::ortho(0.0f, (float)displayWidth, (float)displayHeight, 0.0f, -8000.0f, 8000.0f);
}
else
{
float fieldOfViewRad = glm::radians(fieldOfView);
float cameraDistance = ((float)displayHeight/2.0f) / tan(fieldOfViewRad/2.0f);
float aspectRatio = (float)displayWidth / (float)displayHeight;
projectionMatrix = glm::perspective(fieldOfViewRad, aspectRatio, 0.1f, cameraDistance+512.0f);
projectionMatrix = glm::rotate(projectionMatrix, glm::radians(180.0f), glm::vec3(1, 0, 0));
projectionMatrix = glm::translate(projectionMatrix, glm::vec3(-displayWidth*0.5f, -displayHeight*0.5f, cameraDistance));
}
// Order matters. We use TRSC (Translate, Rotate, Scale, Center)
modelMatrix = glm::translate(glm::identity<glm::mat4>(), mikePosition);
modelMatrix *= glm::yawPitchRoll(glm::radians(mikeRotation.y), glm::radians(mikeRotation.x), glm::radians(mikeRotation.z));
modelMatrix = glm::scale(modelMatrix, mikeScale);
modelMatrix = glm::translate(modelMatrix, -mikeCenterPoint);
defaultProgram->bind();
defaultProgram->setUniform(u_texture0, 0);
if (useFrontToBack) {
glEnable(GL_DEPTH_TEST);
projectionMatrix[2][2] = 0.0f;
projectionMatrix[3][2] = 0.0f;
drawMike();
projectionMatrix[3][2] = 0.1f;
drawBackground();
} else {
glDisable(GL_DEPTH_TEST);
drawBackground();
drawMike();
}
}
virtual void userRenderUI() override
{
char buf[32];
ImGui::NewFrame();
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(displayWidth, displayHeight), ImGuiCond_Always);
ImGui::Begin("VanishPointGizmo", NULL, ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoInputs);
ImGui::GetWindowDrawList()->AddLine(ImVec2(vanishPoint.x, vanishPoint.y-16), ImVec2(vanishPoint.x, vanishPoint.y+16), IM_COL32(255, 0, 0, 255), 3.0f);
ImGui::GetWindowDrawList()->AddLine(ImVec2(vanishPoint.x-16, vanishPoint.y), ImVec2(vanishPoint.x+16, vanishPoint.y), IM_COL32(255, 0, 0, 255), 3.0f);
ImGui::End();
ImGui::Begin("Control Panel");
ImGui::Checkbox("Use Orthographic Projection", &useOrtho);
ImGui::Checkbox("Front to Back", &useFrontToBack);
ImGui::SliderFloat("Field of View", &fieldOfView, 0, 180);
ImGui::SliderFloat("Vanish Point X", &vanishPoint.x, 0, displayWidth);
ImGui::SliderFloat("Vanish Point Y", &vanishPoint.y, 0, displayHeight);
if (ImGui::TreeNode("Projection Matrix Viewer"))
{
if (ImGui::BeginTable("Projection", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg))
{
for (int row = 0; row < 4; row++)
{
ImGui::TableNextRow();
for (int column = 0; column < 4; column++)
{
ImGui::TableSetColumnIndex(column);
snprintf(buf, sizeof(buf), "%f", projectionMatrix[row][column]);
ImGui::TextUnformatted(buf);
}
}
ImGui::EndTable();
}
ImGui::TreePop();
}
if (ImGui::TreeNode("Model Matrix Viewer"))
{
if (ImGui::BeginTable("Model", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg))
{
for (int row = 0; row < 4; row++)
{
ImGui::TableNextRow();
for (int column = 0; column < 4; column++)
{
ImGui::TableSetColumnIndex(column);
snprintf(buf, sizeof(buf), "%f", modelMatrix[row][column]);
ImGui::TextUnformatted(buf);
}
}
ImGui::EndTable();
}
ImGui::TreePop();
}
ImGui::SliderFloat("Translate X", &mikePosition.x, -displayWidth, displayWidth);
ImGui::SliderFloat("Translate Y", &mikePosition.y, -displayWidth, displayWidth);
ImGui::SliderFloat("Translate Z", &mikePosition.z, -8000.0f, 8000.0f);
ImGui::SliderFloat("Rotate X", &mikeRotation.x, 0, 360);
ImGui::SliderFloat("Rotate Y", &mikeRotation.y, 0, 360);
ImGui::SliderFloat("Rotate Z", &mikeRotation.z, 0, 360);
ImGui::SliderFloat("Scale X", &mikeScale.x, -2, 4);
ImGui::SliderFloat("Scale Y", &mikeScale.y, -2, 4);
ImGui::SliderFloat("Center X", &mikeCenterPoint.x, 0, 512);
ImGui::SliderFloat("Center Y", &mikeCenterPoint.y, 0, 512);
ImGui::SliderFloat("Center Z", &mikeCenterPoint.z, 0, 512);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
ImGui::Render();
}
};
DemoApp app;
void step() {
app.step();
}
int main(int argc, char *argv[])
{
if (app.setup("ProjectionTester", 800, 600) == 0)
{
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(step, 0, 1);
#else
while(app.isRunning)
{
step();
}
#endif
app.teardown();
}
}