跟着cherno手搓游戏引擎【29】Batch简单合批

思路:

CPU和GPU都开辟同样大小的一大块内存(为了存储顶点信息)

索引在程序运行时生成对应规则后绑定到索引缓冲中

动态生成顶点信息(现在改成Drawquad只是确定图形顶点的位置)

然后在Endscene,将CPU的动态生成的顶点数据上传给GPU,然后再绘制出来

所以,就是根据所绘制的物体,动态生成索引缓冲区,然后根据索引缓冲区一次性绘制多个物体

实现:

Renderer2D.h:

#pragma once
#include "OrthographicCamera.h"
#include"Texture.h"
namespace YOTO {class Renderer2D{public://为什么渲染器是静态的:static void Init();static void ShutDown();static void BeginScene(const OrthographicCamera& camera);static void EndScene();static void Flush();static void DrawQuad(const glm::vec2& position, const glm::vec2& size ,const glm::vec4& color);static void DrawQuad(const glm::vec3& position, const glm::vec2& size ,const glm::vec4& color);static void DrawQuad(const glm::vec2& position, const glm::vec2& size ,const Ref<Texture2D> texture,float tilingFactor=1.0f,const glm::vec4& tintColor=glm::vec4(1.0f));static void DrawQuad(const glm::vec3& position, const glm::vec2& size ,const Ref<Texture2D> texture,float tilingFactor=1.0f,const glm::vec4& tintColor=glm::vec4(1.0f));static void DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation,const glm::vec4& color);static void DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation,const glm::vec4& color);static void DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation,const Ref<Texture2D> texture, float tilingFactor = 1.0f,  const glm::vec4& tintColor = glm::vec4(1.0f));static void DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation,const Ref<Texture2D> texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f));};
}

Renderer2D.cpp:

#include "ytpch.h"
#include "Renderer2D.h"
#include"VertexArray.h"
#include"Shader.h"
//#include "Platform/OpenGL/OpenGLShader.h"
#include <glm/gtc/matrix_transform.hpp>
#include "RenderCommand.h"
namespace YOTO {/// <summary>/// 为什么QuadVertex的指针可以作为void*data传入glBufferSubData:/// SetLayout配置的就是这三个的顺序,因为glm内部用float实现/// 相当于前三个float是Position,之后四个float组成的Color,/// 最后是两个float组成的TexCoord/// </summary>struct QuadVertex {glm::vec3 Position;glm::vec4 Color;glm::vec2 TexCoord;//,纹理Id,};struct  Renderer2DData {const uint32_t MaxQuads = 10000;const uint32_t MaxVertices = MaxQuads * 4;const uint32_t MaxIndices = MaxQuads * 6;//顶点数组Ref<VertexArray> QuadVertexArray;//定带你缓冲Ref<VertexBuffer> QuadVertexBuffer;//Ref<Shader> FlatColorShader;//ShaderRef<Shader> TextureShader;//纹理Ref<Texture2D> WhiteTexture;//记录索引uint32_t QuadIndexCount =0;QuadVertex* QuadVertexBufferBase=nullptr;QuadVertex* QuadVertexBufferPtr= nullptr;};//CPU开辟的大内存static Renderer2DData s_Data;void Renderer2D::Init(){YT_PROFILE_FUNCTION();//---------------------顶点数组--------------------------//创建顶点数组s_Data.QuadVertexArray = VertexArray::Create();// 创建顶点缓冲区,先在GPU开辟一块s_Data.MaxVertices * sizeof(QuadVertex)大小的内存// 与cpu对应大,是为了传输顶点数据//---------------------顶点缓冲区--------------------------s_Data.QuadVertexBuffer =VertexBuffer::Create(s_Data.MaxVertices*sizeof(QuadVertex));s_Data.QuadVertexBuffer->SetLayout({{ShaderDataType::Float3,"a_Position"},{ShaderDataType::Float4,"a_Color"},{ShaderDataType::Float2,"a_TexCoord"}});//顶点数组添加顶点缓冲区,并且在这个缓冲区中设置布局s_Data.QuadVertexArray->AddVertexBuffer(s_Data.QuadVertexBuffer);// 在CPU开辟存储s_Data.MaxVertices个的QuadVertex的内存s_Data.QuadVertexBufferBase = new QuadVertex[s_Data.MaxVertices];//---------------------索引缓冲区--------------------------//开辟一块索引缓冲区uint32_t* quadIndices = new uint32_t[s_Data.MaxIndices];uint32_t offset = 0;	//配置索引for (uint32_t i = 0; i < s_Data.MaxIndices; i += 6) {quadIndices[i + 0] = offset + 0;quadIndices[i + 1] = offset + 1;quadIndices[i + 2] = offset + 2;quadIndices[i + 3] = offset + 2;quadIndices[i + 4] = offset + 3;quadIndices[i + 5] = offset + 0;offset += 4;}//创建索引缓冲区Ref<IndexBuffer> quardIB;quardIB =IndexBuffer::Create(quadIndices, s_Data.MaxIndices);s_Data.QuadVertexArray->AddIndexBuffer(quardIB);delete[] quadIndices;	// cpu上传到gpu上了可以删除cpu的索引数据块了//---------------------纹理--------------------------// 创建一个白色Textures_Data.WhiteTexture = Texture2D::Create(1, 1);uint32_t whiteTextureData = 0xffffffff;s_Data.WhiteTexture->SetData(&whiteTextureData, sizeof(uint32_t));//---------------------着色器--------------------------//加载shader,并传入shader参数s_Data.TextureShader= Shader::Create("assets/shaders/Texture.glsl");s_Data.TextureShader->Bind();s_Data.TextureShader->SetInt("u_Texture", 0);}void Renderer2D::ShutDown(){YT_PROFILE_FUNCTION();//delete s_Data;}void Renderer2D::BeginScene(const OrthographicCamera& camera){YT_PROFILE_FUNCTION();s_Data.TextureShader->Bind();s_Data.TextureShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix());// 相当于初始化此帧要绘制的索引数量,上传的顶点数据s_Data.QuadIndexCount = 0;//指针指向首部s_Data.QuadVertexBufferPtr = s_Data.QuadVertexBufferBase;}void Renderer2D::EndScene(){YT_PROFILE_FUNCTION();// 计算当前绘制需要多少个顶点数据,注意这里是8!!!!!!uint32_t dataSize = (uint8_t*)s_Data.QuadVertexBufferPtr - (uint8_t*)s_Data.QuadVertexBufferBase;// 截取部分CPU的顶点数据上传OpenGL,s_Data.QuadVertexBuffer->SetData(s_Data.QuadVertexBufferBase, dataSize);Flush();}void Renderer2D::Flush(){RenderCommand::DrawIndexed(s_Data.QuadVertexArray, s_Data.QuadIndexCount);}void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color){DrawQuad({ position.x,position.y,0.0f }, size, color);}void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color){YT_PROFILE_FUNCTION();//s_Data.FlatColorShader->Bind();//s_Data.FlatColorShader->SetFloat4("u_Color", color);//s_Data.TextureShader->Bind();s_Data.QuadVertexBufferPtr->Position = position;s_Data.QuadVertexBufferPtr->Color = color;s_Data.QuadVertexBufferPtr->TexCoord = {0.0f,0.0f};s_Data.QuadVertexBufferPtr++;s_Data.QuadVertexBufferPtr->Position = { position.x+size.x,position.y,0.0f};s_Data.QuadVertexBufferPtr->Color = color;s_Data.QuadVertexBufferPtr->TexCoord = { 1.0f,0.0f };s_Data.QuadVertexBufferPtr++;s_Data.QuadVertexBufferPtr->Position = { position.x + size.x,position.y + size.y,0.0f };s_Data.QuadVertexBufferPtr->Color = color;s_Data.QuadVertexBufferPtr->TexCoord = { 1.0f,1.0f };s_Data.QuadVertexBufferPtr++;s_Data.QuadVertexBufferPtr->Position = { position.x,position.y+size.y,0.0f };s_Data.QuadVertexBufferPtr->Color = color;s_Data.QuadVertexBufferPtr->TexCoord = { 0.0f,1.0f };s_Data.QuadVertexBufferPtr++;s_Data.QuadIndexCount += 6;/*s_Data.TextureShader->SetFloat4("u_Color", color);s_Data.TextureShader->SetFloat("m_TilingFactor", 1.0f);s_Data.WhiteTexture->Bind();*///glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) /**rotation*/ * glm::scale(glm::mat4(1.0f), {size.x,size.y,1.0f});//s_Data.TextureShader->SetMat4("u_Transform", transform);//s_Data.QuadVertexArray->Bind();//RenderCommand::DrawIndexed(s_Data.QuadVertexArray);}void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref<Texture2D> texture,  float tilingFactor, const glm::vec4& tintColor){DrawQuad({ position.x,position.y,0.0f }, size, texture, tilingFactor, tintColor);}void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D> texture, float tilingFactor, const glm::vec4& tintColor){YT_PROFILE_FUNCTION();//s_Data.TextureShader->Bind();s_Data.TextureShader->SetFloat4("u_Color", tintColor);s_Data.TextureShader->SetFloat("m_TilingFactor",tilingFactor);texture->Bind();glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) /**rotation*/ * glm::scale(glm::mat4(1.0f), { size.x,size.y,1.0f });s_Data.TextureShader->SetMat4("u_Transform", transform);s_Data.QuadVertexArray->Bind();RenderCommand::DrawIndexed(s_Data.QuadVertexArray);}void Renderer2D::DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const glm::vec4& color){DrawRotatedQuad({ position.x,position.y,0.0f }, size, rotation,color);}void Renderer2D::DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const glm::vec4& color){YT_PROFILE_FUNCTION();s_Data.TextureShader->SetFloat4("u_Color", color);s_Data.TextureShader->SetFloat("m_TilingFactor", 1.0f);s_Data.WhiteTexture->Bind();glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::rotate(glm::mat4(1.0f), rotation, {0.0f,0.0f,1.0f}) * glm::scale(glm::mat4(1.0f), { size.x,size.y,1.0f });s_Data.TextureShader->SetMat4("u_Transform", transform);s_Data.QuadVertexArray->Bind();RenderCommand::DrawIndexed(s_Data.QuadVertexArray);}void Renderer2D::DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const Ref<Texture2D> texture, float tilingFactor, const glm::vec4& tintColor){DrawRotatedQuad({ position.x,position.y,0.0f }, size, rotation, texture, tilingFactor, tintColor);}void Renderer2D::DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const Ref<Texture2D> texture, float tilingFactor, const glm::vec4& tintColor){YT_PROFILE_FUNCTION();//s_Data.TextureShader->Bind();s_Data.TextureShader->SetFloat4("u_Color", tintColor);s_Data.TextureShader->SetFloat("m_TilingFactor", tilingFactor);texture->Bind();glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::rotate(glm::mat4(1.0f), rotation, { 0.0f,0.0f,1.0f }) * glm::scale(glm::mat4(1.0f), { size.x,size.y,1.0f });s_Data.TextureShader->SetMat4("u_Transform", transform);s_Data.QuadVertexArray->Bind();RenderCommand::DrawIndexed(s_Data.QuadVertexArray);}
}

Buffer.h:添加SetData和Create方法:

#pragma once
namespace YOTO {enum class ShaderDataType{None=0,Float,Float2,Float3,Float4,Mat3,Mat4,Int,Int2,Int3,Int4,Bool,};static uint32_t  ShaderDataTypeSize(ShaderDataType type) {switch (type){case YOTO::ShaderDataType::Float:return 4;break;case YOTO::ShaderDataType::Float2:return 4*2;break;case YOTO::ShaderDataType::Float3:return 4*3;break;case YOTO::ShaderDataType::Float4:return 4*4;break;case YOTO::ShaderDataType::Mat3:return 4*3*3;break;case YOTO::ShaderDataType::Mat4:return 4*4*4;break;case YOTO::ShaderDataType::Int:return 4;break;case YOTO::ShaderDataType::Int2:return 4*2;break;case YOTO::ShaderDataType::Int3:return 4*3;break;case YOTO::ShaderDataType::Int4:return 4*4;break;case YOTO::ShaderDataType::Bool:return 1;break;}YT_CORE_ASSERT(false, "未知的ShaderDataType!");return 0;}struct BufferElement {std::string Name;ShaderDataType Type;uint32_t Size;uint32_t Offset;bool Normalized;BufferElement(){}BufferElement(ShaderDataType type, const std::string& name,bool normalized=false):Name(name), Type(type), Size(ShaderDataTypeSize(type)), Offset(0), Normalized(normalized){}uint32_t GetComponentCount() const{switch (Type){case YOTO::ShaderDataType::Float:return 1;break;case YOTO::ShaderDataType::Float2:return 2;break;case YOTO::ShaderDataType::Float3:return 3;break;case YOTO::ShaderDataType::Float4:return 4;break;case YOTO::ShaderDataType::Mat3:return 3*3;break;case YOTO::ShaderDataType::Mat4:return 4*4;break;case YOTO::ShaderDataType::Int:return 1;break;case YOTO::ShaderDataType::Int2:return 2;break;case YOTO::ShaderDataType::Int3:return 3;break;case YOTO::ShaderDataType::Int4:return 4;break;case YOTO::ShaderDataType::Bool:return 1;break;default:break;}YT_CORE_ASSERT(false, "未知的ShaderDataType!");return 0;}};class BufferLayout {public:BufferLayout(){}BufferLayout(const std::initializer_list<BufferElement>elements):m_Elements(elements) {CalculateOffsetAndStride();} inline uint32_t GetStride()const { return m_Stride; }inline const std::vector<BufferElement>& GetElements()const {return m_Elements;}std::vector<BufferElement>::iterator begin() { return m_Elements.begin(); }std::vector<BufferElement>::iterator end() { return m_Elements.end(); }std::vector<BufferElement>::const_iterator begin() const { return m_Elements.begin(); }std::vector<BufferElement>::const_iterator end() const { return m_Elements.end(); }private:void CalculateOffsetAndStride() {uint32_t offset = 0;m_Stride = 0;for (auto& element : m_Elements) {element.Offset = offset;offset += element.Size;m_Stride += element.Size;}}private:std::vector<BufferElement> m_Elements;uint32_t m_Stride = 0;};class VertexBuffer {public:virtual~VertexBuffer() {}virtual void Bind() const = 0;virtual void UnBind() const = 0;virtual void SetData(const void* data, uint32_t size) = 0;virtual void SetLayout(const BufferLayout& layout) = 0;virtual const BufferLayout& GetLayout()const = 0;static  Ref<VertexBuffer> Create(float* vertices, uint32_t size);static  Ref<VertexBuffer> Create(uint32_t size);};/// <summary>/// 目前索引仅支持32位的索引缓冲区/// </summary>class IndexBuffer {public:virtual~IndexBuffer(){}virtual void Bind() const = 0;virtual void UnBind() const = 0;virtual uint32_t GetCount() const = 0;static  Ref<IndexBuffer> Create(uint32_t* indices, uint32_t count);};
}

Buffer.cpp:

#include"ytpch.h"
#include"Buffer.h"
#include "Renderer.h"#include "Platform/OpenGL/OpenGLBuffer.h"namespace YOTO {Ref<VertexBuffer> VertexBuffer::Create(uint32_t size){switch (Renderer::GetAPI()){case RendererAPI::API::None:YT_CORE_ASSERT(false, "Buffer:API为None不支持");return nullptr;case RendererAPI::API::OpenGL:return std::make_shared<OpenGLVertexBuffer>(size);}YT_CORE_ASSERT(false, "Buffer:未知API");return nullptr;}Ref<VertexBuffer> VertexBuffer::Create(float* vertices, uint32_t size){switch (Renderer::GetAPI()){case RendererAPI::API::None:YT_CORE_ASSERT(false,"Buffer:API为None不支持");return nullptr;case RendererAPI::API::OpenGL:return std::make_shared<OpenGLVertexBuffer>(vertices,size);}YT_CORE_ASSERT(false,"Buffer:未知API");return nullptr;}Ref<IndexBuffer> IndexBuffer::Create(uint32_t* indices, uint32_t count){switch (Renderer::GetAPI()){case RendererAPI::API::None:YT_CORE_ASSERT(false, "Buffer:API为None不支持");return nullptr;case RendererAPI::API::OpenGL:return std::make_shared < OpenGLIndexBuffer>(indices, count);}YT_CORE_ASSERT(false, "Buffer:未知API");return nullptr;}}

OpenGLBuffer.cpp: 实现继承自Buffer的方法

#include"ytpch.h"
#include"OpenGLBuffer.h"
#include <glad/glad.h>
namespace YOTO {// VertexBuffer OpenGLVertexBuffer::OpenGLVertexBuffer(uint32_t size){YT_PROFILE_FUNCTION();glCreateBuffers(1, &m_RendererID);glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_DYNAMIC_DRAW);}OpenGLVertexBuffer::OpenGLVertexBuffer(float* vertices, uint32_t size){	 YT_PROFILE_FUNCTION();glCreateBuffers(1, &m_RendererID);glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);}OpenGLVertexBuffer::~OpenGLVertexBuffer(){YT_PROFILE_FUNCTION();glDeleteBuffers(1, &m_RendererID);}void OpenGLVertexBuffer::Bind() const{YT_PROFILE_FUNCTION();glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);}void OpenGLVertexBuffer::UnBind() const{glBindBuffer(GL_ARRAY_BUFFER, 0);}void OpenGLVertexBuffer::SetData(const void* data, uint32_t size){glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);// 用来更新一个已有缓冲区对象中的一部分数据,//data:一个指向新数据源的指针,将新的数据源拷贝到缓冲区对象中完成更新glBufferSubData(GL_ARRAY_BUFFER,0,size,data);}// IndexBuffer /OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t* indices, uint32_t count):m_Count(count){YT_PROFILE_FUNCTION();glCreateBuffers(1, &m_RendererID);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);glBufferData(GL_ELEMENT_ARRAY_BUFFER, count*sizeof(uint32_t), indices, GL_STATIC_DRAW);}OpenGLIndexBuffer::~OpenGLIndexBuffer(){YT_PROFILE_FUNCTION();glDeleteBuffers(1, &m_RendererID);}void OpenGLIndexBuffer::Bind() const{YT_PROFILE_FUNCTION();glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);}void OpenGLIndexBuffer::UnBind() const{YT_PROFILE_FUNCTION();glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);}
}

RenderAPI.h:创建DrawIndexed方法根据索引绘制图像:

#pragma once
#include<glm/glm.hpp>
#include "VertexArray.h"
namespace YOTO {class RendererAPI{public:enum class API {None = 0,OpenGL = 1};public:virtual void Init() = 0;virtual void SetClearColor(const glm::vec4& color)=0;virtual void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) = 0;virtual void Clear() = 0;virtual void DrawIndexed(const Ref<VertexArray>& vertexArray,uint32_t indexCount = 0)=0;inline static API GetAPI() { return s_API; }private:static API s_API;};
}

OpenGLRendererAPI.cpp: 

#include "ytpch.h"
#include "OpenGLRendererAPI.h"
#include <glad/glad.h>
namespace YOTO {void OpenGLRendererAPI::Init(){YT_PROFILE_FUNCTION();//启用混合glEnable(GL_BLEND);//设置混合函数glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);//深度测试glEnable(GL_DEPTH_TEST);}void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height){glViewport(x, y, width, height);}void OpenGLRendererAPI::SetClearColor(const glm::vec4& color){glClearColor(color.r, color.g, color.b, color.a);}void OpenGLRendererAPI::Clear(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);}void OpenGLRendererAPI::DrawIndexed(const Ref<VertexArray>& vertexArray, uint32_t indexCount){uint32_t count = indexCount ? vertexArray->GetIndexBuffer()->GetCount() : indexCount;glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr);glBindTexture(GL_TEXTURE_2D, 0);}
}

RenderCommand.h: 对API的DrawIndexed封装:

#pragma once
#include"RendererAPI.h"
namespace YOTO {class RenderCommand{public:inline static void Init() {s_RendererAPI->Init();}inline static void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {s_RendererAPI->SetViewport(x,y,width,height);}inline static void SetClearColor(const glm::vec4& color) {s_RendererAPI->SetClearColor(color);}inline static void Clear() {s_RendererAPI->Clear();}inline static void DrawIndexed(const Ref<VertexArray>& vertexArray,uint32_t count=0) {s_RendererAPI->DrawIndexed(vertexArray, count);}private:static RendererAPI* s_RendererAPI;};}

 调用:

Texture.glsl:先改下shader

#type vertex
#version 330 corelayout(location = 0) in vec3 a_Position;
layout(location = 1) in vec4 a_Color;
layout(location = 2) in vec2 a_TexCoord;uniform mat4 u_ViewProjection;
// uniform mat4 u_Transform;out vec4 v_Color;
out vec2 v_TexCoord;void main() {v_Color = a_Color;v_TexCoord = a_TexCoord;// 由规则动态生成的顶点位置(基于本地空间)没有涉及transform变换顶点位置// gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0); gl_Position = u_ViewProjection * vec4(a_Position, 1.0);
}
#type fragment
#version 330 corelayout(location = 0) out vec4 color;in vec4 v_Color;
in vec2 v_TexCoord;uniform vec4 u_Color;
uniform float u_TilingFactor;uniform sampler2D u_Texture;void main() {color = v_Color;
}

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>
#include<vector>
#include<chrono>
template<typename Fn>
class Timer {
public:Timer(const char* name, Fn&&func):m_Name(name),m_Func(func),m_Stopped(false){m_StartTimepoint = std::chrono::high_resolution_clock::now();}~Timer() {if (!m_Stopped) {Stop();}}void Stop() {auto endTimepoint= std::chrono::high_resolution_clock::now();long long start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();m_Stopped = true;float duration = (end - start)*0.001f;m_Func({m_Name,duration});//std::cout << "Timer:"<< m_Name << "时差:" << duration << "ms" << std::endl;}
private:const char* m_Name;std::chrono::time_point<std::chrono::steady_clock>m_StartTimepoint;bool m_Stopped;Fn m_Func;
};
//未找到匹配的重载:auto的问题,改回原来的类型就好了
#define PROFILE_SCOPE(name) Timer timer##__LINE__(name,[&](ProfileResult profileResult) {m_ProfileResults.push_back(profileResult);})
Sandbox2D::Sandbox2D()
:Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true) 
{
}
void Sandbox2D::OnAttach()
{YT_PROFILE_FUNCTION();m_CheckerboardTexture = YOTO::Texture2D::Create("assets/textures/Checkerboard.png");}
void Sandbox2D::OnDetach()
{YT_PROFILE_FUNCTION();
}void Sandbox2D::OnUpdate(YOTO::Timestep ts)
{YT_PROFILE_FUNCTION();//updatem_CameraController.OnUpdate(ts);{YT_PROFILE_SCOPE("Sandbox2D::Renderer Prep");//RenderYOTO::RenderCommand::SetClearColor({ 0.2f, 0.2f, 0.2f, 1.0f });YOTO::RenderCommand::Clear();}{YT_PROFILE_SCOPE("Sandbox2D::Renderer Draw");YOTO::Renderer2D::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)));*///	YOTO::Renderer2D::DrawRotatedQuad({ -1.0f,0.0f }, { 0.8f,0.8f }, glm::radians(45.0f),{ 0.8f,0.2f,0.3f,1.0f });YOTO::Renderer2D::DrawQuad({ -1.0f,0.0f }, { 0.8f,0.8f }, { 0.8f,0.2f,0.3f,1.0f });YOTO::Renderer2D::DrawQuad({ 0.5f,-0.5f }, { 0.5f,0.75f }, { 0.2f,0.3f,0.8f,1.0f });//YOTO::Renderer2D::DrawQuad({ 0.0f,0.0f,-0.1f }, { 10.0f,10.0f }, m_CheckerboardTexture,10.0f,glm::vec4(1.0f,0.9f,0.9f,1.0f));YOTO::Renderer2D::EndScene();}}}
void Sandbox2D::OnImGuiRender()
{YT_PROFILE_FUNCTION();ImGui::Begin("Setting");ImGui::ColorEdit4("Color", glm::value_ptr(m_SquareColor));for (auto& res : m_ProfileResults) {char lable[50];strcpy(lable, "%.3fms  ");strcat(lable, res.Name);ImGui::Text(lable, res.Time);}m_ProfileResults.clear();ImGui::End();
}void Sandbox2D::OnEvent(YOTO::Event& e)
{YT_PROFILE_FUNCTION();m_CameraController.OnEvent(e);
}

cool! 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/286284.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网络安全-文件包含

一、php://input 我们先来看一个简单的代码 <meta charset"utf8"> <?php error_reporting(0); $file $_GET["file"]; if(stristr($file,"php://filter") || stristr($file,"zip://") || stristr($file,"phar://&quo…

如何忽略Chrome最小字号的限制

通过控制台调整字体大小时&#xff0c;可以发现即便设置了小于12px的字号&#xff0c;也并不会变小&#xff0c;这是因为Chrome默认最小字号为12px。 在Chrome设置中的外观选项卡中可以发现&#xff0c;默认字体是16px。将最小字号改为0&#xff0c;就能随意设置小于12px的字号…

我在京东做数据分析,一位京东数据分析师的工作日常

有人说&#xff1a;“种下一棵树最好的时间是十年前&#xff0c;其次是现在”。任何时候&#xff0c;我们都应该抓住机遇&#xff0c;说不定就是改变你现状的一个机会。 2020年&#xff0c;我在疫情得到控制后&#xff0c;面试入职京东大数据组&#xff0c;截止目前&#xff0…

libVLC 视频裁剪

使用 libVLC 进行视频裁剪并不是直接支持的功能&#xff0c;因为 libVLC 主要是一个媒体播放库。然而&#xff0c;你可以通过调整播放窗口的大小和设置视频输出的区域来实现一种“视觉上的裁剪”。这意味着视频本身并没有被修改&#xff0c;但可以控制显示给用户的视频区域。 …

视频推拉流EasyDSS点播平台云端录像播放异常的问题排查与解决

视频推拉流EasyDSS视频直播点播平台可提供一站式的视频转码、点播、直播、视频推拉流、播放H.265视频等服务&#xff0c;搭配RTMP高清摄像头使用&#xff0c;可将无人机设备的实时流推送到平台上&#xff0c;实现无人机视频推流直播、巡检等应用。 有用户反馈&#xff0c;项目现…

Mysql数据库:日志管理、备份与恢复

目录 前言 一、MySQL日志管理 1、存放日志和数据文件的目录 2、日志的分类 2.1 错误日志 2.2 通用查询日志 2.3 二进制日志 2.4 慢查询日志 2.5 中继日志 3、日志综合配置 4、查询日志是否开启 二、数据备份概述 1、数据备份的重要性 2、备份类型 2.1 从物理与…

SpringBoot学习之ElasticSearch下载安装和启动(Windows版)(三十)

本文先写windows下的下载安装和启动,后续有时间再补充其他环境下(Mac、Linux、Docker)的,这里我们后续对ElasticSearch简称为ES,读者习惯这一称呼就好。 一,ES下载 可以百度【ElasticSearch官网】或者直接点击这里的ES官网下载地址:​​​​​ Download Elasticsearch…

数字乡村战略实施:科技引领农村经济社会全面发展

随着信息技术的快速发展&#xff0c;数字化已经成为推动经济社会发展的重要力量。在乡村振兴战略的大背景下&#xff0c;数字乡村战略的实施成为了引领农村经济社会全面发展的关键。本文将从数字乡村战略的内涵、实施现状、面临挑战及未来展望等方面&#xff0c;探讨科技如何引…

Mac 装 虚拟机 vmware、centos7等

vmware&#xff1a; https://www.vmware.com/products/fusion.html centos7 清华镜像&#xff1a; 暂时没有官方的 m1 arm架构镜像 centos7 链接: https://pan.baidu.com/s/1oZw1cLyl6Uo3lAD2_FqfEw?pwdzjt4 提取码: zjt4 复制这段内容后打开百度网盘手机App&#xff0c;操…

[flask]cookie的基本使用/

彻底理解 Cookie - 知乎 (zhihu.com) 是什么 cookie是当你浏览某个网站的时候&#xff0c;由web服务器存储在你的机器硬盘上的一个小的文本文件。它其中记录了你的用户名、密码、浏览的网页、停留的时间等等信息。当你再次来到这个网站时&#xff0c;web服务器会先看看有没有…

[音视频学习笔记]八、FFMpeg结构体分析 -上一个项目用到的数据结构简单解析:AVFrame、AVFormatContext、AVCodecContext

前言 上次我们做了一个简单的视频解码&#xff0c;MediaPlay-FFmpeg - Public 这一次简单对这个代码进行一个剖析&#xff0c;对其中的数据结构进行一个解析。 这些数据结构之间的关系 AVFrame 、AVFormatContext 、AVCodecContext 、AVIOContext 、AVCodec 、AVStream 、AV…

flask_restful规范返回值之类型设置

大型的互联网项目中&#xff0c;返回的数据格式&#xff0c;有时是比较复杂的结构。 如&#xff1a;豆瓣电影 https://movie.douban.com/j/chart/top_list?type24&interval_id 100%3A90&action&start20&limit20 返回的值里有 json 或者列表数据&#xff0c…

第十一届蓝桥杯大赛第二场省赛试题 CC++ 研究生组-子串分值和

solution1&#xff08;通过40%&#xff09; 依次求子串并统计出现过的字母个数 #include<iostream> #include<string> #include<set> using namespace std; int main(){string s, subs;cin >> s;int len s.size(), ans 0;for(int j 1; j < len…

vscode下c++的boost库安装

Boost Downloadshttps://www.boost.org/users/download/下载最新的库文件。在shell中&#xff0c;使用命令bootstrap.bat gcc生成b2.exe文件。然后是.\b2.exe toolsetgcc生成库文件&#xff0c;在stage\lib文件夹下把stage\lib文件夹中的库文件拷贝到mingw64\x86_64-w64-mingw3…

Node.js之沙盒专题

​ Node.js一直是薄弱项&#xff0c;今天特意整理一下&#xff0c;基本上是各个大佬写的大杂烩&#xff0c;仅用于学习记录~~~ 1. child_process 首先介绍一下nodejs中用来执行系统命令的模块child_process。Nodejs通过使用child_process模块来生成多个子进程来处理其他事物…

(三)Qt+OpenCV调用海康工业相机SDK抓拍示例

系列文章目录 提示&#xff1a;这里是该系列文章的所有文章的目录 第一章&#xff1a; &#xff08;一&#xff09;QtOpenCV调用海康工业相机SDK示例开发 第二章&#xff1a; &#xff08;二&#xff09;Qt多线程实现海康工业相机图像实时采集 第三章&#xff1a; &#xff08;…

命令模式(请求与具体实现解耦)

目录 前言 UML plantuml 类图 实战代码 模板 Command Invoker Receiver Client 前言 命令模式解耦了命令请求者&#xff08;Invoker&#xff09;和命令执行者&#xff08;receiver&#xff09;&#xff0c;使得 Invoker 不再直接引用 receiver&#xff0c;而是依赖于…

学习vue3第十一节(依赖注入:provide/inject)

本机介绍&#xff1a;provide/inject 注意&#xff1a;大家在看此小节时候&#xff0c;默认大家已经了解一些组件的使用方法 1、依赖注入的用途&#xff1a; 当嵌套层级多的时候&#xff0c;某个子组件需要较远层级的父组件数据时候&#xff0c;如果我们依然使用props 传递数…

数据挖掘与机器学习 1. 绪论

于高山之巅&#xff0c;方见大河奔涌&#xff1b;于群峰之上&#xff0c;便觉长风浩荡 —— 24.3.24 一、数据挖掘和机器学习的定义 1.数据挖掘的狭义定义 背景&#xff1a;大数据时代——知识贫乏 数据挖掘的狭义定义&#xff1a; 数据挖掘就是从大量的、不完全的、有噪声的、…

CI/CD实战-jenkins结合docker 5

实验准备&#xff1a; 在docker主机要下载git工具 禁掉key的校验 确保在立即构建项目时不会出现任何报错&#xff1a; 自动化构建docker镜像 在server3上安装docker-ce 修改内核参数 拷贝证书 添加默认仓库 添加harbor仓库的解析 测试拉取 登录harbor私有仓库 在jenkins安装d…