跟着cherno手搓游戏引擎【25】封装2DRenderer,封装shader传参,自定义Texture

封装2DRenderer:

Renderer.h:

#include"ytpch.h"
#include"Renderer.h"
#include <Platform/OpenGL/OpenGLShader.h>
#include"Renderer2D.h"
namespace YOTO {Renderer::SceneData* Renderer::m_SceneData = new	Renderer::SceneData;void Renderer::Init(){RenderCommand::Init();Renderer2D::Init();}void Renderer::OnWindowResize(uint32_t width, uint32_t height){RenderCommand::SetViewport(0, 0, width, height);}void Renderer::BeginScene(OrthographicCamera& camera){m_SceneData->ViewProjectionMatrix = camera.GetViewProjectionMatrix();}void Renderer::EndScene(){}void Renderer::Submit(const Ref<Shader>& shader, const Ref<VertexArray>& vertexArray, const glm::mat4& transform){shader->Bind();std::dynamic_pointer_cast<OpenGLShader>(shader)->UploadUniformMat4("u_ViewProjection", m_SceneData->ViewProjectionMatrix);std::dynamic_pointer_cast<OpenGLShader>(shader)->UploadUniformMat4("u_Transform", transform);/*	mi.Bind();*/vertexArray->Bind();RenderCommand::DrawIndexed(vertexArray);}
}

Renderer2D.h:专门渲染2D的类,负责“画什么”的问题

#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 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);static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D> texture);private:private:};
}

 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 {struct  Renderer2DStorage {Ref<VertexArray> QuadVertexArray;//Ref<Shader> FlatColorShader;Ref<Shader> TextureShader;Ref<Texture2D> WhiteTexture;};static Renderer2DStorage* s_Data;void Renderer2D::Init(){s_Data = new  Renderer2DStorage();s_Data->QuadVertexArray = VertexArray::Create();float squareVertices[5 * 4] = {-0.5f,-0.5f,0.0f,0.0f,0.0f,0.5f,-0.5f,0.0f,1.0f,0.0f,0.5f,0.5f,0.0f,1.0f,1.0f,-0.5f,0.5f,0.0f,0.0f,1.0f,};Ref<VertexBuffer> squareVB;squareVB.reset(VertexBuffer::Create(squareVertices, sizeof(squareVertices)));squareVB->SetLayout({{ShaderDataType::Float3,"a_Position"},{ShaderDataType::Float2,"a_TexCoord"}});s_Data->QuadVertexArray->AddVertexBuffer(squareVB);uint32_t squareIndices[6] = { 0,1,2,2,3,0 };Ref<IndexBuffer> squareIB;squareIB.reset((IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))));s_Data->QuadVertexArray->AddIndexBuffer(squareIB);s_Data->WhiteTexture = Texture2D::Create(1, 1);uint32_t whiteTextureData = 0xffffffff;s_Data->WhiteTexture->SetData(&whiteTextureData,sizeof(uint32_t));//s_Data->FlatColorShader =Shader::Create("assets/shaders/FlatColor.glsl");s_Data->TextureShader= Shader::Create("assets/shaders/Texture.glsl");s_Data->TextureShader->Bind();s_Data->TextureShader->SetInt("u_Texture", 0);}void Renderer2D::ShutDown(){delete s_Data;}void Renderer2D::BeginScene(const OrthographicCamera& camera){/*s_Data->FlatColorShader->Bind();s_Data->FlatColorShader->SetMat4("u_ViewProjection",camera.GetViewProjectionMatrix());*/s_Data->TextureShader->Bind();s_Data->TextureShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix());}void Renderer2D::EndScene(){}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){//s_Data->FlatColorShader->Bind();//s_Data->FlatColorShader->SetFloat4("u_Color", color);//s_Data->TextureShader->Bind();s_Data->TextureShader->SetFloat4("u_Color", color);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){DrawQuad({ position.x,position.y,0.0f }, size, texture);}void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D> texture){//s_Data->TextureShader->Bind();s_Data->TextureShader->SetFloat4("u_Color", {0.2f,0.3f,0.8f,0.5f});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);}
}

封装Shader传参:

Shader.h:添加SetInt、SetFloat3...等等Set方法

#pragma once
#include <string>
#include<glm/glm.hpp>
namespace YOTO {class Shader {public:virtual~Shader()=default;virtual void Bind()const=0;virtual void UnBind()const=0;virtual void SetInt(const std::string& name,int value) = 0;virtual void SetFloat3(const std::string& name, const glm::vec3& value) = 0;virtual void SetFloat4(const std::string& name, const glm::vec4& value) = 0;virtual void SetMat4(const std::string& name, const glm::mat4& value) = 0;virtual const std::string& GetName()const = 0;static Ref<Shader> Create(const std::string& filepath);static Ref<Shader> Create(const std::string&name, const std::string& vertexSrc, const std::string& fragmentSrc);};class ShaderLibrary {public:void Add(const Ref<Shader>& shader);void Add(const std::string &name,const Ref<Shader>& shader);Ref<Shader> Load(const std::string filepath);Ref<Shader> Load(const std::string &name,const std::string filepath);Ref<Shader> Get(const std::string& name);bool Exists(const std::string& name);private:std::unordered_map<std::string,Ref<Shader>> m_Shaders;};
}

OpenGLShader.h:同Shader.h

#pragma once
#include <string>
#include "YOTO/Renderer/Shader.h"
#include <glm/glm.hpp>
typedef unsigned int GLenum;
namespace YOTO {class OpenGLShader:public Shader {public:OpenGLShader(const std::string& filepath);OpenGLShader(const std::string &name,const std::string& vertexSrc, const std::string& fragmentSrc);~OpenGLShader();void Bind()const override;void UnBind()const override;virtual void SetInt(const std::string& name, int value) override;virtual void SetFloat3(const std::string& name, const glm::vec3& value) override;virtual void SetFloat4(const std::string& name, const glm::vec4& value)override;virtual void SetMat4(const std::string& name, const glm::mat4& value) override;virtual const std::string& GetName()const override { return m_Name; }void UploadUniformMat4(const std::string& name, const glm::mat4& matrix);void UploadUniformMat3(const std::string& name, const glm::mat3& matrix);void UploadUniformFloat4(const std::string& name, const glm::vec4& values);void UploadUniformFloat3(const std::string& name, const glm::vec3& values);void UploadUniformFloat2(const std::string& name, const glm::vec2& values);void UploadUniformFloat(const std::string& name, float values);void UploadUniformInt(const std::string& name, int values);private:std::string ReadFile(const std::string filepath);std::unordered_map<GLenum,std::string> PreProcess(const std::string& source);void Compile(const std::unordered_map<GLenum, std::string>& shaderSources);private:uint32_t m_RendererID;std::string m_Name;};
} 

OpenGLShader.cpp: 

#include "ytpch.h"
#include "OpenGLShader.h"#include <glad/glad.h>
#include<glm/gtc/type_ptr.hpp>
namespace YOTO {static GLenum ShaderTypeFromString(const std::string& type) {if (type == "vertex") {return GL_VERTEX_SHADER;}if (type == "fragment" || type == "pixel") {return GL_FRAGMENT_SHADER;}YT_CORE_ASSERT(false, "不知道的shader类型");return 0;}OpenGLShader::OpenGLShader(const std::string& filepath){std::string source = ReadFile(filepath);YT_CORE_ASSERT(source.size(), "GLSL读取的字符串为空");auto shaderSources = PreProcess(source);Compile(shaderSources);auto lastSlash = filepath.find_last_of("/\\");lastSlash = lastSlash == std::string::npos ? 0 : lastSlash + 1;auto lastDot = filepath.rfind('.');auto count = lastDot == std::string::npos ? filepath.size() - lastSlash : lastDot - lastSlash;m_Name=filepath.substr(lastSlash, count);}OpenGLShader::OpenGLShader(const std::string& name, const std::string& vertexSrc, const std::string& fragmentSrc):m_Name(name){std::unordered_map<GLenum, std::string >sources;sources[GL_VERTEX_SHADER] = vertexSrc;sources[GL_FRAGMENT_SHADER] = fragmentSrc;Compile(sources);}OpenGLShader::~OpenGLShader(){glDeleteProgram(m_RendererID);}std::string OpenGLShader::ReadFile(const std::string filepath){std::string result;std::ifstream in(filepath, std::ios::in | std::ios::binary);if (in) {in.seekg(0, std::ios::end);			// 将指针放在最后面result.resize(in.tellg());			// 初始化string的大小, in.tellg()返回位置in.seekg(0, std::ios::beg);			// in指回头部in.read(&result[0], result.size());	// in读入放在result指向的内存中}else {YT_CORE_ERROR("不能打开文件:{0}", filepath);}return result;}std::unordered_map<GLenum, std::string> OpenGLShader::PreProcess(const std::string& source){std::unordered_map<GLenum, std::string> shaderSources;std::string typeToken = "#type";size_t typeTokenLen = typeToken.size();size_t findCurPos = source.find(typeToken, 0);size_t findNextPos = findCurPos;while (findNextPos != std::string::npos) {size_t curlineEndPos = source.find_first_of("\r\n", findCurPos);///r/n写错为/r/nYT_CORE_ASSERT(curlineEndPos != std::string::npos, "解析shader失败");size_t begin = findCurPos + typeTokenLen + 1;std::string type = source.substr(begin, curlineEndPos - begin);// 获取到是vertex还是fragmentYT_CORE_ASSERT(ShaderTypeFromString(type), "无效的shader的类型	");size_t nextLinePos = source.find_first_not_of("\r\n", curlineEndPos);findNextPos = source.find(typeToken, nextLinePos);// 获取到具体的shader代码shaderSources[ShaderTypeFromString(type)] = source.substr(nextLinePos, findNextPos - (nextLinePos == std::string::npos ? source.size() - 1 : nextLinePos));findCurPos = findNextPos;}return shaderSources;/*用find,而不是find_firtst_of,因为find返回完全匹配的字符串的的位置;find_first_of返回被查匹配字符串中某个字符的第一次出现位置。std::string::npos是一个非常大的数source.substr(0, source.size() + 10000)截取到从头到末尾,不会报错*/}void OpenGLShader::Compile(const std::unordered_map<GLenum, std::string>& shaderSources){GLuint program = glCreateProgram();YT_CORE_ASSERT(shaderSources.size()<=2,"OpenGLShader:shader只支持两种!")std::array<GLenum,2>glShaderIDs;int glShaderIDIndex=0;for (auto& kv : shaderSources) {GLenum type = kv.first;const std::string& source = kv.second;// Create an empty vertex shader handleGLuint shader = glCreateShader(type);// Send the vertex shader source code to GL// Note that std::string's .c_str is NULL character terminated.const GLchar* sourceCStr = source.c_str();glShaderSource(shader, 1, &sourceCStr, 0);// Compile the vertex shaderglCompileShader(shader);GLint isCompiled = 0;glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);if (isCompiled == GL_FALSE){GLint maxLength = 0;glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);// The maxLength includes the NULL characterstd::vector<GLchar> infoLog(maxLength);glGetShaderInfoLog(shader, maxLength, &maxLength, &infoLog[0]);// We don't need the shader anymore.glDeleteShader(shader);// Use the infoLog as you see fit.// In this simple program, we'll just leaveYT_CORE_ERROR("{0} ", infoLog.data());YT_CORE_ASSERT(false, "shader 编译失败!");break;}// Attach our shaders to our programglAttachShader(program, shader);glShaderIDs[glShaderIDIndex++]=shader;}// Link our programglLinkProgram(program);// Note the different functions here: glGetProgram* instead of glGetShader*.GLint isLinked = 0;glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked);if (isLinked == GL_FALSE){GLint maxLength = 0;glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);// The maxLength includes the NULL characterstd::vector<GLchar> infoLog(maxLength);glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);// We don't need the program anymore.glDeleteProgram(program);// Don't leak shaders either.for (auto id : glShaderIDs) {glDeleteShader(id);}// Use the infoLog as you see fit.// In this simple program, we'll just leaveYT_CORE_ERROR("{0} ", infoLog.data());YT_CORE_ASSERT(false, "shader link failure!");return;}// Always detach shaders after a successful link.for (auto id : glShaderIDs) {glDetachShader(program, id);}m_RendererID = program;}void OpenGLShader::Bind() const{glUseProgram(m_RendererID);}void OpenGLShader::UnBind() const{glUseProgram(0);}void OpenGLShader::SetInt(const std::string& name, int value){UploadUniformInt(name, value);}void OpenGLShader::SetFloat3(const std::string& name, const glm::vec3& value){UploadUniformFloat3(name, value);}void OpenGLShader::SetFloat4(const std::string& name, const glm::vec4& value){UploadUniformFloat4(name, value);}void OpenGLShader::SetMat4(const std::string& name, const glm::mat4& value){UploadUniformMat4(name, value);}void OpenGLShader::UploadUniformMat4(const std::string& name, const glm::mat4& matrix){GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());glUniformMatrix4fv(loacation, 1, GL_FALSE, glm::value_ptr(matrix));}void OpenGLShader::UploadUniformMat3(const std::string& name, const glm::mat3& matrix){GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());glUniformMatrix3fv(loacation, 1, GL_FALSE, glm::value_ptr(matrix)); }void OpenGLShader::UploadUniformFloat4(const std::string& name, const glm::vec4& values){GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());glUniform4f(loacation, values.x, values.y, values.z, values.w);}void OpenGLShader::UploadUniformFloat3(const std::string& name, const glm::vec3& values){GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());glUniform3f(loacation, values.x, values.y, values.z);}void OpenGLShader::UploadUniformFloat2(const std::string& name, const glm::vec2& values){GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());glUniform2f(loacation, values.x, values.y);}void OpenGLShader::UploadUniformFloat(const std::string& name, float values){GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());glUniform1f(loacation, values);}void OpenGLShader::UploadUniformInt(const std::string& name, int values){GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());glUniform1i(loacation, values);}}

添加自定义Texture方法:

Texture.h:添加新的Create方法:根据宽高添加

#pragma once
namespace YOTO {class Texture{public:virtual ~Texture() = default;virtual uint32_t GetWidth()const = 0;virtual uint32_t GetHeight()const = 0;virtual void SetData(void* data, uint32_t size) = 0;virtual void Bind(uint32_t slot=0)const = 0;};class Texture2D :public Texture {public:static Ref<Texture2D>Create(uint32_t width,uint32_t height);static Ref<Texture2D>Create(const std::string& path);};
}

Texture.cpp: 

#include "ytpch.h"
#include "Texture.h"
#include"Renderer.h"
#include "Platform/OpenGL/OpenGLTexture.h"
namespace YOTO {Ref<Texture2D> Texture2D::Create(uint32_t width, uint32_t height){switch (Renderer::GetAPI()){case RendererAPI::API::None:YT_CORE_ASSERT(false, "Texture2D:API为None不支持");return nullptr;case RendererAPI::API::OpenGL:return  CreateRef<OpenGLTexture2D>(width,height);}YT_CORE_ASSERT(false, "Buffer:未知API");return nullptr;}Ref<Texture2D> Texture2D::Create(const std::string& path){switch (Renderer::GetAPI()){case RendererAPI::API::None:YT_CORE_ASSERT(false, "Texture2D:API为None不支持");return nullptr;case RendererAPI::API::OpenGL:return  CreateRef<OpenGLTexture2D>(path);}YT_CORE_ASSERT(false, "Buffer:未知API");return nullptr;}
}

OpenGLTexture.h:同Texture.h相同,且添加设置texture数据的方法SetData:

#pragma once
#include"YOTO/Renderer/Texture.h"
#include<glad/glad.h>
namespace YOTO {class OpenGLTexture2D:public Texture2D{public:OpenGLTexture2D(uint32_t width,uint32_t height);OpenGLTexture2D(const std::string path);virtual~OpenGLTexture2D();virtual uint32_t GetWidth()const override { return m_Width; }virtual uint32_t GetHeight()const override { return m_Height; }virtual void SetData(void* data, uint32_t size)override;virtual void Bind(uint32_t slot=0)const override;private:std::string m_Path;uint32_t m_Width, m_Height;uint32_t m_RendererID;GLenum m_InternalFormat, m_DataFormat;};
}

OpenGLTexture.cpp: 

#include "ytpch.h"
#include "OpenGLTexture.h"
#include<glad/glad.h>
#include"stb_image.h"
namespace YOTO {OpenGLTexture2D::OpenGLTexture2D(uint32_t width, uint32_t height):m_Width(width),m_Height(height){m_InternalFormat = GL_RGBA8;m_DataFormat = GL_RGBA;glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);///告诉OpenGLm_RendererID的纹理存储的是rbg8位,宽高的缓冲区glTextureStorage2D(m_RendererID, 1, m_InternalFormat, m_Width, m_Height);//配置参数:纹理放大时用周围颜色的平均值过滤glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT);glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT);}OpenGLTexture2D::OpenGLTexture2D(const std::string path):m_Path(path){int width, height, channels;stbi_set_flip_vertically_on_load(1);//翻转stbi_uc*data=stbi_load(path.c_str(),&width,&height,&channels,0);YT_CORE_ASSERT(data, "图片加载错误");m_Width = width;m_Height = height;GLenum internalFormat = 0,dataFormat=0;if (channels == 4) {internalFormat = GL_RGBA8;dataFormat = GL_RGBA;}else if (channels==3) {internalFormat = GL_RGB8;dataFormat = GL_RGB;}m_InternalFormat = internalFormat;m_DataFormat = dataFormat;YT_CORE_ASSERT(internalFormat& dataFormat,"OpenGLTexture2D:不支持的颜色格式")//创建纹理glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);///告诉OpenGLm_RendererID的纹理存储的是rbg8位,宽高的缓冲区glTextureStorage2D(m_RendererID, 1, internalFormat,m_Width,m_Height);//配置参数:纹理放大时用周围颜色的平均值过滤glTextureParameteri(m_RendererID,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT);glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT);glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, dataFormat,GL_UNSIGNED_BYTE,data);stbi_image_free(data);}OpenGLTexture2D::~OpenGLTexture2D(){glDeleteTextures(1,&m_RendererID);}void OpenGLTexture2D::SetData(void* data, uint32_t size){uint32_t bpc = m_DataFormat == GL_RGBA ? 4 : 3;YT_CORE_ASSERT(size == m_Width * m_Height * bpc,"OpenGLTexture2D:数据必须是完整的!");glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height,m_DataFormat, GL_UNSIGNED_BYTE,data);}void OpenGLTexture2D::Bind(uint32_t slot) const{glBindTextureUnit(slot, m_RendererID);}
}

Sandbox2D.h:添加m_CheckerboardTexture

#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;YOTO::Ref<YOTO::Texture2D>m_CheckerboardTexture;glm::vec4 m_SquareColor = { 0.2f,0.3f,0.7f,1.0f };
};

化简Sandbox2D:

Sandbox2D.cpp:把传shader参数的代码注释掉 ,统一使用DrawQuad封装;负责“怎么画”的问题:

#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_CheckerboardTexture = YOTO::Texture2D::Create("assets/textures/Checkerboard.png");}
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::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::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);YOTO::Renderer2D::EndScene();}
}
void Sandbox2D::OnImGuiRender()
{ImGui::Begin("设置");ImGui::ColorEdit4("正方形颜色", glm::value_ptr(m_SquareColor));ImGui::End();
}void Sandbox2D::OnEvent(YOTO::Event& e)
{m_CameraController.OnEvent(e);
}

小修改:

OpenGLRendererAPI.cpp:DrawIndexed中添加解绑Texture:

#include "ytpch.h"
#include "OpenGLRendererAPI.h"
#include <glad/glad.h>
namespace YOTO {void OpenGLRendererAPI::Init(){//启用混合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){glDrawElements(GL_TRIANGLES, vertexArray->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr);glBindTexture(GL_TEXTURE_2D, 0);}
}

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;	}

Texture.glsl:

		#type vertex#version 330 corelayout(location = 0) in vec3 a_Position;layout(location = 1) in vec2 a_TexCoord;uniform mat4 u_ViewProjection;uniform mat4 u_Transform;out vec2 v_TexCoord;out vec3 v_Position;void main(){v_TexCoord=a_TexCoord;v_Position=a_Position;gl_Position =u_ViewProjection*u_Transform*vec4( a_Position,1.0);}#type fragment#version 330 corelayout(location = 0) out vec4 color;in vec3 v_Position;in vec2 v_TexCoord;uniform vec4 u_Color ;uniform sampler2D u_Texture ;void main(){color = texture(u_Texture, v_TexCoord*10.0f)*u_Color;	}

YOTO.h:添加:

#include"YOTO/Renderer/Renderer2D.h"

Core.h:

#pragma once
#include<memory>
//用于dll的宏
#ifdef YT_PLATFORM_WINDOWS
#if YT_DYNAMIC_LINK#ifdef YT_BUILD_DLL#define YOTO_API __declspec(dllexport) #else#define YOTO_API __declspec(dllimport) #endif // DEBUG
#else#define YOTO_API
#endif
#else
#error YOTO_ONLY_SUPPORT_WINDOWS
#endif // YOTO_PLATFORM_WINDOWS#ifdef YT_DEBUG
#define YT_ENABLE_ASSERTS
#endif#ifdef YT_ENABLE_ASSERTS
#define YT_CLIENT_ASSERT(x,...) {if(!(x)){YT_CLIENT_ERROR("断言错误:{0}",__VA_ARGS__);__debugbreak();}}
#define YT_CORE_ASSERT(x,...) {if(!(x)){YT_CORE_ERROR("断言错误:{0}",__VA_ARGS__);__debugbreak();}}
#else
#define YT_CLIENT_ASSERT(x,...)
#define YT_CORE_ASSERT(x,...)#endif // YT_ENABLE_ASSERTS#define BIT(x)(1<<x)
//绑定事件定义
#define YT_BIND_EVENT_FN(fn) std::bind(&fn, this, std::placeholders::_1)namespace YOTO {template<typename T>using  Scope = std::unique_ptr<T>;template<typename T>using  Ref = std::shared_ptr<T>;template<typename T,typename ...Args>constexpr Scope<T> CreateScope(Args&&...args) {return std::make_shared<T>(std::forward<Args>(args)...);}template<typename T, typename ...Args>constexpr Ref<T> CreateRef(Args&&...args) {return std::make_shared<T>(std::forward<Args>(args)...);}}

测试:

cool! 

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

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

相关文章

python中“全局变量”之谜

全局变量&#xff0c;是不是以为着在整个程序中的值都是一样的&#xff0c;是不是都是同一个呢&#xff1f; 我们通过下面的例子来看全局"变量之变” 上面的程序运行结果如下&#xff1a; 从上面的运行结果可以看出&#xff1a;尽管变量num被声明为全局变量&#xff0c;在…

测试基础2:接口测试入门儿 哟呼开心

单元测试&#xff1a;一段代码的功能是否正确&#xff0c;在软件开发中进行的测试活动&#xff1b; 单元测试框架&#xff1a; java&#xff1a;junit&#xff0c;testNG c#:NUint python&#xff1a;pytest、unittest接口测试&#xff1a;检测外部系统与系统之间以及内部各个子…

腾讯云4核8g云服务器能承受多少人访问?

腾讯云4核8G服务器支持多少人在线访问&#xff1f;支持25人同时访问。实际上程序效率不同支持人数在线人数不同&#xff0c;公网带宽也是影响4核8G服务器并发数的一大因素&#xff0c;假设公网带宽太小&#xff0c;流量直接卡在入口&#xff0c;4核8G配置的CPU内存也会造成计算…

MFC web文件 CHttpFile的使用初探

MFC CHttpFile的使用 两种方式&#xff0c;第一种OpenURL&#xff0c;第二种SendRequest&#xff0c;以前捣鼓过&#xff0c;今天再次整结果发现各种踩坑&#xff0c;好记性不如烂笔头&#xff0c;记录下来。 OpenURL 这种方式简单粗暴&#xff0c;用着舒服。 try {//OpenU…

图形系统开发实战课程:进阶篇(上)——7.图形交互操作: 视点控制与动画

图形开发学院&#xff5c;GraphAnyWhere 课程名称&#xff1a;图形系统开发实战课程&#xff1a;进阶篇(上)课程章节&#xff1a;“图形交互操作: 视点控制与动画”原文地址&#xff1a;https://www.graphanywhere.com/graph/advanced/2-7.html 第七章 图形交互操作: 视点控制与…

[云原生] 二进制安装K8S(中)部署网络插件和DNS

书接上文&#xff0c;我们继续部署剩余的插件 一、K8s的CNI网络插件模式 2.1 k8s的三种网络模式 K8S 中 Pod 网络通信&#xff1a; &#xff08;1&#xff09;Pod 内容器与容器之间的通信 在同一个 Pod 内的容器&#xff08;Pod 内的容器是不会跨宿主机的&#xff09;共享…

c语言经典测试题4

1.题1 #include <stdio.h>//没有break的话&#xff0c;输入什么都会往下一直执行下去&#xff0c;而且default在最后就会全都执行 int main() {char c;int v0 0, v1 0, v2 0;do{switch (c getchar())// 输入ADescriptor{casea:caseA:casee:caseE:casei:caseI:caseo:…

【Python笔记-设计模式】责任链模式

一、说明 旨在将请求沿着处理者链进行发送。收到请求后&#xff0c;每个处理者均可对请求进行处理&#xff0c;或将其传递给链上的下个处理者。 (一) 解决问题 将请求的发送者和接受者解耦&#xff0c;并使请求随着处理对象链传递&#xff0c;优化系统内部处理逻辑 (二) 使…

【算法 - 动态规划】找零钱问题Ⅰ

在前面的动态规划系列文章中&#xff0c;关于如何对递归进行分析的四种基本模型都介绍完了&#xff0c;再来回顾一下&#xff1a; 从左到右模型 &#xff1a;arr[index ...] 从 index 之前的不用考虑&#xff0c;只考虑后面的该如何选择 。范围尝试模型 &#xff1a;思考 [L ,…

【BUG 记录】MyBatis-Plus 处理枚举字段和 JSON 字段

【BUG 记录】MyBatis-Plus 处理枚举字段和JSON字段 一、枚举字段&#xff08;mysql环境已测、postgresql环境已测&#xff09;1.1 场景1.2 定义枚举常量1.3 配置枚举处理器1.4 测试 二、JSON字段&#xff08;mysql环境已测&#xff09;2.1 导包2.2 使用对象接受2.3 测试 三、JS…

图片录入设备、方式与质量对图片转Excel的影响

随着数字化时代的到来&#xff0c;图片已经成为人们日常生活中不可或缺的一部分。在各行各业中&#xff0c;图片的应用越发广泛&#xff0c;从而促使了图片处理技术的快速发展。然而&#xff0c;图片的质量对于后续数据处理和分析的准确性和可靠性有着至关重要的影响。本文将从…

【k8s配置与存储--持久化存储(PV、PVC、存储类)】

1、PV与PVC 介绍 持久卷&#xff08;PersistentVolume&#xff0c;PV&#xff09; 是集群中的一块存储&#xff0c;可以由管理员事先制备&#xff0c; 或者使用存储类&#xff08;Storage Class&#xff09;来动态制备。 持久卷是集群资源&#xff0c;就像节点也是集群资源一样…

年龄性别预测3:Android实现年龄性别预测和识别(含源码,可实时预测)

年龄性别预测3&#xff1a;Android实现年龄性别预测和识别(含源码&#xff0c;可实时预测) 目录 年龄性别预测3&#xff1a;Android实现年龄性别预测和识别(含源码&#xff0c;可实时预测) 1.年龄性别预测和识别方法 2.人脸检测方法 3.年龄性别预测和识别模型训练 &#…

【Golang】Golang使用embed加载、打包静态资源文件

【Golang】Golang使用embed加载、打包静态资源文件 大家好 我是寸铁&#x1f44a; 总结了一篇Golang使用embed加载静态资源文件的文章✨ 喜欢的小伙伴可以点点关注 &#x1f49d; 前言 事情是这样的&#xff1a;前不久&#xff0c;有同学问我,golang怎么把静态资源文件打包成一…

【Node.js】自动生成 API 文档

目录 1、直接使用swagger-ui-express 2、配合swagger-jsdoc 如何在Node.js项目中使用 Swagger 来自动生成 API接口文档&#xff0c;使用生成方式有很多种。本文基于swagger-jsdocswagger-ui-express快速实现 1、直接使用swagger-ui-express // 方便来浏览和测试api npm i sw…

GDPU Java 天码行空 1

&#x1f496; 配置环境 &#x1f468;‍&#x1f3eb; JDK17 配置教程 &#x1f338; CMD 查看本机 JDK 版本命令&#xff1a; java -version1. 输出 Hello World&#xff01; (1) 新建 Java 文件 文件名&#xff1a;HelloWorld.java 文件内容&#xff1a; public clas…

家政小程序有哪些功能 怎么制作

随着人们生活节奏的加快&#xff0c;家政服务变得越来越受到人们的青睐。为了提升家政服务的便捷性和高效性&#xff0c;家政小程序成为了越来越受欢迎的选择。下面具体介绍家政小程序有哪些功能&#xff0c;如何制作。 1. 展示家政服务 在小程序中&#xff0c;上传所有的家政…

C# OpenCvSharp DNN Yolov8-OBB 旋转目标检测

目录 效果 模型信息 项目 代码 下载 C# OpenCvSharp DNN Yolov8-OBB 旋转目标检测 效果 模型信息 Model Properties ------------------------- date&#xff1a;2024-02-26T08:38:44.171849 description&#xff1a;Ultralytics YOLOv8s-obb model trained on runs/DOT…

一周学会Django5 Python Web开发-Django5二进制文件下载响应

锋哥原创的Python Web开发 Django5视频教程&#xff1a; 2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~共计25条视频&#xff0c;包括&#xff1a;2024版 Django5 Python we…

【Git教程】(三)提交详解 —— add、commit、status、stach命令的说明,提交散列值与历史,多次提交及忽略 ~

Git教程 提交详解 1️⃣ 访问权限与时间戳2️⃣ add命令与 commit 命令3️⃣ 提交散列值4️⃣ 提交历史5️⃣ 一种特别的提交查看方法6️⃣ 同一项目的多部不同历史6.1 部分输出&#xff1a;-n6.2 格式化输出&#xff1a;--format、--oneline6.3 统计修改信息&#xff1a;--st…