Skip to content

Commit

Permalink
use single shader for two render
Browse files Browse the repository at this point in the history
  • Loading branch information
qiyuewuyi2333 committed Aug 19, 2024
1 parent 9459f2a commit d1c618e
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 58 deletions.
3 changes: 2 additions & 1 deletion Assets/Shaders/flat_texture/flat_texture.frag
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ in vec3 vPosition;
in vec2 vTextureCoord;

uniform sampler2D u_Texture;
uniform vec4 u_Color;

out vec4 fragColor;

void main()
{
// 使用正确的 mix 函数来混合纹理颜色与顶点颜色
vec4 texColor = texture(u_Texture, vTextureCoord.xy); // 获取纹理颜色
vec4 texColor = texture(u_Texture, vTextureCoord ) * u_Color; // 获取纹理颜色
fragColor = texColor; // 设置输出颜色
}
2 changes: 0 additions & 2 deletions Assets/Shaders/flat_texture/flat_texture.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec2 inTextureCoord;

out vec3 vPosition;
out vec2 vTextureCoord;

uniform mat4 u_ViewProjection;
uniform mat4 u_Transform;

void main()
{
vPosition = inPosition;
vTextureCoord = inTextureCoord;
gl_Position = u_ViewProjection * u_Transform * vec4(inPosition, 1.0);
}
17 changes: 0 additions & 17 deletions Huan/CMakeSettings.json

This file was deleted.

6 changes: 5 additions & 1 deletion Huan/include/Platform/OpenGL/Texture/OpenGLTexture2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

namespace Huan
{

class HUAN_API OpenGLTexture2D : public Texture2D
{
public:
OpenGLTexture2D(const std::string& path);
virtual ~OpenGLTexture2D();
OpenGLTexture2D(uint32_t width, uint32_t height);
~OpenGLTexture2D() override;
virtual void bind(uint32_t slot = 0) const override;
virtual void setData(void* data, unsigned int size) override;

virtual constexpr uint32_t getWidth() const override { return myWidth; }
virtual constexpr uint32_t getHeight() const override { return myWidth; }
virtual constexpr uint32_t getMyID() const override {return myRendererID; };
Expand Down
4 changes: 3 additions & 1 deletion Huan/include/Renderer/Renderer2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

namespace Huan
{

struct Renderer2DData
{
Ref<Camera> camera;
Ref<Shader> platSingleColorShader;
Ref<VertexArray> quadVertexArray;
Ref<VertexArray> quadTextureVertexArray;
Ref<Shader> quadTextureShader;
Ref<Texture> myTexture;
Ref<Texture> myWhiteTexture;
};


class HUAN_API Renderer2D
{
public:
Expand Down
1 change: 1 addition & 0 deletions Huan/include/Renderer/Texture/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Texture
virtual constexpr unsigned int getMyID() const = 0;

virtual void bind(unsigned int slot = 0) const = 0;
virtual void setData(void* data, unsigned int size) = 0;
};

} // namespace Huan
2 changes: 1 addition & 1 deletion Huan/src/Huan/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Renderer/Buffer/VertexBuffer.h"
#include "Renderer/RenderCommand.h"
#include "Renderer/Renderer.h"
#include "Renderer/Renderer2D.h"
#include "Renderer/Scene.h"
#include "Renderer/Shader.h"
#include "Renderer/Utils/Camera.h"
Expand Down Expand Up @@ -100,7 +101,6 @@ Application::Application() : myLayerStack()
myWindow = Scope<Window>(Window::create());
// use Application's onEvent as the window's callback func
myWindow->setEventCallback(BIND_EVENT_FUNC(Application::onEvent));
Renderer::getInstance().init();

imGuiLayer = new ImGuiLayer();
pushOverlay(imGuiLayer);
Expand Down
3 changes: 2 additions & 1 deletion Huan/src/Platform/OpenGL/OpenGLRendererAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Huan
{
void OpenGLRendererAPI::init()
{
// glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_TEST);
// glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Expand All @@ -22,6 +22,7 @@ void OpenGLRendererAPI::clear()
void OpenGLRendererAPI::drawIndexed(const VertexArray& vertexArray)
{
glDrawElements(GL_TRIANGLES, vertexArray.getIndexBuffer()->getCount(), GL_UNSIGNED_INT, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
}

void OpenGLRendererAPI::enableBlend()
Expand Down
1 change: 1 addition & 0 deletions Huan/src/Platform/OpenGL/OpenGLShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void OpenGLShader::readFile(const std::string& filepath)
}
shaderSources[shaderTypeFromString(p.extension().string())] = code;
myName = p.stem().string();

}
void OpenGLShader::bind() const
{
Expand Down
22 changes: 21 additions & 1 deletion Huan/src/Platform/OpenGL/Texture/OpenGLTexture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ OpenGLTexture2D::OpenGLTexture2D(const std::string& path) : myPath(path)
default:
HUAN_CORE_ASSERT(false, "Unsupported format");
}

glCreateTextures(GL_TEXTURE_2D, 1, &myRendererID);
glTextureStorage2D(myRendererID, 1, mySizedFormat, myWidth, myHeight);

Expand All @@ -44,6 +44,20 @@ OpenGLTexture2D::OpenGLTexture2D(const std::string& path) : myPath(path)
glGenerateTextureMipmap(myRendererID);
HUAN_CORE_TRACE("Loaded image {0} with width {1} and height {2}", path, width, height);
}
OpenGLTexture2D::OpenGLTexture2D(uint32_t width, uint32_t height) : myWidth(width), myHeight(height)
{
mySizedFormat = GL_RGBA8;
myFormat = GL_RGBA;

glCreateTextures(GL_TEXTURE_2D, 1, &myRendererID);
glTextureStorage2D(myRendererID, 1, mySizedFormat, myWidth, myHeight);

glTextureParameteri(myRendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(myRendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTextureParameteri(myRendererID, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(myRendererID, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
OpenGLTexture2D::~OpenGLTexture2D()
{
glDeleteTextures(1, &myRendererID);
Expand All @@ -54,4 +68,10 @@ void OpenGLTexture2D::bind(uint32_t slot) const
glBindTextureUnit(slot, myRendererID);
// HUAN_CORE_TRACE("Bind texture, ID: {0}, path: {1}, slot: {2}", myRendererID, myPath, slot);
}
void OpenGLTexture2D::setData(void* data, unsigned int size)
{
uint32_t bpp = myFormat == GL_RGBA ? 4 : 3;
HUAN_CORE_ASSERT(size == myWidth * myHeight * bpp, "Data must be entire texture");
glTextureSubImage2D(myRendererID, 0, 0, 0, myWidth, myHeight, myFormat, GL_UNSIGNED_BYTE, data);
}
} // namespace Huan
49 changes: 16 additions & 33 deletions Huan/src/Renderer/Renderer2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ Renderer2D::Renderer2D()

void Renderer2D::init()
{

myRenderCommand = std::make_unique<RenderCommand>();
myRenderCommand->init();

myData = new Renderer2DData();
myData->platSingleColorShader = std::make_shared<CurrentShader>("../../../../Assets/Shaders/flat/flat.vert",
"../../../../Assets/Shaders/flat/flat.frag");
myData->quadTextureShader = std::make_shared<CurrentShader>("../../../../Assets/Shaders/flat_texture/flat_texture.vert",
"../../../../Assets/Shaders/flat_texture/flat_texture.frag");
myData->quadTextureShader =
std::make_shared<CurrentShader>("../../../../Assets/Shaders/flat_texture/flat_texture.vert",
"../../../../Assets/Shaders/flat_texture/flat_texture.frag");
// data
float squareVertices[3 * 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};
uint32_t squareIndices[6] = {0, 1, 2, 2, 3, 0};
Expand All @@ -27,8 +28,7 @@ void Renderer2D::init()
// va vb ib
myData->quadVertexArray = std::make_shared<CurrentVertexArray>();
myData->quadVertexArray->bind();
Ref<VertexBuffer> squareVB =
std::make_shared<CurrentVertexBuffer>(squareVertices, sizeof(squareVertices));
Ref<VertexBuffer> squareVB = std::make_shared<CurrentVertexBuffer>(squareVertices, sizeof(squareVertices));
squareVB->bind();
BufferLayout squareLayout = {{ShaderDataType::Float3, "inVertice"}};
squareVB->setLayout(squareLayout);
Expand All @@ -50,6 +50,9 @@ void Renderer2D::init()
squareIB->bind();
myData->quadTextureVertexArray->setIndexBuffer(squareIB);

myData->myWhiteTexture = std::make_shared<Huan::CurrentTexture2D>(1, 1);
uint32_t whiteTextureData = 0xffffffff;
myData->myWhiteTexture->setData(&whiteTextureData, sizeof(uint32_t));
}

void Renderer2D::shutdown()
Expand All @@ -67,49 +70,26 @@ void Renderer2D::loadScene(Ref<Scene> scene)
myData->myTexture = myScene->getTexture();
}


void Renderer2D::beginScene()
{
HUAN_CORE_ASSERT((myScene != nullptr), "The scene of Renderer2D hasn't been loaded!");

myData->platSingleColorShader->bind();
myData->platSingleColorShader->setMat4("u_ViewProjection", myData->camera->getViewProjectionMatrix());
myData->quadTextureShader->bind();
myData->quadTextureShader->setMat4("u_ViewProjection", myData->camera->getViewProjectionMatrix());


if (myScene->uColor != nullptr)
myData->platSingleColorShader->setVec4("u_Color", *(myScene->uColor));

// 如果场景中包含纹理,则绑定该纹理并设置对应的uniform变量
/* if (myScene->getTexture() != nullptr)
{
myScene.getTexture()->bind();
shader->setInt("u_Texture", 0);
}*/

}

void Renderer2D::renderScene()
{
// 遍历场景中的所有顶点数组,对每个顶点数组进行渲染
for (auto& va : myScene->getVertexArrays())
{
// 绑定顶点数组,以便访问其包含的顶点数据
va->bind();

// 执行绘制命令,这里的“索引化绘制”是渲染过程中最关键的一环,它决定了模型如何根据顶点数据绘制到屏幕上

myRenderCommand->drawIndexed(*va);

// 解绑顶点数组,避免影响后续操作
va->unbind();
}
}

void Renderer2D::endScene()
{

}

void Renderer2D::setScene(Ref<Scene> scene)
Expand All @@ -124,8 +104,12 @@ void Renderer2D::drawQuad(const glm::vec2& position, const glm::vec2& size, cons

void Renderer2D::drawQuad(const glm::vec3& position, const glm::vec2& size, const Color& color)
{
myData->platSingleColorShader->bind();
myData->platSingleColorShader->setVec4("u_Color", color.myColorValue);
myData->quadTextureShader->bind();
myData->quadTextureShader->setVec4("u_Color", color.myColorValue);
glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
myData->quadTextureShader->setMat4("u_Transform", transform);
myData->myWhiteTexture->bind();

myData->quadVertexArray->bind();
myRenderCommand->drawIndexed(*myData->quadVertexArray);
}
Expand All @@ -138,8 +122,7 @@ void Renderer2D::drawQuad(const glm::vec2& position, const glm::vec2& size, cons
void Renderer2D::drawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture>& texture)
{
myData->quadTextureShader->bind();
myData->quadTextureShader->setInt("u_Texture", 0);

myData->quadTextureShader->setVec4("u_Color", glm::vec4(1.0f));
glm::mat4 transform =
glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), {size.x, size.y, 1.0f});
myData->quadTextureShader->setMat4("u_Transform", transform);
Expand Down
1 change: 1 addition & 0 deletions Sandbox/include/Sandbox2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ class Sandbox2D : public Huan::Layer
Huan::Ref<Huan::Shader> myFlatColorShader;
Huan::Ref<Huan::Quad2D> myQuad2D;
Huan::Ref<Huan::Quad2D> myQuad2DTex;
Huan::Ref<Huan::Texture> myCheckboardTexture;
};
6 changes: 6 additions & 0 deletions Sandbox/src/Sandbox2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void Sandbox2D::onAttach()
myScene->uColor = std::make_shared<glm::vec4>(1.0f);
myScene->loadTexture("../../../../Assets/Textures/logo.png");

myCheckboardTexture = std::make_shared<Huan::CurrentTexture2D>("../../../../Assets/Textures/Checkerboard.png");
myRenderer.loadScene(myScene);

myQuad2D = std::make_shared<Huan::Quad2D>(Huan::Point2D{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f},
Expand All @@ -36,8 +37,13 @@ void Sandbox2D::onUpdate(Huan::TimeStep ts)
myRenderer.getRenderCommand()->setClearColor({0.1f, 0.1f, 0.1f, 1.0f});
myRenderer.getRenderCommand()->clear();
myRenderer.beginScene();
// first render the opaque object
myRenderer.drawQuad(glm::vec3(myQuad2DTex->myCenterPostion.x, myQuad2DTex->myCenterPostion.y, -0.1f),
myQuad2DTex->mySideLength * 10.0f, myCheckboardTexture);
myRenderer.drawQuad(myQuad2D->myCenterPostion, myQuad2D->mySideLength, myQuad2D->myColor);
// second render the transparent object
myRenderer.drawQuad(myQuad2DTex->myCenterPostion, myQuad2DTex->mySideLength, myScene->getTexture());

myRenderer.endScene();
}

Expand Down

0 comments on commit d1c618e

Please sign in to comment.