-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGL3D.cpp
More file actions
277 lines (226 loc) · 6.88 KB
/
GL3D.cpp
File metadata and controls
277 lines (226 loc) · 6.88 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
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
#include "Model.hpp"
void GLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
std::cerr << "OpenGL ";
switch (severity) {
case GL_DEBUG_SEVERITY_HIGH:
std::cerr << "High"; break;
case GL_DEBUG_SEVERITY_MEDIUM:
std::cerr << "Medium"; break;
case GL_DEBUG_SEVERITY_LOW:
std::cerr << "Low"; break;
case GL_DEBUG_SEVERITY_NOTIFICATION:
std::cerr << "Info"; break;
default: break;
}
std::cerr << ", ";
switch (type) {
case GL_DEBUG_TYPE_ERROR:
std::cerr << "Error"; break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
std::cerr << "Deprecation"; break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
std::cerr << "Undefined behaviour/bug"; break;
case GL_DEBUG_TYPE_PERFORMANCE:
std::cerr << "Performance"; break;
case GL_DEBUG_TYPE_PORTABILITY:
std::cerr << "Portability"; break;
default:
std::cerr << "Unknown (" << type << ")";
}
std::cerr << ", " << message << " (" << id << ")" << std::endl;
}
static float CameraXOffset = 0.0f;
static float CameraYOffset = 0.0f;
static float CameraZOffset = 0.0f;
static float SPEED = 0.05;
static bool closeWindow = false;
void KeyCallback(GLFWwindow* aWindow, int aKey, int aScancode, int aAction, int aModifiers) {
if(aAction == GLFW_RELEASE || glfwGetInputMode(aWindow, GLFW_CURSOR) == GLFW_CURSOR_NORMAL) return;
switch(aKey) {
case GLFW_KEY_ESCAPE:
glfwSetInputMode(aWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
break;
case GLFW_KEY_W:
CameraZOffset -= SPEED;
break;
case GLFW_KEY_A:
CameraXOffset -= SPEED;
break;
case GLFW_KEY_S:
CameraZOffset += SPEED;
break;
case GLFW_KEY_D:
CameraXOffset += SPEED;
break;
case GLFW_KEY_R:
CameraYOffset += SPEED;
break;
case GLFW_KEY_F:
CameraYOffset -= SPEED;
break;
case GLFW_KEY_Q:
CameraXOffset = 0.0f;
CameraYOffset = 0.0f;
CameraZOffset = 0.0f;
break;
case GLFW_KEY_X:
closeWindow = true;
break;
}
}
const static double FOV = 45.0;
static bool FirstMove = true;
static double LastX = 400, LastY = 400;
static double Pitch = 0.0;
static double Yaw = -90.0; //so we start oriented correctly: 0 is to the right of X axis, 90 is back
glm::vec3 Direction;
void MouseCallback(GLFWwindow* aWindow, double aX, double aY) {
if(glfwGetInputMode(aWindow, GLFW_CURSOR) == GLFW_CURSOR_NORMAL) return;
if(FirstMove) {
LastX = aX;
LastY = aY;
FirstMove = false;
}
double DX = aX - LastX;
double DY = LastY - aY;
LastX = aX;
LastY = aY;
double Sensitivity = 0.1f;
DX *= Sensitivity;
DY *= Sensitivity;
Yaw += DX;
Pitch += DY;
//limits
if(Pitch > 89.0f) {
Pitch = 89.0f;
}
if(Pitch < -89.0f) {
Pitch = -89.0f;
}
Direction = glm::vec3();
Direction.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Direction.y = sin(glm::radians(Pitch));
Direction.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Direction = glm::normalize(Direction);
}
void MouseKeyCallback(GLFWwindow* aWindow, int aKey, int aAction, int aModifiers) {
if(aKey == GLFW_MOUSE_BUTTON_RIGHT && aAction == GLFW_PRESS) {
std::cout << "Window clicked!";
glfwSetInputMode(aWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
return;
}
}
int main() {
if(glfwInit() != GLFW_TRUE) {
std::cerr << "GLFW init fail! " << glfwGetError(NULL) << "\n";
return -1;
};
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
GLFWwindow* window = glfwCreateWindow(800, 800, "OpenGL 3D", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, KeyCallback);
glfwSetMouseButtonCallback(window, MouseKeyCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, MouseCallback);
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cerr << "GLAD failed to initialize. \n";
return -1;
}
glDebugMessageCallback(GLDebugCallback, nullptr);
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glEnable(GL_BLEND); //texture blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST); //depth testing
glDepthFunc(GL_LESS);
/*
glEnable(GL_CULL_FACE); //backface culling
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
*/
glEnable(GL_MULTISAMPLE); //anti aliasing
//imgui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 450 core");
Model m(std::filesystem::path("./T3.glb"));
GLint samplers[] = {
0, 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
};
Shader sj("vertJoint.glsl", "fragJoint.glsl");
sj.bind();
Shader s("vertBase.glsl", "fragBase.glsl");
s.bind();
glUniform1iv(16, 32, &samplers[0]);
Shader sa("vertAnim.glsl", "fragAnim.glsl");
sa.bind();
glUniform1iv(16, 32, &samplers[0]);
glm::mat4 matrix;
glm::mat4 proj = glm::mat4(1.0f);
proj = glm::perspective(glm::radians((float)FOV), 800.0f/800.0f, 0.1f, 1000.0f);
//proj = glm::ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 1000.0f);
glm::mat4 view = glm::mat4(1.0f);
bool renderBase = false;
bool overrideAnimTime = false;
bool allAnims = false;
float animTime = 0.0;
int animId = 0;
bool joints = false;
while (!glfwWindowShouldClose(window) && !closeWindow) {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//camera
glm::vec3 camera_pos = glm::vec3(CameraXOffset, CameraYOffset, CameraZOffset);
view = glm::lookAt(camera_pos, camera_pos + Direction, glm::vec3(0.0, 1.0, 0.0));
matrix = proj * view;
if(!overrideAnimTime) {
animTime = std::fmod(glfwGetTime(), m.getAnimationMaxTime(animId));
}
if(allAnims) {
m.setStateAtTimeAll(animTime);
}
else {
m.setStateAtTime(animId, animTime);
}
if(renderBase) {
//base model
s.bind();
m.draw(matrix, true);
}
//animated model
sa.bind();
m.draw(matrix);
if(joints) {
sj.bind();
m.drawBones(matrix);
}
//gui for control and debugging
ImGui::Begin("Anim control");
//when changing id everything breaks
ImGui::SliderInt("ID of animation", &animId, 0, m.getAnimationAmount()-1);
ImGui::SliderFloat("Speed of camera", &SPEED, 0, 1.0);
ImGui::Checkbox("Render base model", &renderBase);
ImGui::Checkbox("Override time", &overrideAnimTime);
ImGui::Checkbox("Update all animations", &allAnims);
ImGui::Checkbox("Joints", &joints);
ImGui::SliderFloat("Anim seconds", &animTime, 0, allAnims ? 5 : m.getAnimationMaxTime(animId));
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}