|
|
@@ -5,25 +5,49 @@ TestLight TestLight::_self;
|
|
|
|
|
|
void TestLight::OnEnter(GLFWwindow* window)
|
|
|
{
|
|
|
- // 坐标 颜色 UV 坐标 贴图序号
|
|
|
- m_vertexs.push_back({ 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0 });
|
|
|
- m_vertexs.push_back({ 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0 });
|
|
|
- m_vertexs.push_back({ -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1 });
|
|
|
- m_vertexs.push_back({ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1 });
|
|
|
- m_vertexs.push_back({ 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0 });
|
|
|
- m_vertexs.push_back({ 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0 });
|
|
|
- m_vertexs.push_back({ -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1 });
|
|
|
- m_vertexs.push_back({ -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1 });
|
|
|
-
|
|
|
- GLuint indices[] = {
|
|
|
- 0, 1, 2, 2, 3, 0, // 组成下面的两个三角形
|
|
|
- 4, 5, 6, 6, 7, 4,
|
|
|
- 1, 2, 6, 6, 5, 1,
|
|
|
- 0, 1, 5, 5, 4, 0,
|
|
|
- 2, 3, 7, 7, 6, 2,
|
|
|
- 0, 3, 4, 4, 7, 3,
|
|
|
- };
|
|
|
-
|
|
|
+ // 坐标 顶点法线
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f });
|
|
|
+ m_vertexs.push_back({ 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f });
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f });
|
|
|
+
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f });
|
|
|
+
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, });
|
|
|
+
|
|
|
+ m_vertexs.push_back({ 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, });
|
|
|
+ m_vertexs.push_back({ 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, });
|
|
|
+
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f });
|
|
|
+ m_vertexs.push_back({ 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f });
|
|
|
+ m_vertexs.push_back({ 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f });
|
|
|
+
|
|
|
+ m_vertexs.push_back({ -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f });
|
|
|
+ m_vertexs.push_back({ -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f });
|
|
|
+
|
|
|
// 启动深度测试
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
@@ -35,41 +59,34 @@ void TestLight::OnEnter(GLFWwindow* window)
|
|
|
|
|
|
// 创建 VBO IB
|
|
|
glGenBuffers(1, &m_VBO);
|
|
|
- glGenBuffers(1, &m_IB);
|
|
|
|
|
|
// 绑定 VBO 和 数据
|
|
|
GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, m_VBO));
|
|
|
- GL_CALL(glBufferData(GL_ARRAY_BUFFER, m_vertexs.size() * sizeof(Vertex_v0), m_vertexs.data(), GL_STATIC_DRAW));
|
|
|
+ GL_CALL(glBufferData(GL_ARRAY_BUFFER, m_vertexs.size() * sizeof(Vertex_v1), m_vertexs.data(), GL_STATIC_DRAW));
|
|
|
|
|
|
- // 绑定 IB 和 数据
|
|
|
- GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IB));
|
|
|
- GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW));
|
|
|
+ // 偷懒 就不用 index buffer 了
|
|
|
+ //GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IB));
|
|
|
+ //GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW));
|
|
|
|
|
|
// 设置 VAO 内存结构
|
|
|
GL_CALL(glEnableVertexAttribArray(0));
|
|
|
- GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex_v0), (void *)offsetof(Vertex_v0, position)));
|
|
|
+ GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex_v1), (void *)offsetof(Vertex_v1, position)));
|
|
|
|
|
|
GL_CALL(glEnableVertexAttribArray(1));
|
|
|
- GL_CALL(glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex_v0), (void*)offsetof(Vertex_v0, color)));
|
|
|
-
|
|
|
- GL_CALL(glEnableVertexAttribArray(2));
|
|
|
- GL_CALL(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex_v0), (void*)offsetof(Vertex_v0, texCoord)));
|
|
|
-
|
|
|
- GL_CALL(glEnableVertexAttribArray(3));
|
|
|
- GL_CALL(glVertexAttribPointer(3, 1, GL_INT, GL_FALSE, sizeof(Vertex_v0), (void*)offsetof(Vertex_v0, texIndex)));
|
|
|
+ GL_CALL(glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex_v1), (void*)offsetof(Vertex_v1, normal)));
|
|
|
|
|
|
// 初始化 shader
|
|
|
//m_Shader.NewInit("res/shader/TextPosition/Vertex.vert", "res/shader/TextPosition/Fragment.frag");
|
|
|
- m_Shader.Init("res/shader/TextPosition/Vertex.vert", "res/shader/TextPosition/Fragment.frag");
|
|
|
+ m_Shader.Init("res/shader/Light/Vertex.vert", "res/shader/Light/Fragment.frag");
|
|
|
+
|
|
|
+ m_Shader.UnBind();
|
|
|
|
|
|
- // 初始化 texture
|
|
|
- m_Tex1.Init("res/textures/test2.png");
|
|
|
- m_Tex2.Init("res/textures/test3.png");
|
|
|
+ m_LightShader.Init("res/shader/Light/LightVertex.vert", "res/shader/Light/LightFragment.frag");
|
|
|
+ m_LightShader.UnBind();
|
|
|
|
|
|
- m_Tex1.Bind(0);
|
|
|
- m_Tex2.Bind(1);
|
|
|
+ CreateLight();
|
|
|
|
|
|
- m_Shader.UnBind();
|
|
|
+ m_Camera.SetLocation(glm::vec3(0.0f, 0.0f, 3.0f));
|
|
|
|
|
|
BindMouse(window);
|
|
|
}
|
|
|
@@ -82,7 +99,6 @@ void TestLight::OnExit(GLFWwindow* window)
|
|
|
|
|
|
void TestLight::UpdateLogic(float delayTime)
|
|
|
{
|
|
|
- m_model = glm::mat4(1.0f);
|
|
|
m_view = m_Camera.GetView();
|
|
|
m_Camera.SetMoveSpeed(m_CameraMoveSpeed);
|
|
|
m_Camera.SetRotateSpeed(m_CameraRotateSpeed);
|
|
|
@@ -99,25 +115,46 @@ void TestLight::ClearRender(GLFWwindow* window)
|
|
|
void TestLight::Render(GLFWwindow* window)
|
|
|
{
|
|
|
m_Shader.Bind();
|
|
|
- m_Shader.SetUniform1i("u_Texture0", 0);
|
|
|
- m_Shader.SetUniform1i("u_Texture1", 1);
|
|
|
|
|
|
m_Shader.SetUniformMat4f("model", m_model);
|
|
|
m_Shader.SetUniformMat4f("view", m_view);
|
|
|
m_Shader.SetUniformMat4f("projection", m_proj);
|
|
|
|
|
|
+ m_Shader.SetUniform3f("objectColor", 1.0f, 0.5f, 0.31f);
|
|
|
+ m_Shader.SetUniform3f("lightColor", 1.0f, 1.0f, 1.0f);
|
|
|
+
|
|
|
+ m_Shader.SetUniform1f("ambientStrength", m_ambientStrength); // 设置强度
|
|
|
+ m_Shader.SetUniform1f("diffuseStrength", m_diffuseStrength);
|
|
|
+ m_Shader.SetUniform1f("specularStrength",m_specularStrength);
|
|
|
+
|
|
|
+ m_Shader.SetUniform3f("viewPos", m_Camera.GetCameraLocation().x, m_Camera.GetCameraLocation().y, m_Camera.GetCameraLocation().z);
|
|
|
+ m_Shader.SetUniform3f("lightPos", m_LightPos.x, m_LightPos.y, m_LightPos.z);
|
|
|
+
|
|
|
glBindVertexArray(m_VAO);
|
|
|
- glDrawElements(GL_TRIANGLES, 6 * 6, GL_UNSIGNED_INT, 0);
|
|
|
+ glDrawArrays(GL_TRIANGLES, 0, 6 * 6);
|
|
|
+
|
|
|
+ auto lightModule = glm::mat4(1.0f);
|
|
|
+ lightModule = glm::translate(lightModule, m_LightPos);
|
|
|
+ lightModule = glm::scale(lightModule, glm::vec3(0.2f)); // a smaller cube
|
|
|
+
|
|
|
+ m_LightShader.Bind();
|
|
|
+ m_LightShader.SetUniformMat4f("model", lightModule);
|
|
|
+ m_LightShader.SetUniformMat4f("view", m_view);
|
|
|
+ m_LightShader.SetUniformMat4f("projection", m_proj);
|
|
|
+
|
|
|
+ glBindVertexArray(m_LightVAO);
|
|
|
+ glDrawArrays(GL_TRIANGLES, 0, 6 * 6);
|
|
|
}
|
|
|
|
|
|
void TestLight::UpdateImGUI(GLFWwindow* window)
|
|
|
{
|
|
|
const auto& io = ImGui::GetIO();
|
|
|
|
|
|
- ImGui::Begin("Camera");
|
|
|
+ ImGui::Begin("Light");
|
|
|
|
|
|
- ImGui::SliderFloat("Camera Move Speed", &m_CameraMoveSpeed, 0.0f, 0.1f);
|
|
|
- ImGui::SliderFloat("Camera Rotate Speed", &m_CameraRotateSpeed, 0.01f, 0.1f);
|
|
|
+ ImGui::SliderFloat("Light ambientStrength", &m_ambientStrength, 0.0f, 1.0f);
|
|
|
+ ImGui::SliderFloat("Light diffuseStrength", &m_diffuseStrength, 0.0f, 5.0f);
|
|
|
+ ImGui::SliderFloat("Light specularStrength", &m_specularStrength, 0.0f, 5.0f);
|
|
|
|
|
|
if (ImGui::Button("Close Window"))
|
|
|
glfwSetWindowShouldClose(window, true);
|
|
|
@@ -160,3 +197,17 @@ void TestLight::UnBindMouse(GLFWwindow* window)
|
|
|
m_Camera.SetFirstMouse(false);
|
|
|
TestWithMouseBase::UnBindMouse(window);
|
|
|
}
|
|
|
+
|
|
|
+void TestLight::CreateLight()
|
|
|
+{
|
|
|
+ glGenVertexArrays(1, &m_LightVAO);
|
|
|
+ glBindVertexArray(m_LightVAO);
|
|
|
+
|
|
|
+ // 直接沿用立方体数据
|
|
|
+ glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
|
|
|
+
|
|
|
+ // 设置 VAO 内存结构
|
|
|
+ // 灯光只需要位置数据
|
|
|
+ GL_CALL(glEnableVertexAttribArray(0));
|
|
|
+ GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex_v1), (void*)offsetof(Vertex_v1, position)));
|
|
|
+}
|