-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.cpp
199 lines (166 loc) · 6.05 KB
/
demo.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
#include <iostream>
#include <string>
#include <vector>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#include <imgui/imgui.h>
#include <imgui/imgui_impl_glfw.h>
#include <imgui/imgui_impl_opengl3.h>
#include <core/clock.hpp>
#include <core/controls.hpp>
#include <core/mouse.hpp>
#include <orc/cube.hpp>
#include <orc/cubemap.hpp>
#include <orc/light.hpp>
#include <orc/model.hpp>
#include <orc/object.hpp>
#include <orc/scene.hpp>
#include <orc/skybox.hpp>
int kill(const char *message)
{
std::cerr << message << std::endl;
glfwTerminate();
return -1;
}
// Set OpenGL version and core profile
void initializeGLFW()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
void initializeDebugUi(GLFWwindow *window)
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui::GetIO().IniFilename = "var/imgui.ini";
ImGui::GetIO().LogFilename = "var/log/imgui.log";
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
}
const GLFWvidmode *getVideoMode()
{
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
return glfwGetVideoMode(monitor);
}
GLFWwindow *openWindowedFullscreenWindow(const char *title)
{
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
return glfwCreateWindow(mode->width, mode->height, title, monitor, NULL);
}
void buildDebugUi(const orc::Scene &scene)
{
glm::vec3 cameraPos = scene.GetCamera().GetPosition();
ImGui::Begin("Debug Window");
ImGui::Text("Camera Position: (%.1f, %.1f, %.1f)", cameraPos.x, cameraPos.y, cameraPos.z);
ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate);
ImGui::End();
}
void cleanupDebugUi()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
int main()
{
srand(time(nullptr)); // Initialize RNG
initializeGLFW();
GLFWwindow *window = openWindowedFullscreenWindow("Grafix Demo");
if (window == NULL)
{
return kill("Failed to create window");
}
glfwMakeContextCurrent(window);
initializeDebugUi(window);
int windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
return kill("Failed to initialize GLAD");
}
// TODO: Engine-supported API for initialization of graphics/rendering APIs
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_FRAMEBUFFER_SRGB); // Enable hardware-implemented gamma correction
glEnable(GL_CULL_FACE); // Enable face culling (don't render faces that are facing away from us)
glCullFace(GL_BACK);
mouse::listenForMovement(window);
orc::Scene scene;
scene.SetSkybox(std::make_unique<orc::Skybox>(std::make_unique<orc::CubemapRef>(
"data/textures/skybox/right.jpg",
"data/textures/skybox/left.jpg",
"data/textures/skybox/top.jpg",
"data/textures/skybox/bottom.jpg",
"data/textures/skybox/front.jpg",
"data/textures/skybox/back.jpg"
)));
scene.GetCamera().SetAspectRatio((float)windowWidth/(float)windowHeight);
scene.GetCamera().Translate(13, 1.5, 0);
scene.GetCamera().Rotate(glm::radians(90.0f), 0, 0);
// This model is not checked into source control for practical reasons
std::shared_ptr<orc::Object> object = orc::LoadModel("data/models/sponza_scene/scene.gltf");
object->Scale(1.5, 1.5, 1.5);
scene.GetRoot().AttachChild(object);
std::shared_ptr<orc::Object> object2 = orc::LoadModel("data/models/legion_commander/scene.gltf");
object2->Scale(0.01, 0.01, 0.01);
object2->Translate(0, 0.05, 0);
object2->Rotate(glm::radians(90.0f), 0, 0);
scene.GetRoot().AttachChild(object2);
std::shared_ptr<orc::SpotLight> flash = orc::SpotLight::Create();
Clock clock;
KeyboardMouseControls ctrl(*window, 0.2f);
bool isFlashlightOn = false;
while(!glfwWindowShouldClose(window))
{
// Notify ImGui of each frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
clock.tick();
ctrl.newFrame(clock.getElapsedSeconds());
// TODO: Build orientation-specific translation into Node API (e.g. TranslateFront())
glm::vec3 cameraTranslation = 6.0f * (
scene.GetCamera().GetFront() * (float)ctrl.getValue(Controls::Signal::moveY) +
scene.GetCamera().GetRight() * (float)ctrl.getValue(Controls::Signal::moveX)
);
glm::vec3 cameraRotation(
-glm::radians(ctrl.getValue(Controls::Signal::aimX)),
glm::radians(ctrl.getValue(Controls::Signal::aimY)),
0
);
scene.GetCamera().Translate(cameraTranslation.x, cameraTranslation.y, cameraTranslation.z);
scene.GetCamera().Rotate(cameraRotation.x, cameraRotation.y, cameraRotation.z);
if (ctrl.isLeading(Controls::Signal::exit)) glfwSetWindowShouldClose(window, true);
if (ctrl.isLeading(Controls::Signal::action2))
{
isFlashlightOn = !isFlashlightOn;
if (isFlashlightOn) scene.GetCamera().AttachChild(flash);
else flash->Detach();
}
// Fill background color first
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
scene.Update();
scene.Draw();
// Render the debug UI
buildDebugUi(scene);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwPollEvents();
}
cleanupDebugUi();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}