当前位置: 首页 > news >正文

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

 项目维护:

修改文件结构:

头文件自己改改就好了

 创建2DRendererLayer:

Sandbox2D.h:

#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: 

#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)
{	//updatem_CameraController.OnUpdate(ts);//RenderYOTO::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:


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:

		#type vertex#version 330 corelayout(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 corelayout(location = 0) out vec4 color;uniform vec4 u_Color ;void main(){color =u_Color;	}

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

#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"

测试:

能跑就行!

相关文章:

  • 西工大计算机学院复试问题整理
  • 第6章 智能租房——前期准备
  • 第59讲订单数据下拉实现
  • 《剑指 Offer》专项突破版 - 面试题 36 : 详解后缀表达式(C++ 实现)
  • Android 11 webview webrtc无法使用问题
  • 《Django+React前后端分离项目开发实战:爱计划》 03 理解项目结构
  • 【More Effective C++】条款2:使用C++转型操作符
  • 微服务OAuth 2.1扩展额外信息到JWT并解析(Spring Security 6)
  • 力扣231. 2 的幂(数学,二分查找,位运算)
  • 亚马逊认证考试系列 - 知识点 - LightSail介绍
  • 网络选择流程分析(首选网络类型切换流程)
  • Git中为常用指令配置别名
  • 【漏洞复现】狮子鱼CMS某SQL注入漏洞01
  • 服务器禁用了请求中指定的方法
  • Gateway API 实践之(九)FSM Gateway 的双向 TLS
  • 2017年终总结、随想
  • Angular 4.x 动态创建组件
  • HTML中设置input等文本框为不可操作
  • vue脚手架vue-cli
  • 高性能JavaScript阅读简记(三)
  • 关于 Cirru Editor 存储格式
  • 缓存与缓冲
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 十年未变!安全,谁之责?(下)
  • 使用 @font-face
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 小李飞刀:SQL题目刷起来!
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • k8s使用glusterfs实现动态持久化存储
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • (14)学习笔记:动手深度学习(Pytorch神经网络基础)
  • (2)Java 简介
  • (4)(4.6) Triducer
  • (4)事件处理——(7)简单事件(Simple events)
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (七)Java对象在Hibernate持久化层的状态
  • (三)elasticsearch 源码之启动流程分析
  • (十八)SpringBoot之发送QQ邮件
  • (十六)一篇文章学会Java的常用API
  • (转)eclipse内存溢出设置 -Xms212m -Xmx804m -XX:PermSize=250M -XX:MaxPermSize=356m
  • (转)Linux下编译安装log4cxx
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (转载)利用webkit抓取动态网页和链接
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .NET Framework 4.6.2改进了WPF和安全性
  • .net程序集学习心得
  • ::