当前位置:   article > 正文

跟着cherno手搓游戏引擎【23】项目维护、2D引擎之前的一些准备

跟着cherno手搓游戏引擎【23】项目维护、2D引擎之前的一些准备

 项目维护:

修改文件结构

头文件自己改改就好了

 创建2DRendererLayer:

Sandbox2D.h:

  1. #pragma once
  2. #include "YOTO.h"
  3. class Sandbox2D :public YOTO::Layer
  4. {public:
  5. Sandbox2D();
  6. virtual ~Sandbox2D() = default;
  7. virtual void OnAttach()override;
  8. virtual void OnDetach()override;
  9. void OnUpdate(YOTO::Timestep ts)override;
  10. virtual void OnImGuiRender() override;
  11. void OnEvent(YOTO::Event& e)override;
  12. private:
  13. YOTO::OrthographicCameraController m_CameraController;
  14. YOTO::Ref<YOTO::Shader> m_FlatColorShader;
  15. YOTO::Ref<YOTO::VertexArray> m_SquareVA;
  16. glm::vec4 m_SquareColor = { 0.2f,0.3f,0.7f,1.0f };
  17. };

Sandbox2D.cpp: 

  1. #include "Sandbox2D.h"
  2. #include <imgui/imgui.h>
  3. #include <glm/gtc/matrix_transform.hpp>
  4. #include <Platform/OpenGL/OpenGLShader.h>
  5. #include <glm/gtc/type_ptr.hpp>
  6. Sandbox2D::Sandbox2D()
  7. :Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true)
  8. {
  9. }
  10. void Sandbox2D::OnAttach()
  11. {
  12. m_SquareVA = (YOTO::VertexArray::Create());
  13. float squareVertices[5 * 4] = {
  14. -0.5f,-0.5f,0.0f,
  15. 0.5f,-0.5f,0.0f,
  16. 0.5f,0.5f,0.0f,
  17. -0.5f,0.5f,0.0f,
  18. };
  19. YOTO::Ref<YOTO::VertexBuffer> squareVB;
  20. squareVB.reset(YOTO::VertexBuffer::Create(squareVertices, sizeof(squareVertices)));
  21. squareVB->SetLayout({
  22. {YOTO::ShaderDataType::Float3,"a_Position"}
  23. });
  24. m_SquareVA->AddVertexBuffer(squareVB);
  25. uint32_t squareIndices[6] = { 0,1,2,2,3,0 };
  26. YOTO::Ref<YOTO::IndexBuffer> squareIB;
  27. squareIB.reset((YOTO::IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))));
  28. m_SquareVA->AddIndexBuffer(squareIB);
  29. m_FlatColorShader = YOTO::Shader::Create("assets/shaders/FlatColor.glsl");
  30. }
  31. void Sandbox2D::OnDetach()
  32. {
  33. }
  34. void Sandbox2D::OnUpdate(YOTO::Timestep ts)
  35. { //update
  36. m_CameraController.OnUpdate(ts);
  37. //Render
  38. YOTO::RenderCommand::SetClearColor({ 0.2f, 0.2f, 0.2f, 1.0f });
  39. YOTO::RenderCommand::Clear();
  40. YOTO::Renderer::BeginScene(m_CameraController.GetCamera());
  41. {
  42. static glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
  43. glm::vec4 redColor(0.8f, 0.3f, 0.3f, 1.0f);
  44. glm::vec4 blueColor(0.2f, 0.3f, 0.8f, 1.0f);
  45. std::dynamic_pointer_cast<YOTO::OpenGLShader>(m_FlatColorShader)->Bind();
  46. std::dynamic_pointer_cast<YOTO::OpenGLShader>(m_FlatColorShader)->UploadUniformFloat4("u_Color", m_SquareColor);
  47. YOTO::Renderer::Submit(m_FlatColorShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f)));
  48. }
  49. }
  50. void Sandbox2D::OnImGuiRender()
  51. {
  52. ImGui::Begin("设置");
  53. ImGui::ColorEdit4("正方形颜色", glm::value_ptr(m_SquareColor));
  54. ImGui::End();
  55. }
  56. void Sandbox2D::OnEvent(YOTO::Event& e)
  57. {
  58. m_CameraController.OnEvent(e);
  59. }

SandBoxApp.cpp:

  1. class Sandbox:public YOTO::Application
  2. {
  3. public:
  4. Sandbox(){
  5. //PushLayer(new ExampleLayer());
  6. //PushLayer(new YOTO::ImGuiLayer());
  7. PushLayer(new Sandbox2D());
  8. }
  9. ~Sandbox() {
  10. }
  11. private:
  12. };
  13. YOTO::Application* YOTO::CreateApplication() {
  14. printf("helloworld");
  15. return new Sandbox();
  16. }

flatColor.glsl:

  1. #type vertex
  2. #version 330 core
  3. layout(location = 0) in vec3 a_Position;
  4. uniform mat4 u_ViewProjection;
  5. uniform mat4 u_Transform;
  6. void main(){
  7. gl_Position =u_ViewProjection*u_Transform*vec4( a_Position,1.0);
  8. }
  9. #type fragment
  10. #version 330 core
  11. layout(location = 0) out vec4 color;
  12. uniform vec4 u_Color ;
  13. void main(){
  14. color =u_Color;
  15. }

YOTO.h:注意,删掉了入口点,放到了SandboxApp中:

  1. #pragma once
  2. //用于YOTO APP
  3. #include "YOTO/Core/Application.h"
  4. #include"YOTO/Core/Layer.h"
  5. #include "YOTO/Core/Log.h"
  6. #include"YOTO/Core/Timestep.h"
  7. #include"YOTO/Core/Input.h"
  8. #include"YOTO/Core/KeyCode.h"
  9. #include"YOTO/Core/MouseButtonCodes.h"
  10. #include "YOTO/Renderer/OrthographicCameraController.h"
  11. #include"YOTO/ImGui/ImGuiLayer.h"
  12. //Renderer
  13. #include"YOTO/Renderer/Renderer.h"
  14. #include"YOTO/Renderer/RenderCommand.h"
  15. #include"YOTO/Renderer/Buffer.h"
  16. #include"YOTO/Renderer/Shader.h"
  17. #include"YOTO/Renderer/Texture.h"
  18. #include"YOTO/Renderer/VertexArray.h"
  19. #include"YOTO/Renderer/OrthographicCamera.h"

测试:

能跑就行!

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号