-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
292 lines (246 loc) · 9.98 KB
/
Copy pathmain.cpp
File metadata and controls
292 lines (246 loc) · 9.98 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <glad/glad.h>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "Shader.h"
#include "Camera.h"
#include <windows.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include "Model.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window,double xPos,double yPos);
void scroll_callback(GLFWwindow* window, double xOff,double yOff);
void processInput(GLFWwindow *window);
// settings
unsigned int SCR_WIDTH = 1000;
unsigned int SCR_HEIGHT = 1000;
Camera ourCamera(SCR_WIDTH,SCR_HEIGHT);
int main()
{
// 切换到英语键盘
// -----------------------------------
HKL hCurKL;
hCurKL = GetKeyboardLayout(0);
LoadKeyboardLayoutA(reinterpret_cast<LPCSTR>((LPCWSTR) ("0x0409")), KLF_ACTIVATE);
float vScale = 1.0f;
float my_color[4] = {1.0f};
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Simple3DViewer", nullptr, nullptr);
if (window == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window,mouse_callback);
glfwSetScrollCallback(window,scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPos(window, 0, 0);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// y-axis
stbi_set_flip_vertically_on_load(true);
// 开启z-buffer
// --------------------------
glEnable(GL_DEPTH_TEST);
// Setup Dear ImGui context
// -----------------------------------------------------
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
// light
// ---------------------------------------------------------------
float vertices[] = {
// positions // normals // texture coords
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f
};
// light
// --------------------------------------------
unsigned int VBO, lightVAO;
glGenVertexArrays(1, &lightVAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// 给缓冲区加值
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)nullptr);
glEnableVertexAttribArray(0);
// 好像是解绑
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// light pos
// -----------------------------------------------
glm::vec3 lightPos(5.0f, 10.0f, 20.0f);
float lightPos_x = lightPos.x;
float lightPos_y = lightPos.y;
// import shader and model
//--------------------------------------------------------------------------------
Shader ourShader("../Shader/shader.vs","../Shader/shader.fs");
Shader lightShader("../Shader/light.vs","../Shader/light.fs");
Model ourModel("../model/keli/keli.pmx");
std::cout<<"before while"<<std::endl;
while (!glfwWindowShouldClose(window))
{
// input
processInput(window);
ourCamera.listenKeyboardEvent(window);
// render
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
// glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Start the Dear ImGui frame
// -------------------------------------
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ourShader.use();
glm::mat4 mScale = glm::mat4(1.0f);
ImGui::Begin("Simple3DViewer");
ImGui::Text("console");
ImGui::SliderFloat("Scale Model", &vScale, 0.0f, 1.0f);
ImGui::SliderFloat("lightPos_x",&lightPos_x,-10.0f,10.0f);
ImGui::SliderFloat("lightPos_y",&lightPos_y,-10.0f,10.0f);
ImGui::End();
ImGui::Render();
// set light pos
// ---------------------------
lightPos.x = lightPos_x;
lightPos.y = lightPos_y;
// 绘制图形
// ------------------------------------------------------------------------
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene
ourShader.setMat4("model", model);
mScale = glm::scale(mScale,glm::vec3(vScale));
ourShader.setMat4("scale",mScale);
ourShader.setInt("material.specular", 1);
ourShader.setFloat("material.shininess", 164.0f);
ourShader.setVec3("light.ambient", 0.5f, 0.5f, 0.5f);
ourShader.setVec3("light.diffuse", 0.7f, 0.7f, 0.7f); // 将光照调暗了一些以搭配场景
ourShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
ourShader.setVec3("light.position",lightPos);
ourShader.setVec3("light.direction", 0.0f,0.0f,-1.0f);
ourShader.setFloat("light.cutOff",glm::cos(glm::radians(45.5f)));
ourShader.setFloat("light.outerCutOff", glm::cos(glm::radians(60.5f)));
ourShader.setVec3("viewPos",ourCamera.cameraPos);
ourShader.setFloat("light.constant", 1.0f);
ourShader.setFloat("light.linear", 0.022f);
ourShader.setFloat("light.quadratic", 0.0019f);
ourShader.setMat4("projection",ourCamera.getProjectionMat4());
ourShader.setMat4("view",ourCamera.getViewMat4());
ourModel.Draw(ourShader);
// 绘制发光体
// -----------------------------------------------------------
lightShader.use();
glBindVertexArray(lightVAO);
lightShader.setMat4("projection",ourCamera.getProjectionMat4());
lightShader.setMat4("view",ourCamera.getViewMat4());
glm::mat4 lightModel = glm::mat4(1.0f);
lightModel = glm::translate(lightModel,lightPos);
lightModel = glm::scale(lightModel,glm::vec3(1.0f));
lightShader.setMat4("model",lightModel);
glDrawArrays(GL_TRIANGLES,0,36);
glBindVertexArray(0);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwSwapInterval(1);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
// glfw: terminate, clearing all previously allocated GLFW resources.
glfwDestroyWindow(window);
glfwTerminate();
// 将键盘切换回来
// --------------------
ActivateKeyboardLayout(hCurKL, 0);
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
void processInput(GLFWwindow *window){
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if(glfwGetKey(window,GLFW_KEY_LEFT_ALT) == GLFW_PRESS){
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetCursorPosCallback(window, nullptr);
}
if(glfwGetKey(window,GLFW_KEY_LEFT_ALT) == GLFW_RELEASE){
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
}
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height){
SCR_HEIGHT = height;
SCR_WIDTH = width;
glViewport(0, 0, width, height);
}
void mouse_callback(GLFWwindow* window,double xPos,double yPos){
ourCamera.mouseEvent(window,xPos,yPos);
}
void scroll_callback(GLFWwindow* window, double xOff,double yOff){
ourCamera.scrollEvent(window,xOff,yOff,SCR_WIDTH,SCR_HEIGHT);
}