-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
348 lines (332 loc) · 12.7 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <stb_image.h>
#include <GLContext.h>
#include <Model/SpineModel.h>
#include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <ResourceManager/ShaderManager.h>
#include <ResourceManager/TextureManager.h>
#include <ResourceManager/VertexArrayManager.h>
#include <ResourceManager/ModelManager.h>
#include <Model/MeshModel.h>
#include <Camera.h>
// window
GLContext context;
int SCR_WIDTH = 1600;
int SCR_HEIGHT = 900;
double SCALE_X = 1.0f;
double SCALE_Y = 1.0f;
// mouse
double mouse_xpos = SCR_WIDTH / 2.0f;
double mouse_ypos = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
// fps
float deltaTime = 0.0f; // 当前帧与上一帧的时间差
float lastFrame = 0.0f; // 上一帧的时间
int FPS;
// camera
Camera camera(glm::vec3(0.5f, 10.0f, 0.5f));
// test
bool TESTENV = true;
// 回调函数
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mousebutton_callback(GLFWwindow* window, int button, int action, int mods);
void cursorpos_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
//工具函数
glm::vec4 screenToWorld(double mouse_xpos, double mouse_ypos);
// 初始化OpenGL
bool InitOpenGL(){
if (!GLContext::initGLFW()) {
return false;
}
context.create(SCR_WIDTH, SCR_HEIGHT);
//把OpenGL的函数指针导入给GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
printf("Failed to initialize GLAD");
return false;
}
//测试环境开启鼠标捕获
if (TESTENV) {
//设置鼠标捕获
glfwSetInputMode(context.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
//设置各种回调
glfwSetKeyCallback(context.getWindow(), key_callback);
glfwSetMouseButtonCallback(context.getWindow(), mousebutton_callback);
glfwSetCursorPosCallback(context.getWindow(), cursorpos_callback);
glfwSetScrollCallback(context.getWindow(), scroll_callback);
//开启深度测试
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
//开启模板测试
glEnable(GL_STENCIL_TEST);
//开启混合
glEnable(GL_BLEND);
//glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return true;
}
// 初始化实体
void InitObject() {
//mesh
MeshModel* meshmodel = new MeshModel(50, 1);
meshmodel->UpdataMesh(glm::vec3(0.0f));
ModelManager::Register(meshmodel, "mesh");
ShaderManager::LoadShader(MeshModel::vsPath.c_str(), MeshModel::fsPath.c_str(), nullptr, "mesh");
VertexArrayManager::LoadVertexArray(meshmodel, "mesh");
//spine-youlika
SpineModel* spinemodel = new SpineModel("youlika", "res/spine/test.atlas", "res/spine/test.skel", DataMode::BINARY);
spinemodel->SetState("Move", 0, 0, 0);
spinemodel->UpdataMesh(glfwGetTime());
ModelManager::Register(spinemodel, "youlika");
ShaderManager::LoadShader(SpineModel::vsPath.c_str(), SpineModel::fsPath.c_str(), nullptr, "spine");
VertexArrayManager::LoadVertexArray(spinemodel, "youlika");
//spine-lisa
spinemodel = new SpineModel("lisa", "res/spine/lisa.atlas", "res/spine/lisa.skel", DataMode::BINARY);
spinemodel->SetState("Move", 0, 0, 0);
spinemodel->UpdataMesh(glfwGetTime());
ModelManager::Register(spinemodel, "lisa");
VertexArrayManager::LoadVertexArray(spinemodel, "lisa");
//outline
OutLineModel* outlinemodel = new OutLineModel();
ModelManager::Register(outlinemodel, "outline");
ShaderManager::LoadShader(OutLineModel::vsPath.c_str(), OutLineModel::fsPath.c_str(), nullptr, "outline");
VertexArrayManager::LoadVertexArray(outlinemodel, "outline");
//floor
PlaneModel* floor = new PlaneModel("res/texture/floor/marble.jpg");
floor->SetRepeat(10, 10);
ModelManager::Register(floor, "floor");
ShaderManager::LoadShader(PlaneModel::vsPath.c_str(), PlaneModel::fsPath.c_str(), nullptr, "floor");
TextureManager::LoadTexture(floor->texPath, "floor");
VertexArrayManager::LoadVertexArray(floor, "floor");
//button
PlaneModel* button = new PlaneModel("res/texture/block.png");
ModelManager::Register(button, "button");
ShaderManager::LoadShader("res/shader/UI/ui.vs", "res/shader/UI/ui.fs", nullptr, "ui");
TextureManager::LoadTexture(button->texPath, "button");
VertexArrayManager::LoadVertexArray(button, "button");
//预设置透视矩阵
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)(SCR_WIDTH * SCALE_X) / (float)(SCR_HEIGHT * SCALE_Y), 0.1f, 100.0f);
for (auto iter : ShaderManager::Shaders) {
iter.second.Use();
iter.second.SetMatrix4("view", view);
iter.second.SetMatrix4("projection", projection);
}
}
// 更新实体
void UpdataObject() {
//mesh
glm::vec3 offset = static_cast<MeshModel*>(ModelManager::GetModel("mesh"))->GetOffset(camera.Position);
glm::mat4 model(1.0f);
model = glm::translate(model, offset);
ShaderManager::GetShader("mesh")->Use();
ShaderManager::GetShader("mesh")->SetMatrix4("model", model);
VertexArrayManager::GetVertexArray("mesh")->Bind();
ModelManager::GetModel("mesh")->GetRenderer().Render();
//floor
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, -0.01f, 0.0f));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::scale(model, glm::vec3(20.0f, 20.0f, 1.0f));
ShaderManager::GetShader("floor")->Use();
ShaderManager::GetShader("floor")->SetMatrix4("model", model);
TextureManager::GetTexture("floor")->Bind();
VertexArrayManager::GetVertexArray("floor")->Bind();
Renderer render = ModelManager::GetModel("floor")->GetRenderer();
render.SetID(1);
render.Render();
//outline
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.5f, 0.0f, 0.5f));
model = glm::scale(model, glm::vec3(1.0f, 1.2f, 1.0f));
ShaderManager::GetShader("outline")->Use();
ShaderManager::GetShader("outline")->SetMatrix4("model", model);
VertexArrayManager::GetVertexArray("outline")->Bind();
ModelManager::GetModel("outline")->GetRenderer().Render();
//youlika
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.5f, 0.0f, 0.5f));
model = glm::scale(model, glm::vec3(0.003f, 0.003f, 0.003f));
ShaderManager::GetShader("spine")->Use();
ShaderManager::GetShader("spine")->SetMatrix4("model", model);
static_cast<SpineModel*>(ModelManager::GetModel("youlika"))->UpdataMesh(deltaTime);
ModelManager::GetModel("youlika")->UpdataVertexArray(VertexArrayManager::GetVertexArray("youlika"));
TextureManager::GetTexture("youlika")->Bind();
VertexArrayManager::GetVertexArray("youlika")->Bind();
render = ModelManager::GetModel("youlika")->GetRenderer();
render.SetID(9);
render.Render();
//lisa
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(glfwGetTime(), 0.0f, 0.5f));
model = glm::scale(model, glm::vec3(0.003f, 0.003f, 0.003f));
ShaderManager::GetShader("spine")->Use();
ShaderManager::GetShader("spine")->SetMatrix4("model", model);
static_cast<SpineModel*>(ModelManager::GetModel("lisa"))->UpdataMesh(deltaTime);
ModelManager::GetModel("lisa")->UpdataVertexArray(VertexArrayManager::GetVertexArray("lisa"));
TextureManager::GetTexture("lisa")->Bind();
VertexArrayManager::GetVertexArray("lisa")->Bind();
render = ModelManager::GetModel("lisa")->GetRenderer();
render.SetID(10);
render.Render();
//button
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-0.9f, 0.8f, 0.0f));
model = glm::scale(model, glm::vec3(0.05f, 0.05f * (float)(SCR_WIDTH * SCALE_X) / (float)(SCR_HEIGHT * SCALE_Y), 0.1f));
ShaderManager::GetShader("ui")->Use();
ShaderManager::GetShader("ui")->SetMatrix4("model", model);
TextureManager::GetTexture("button")->Bind();
VertexArrayManager::GetVertexArray("button")->Bind();
render = ModelManager::GetModel("button")->GetRenderer();
render.SetID(5);
render.Render();
}
//主程序入口
int main() {
//初始化GLFW并创建GLFW窗口
if (!InitOpenGL()) {
return -1;
}
//预加载实体
InitObject();
//循环渲染
while (context.next()) {
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(context.getWindow());
context.beforeDrawFrame();
UpdataObject();
context.afterDrawFrame();
}
ShaderManager::Clear();
TextureManager::Clear();
VertexArrayManager::Clear();
ModelManager::Clear();
context.destroy();
return 0;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
std::cout << "渲染循环结束" << std::endl;
glfwSetWindowShouldClose(window, GL_TRUE);
}
else if (key == GLFW_KEY_ENTER && action == GLFW_PRESS) {
std::cout << "当前鼠标坐标:" << mouse_xpos << "," << mouse_ypos << std::endl;
}
else if (key == GLFW_KEY_N && action == GLFW_PRESS) {
TESTENV = !TESTENV;
if (TESTENV) {
glfwSetInputMode(context.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
firstMouse = true;
std::cout << "测试模式启动" << std::endl;
}
else {
glfwSetInputMode(context.getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
std::cout << "测试模式关闭" << std::endl;
}
}
}
void mousebutton_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
GLfloat ref;
glReadPixels(mouse_xpos, (SCR_HEIGHT * SCALE_Y)-mouse_ypos, 1, 1, GL_STENCIL_INDEX, GL_FLOAT, &ref);
std::cout << "屏幕坐标:" << mouse_xpos << "," << mouse_ypos << "|模板缓冲值:" << ref << std::endl;
if (ref == 1) {
double xndc = (2 * mouse_xpos) / (SCR_WIDTH * SCALE_X) - 1;
double yndc = 1 - (2 * mouse_ypos) / (SCR_HEIGHT * SCALE_Y);
glm::vec4 world = screenToWorld(mouse_xpos, mouse_ypos);
std::cout << "世界坐标:" << world.x << "," << world.y << "," << world.z << std::endl;
int x = floor(world.x);
int z = floor(world.z);
std::cout << "网格坐标:" << x << "," << z << std::endl;
//vec3 position(mesh_x + xoffset + 0.5, camera.Position.y, mesh_y + zoffset + 0.5);
}
else if (ref == 9) {
}
}
}
void cursorpos_callback(GLFWwindow* window, double xpos, double ypos)
{
float xposf = static_cast<float>(xpos);
float yposf = static_cast<float>(ypos);
if (firstMouse)
{
mouse_xpos = xposf;
mouse_ypos = yposf;
firstMouse = false;
}
float xoffset = xposf - mouse_xpos;
float yoffset = mouse_ypos - yposf; // reversed since y-coordinates go from bottom to top
mouse_xpos = xpos;
mouse_ypos = ypos;
if (TESTENV) {
camera.ProcessMouseMovement(xoffset, yoffset);
}
else {
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) {
camera.ProcessMouseMovement(xoffset, 0, yoffset);
}
}
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
}
void processInput(GLFWwindow* window)
{
if (TESTENV) {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
camera.ProcessKeyboard(UP, deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
camera.ProcessKeyboard(DOWN, deltaTime);
}
else {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
}
}
glm::vec4 screenToWorld(double mouse_xpos,double mouse_ypos) {
//NDC
double xndc = (2.0f * mouse_xpos) / (SCR_WIDTH * SCALE_X) - 1.0f;
double yndc = 1.0f - (2.0f * mouse_ypos) / (SCR_HEIGHT * SCALE_Y);
glm::vec4 proj_p = glm::vec4(xndc, yndc, 1.0f, 1.0f);
//Projection
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)(SCR_WIDTH * SCALE_X) / (float)(SCR_HEIGHT * SCALE_Y), 0.1f, 100.0f);
glm::mat4 inv_proj = glm::inverse(projection);
glm::vec4 view_p = inv_proj * proj_p;
//View
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 inv_view = glm::inverse(view);
glm::vec4 world_p = inv_view * view_p;
if (world_p.y != 0.0f) {
world_p = world_p / world_p.w;
world_p.x = world_p.x - camera.Position.x;
world_p.y = world_p.y - camera.Position.y;
world_p.z = world_p.z - camera.Position.z;
world_p = camera.Position.y * world_p / world_p.y;
world_p.x = camera.Position.x - world_p.x;
world_p.y = camera.Position.y - world_p.y;
world_p.z = camera.Position.z - world_p.z;
}
return world_p;
}