Cherno 视频:https://www.youtube.com/watch?v=2k5FjtMNQsM&list=PLlrATfBNZ98dC-V-N3m0Go4deliWHPFwT&index=39
代码:https://github.com/DXT00/Hazel_study/tree/a6b65fa3835791f13e1566e84b094d0708a9f2e6/Hazel
For every object -->Transform have to be per Mesh or per Object
For camera transform--> Transform is for the whole scene
即:为每个Object传递Model Matrix(Transform matrix)
首先改变Renderer接口,提交CameraMatrix同时,还要提交Object的TransformMatrix
Object的TransformMatrix会通过Uniform传递到Shader!
void Renderer::Submit(const std::shared_ptr<VertexArray>& vertexArray, const std::shared_ptr<Shader>& shader,const glm::mat4& transform)
{
shader->Bind();
shader->SetUniformMat4("u_ViewProjection", s_SceneData->ViewProjectionMatrix);
shader->SetUniformMat4("u_Transform", transform);
vertexArray->Bind();
RenderCommand::DrawIndex(vertexArray);
}
通过四个按键控制Object的Position :J,L,I,K键
if (Hazel::Input::IsKeyPressed(HZ_KEY_J))
{
m_SquarePosition.x -= m_SquareMoveSpeed * ts;
}
else if (Hazel::Input::IsKeyPressed(HZ_KEY_L))
{
m_SquarePosition.x += m_SquareMoveSpeed * ts;
}
if (Hazel::Input::IsKeyPressed(HZ_KEY_I))
{
m_SquarePosition.y += m_SquareMoveSpeed * ts;
}
else if (Hazel::Input::IsKeyPressed(HZ_KEY_K))
{
m_SquarePosition.y -= m_SquareMoveSpeed * ts;
}
glm::mat4 transform = glm::translate(glm::mat4(1.0f), m_SquarePosition);
Hazel::Renderer::Submit(m_SquareVA, m_SquareShader, transform);
F5:
可以看到,我们按下 J,K,L,I会改变方形的位置,而如果改变相机的Matrix,场景中的所有物体的位置或者旋转会一并改变
还使用glm::scale对方形进行缩放
glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
glm::mat4 transform = glm::translate(glm::mat4(1.0f), m_SquarePosition)*scale;
Hazel::Renderer::Submit(m_SquareVA, m_SquareShader, transform);
创造多个Objects:
void OnUpdate(Hazel::TimeStep ts) override
{
HZ_TRACE("Delta time: {0}s ({1}ms)", ts.GetSeconds(), ts.GetMilliseconds());
//float timestep = ts;//相当一 timestep = float(ts)
if (Hazel::Input::IsKeyPressed(HZ_KEY_LEFT))
{
m_CameraPosition.x -= m_CameraMoveSpeed * ts;
}
else if (Hazel::Input::IsKeyPressed(HZ_KEY_RIGHT))
{
m_CameraPosition.x += m_CameraMoveSpeed * ts;
}
if (Hazel::Input::IsKeyPressed(HZ_KEY_UP))
{
m_CameraPosition.y += m_CameraMoveSpeed * ts;
}
else if (Hazel::Input::IsKeyPressed(HZ_KEY_DOWN))
{
m_CameraPosition.y -= m_CameraMoveSpeed * ts;
}
if (Hazel::Input::IsKeyPressed(HZ_KEY_A))
{
m_CameraRotation += m_CameraRotateSpeed * ts;
}
else if (Hazel::Input::IsKeyPressed(HZ_KEY_D))
{
m_CameraRotation -= m_CameraRotateSpeed * ts;
}
/*if (Hazel::Input::IsKeyPressed(HZ_KEY_J))
{
m_SquarePosition.x -= m_SquareMoveSpeed * ts;
}
else if (Hazel::Input::IsKeyPressed(HZ_KEY_L))
{
m_SquarePosition.x += m_SquareMoveSpeed * ts;
}
if (Hazel::Input::IsKeyPressed(HZ_KEY_I))
{
m_SquarePosition.y += m_SquareMoveSpeed * ts;
}
else if (Hazel::Input::IsKeyPressed(HZ_KEY_K))
{
m_SquarePosition.y -= m_SquareMoveSpeed * ts;
}
*/
Hazel::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 });
Hazel::RenderCommand::Clear();
/* m_Camera.SetPosition({ 0.5f,0.5f,0.0f });
m_Camera.SetRotation(45);*/
m_Camera.SetPosition(m_CameraPosition);
m_Camera.SetRotation(m_CameraRotation);
Hazel::Renderer::BeginScene(m_Camera);
glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
for (int x = 0; x <5; x++)
{
for (int y = 0; y < 5; y++)
{
glm::vec3 pos(x*0.11f, y*0.11f, 0.0f);
glm::mat4 transform = glm::translate(glm::mat4(1.0f), pos)*scale;
Hazel::Renderer::Submit(m_SquareVA, m_SquareShader, transform);
}
}
//glm::mat4 transform = glm::translate(glm::mat4(1.0f), m_SquarePosition)*scale;
//Hazel::Renderer::Submit(m_SquareVA, m_SquareShader, transform);
Hazel::Renderer::Submit(m_VertexArray, m_Shader);
Hazel::Renderer::EndScene();
}