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

项目维护:

修改文件结构:

头文件自己改改就好了

创建2DRendererLayer:

Sandbox2D.h:

cpp 复制代码
#pragma once
#include "YOTO.h"
class Sandbox2D :public YOTO::Layer
{public:
	Sandbox2D();
	virtual ~Sandbox2D() = default;
	virtual void OnAttach()override;
	virtual void OnDetach()override;

	void OnUpdate(YOTO::Timestep ts)override;
	virtual void OnImGuiRender() override;
	void OnEvent(YOTO::Event& e)override;
private:
	YOTO::OrthographicCameraController m_CameraController;

	YOTO::Ref<YOTO::Shader> m_FlatColorShader;
	YOTO::Ref<YOTO::VertexArray> m_SquareVA;

	glm::vec4 m_SquareColor = { 0.2f,0.3f,0.7f,1.0f };
};

Sandbox2D.cpp:

cpp 复制代码
#include "Sandbox2D.h"
#include <imgui/imgui.h>
#include <glm/gtc/matrix_transform.hpp>
#include <Platform/OpenGL/OpenGLShader.h>
#include <glm/gtc/type_ptr.hpp>
Sandbox2D::Sandbox2D()
:Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true) 
{
}
void Sandbox2D::OnAttach()
{
	m_SquareVA = (YOTO::VertexArray::Create());

	float squareVertices[5 * 4] = {
		-0.5f,-0.5f,0.0f,
		0.5f,-0.5f,0.0f,
		0.5f,0.5f,0.0f,
		-0.5f,0.5f,0.0f,
	};
	YOTO::Ref<YOTO::VertexBuffer> squareVB;
	squareVB.reset(YOTO::VertexBuffer::Create(squareVertices, sizeof(squareVertices)));
	squareVB->SetLayout({
		{YOTO::ShaderDataType::Float3,"a_Position"}
		});
	m_SquareVA->AddVertexBuffer(squareVB);
	uint32_t squareIndices[6] = { 0,1,2,2,3,0 };
	YOTO::Ref<YOTO::IndexBuffer> squareIB;

	squareIB.reset((YOTO::IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))));

	m_SquareVA->AddIndexBuffer(squareIB);
	m_FlatColorShader = YOTO::Shader::Create("assets/shaders/FlatColor.glsl");

}
void Sandbox2D::OnDetach()
{
}

void Sandbox2D::OnUpdate(YOTO::Timestep ts)
{	//update
	m_CameraController.OnUpdate(ts);

	//Render
	YOTO::RenderCommand::SetClearColor({ 0.2f, 0.2f, 0.2f, 1.0f });
	YOTO::RenderCommand::Clear();

	YOTO::Renderer::BeginScene(m_CameraController.GetCamera());
	{
		static glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
		glm::vec4  redColor(0.8f, 0.3f, 0.3f, 1.0f);
		glm::vec4  blueColor(0.2f, 0.3f, 0.8f, 1.0f);


		std::dynamic_pointer_cast<YOTO::OpenGLShader>(m_FlatColorShader)->Bind();
		std::dynamic_pointer_cast<YOTO::OpenGLShader>(m_FlatColorShader)->UploadUniformFloat4("u_Color", m_SquareColor);
	

		YOTO::Renderer::Submit(m_FlatColorShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f)));
	}
}
void Sandbox2D::OnImGuiRender()
{
	ImGui::Begin("设置");
	ImGui::ColorEdit4("正方形颜色", glm::value_ptr(m_SquareColor));
	ImGui::End();
}

void Sandbox2D::OnEvent(YOTO::Event& e)
{
	m_CameraController.OnEvent(e);
}

SandBoxApp.cpp:

cpp 复制代码
class Sandbox:public YOTO::Application
{
public:
	Sandbox(){
		//PushLayer(new ExampleLayer());
		//PushLayer(new YOTO::ImGuiLayer());
		PushLayer(new Sandbox2D());
	}
	~Sandbox() {

	}

private:

};

YOTO::Application* YOTO::CreateApplication() {
	printf("helloworld");
	return new Sandbox();
}

flatColor.glsl:

cpp 复制代码
		#type vertex
		#version 330 core
		layout(location = 0) in vec3 a_Position;

		uniform mat4 u_ViewProjection;
		uniform mat4 u_Transform;
		


		void main(){
		gl_Position =u_ViewProjection*u_Transform*vec4( a_Position,1.0);
		}

		#type fragment
		#version 330 core
		layout(location = 0) out vec4 color;
	
		uniform vec4 u_Color ;
		void main(){
		color =u_Color;	
		}
		

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

cpp 复制代码
#pragma once

//用于YOTO APP

#include "YOTO/Core/Application.h"
#include"YOTO/Core/Layer.h"
#include "YOTO/Core/Log.h"

#include"YOTO/Core/Timestep.h"

#include"YOTO/Core/Input.h"
#include"YOTO/Core/KeyCode.h"
#include"YOTO/Core/MouseButtonCodes.h"
#include "YOTO/Renderer/OrthographicCameraController.h"

#include"YOTO/ImGui/ImGuiLayer.h"

//Renderer
#include"YOTO/Renderer/Renderer.h"
#include"YOTO/Renderer/RenderCommand.h"

#include"YOTO/Renderer/Buffer.h"
#include"YOTO/Renderer/Shader.h"
#include"YOTO/Renderer/Texture.h"
#include"YOTO/Renderer/VertexArray.h"

#include"YOTO/Renderer/OrthographicCamera.h"

测试:

能跑就行!

相关推荐
神码编程3 小时前
【Unity功能集】TextureShop纹理工坊(五)选区
unity·游戏引擎·shader·ps选区
benben0449 小时前
Unity3D仿星露谷物语开发7之事件创建动画
unity·游戏引擎
虾球xz12 小时前
游戏引擎学习第57天
学习·游戏引擎
异次元的归来1 天前
Unity DOTS中的share component
unity·游戏引擎
向宇it1 天前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
向宇it1 天前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎
每日出拳老爷子1 天前
【图形渲染】【Unity Shader】【Nvidia CG】有用的参考资料链接
unity·游戏引擎·图形渲染
YY-nb2 天前
Unity Apple Vision Pro 开发教程:物体识别跟踪
unity·游戏引擎·apple vision pro
向宇it2 天前
【从零开始入门unity游戏开发之——C#篇23】C#面向对象继承——`as`类型转化和`is`类型检查、向上转型和向下转型、里氏替换原则(LSP)
java·开发语言·unity·c#·游戏引擎·里氏替换原则
Cool-浩2 天前
Unity 开发Apple Vision Pro空间锚点应用Spatial Anchor
unity·游戏引擎·apple vision pro·空间锚点·spatial anchor·visionpro开发