SandBox2D粒子测试
This commit is contained in:
@ -10,6 +10,8 @@
|
||||
|
||||
namespace Hazel
|
||||
{
|
||||
|
||||
|
||||
class HAZEL_API OrthographicCamera
|
||||
{
|
||||
public:
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
namespace Hazel
|
||||
{
|
||||
OrthographicCameraController::OrthographicCameraController(float aspectRatio, bool isRotation) : m_AspectRatio(aspectRatio), m_Camera(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel), m_isRotation(isRotation)
|
||||
OrthographicCameraController::OrthographicCameraController(float aspectRatio, bool isRotation) : m_AspectRatio(aspectRatio), m_isRotation(isRotation),m_Bounds(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel), m_Camera(m_Bounds.left, m_Bounds.right, m_Bounds.bottom, m_Bounds.top)
|
||||
{
|
||||
}
|
||||
|
||||
@ -69,7 +69,8 @@ namespace Hazel
|
||||
{
|
||||
m_ZoomLevel += e.wheel.y * 0.25f;
|
||||
m_ZoomLevel = std::max(m_ZoomLevel, 0.25f);
|
||||
m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);
|
||||
m_Bounds = { -m_AspectRatio * m_ZoomLevel,m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel };
|
||||
m_Camera.SetProjection(m_Bounds.left, m_Bounds.right, m_Bounds.bottom, m_Bounds.top);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@ -81,7 +82,8 @@ namespace Hazel
|
||||
if (e.type == SDL_EVENT_WINDOW_RESIZED && (e.window.windowID == Application::Get().GetWindow().GetMainWindowID()))
|
||||
{
|
||||
m_AspectRatio = static_cast<float>(e.window.data1) / static_cast<float>(e.window.data2);
|
||||
m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);
|
||||
m_Bounds = { -m_AspectRatio * m_ZoomLevel,m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel };
|
||||
m_Camera.SetProjection(m_Bounds.left, m_Bounds.right, m_Bounds.bottom, m_Bounds.top);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
@ -15,6 +15,14 @@
|
||||
|
||||
namespace Hazel
|
||||
{
|
||||
|
||||
struct OrthographicCameraBounds
|
||||
{
|
||||
float left, right, bottom, top;
|
||||
float GetWidth() { return right - left; }
|
||||
float GetHeight() { return top - bottom; }
|
||||
};
|
||||
|
||||
class HAZEL_API OrthographicCameraController
|
||||
{
|
||||
public:
|
||||
@ -31,6 +39,8 @@ namespace Hazel
|
||||
OrthographicCamera& GetCamera() {return m_Camera;}
|
||||
const OrthographicCamera& GetCamera() const {return m_Camera;}
|
||||
|
||||
const OrthographicCameraBounds& GetBounds() const {return m_Bounds;}
|
||||
|
||||
private:
|
||||
bool OnMouseScrolled(SDL_Event& e);
|
||||
bool OnWindowResized(SDL_Event& e);
|
||||
@ -38,9 +48,11 @@ namespace Hazel
|
||||
private:
|
||||
float m_AspectRatio;
|
||||
float m_ZoomLevel = 1.0f;
|
||||
OrthographicCamera m_Camera;
|
||||
bool m_isRotation;
|
||||
|
||||
OrthographicCameraBounds m_Bounds;
|
||||
OrthographicCamera m_Camera;
|
||||
|
||||
glm::vec3 m_CameraPosition = { 0.0f, 0.0f, 0.0f };
|
||||
float m_CameraRotation = 0.0f;
|
||||
float m_CameraTranslationSpeed = 20.0f, m_CameraRotationSpeed = 30.0f;
|
||||
|
||||
@ -23,6 +23,7 @@ namespace Hazel
|
||||
glm::vec4 Color;
|
||||
glm::vec2 TexCoord;
|
||||
float TexIndex;
|
||||
float TilingFactor;
|
||||
// TODO:
|
||||
};
|
||||
|
||||
@ -44,6 +45,10 @@ namespace Hazel
|
||||
|
||||
std::array<Ref<Texture2D>, MaxTextureSlots> TextureSlots;
|
||||
uint32_t TextureSlotIndex = 1; // 0 use white texture
|
||||
|
||||
glm::vec4 QuadVertexPosition[4];
|
||||
|
||||
Renderer2D::Statistic Stats;
|
||||
};
|
||||
|
||||
static Renderer2DStorage s_Data;
|
||||
@ -62,7 +67,8 @@ namespace Hazel
|
||||
{ShaderDataType::Float3, "a_Postion"},
|
||||
{ShaderDataType::Float4, "a_Color"},
|
||||
{ShaderDataType::Float2, "a_TexCoord"},
|
||||
{ShaderDataType::Float, "a_TexIndex"}
|
||||
{ShaderDataType::Float, "a_TexIndex"},
|
||||
{ShaderDataType::Float, "a_TilingFactor"}
|
||||
});
|
||||
s_Data.QuadVertexArray->AddVertexBuffer(s_Data.QuadVertexBuffer);
|
||||
|
||||
@ -114,6 +120,11 @@ namespace Hazel
|
||||
s_Data.TextureSlots[i] = 0;
|
||||
}
|
||||
*/
|
||||
|
||||
s_Data.QuadVertexPosition[0] = {-0.5f, -0.5f, 0.0f, 1.0f};
|
||||
s_Data.QuadVertexPosition[1] = {0.5f, -0.5f, 0.0f, 1.0f};
|
||||
s_Data.QuadVertexPosition[2] = {0.5f, 0.5f, 0.0f, 1.0f};
|
||||
s_Data.QuadVertexPosition[3] = {-0.5f, 0.5f, 0.0f, 1.0f};
|
||||
}
|
||||
|
||||
void Renderer2D::BeginScene(const OrthographicCamera& camera)
|
||||
@ -143,8 +154,9 @@ namespace Hazel
|
||||
{
|
||||
s_Data.TextureSlots[i]->Bind(i);
|
||||
}
|
||||
s_Data.WhiteTexture->Bind();
|
||||
RendererCommand::DrawIndexed(s_Data.QuadVertexArray, s_Data.QuadIndexCount);
|
||||
|
||||
s_Data.Stats.DrawCalls++;
|
||||
}
|
||||
|
||||
void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color)
|
||||
@ -155,48 +167,47 @@ namespace Hazel
|
||||
void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color)
|
||||
{
|
||||
HZ_PROFILE_FUNCTION();
|
||||
if (s_Data.QuadIndexCount >= s_Data.MaxIndices)
|
||||
{
|
||||
FlushAndReset();
|
||||
}
|
||||
|
||||
const float texIndex = 0.0f;
|
||||
constexpr float texIndex = 0.0f;
|
||||
constexpr float tilingFactor = 1.0f;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = position;
|
||||
const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[0];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = texIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = { position.x + size.x, position.y, 0.0f };
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[1];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = texIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = {position.x + size.x, position.y + size.y, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[2];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 1.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = texIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = {position.x, position.y + size.y, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[3];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 1.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = texIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadIndexCount += 6;
|
||||
|
||||
/*
|
||||
s_Data.TextureShader->SetFloat4("u_Color", color);
|
||||
s_Data.TextureShader->SetFloat("u_TilingFactor", 1.f);
|
||||
|
||||
s_Data.WhiteTexture->Bind();
|
||||
|
||||
|
||||
const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
|
||||
s_Data.TextureShader->SetMat4("u_Transform", transform);
|
||||
|
||||
s_Data.QuadVertexArray->Bind();
|
||||
RendererCommand::DrawIndexed(s_Data.QuadVertexArray);
|
||||
*/
|
||||
s_Data.Stats.QuadCount ++;
|
||||
}
|
||||
|
||||
void Renderer2D::Shutdown()
|
||||
@ -204,6 +215,27 @@ namespace Hazel
|
||||
HZ_PROFILE_FUNCTION();
|
||||
}
|
||||
|
||||
void Renderer2D::ResetStats()
|
||||
{
|
||||
memset(&s_Data.Stats, 0, sizeof(Statistic));
|
||||
}
|
||||
|
||||
Renderer2D::Statistic Renderer2D::GetStats()
|
||||
{
|
||||
return s_Data.Stats;
|
||||
}
|
||||
|
||||
void Renderer2D::FlushAndReset()
|
||||
{
|
||||
EndScene();
|
||||
|
||||
s_Data.QuadIndexCount = 0;
|
||||
s_Data.QuadVertexBufferPtr = s_Data.QuadVertexBufferBase;
|
||||
|
||||
s_Data.TextureSlotIndex = 1;
|
||||
|
||||
}
|
||||
|
||||
void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref<Texture2D>& texture, const float tilingFactor, const glm::vec4& tintColor)
|
||||
{
|
||||
DrawQuad({position.x, position.y, 0.0f}, size, texture, tilingFactor, tintColor);
|
||||
@ -211,13 +243,15 @@ namespace Hazel
|
||||
void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D>& texture, const float tilingFactor, const glm::vec4& tintColor)
|
||||
{
|
||||
HZ_PROFILE_FUNCTION();
|
||||
if (s_Data.QuadIndexCount >= s_Data.MaxIndices)
|
||||
{
|
||||
FlushAndReset();
|
||||
}
|
||||
|
||||
constexpr glm::vec4 color = {1.0f, 1.0f, 1.0f, 1.0f};
|
||||
|
||||
float textureIndex = 0.0f;
|
||||
|
||||
auto &a = s_Data;
|
||||
|
||||
for (uint32_t i = 1; i < s_Data.TextureSlotIndex; i++)
|
||||
{
|
||||
if (*s_Data.TextureSlots[i].get() == *texture.get())
|
||||
@ -235,46 +269,39 @@ namespace Hazel
|
||||
s_Data.TextureSlotIndex++;
|
||||
}
|
||||
|
||||
const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = position;
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[0];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = { position.x + size.x, position.y, 0.0f };
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[1];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = {position.x + size.x, position.y + size.y, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[2];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 1.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = {position.x, position.y + size.y, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[3];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 1.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadIndexCount += 6;
|
||||
|
||||
s_Data.TextureShader->SetFloat4("u_Color", tintColor);
|
||||
s_Data.TextureShader->SetFloat("u_TilingFactor", tilingFactor);
|
||||
s_Data.TextureShader->Bind();
|
||||
|
||||
/*
|
||||
texture->Bind();
|
||||
|
||||
const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
|
||||
s_Data.TextureShader->SetMat4("u_Transform", transform);
|
||||
|
||||
s_Data.TextureShader->Bind();
|
||||
RendererCommand::DrawIndexed(s_Data.QuadVertexArray, s_Data.QuadIndexCount);
|
||||
*/
|
||||
s_Data.Stats.QuadCount ++;
|
||||
}
|
||||
|
||||
void Renderer2D::DrawRotateQuad(const glm::vec2& position, const glm::vec2& size, const float rotation,
|
||||
@ -287,17 +314,50 @@ namespace Hazel
|
||||
const glm::vec4& color)
|
||||
{
|
||||
HZ_PROFILE_FUNCTION();
|
||||
s_Data.TextureShader->SetFloat4("u_Color", color);
|
||||
s_Data.TextureShader->SetFloat("u_TilingFactor", 1.0f);
|
||||
s_Data.TextureShader->Bind();
|
||||
|
||||
s_Data.WhiteTexture->Bind();
|
||||
|
||||
if (s_Data.QuadIndexCount >= s_Data.MaxIndices)
|
||||
{
|
||||
FlushAndReset();
|
||||
}
|
||||
|
||||
constexpr float texIndex = 0.0f;
|
||||
constexpr float tilingFactor = 1.0f;
|
||||
|
||||
const 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.TextureShader->Bind();
|
||||
RendererCommand::DrawIndexed(s_Data.QuadVertexArray);
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[0];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = texIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[1];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = texIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[2];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 1.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = texIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[3];
|
||||
s_Data.QuadVertexBufferPtr->Color = color;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 1.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = texIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
|
||||
s_Data.QuadIndexCount += 6;
|
||||
|
||||
s_Data.Stats.QuadCount ++;
|
||||
}
|
||||
|
||||
void Renderer2D::DrawRotateQuad(const glm::vec2& position, const glm::vec2& size, const float rotation,
|
||||
@ -310,17 +370,64 @@ namespace Hazel
|
||||
const Ref<Texture2D>& texture, const float tilingFactor, const glm::vec4& tintColor)
|
||||
{
|
||||
HZ_PROFILE_FUNCTION();
|
||||
s_Data.TextureShader->SetFloat4("u_Color", tintColor);
|
||||
s_Data.TextureShader->SetFloat("u_TilingFactor", tilingFactor);
|
||||
s_Data.TextureShader->Bind();
|
||||
|
||||
texture->Bind();
|
||||
if (s_Data.QuadIndexCount >= s_Data.MaxIndices)
|
||||
{
|
||||
FlushAndReset();
|
||||
}
|
||||
|
||||
float textureIndex = 0.0f;
|
||||
|
||||
for (uint32_t i = 1; i < s_Data.TextureSlotIndex; i++)
|
||||
{
|
||||
if (*s_Data.TextureSlots[i].get() == *texture.get())
|
||||
{
|
||||
textureIndex = (float)i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (textureIndex == 0.0f)
|
||||
{
|
||||
textureIndex = (float)s_Data.TextureSlotIndex;
|
||||
s_Data.TextureSlots[s_Data.TextureSlotIndex] = texture;
|
||||
s_Data.TextureSlotIndex++;
|
||||
}
|
||||
|
||||
const 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.TextureShader->Bind();
|
||||
RendererCommand::DrawIndexed(s_Data.QuadVertexArray);
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[0];
|
||||
s_Data.QuadVertexBufferPtr->Color = tintColor;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[1];
|
||||
s_Data.QuadVertexBufferPtr->Color = tintColor;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 0.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[2];
|
||||
s_Data.QuadVertexBufferPtr->Color = tintColor;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 1.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[3];
|
||||
s_Data.QuadVertexBufferPtr->Color = tintColor;
|
||||
s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 1.0f};
|
||||
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
|
||||
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
|
||||
s_Data.QuadVertexBufferPtr ++;
|
||||
|
||||
s_Data.QuadIndexCount += 6;
|
||||
|
||||
s_Data.Stats.QuadCount ++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -32,6 +32,17 @@ namespace Hazel
|
||||
|
||||
static void Shutdown();
|
||||
|
||||
struct Statistic
|
||||
{
|
||||
uint32_t DrawCalls = 0;
|
||||
uint32_t QuadCount = 0;
|
||||
uint32_t GetTotalVertexCount() const { return QuadCount * 4; }
|
||||
uint32_t GetTotalIndexCount() const { return QuadCount * 6; }
|
||||
};
|
||||
static void ResetStats();
|
||||
static Statistic GetStats();
|
||||
private:
|
||||
static void FlushAndReset();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -21,3 +21,14 @@ add_executable(${DEMO_PROJECT} ${DEMO_SOURCES})
|
||||
|
||||
target_link_libraries(${DEMO_PROJECT} PRIVATE Hazel)
|
||||
|
||||
|
||||
# Example
|
||||
|
||||
set(EXAMPLE_PROJECT "${PROJECT_NAME}-Example")
|
||||
file(GLOB_RECURSE EXAMPLE_SOURCES
|
||||
src/SandboxApp.cpp
|
||||
src/Example/*.cpp)
|
||||
|
||||
add_executable(${EXAMPLE_PROJECT} ${EXAMPLE_SOURCES})
|
||||
|
||||
target_link_libraries(${EXAMPLE_PROJECT} PRIVATE Hazel)
|
||||
|
||||
@ -5,6 +5,7 @@ layout(location = 0) in vec3 a_Position;
|
||||
layout(location = 1) in vec4 a_Color;
|
||||
layout(location = 2) in vec2 a_TexCoord;
|
||||
layout(location = 3) in float a_TexIndex;
|
||||
layout(location = 4) in float a_TilingFactor;
|
||||
|
||||
uniform mat4 u_ViewProjection;
|
||||
//uniform mat4 u_Transform;
|
||||
@ -12,11 +13,13 @@ uniform mat4 u_ViewProjection;
|
||||
out vec2 v_TexCoord;
|
||||
out vec4 v_Color;
|
||||
out float v_TexIndex;
|
||||
out float v_TilingFactor;
|
||||
|
||||
void main() {
|
||||
v_TexCoord = a_TexCoord;
|
||||
v_Color = a_Color;
|
||||
v_TexIndex = a_TexIndex;
|
||||
v_TilingFactor = a_TilingFactor;
|
||||
|
||||
gl_Position = u_ViewProjection * vec4(a_Position, 1.0f);
|
||||
}
|
||||
@ -29,13 +32,13 @@ layout(location = 0) out vec4 color;
|
||||
in vec2 v_TexCoord;
|
||||
in vec4 v_Color;
|
||||
in float v_TexIndex;
|
||||
in float v_TilingFactor;
|
||||
|
||||
uniform float u_TilingFactor;
|
||||
uniform vec4 u_Color;
|
||||
uniform sampler2D u_Textures[32];
|
||||
|
||||
void main() {
|
||||
|
||||
color = texture(u_Textures[int(v_TexIndex)], v_TexCoord * u_TilingFactor) * v_Color;
|
||||
color = texture(u_Textures[int(v_TexIndex)], v_TexCoord * v_TilingFactor) * v_Color;
|
||||
// color = v_Color;
|
||||
}
|
||||
|
||||
@ -4,23 +4,17 @@
|
||||
|
||||
#include "GameLayer.h"
|
||||
|
||||
#include <imgui_internal.h>
|
||||
#include <Hazel/Core/Application.h>
|
||||
#include <Hazel/Renderer/Renderer2D.h>
|
||||
#include <Hazel/Renderer/RendererCommand.h>
|
||||
|
||||
GameLayer::GameLayer() : Layer("GameLayer")
|
||||
GameLayer::GameLayer() : Layer("GameLayer") , m_cameraController((float)(Application::Get().GetWindow().GetWidth()) / (float)(Application::Get().GetWindow().GetHeight()))
|
||||
{
|
||||
auto& window = Application::Get().GetWindow();
|
||||
CreateCamera(window.GetWidth(), window.GetHeight());
|
||||
|
||||
Random::Init();
|
||||
}
|
||||
|
||||
|
||||
void GameLayer::OnAttach()
|
||||
{
|
||||
m_Level.Init();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
}
|
||||
|
||||
void GameLayer::OnDetech()
|
||||
@ -29,31 +23,31 @@ void GameLayer::OnDetech()
|
||||
|
||||
void GameLayer::OnUpdate(TimeStep& ts)
|
||||
{
|
||||
m_Time += ts;
|
||||
auto mouseState = SDL_GetMouseState(NULL, NULL);
|
||||
static Uint32 prevMouseState = 0;
|
||||
m_cameraController.OnUpdate(ts);
|
||||
|
||||
RendererCommand::SetClearColor({0.2f, 0.2f, 0.2f, 1.0f});
|
||||
RendererCommand::Clear();
|
||||
|
||||
Renderer2D::BeginScene(m_cameraController.GetCamera());
|
||||
|
||||
Hazel::Renderer2D::DrawQuad({0.0f, 0.0f}, {1.0f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f});
|
||||
|
||||
Renderer2D::EndScene();
|
||||
|
||||
if ((mouseState & SDL_BUTTON_LMASK) && !(prevMouseState & SDL_BUTTON_LMASK))
|
||||
{
|
||||
HZ_CORE_INFO("LEFT Mouse Clicked!");
|
||||
}
|
||||
prevMouseState = mouseState;
|
||||
}
|
||||
|
||||
void GameLayer::OnEvent(SDL_Event& e)
|
||||
{
|
||||
if (const auto& window = Application::Get().GetWindow(); e.type == SDL_EVENT_WINDOW_RESIZED && e.window.windowID == window.GetMainWindowID())
|
||||
{
|
||||
CreateCamera(window.GetWidth(), window.GetHeight());
|
||||
}
|
||||
m_cameraController.OnEvent(e);
|
||||
}
|
||||
|
||||
void GameLayer::OnImGuiRender()
|
||||
{
|
||||
}
|
||||
|
||||
void GameLayer::CreateCamera(uint32_t width, uint32_t height)
|
||||
{
|
||||
const float aspect = static_cast<float>(width) / static_cast<float>(height);
|
||||
|
||||
const float camWidth = 8.0f;
|
||||
const float bottom = -camWidth;
|
||||
const float top = camWidth;
|
||||
const float left = bottom * aspect;
|
||||
const float right = top * aspect;
|
||||
|
||||
m_Camera = OrthographicCamera(left, right, bottom, top);
|
||||
}
|
||||
}
|
||||
@ -5,9 +5,8 @@
|
||||
#ifndef GAMELAYER_H
|
||||
#define GAMELAYER_H
|
||||
#include <Hazel/Core/Layer.h>
|
||||
#include <Hazel/Renderer/OrthographicCamera.h>
|
||||
#include <Hazel/Renderer/OrthographicCameraController.h>
|
||||
|
||||
#include "Random.h"
|
||||
|
||||
using namespace Hazel;
|
||||
|
||||
@ -22,20 +21,8 @@ public:
|
||||
void OnEvent(SDL_Event& e) override;
|
||||
void OnImGuiRender() override;
|
||||
|
||||
void CreateCamera(uint32_t width, uint32_t height);
|
||||
|
||||
private:
|
||||
std::unique_ptr<OrthographicCamera> m_Camera;
|
||||
float m_Time = 0.0f;
|
||||
|
||||
bool m_Blink = false;
|
||||
|
||||
enum class GameState
|
||||
{
|
||||
Play = 0, MainMenu, GameOver
|
||||
};
|
||||
|
||||
GameState m_GameState = GameState::MainMenu;
|
||||
OrthographicCameraController m_cameraController;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
//
|
||||
// Created by sfd on 25-5-18.
|
||||
//
|
||||
|
||||
#include "Random.h"
|
||||
@ -1,28 +0,0 @@
|
||||
//
|
||||
// Created by sfd on 25-5-18.
|
||||
//
|
||||
|
||||
#ifndef RANDOM_H
|
||||
#define RANDOM_H
|
||||
|
||||
#include <random>
|
||||
|
||||
|
||||
|
||||
class Random {
|
||||
public:
|
||||
static void Init() {
|
||||
s_RandomEngine.seed(std::random_device()());
|
||||
}
|
||||
static float Float()
|
||||
{
|
||||
return (float)s_Distribution(s_RandomEngine) / (float)std::numeric_limits<uint32_t>::max();
|
||||
}
|
||||
private:
|
||||
static std::mt19937 s_RandomEngine;
|
||||
static std::uniform_int_distribution<std::mt19937::result_type> s_Distribution;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //RANDOM_H
|
||||
214
Sandbox/src/Example/ExampleLayer.cpp
Normal file
214
Sandbox/src/Example/ExampleLayer.cpp
Normal file
@ -0,0 +1,214 @@
|
||||
//
|
||||
// Created by sfd on 25-5-21.
|
||||
//
|
||||
|
||||
#include "ExampleLayer.h"
|
||||
|
||||
#include <imgui.h>
|
||||
#include <Hazel/Renderer/Renderer.h>
|
||||
#include <Hazel/Renderer/RendererCommand.h>
|
||||
#include <Platform/OpenGL/OpenGLShader.h>
|
||||
|
||||
#include "glm/gtc/type_ptr.hpp"
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include "glm/gtx/transform.hpp"
|
||||
|
||||
ExampleLayer::ExampleLayer() : Layer("ExampleLayer"), m_CameraController(1280.0f / 720.0f), m_SquarePosition(glm::vec3(0.0f))
|
||||
{
|
||||
// ------------------------------------------------------------test------------------------------------------------------------
|
||||
// Vertex Array
|
||||
m_VertexArray = Hazel::VertexArray::Create();
|
||||
|
||||
// Vertex Buffer
|
||||
float vertices[3 * 7] = {
|
||||
-0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f,
|
||||
0.5f, -0.5f, 0.0f, 0.2f, 0.3f, 0.8f, 1.0f,
|
||||
0.0f, 0.5f, 0.0f, 0.8f, 0.8f, 0.2f, 1.0f
|
||||
};
|
||||
std::shared_ptr<Hazel::VertexBuffer> m_VertexBuffer;
|
||||
m_VertexBuffer = Hazel::VertexBuffer::Create(vertices, sizeof(vertices));
|
||||
Hazel::BufferLayout layout = {
|
||||
{Hazel::ShaderDataType::Float3, "a_Postion"},
|
||||
{Hazel::ShaderDataType::Float4, "a_Color"},
|
||||
};
|
||||
m_VertexBuffer->SetLayout(layout);
|
||||
m_VertexArray->AddVertexBuffer(m_VertexBuffer);
|
||||
|
||||
|
||||
uint32_t indices[6] = {0, 1, 2};
|
||||
std::shared_ptr<Hazel::IndexBuffer> m_IndexBuffer;
|
||||
m_IndexBuffer = Hazel::IndexBuffer::Create(indices, sizeof(indices) / sizeof(indices[0]));
|
||||
m_VertexArray->SetIndexBuffer(m_IndexBuffer);
|
||||
|
||||
// Shader
|
||||
std::string vertexSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) in vec3 a_Position;
|
||||
layout(location = 1) in vec3 a_Color;
|
||||
|
||||
uniform mat4 u_ViewProjection;
|
||||
uniform mat4 u_Transform;
|
||||
|
||||
out vec3 v_Position;
|
||||
out vec3 v_Color;
|
||||
|
||||
void main() {
|
||||
v_Position = a_Position;
|
||||
v_Color = a_Color;
|
||||
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
|
||||
}
|
||||
|
||||
)";
|
||||
std::string fragmentSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
in vec3 v_Position;
|
||||
in vec3 v_Color;
|
||||
|
||||
void main() {
|
||||
color = vec4(v_Color, 1.0f);
|
||||
}
|
||||
)";
|
||||
m_Shader = Hazel::Shader::Create("demoShader", vertexSrc, fragmentSrc);
|
||||
|
||||
|
||||
m_SquareVA = Hazel::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
|
||||
};
|
||||
std::shared_ptr<Hazel::VertexBuffer> squareVB; //= VertexBuffer::Create(squareVertices, sizeof(squareVertices));
|
||||
squareVB = Hazel::VertexBuffer::Create(squareVertices, sizeof(squareVertices));
|
||||
squareVB->SetLayout(
|
||||
{
|
||||
{Hazel::ShaderDataType::Float3, "a_Postion"},
|
||||
{Hazel::ShaderDataType::Float2, "a_TexCoord"}
|
||||
});
|
||||
m_SquareVA->AddVertexBuffer(squareVB);
|
||||
|
||||
|
||||
uint32_t squareIndice[6] = {0, 1, 2, 2, 3, 0};
|
||||
std::shared_ptr<Hazel::IndexBuffer> squareIB;
|
||||
// = std::make_shared<IndexBuffer>(IndexBuffer::Create(squareIndice, sizeof(squareIndice) / sizeof(uint32_t)));
|
||||
squareIB = Hazel::IndexBuffer::Create(squareIndice, sizeof(squareIndice) / sizeof(uint32_t));
|
||||
m_SquareVA->SetIndexBuffer(squareIB);
|
||||
|
||||
std::string coloeVertexSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(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;
|
||||
|
||||
void main() {
|
||||
v_TexCoord = a_TexCoord;
|
||||
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
|
||||
}
|
||||
)";
|
||||
std::string colorFragmentSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
in vec3 v_Position;
|
||||
uniform vec4 u_Color;
|
||||
|
||||
void main() {
|
||||
color = u_Color;
|
||||
}
|
||||
)";
|
||||
m_colorShader = Hazel::Shader::Create("ColorShader", coloeVertexSrc, colorFragmentSrc);
|
||||
|
||||
|
||||
// Texture.Shader
|
||||
// m_TextureShader = Hazel::Shader::Create("assets/shaders/Texture.glsl");
|
||||
auto textureShader = m_ShaderLibrary.Load("assets/shaders/Texture.glsl");
|
||||
|
||||
|
||||
// Texture
|
||||
m_Texture = Hazel::Texture2D::Create("assets/textures/Checkerboard.png");
|
||||
m_logoTexture = Hazel::Texture2D::Create("assets/textures/iceLogo.png");
|
||||
|
||||
std::dynamic_pointer_cast<Hazel::OpenGLShader>(textureShader)->Bind();
|
||||
std::dynamic_pointer_cast<Hazel::OpenGLShader>(textureShader)->UploadUniformInt("u_Texture", 0);
|
||||
}
|
||||
|
||||
|
||||
void ExampleLayer::OnUpdate(Hazel::TimeStep& ts)
|
||||
{
|
||||
m_CameraController.OnUpdate(ts);
|
||||
|
||||
// key event
|
||||
{
|
||||
float time = ts;
|
||||
|
||||
const bool* state = SDL_GetKeyboardState(NULL);
|
||||
|
||||
if (state[SDL_SCANCODE_I])
|
||||
{
|
||||
m_SquarePosition.y += 5 * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_K])
|
||||
{
|
||||
m_SquarePosition.y -= 5 * time;
|
||||
}
|
||||
if (state[SDL_SCANCODE_J])
|
||||
{
|
||||
m_SquarePosition.x -= 5 * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_L])
|
||||
{
|
||||
m_SquarePosition.x += 5 * time;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Hazel::RendererCommand::SetClearColor(m_ScreenClearColor);
|
||||
Hazel::RendererCommand::Clear();
|
||||
|
||||
Hazel::Renderer::BeginScene(m_CameraController.GetCamera());
|
||||
|
||||
auto transform = glm::translate(glm::mat4(1.0f), m_SquarePosition);
|
||||
|
||||
auto textureShader = m_ShaderLibrary.Get("Texture");
|
||||
m_Texture->Bind();
|
||||
Hazel::Renderer::Submit(textureShader, m_SquareVA, transform);
|
||||
|
||||
m_logoTexture->Bind();
|
||||
Hazel::Renderer::Submit(textureShader, m_SquareVA);
|
||||
|
||||
Hazel::Renderer::EndScene();
|
||||
}
|
||||
|
||||
|
||||
void ExampleLayer::OnEvent(SDL_Event& e)
|
||||
{
|
||||
m_CameraController.OnEvent(e);
|
||||
}
|
||||
|
||||
void ExampleLayer::OnImGuiRender()
|
||||
{
|
||||
ImGui::ShowDemoWindow();
|
||||
|
||||
const auto cameraRotation = m_CameraController.GetCamera().GetRotation();
|
||||
const auto cameraPosition = m_CameraController.GetCamera().GetPosition();
|
||||
ImGui::Begin("Hazel Layer");
|
||||
ImGui::Text("Rotation: %f", cameraRotation);
|
||||
ImGui::Text("Position: ( %.2f, %.2f, %.2f)", cameraPosition.x, cameraPosition.y, cameraPosition.z);
|
||||
ImGui::Text("frame: %.3f", ImGui::GetIO().Framerate);
|
||||
ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor));
|
||||
ImGui::NewLine();
|
||||
ImGui::ColorEdit4("Screen Clear Color", glm::value_ptr(m_ScreenClearColor));
|
||||
ImGui::End();
|
||||
}
|
||||
46
Sandbox/src/Example/ExampleLayer.h
Normal file
46
Sandbox/src/Example/ExampleLayer.h
Normal file
@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by sfd on 25-5-21.
|
||||
//
|
||||
|
||||
#ifndef EXAMPLELAYER_H
|
||||
#define EXAMPLELAYER_H
|
||||
|
||||
#include <Hazel/Core/Layer.h>
|
||||
#include <Hazel/Renderer/OrthographicCameraController.h>
|
||||
#include <Hazel/Renderer/Shader.h>
|
||||
#include <Hazel/Renderer/Texture.h>
|
||||
#include <Hazel/Renderer/VertexArray.h>
|
||||
|
||||
class ExampleLayer : public Hazel::Layer
|
||||
{
|
||||
public:
|
||||
ExampleLayer();
|
||||
|
||||
void OnUpdate(Hazel::TimeStep& ts) override;
|
||||
void OnEvent(SDL_Event& event) override;
|
||||
void OnImGuiRender() override;
|
||||
|
||||
|
||||
private:
|
||||
Hazel::ShaderLibrary m_ShaderLibrary;
|
||||
Hazel::Ref<Hazel::Shader> m_Shader;
|
||||
Hazel::Ref<Hazel::VertexArray> m_VertexArray;
|
||||
|
||||
Hazel::Ref<Hazel::VertexArray> m_SquareVA;
|
||||
Hazel::Ref<Hazel::Shader> m_colorShader;
|
||||
|
||||
Hazel::Ref<Hazel::Texture2D> m_Texture, m_logoTexture;
|
||||
|
||||
Hazel::OrthographicCameraController m_CameraController;
|
||||
|
||||
glm::vec3 m_SquarePosition;
|
||||
float m_SquareMoveSpeed = 1.0f;
|
||||
|
||||
glm::vec3 m_SquareColor = {0.2f, 0.3f, 0.8f};
|
||||
|
||||
glm::vec4 m_ScreenClearColor = {0.1f, 0.1f, 0.1f, 1.0f};
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //EXAMPLELAYER_H
|
||||
96
Sandbox/src/SandBox2D/ParticleSystem.cpp
Normal file
96
Sandbox/src/SandBox2D/ParticleSystem.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
//
|
||||
// Created by sfd on 25-5-21.
|
||||
//
|
||||
|
||||
#include "ParticleSystem.h"
|
||||
|
||||
#include <random>
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include "glm/gtx/compatibility.hpp"
|
||||
|
||||
|
||||
class Random
|
||||
{
|
||||
public:
|
||||
static void Init()
|
||||
{
|
||||
s_RandEngine.seed(std::random_device()());
|
||||
}
|
||||
static float Float()
|
||||
{
|
||||
return (float)s_Distribution(s_RandEngine) / (float)std::numeric_limits<uint32_t>::max();
|
||||
}
|
||||
private:
|
||||
static std::mt19937 s_RandEngine;
|
||||
static std::uniform_int_distribution<std::mt19937::result_type> s_Distribution;
|
||||
};
|
||||
|
||||
|
||||
std::mt19937 Random::s_RandEngine;
|
||||
std::uniform_int_distribution<std::mt19937::result_type> Random::s_Distribution;
|
||||
|
||||
|
||||
ParticleSystem::ParticleSystem()
|
||||
{
|
||||
m_particlePool.resize(1000);
|
||||
}
|
||||
|
||||
void ParticleSystem::OnUpdate(Hazel::TimeStep& ts)
|
||||
{
|
||||
for (auto& particle : m_particlePool)
|
||||
{
|
||||
if (!particle.Active)
|
||||
continue;
|
||||
if (particle.LifeRemaing <= 0.0f)
|
||||
{
|
||||
particle.Active = false;
|
||||
continue;
|
||||
}
|
||||
particle.LifeRemaing -= ts;
|
||||
particle.Position += particle.Velocity * static_cast<float>(ts);
|
||||
particle.Rotation += 0.01f * ts;
|
||||
}
|
||||
}
|
||||
|
||||
void ParticleSystem::OnRender(Hazel::OrthographicCamera& camera)
|
||||
{
|
||||
Hazel::Renderer2D::BeginScene(camera);
|
||||
|
||||
for (auto& particle : m_particlePool)
|
||||
{
|
||||
if (!particle.Active)
|
||||
continue;
|
||||
float life = particle.LifeRemaing / particle.LifeTime;
|
||||
glm::vec4 color = glm::lerp(particle.ColorEnd, particle.ColorBegin, life);
|
||||
|
||||
float size = glm::lerp(particle.SizeEnd, particle.SizeBegin, life);
|
||||
|
||||
Hazel::Renderer2D::DrawRotateQuad(particle.Position, {size, size}, particle.Rotation, color);
|
||||
}
|
||||
Hazel::Renderer2D::EndScene();
|
||||
}
|
||||
|
||||
void ParticleSystem::Emit(const ParticleProp& particuleProps)
|
||||
{
|
||||
Particle& particle = m_particlePool[m_PoolIndex];
|
||||
particle.Active = true;
|
||||
particle.Position = particuleProps.Position;
|
||||
particle.Rotation = Random::Float() * 2.0f * glm::pi<float>();
|
||||
|
||||
// Velocity
|
||||
particle.Velocity = particuleProps.Velocity;
|
||||
particle.Velocity.x += particuleProps.VelocityVariation.x * (Random::Float() - 0.5f);
|
||||
particle.Velocity.y += particuleProps.VelocityVariation.y * (Random::Float() - 0.5f);
|
||||
|
||||
// Color
|
||||
particle.ColorBegin = particuleProps.ColorBegin;
|
||||
particle.ColorEnd = particuleProps.ColorEnd;
|
||||
|
||||
particle.LifeTime = particuleProps.LifeTime;
|
||||
particle.LifeRemaing = particuleProps.SizeBegin;
|
||||
particle.SizeBegin = particuleProps.SizeBegin + particuleProps.SizeVariation * (Random::Float() - 0.5f);
|
||||
particle.SizeEnd = particuleProps.SizeEnd;
|
||||
|
||||
m_PoolIndex = --m_PoolIndex % m_particlePool.size();
|
||||
}
|
||||
49
Sandbox/src/SandBox2D/ParticleSystem.h
Normal file
49
Sandbox/src/SandBox2D/ParticleSystem.h
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// Created by sfd on 25-5-21.
|
||||
//
|
||||
|
||||
#ifndef PARTICLESYSTEM_H
|
||||
#define PARTICLESYSTEM_H
|
||||
|
||||
#include <Hazel.h>
|
||||
|
||||
struct ParticleProp
|
||||
{
|
||||
glm::vec2 Position;
|
||||
glm::vec2 Velocity, VelocityVariation;
|
||||
glm::vec4 ColorBegin, ColorEnd;
|
||||
float SizeBegin, SizeEnd, SizeVariation;
|
||||
float LifeTime = 1.0f;
|
||||
};
|
||||
|
||||
class ParticleSystem
|
||||
{
|
||||
public:
|
||||
ParticleSystem();
|
||||
|
||||
void OnUpdate(Hazel::TimeStep& ts);
|
||||
void OnRender(Hazel::OrthographicCamera& camera);
|
||||
|
||||
void Emit(const ParticleProp& particuleProps);
|
||||
|
||||
private:
|
||||
struct Particle
|
||||
{
|
||||
glm::vec2 Position;
|
||||
glm::vec2 Velocity;
|
||||
glm::vec4 ColorBegin, ColorEnd;
|
||||
float Rotation = 0.0f;
|
||||
float SizeBegin, SizeEnd;
|
||||
|
||||
float LifeTime = 1.0f;
|
||||
float LifeRemaing = 0.0f;
|
||||
|
||||
bool Active = false;
|
||||
};
|
||||
|
||||
std::vector<Particle> m_particlePool;
|
||||
uint32_t m_PoolIndex = 999;
|
||||
};
|
||||
|
||||
|
||||
#endif //PARTICLESYSTEM_H
|
||||
@ -17,6 +17,14 @@ void SandBox2D::OnAttach()
|
||||
{
|
||||
HZ_PROFILE_FUNCTION();
|
||||
m_LogoTexture = Hazel::Texture2D::Create("assets/textures/iceLogo.png");
|
||||
|
||||
m_Particle.ColorBegin = {254 / 255.f, 212 /255.f, 123 / 255.f,1.0f};
|
||||
m_Particle.ColorEnd = {254 / 255.f, 109 /255.f, 41 / 255.f,1.0f};
|
||||
m_Particle.SizeBegin = 0.5f, m_Particle.SizeVariation = 0.3f, m_Particle.SizeEnd = 0.0f;
|
||||
m_Particle.LifeTime = 1.0f;
|
||||
m_Particle.Velocity = {0.0f, 0.0f};
|
||||
m_Particle.VelocityVariation = {2.0f, 2.0f};
|
||||
m_Particle.Position = {0.0f, 0.0f};
|
||||
}
|
||||
|
||||
void SandBox2D::OnDetech()
|
||||
@ -26,9 +34,11 @@ void SandBox2D::OnDetech()
|
||||
|
||||
void SandBox2D::OnUpdate(Hazel::TimeStep& ts)
|
||||
{
|
||||
auto mouseState = SDL_GetMouseState(NULL, NULL);
|
||||
// PROFILE_SCOPE("SandBox2D::OnUpdate");
|
||||
HZ_PROFILE_FUNCTION();
|
||||
|
||||
Hazel::Renderer2D::ResetStats();
|
||||
m_CameraController.OnUpdate(ts);
|
||||
|
||||
{
|
||||
@ -40,35 +50,67 @@ void SandBox2D::OnUpdate(Hazel::TimeStep& ts)
|
||||
HZ_PROFILE_SCOPE("Renderer Draw");
|
||||
Hazel::Renderer2D::BeginScene(m_CameraController.GetCamera());
|
||||
|
||||
// Hazel::Renderer2D::DrawQuad({0.0f, 0.0f}, {0.31f, 0.31f}, {0.2f, 0.3f, 0.8f, 1.0f});
|
||||
Hazel::Renderer2D::DrawQuad({-1.0f, 0.0f}, {0.7f, 0.7f}, {0.2f, 0.3f, 0.8f, 1.0f});
|
||||
Hazel::Renderer2D::DrawQuad({0.5f, 0.5f}, {0.5f, 0.7f}, {0.8f, 0.2f, 0.3f, 1.0f});
|
||||
// Hazel::Renderer2D::DrawQuad({0.0f, 0.0f, -0.1f}, {2.0f, 2.0f}, m_LogoTexture, 10.0f);
|
||||
// Hazel::Renderer2D::DrawRotateQuad({0.0f, 0.0f, 0.1f}, {2.0f, 2.0f}, glm::radians(45.f), m_LogoTexture, 10.0f, glm::vec4(1.0f, 1.0f, 1.0f, 1.f));
|
||||
Hazel::Renderer2D::DrawQuad({0.0f, 0.0f, -0.1f}, {2.0f, 2.0f}, m_LogoTexture, 10.0f);
|
||||
|
||||
// Hazel::Renderer2D::DrawRotateQuad({0.0f, 0.0f}, {1.0f, 1.0f}, 45.f, {1.0f, 1.0f, 1.0f, 1.0f});
|
||||
// Hazel::Renderer2D::DrawRotateQuad({-1.0f, 0.0f}, {0.5f, 0.5f}, 75.f, {1.0f, 0.0f, 1.0f, 1.0f});
|
||||
// Hazel::Renderer2D::DrawRotateQuad({0.0f, 0.0f}, {1.0f, 1.0f}, rotation, m_LogoTexture, 10.f);
|
||||
|
||||
// for (float y = -5.0f; y < 5.0f; y += 0.5f)
|
||||
// {
|
||||
// for (float x = -5.0f; x < 5.0f; x += 0.5f)
|
||||
// {
|
||||
// auto color = glm::vec4((x + 5.0f ) /10.0f, 0.4f, (y + 5.0f) / 10.0f, 1.0f);
|
||||
// Hazel::Renderer2D::DrawQuad({x, y}, {0.45f, 0.45f}, color);
|
||||
// }
|
||||
// }
|
||||
Hazel::Renderer2D::EndScene();
|
||||
|
||||
if (mouseState & SDL_BUTTON_LMASK)
|
||||
{
|
||||
auto width = Hazel::Application::Get().GetWindow().GetWidth();
|
||||
auto height = Hazel::Application::Get().GetWindow().GetHeight();
|
||||
|
||||
float x, y;
|
||||
SDL_GetMouseState(&x, &y);
|
||||
auto bounds = m_CameraController.GetBounds();
|
||||
auto cameraPos = m_CameraController.GetCamera().GetPosition();
|
||||
|
||||
x = (x / width) * bounds.GetWidth() - bounds.GetWidth() * 0.5f;
|
||||
y = bounds.GetHeight() * 0.5f - (y / height) * bounds.GetHeight();
|
||||
m_Particle.Position = {x + cameraPos.x, y + cameraPos.y};
|
||||
for (int i = 0; i < 5; i ++)
|
||||
{
|
||||
m_ParticleSystem.Emit(m_Particle);
|
||||
}
|
||||
}
|
||||
|
||||
m_ParticleSystem.OnUpdate(ts);
|
||||
m_ParticleSystem.OnRender(m_CameraController.GetCamera());
|
||||
}
|
||||
/*
|
||||
std::dynamic_pointer_cast<Hazel::OpenGLShader>(m_FlatColorShader)->Bind();
|
||||
std::dynamic_pointer_cast<Hazel::OpenGLShader>(m_FlatColorShader)->UploadUniformFloat4("u_Color", m_SquareColor);
|
||||
Hazel::Renderer::Submit(m_FlatColorShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f)));
|
||||
*/
|
||||
}
|
||||
|
||||
void SandBox2D::OnImGuiRender()
|
||||
{
|
||||
const auto cameraRotation = m_CameraController.GetCamera().GetRotation();
|
||||
// const auto cameraRotation = m_CameraController.GetCamera().GetRotation();
|
||||
const auto cameraPosition = m_CameraController.GetCamera().GetPosition();
|
||||
const auto windowWidth = Hazel::Application::Get().GetWindow().GetWidth();
|
||||
const auto windowHeight = Hazel::Application::Get().GetWindow().GetHeight();
|
||||
|
||||
auto stats = Hazel::Renderer2D::GetStats();
|
||||
|
||||
ImGui::Begin("Hazel Layer");
|
||||
|
||||
ImGui::Text("Renderer BatchInfo: ");
|
||||
ImGui::Text("Draw Calls: %d", stats.DrawCalls);
|
||||
ImGui::Text("Quads: %d", stats.QuadCount);
|
||||
ImGui::Text("Vertices: %d", stats.GetTotalVertexCount());
|
||||
ImGui::Text("Indices: %d", stats.GetTotalIndexCount());
|
||||
|
||||
ImGui::Text("WindowSize: (%u, %u)", windowWidth, windowHeight);
|
||||
|
||||
ImGui::Text("Camera");
|
||||
ImGui::Text("Rotation: %f", cameraRotation);
|
||||
ImGui::Text("Position: ( %.2f, %.2f, %.2f)", cameraPosition.x, cameraPosition.y, cameraPosition.z);
|
||||
// ImGui::Text("Camera Rotation: %f", cameraRotation);
|
||||
ImGui::Text("Camera Position: ( %.2f, %.2f, %.2f)", cameraPosition.x, cameraPosition.y, cameraPosition.z);
|
||||
|
||||
ImGui::NewLine();
|
||||
ImGui::Text("frame: %.3f", ImGui::GetIO().Framerate);
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#define SANDBOX2D_H
|
||||
#include <Hazel.h>
|
||||
|
||||
#include "ParticleSystem.h"
|
||||
|
||||
class SandBox2D : public Hazel::Layer{
|
||||
public:
|
||||
@ -33,6 +34,9 @@ private:
|
||||
};
|
||||
|
||||
std::vector<ProfileResult> m_ProfileResults;
|
||||
|
||||
ParticleSystem m_ParticleSystem;
|
||||
ParticleProp m_Particle;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -1,232 +1,9 @@
|
||||
#include <Hazel.h>
|
||||
#include "Hazel/Core/EntryPoint.h"
|
||||
|
||||
// #include "SandBox2D/SandBox2D.h"
|
||||
// #include "DemoBox/GameLayer.h"
|
||||
#include "SandBox2D/SandBox2D.h"
|
||||
|
||||
/*
|
||||
class ExampleLayer : public Hazel::Layer
|
||||
{
|
||||
public:
|
||||
|
||||
ExampleLayer() : Layer("ExampleLayer"), m_CameraController(1280.0f / 720.0f), m_SquarePosition(glm::vec3(0.0f))
|
||||
{
|
||||
// ------------------------------------------------------------test------------------------------------------------------------
|
||||
// Vertex Array
|
||||
m_VertexArray = Hazel::VertexArray::Create();
|
||||
|
||||
// Vertex Buffer
|
||||
float vertices[3 * 7] = {
|
||||
-0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f,
|
||||
0.5f, -0.5f, 0.0f, 0.2f, 0.3f, 0.8f, 1.0f,
|
||||
0.0f, 0.5f, 0.0f, 0.8f, 0.8f, 0.2f, 1.0f
|
||||
};
|
||||
std::shared_ptr<Hazel::VertexBuffer> m_VertexBuffer;
|
||||
m_VertexBuffer.reset(Hazel::VertexBuffer::Create(vertices, sizeof(vertices)));
|
||||
Hazel::BufferLayout layout = {
|
||||
{Hazel::ShaderDataType::Float3, "a_Postion"},
|
||||
{Hazel::ShaderDataType::Float4, "a_Color"},
|
||||
};
|
||||
m_VertexBuffer->SetLayout(layout);
|
||||
m_VertexArray->AddVertexBuffer(m_VertexBuffer);
|
||||
|
||||
|
||||
uint32_t indices[6] = {0, 1, 2};
|
||||
std::shared_ptr<Hazel::IndexBuffer> m_IndexBuffer;
|
||||
m_IndexBuffer.reset(Hazel::IndexBuffer::Create(indices, sizeof(indices) / sizeof(indices[0])));
|
||||
m_VertexArray->SetIndexBuffer(m_IndexBuffer);
|
||||
|
||||
// Shader
|
||||
std::string vertexSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) in vec3 a_Position;
|
||||
layout(location = 1) in vec3 a_Color;
|
||||
|
||||
uniform mat4 u_ViewProjection;
|
||||
uniform mat4 u_Transform;
|
||||
|
||||
out vec3 v_Position;
|
||||
out vec3 v_Color;
|
||||
|
||||
void main() {
|
||||
v_Position = a_Position;
|
||||
v_Color = a_Color;
|
||||
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
|
||||
}
|
||||
|
||||
)";
|
||||
std::string fragmentSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
in vec3 v_Position;
|
||||
in vec3 v_Color;
|
||||
|
||||
void main() {
|
||||
color = vec4(v_Color, 1.0f);
|
||||
}
|
||||
)";
|
||||
m_Shader = Hazel::Shader::Create("demoShader", vertexSrc, fragmentSrc);
|
||||
|
||||
|
||||
m_SquareVA = Hazel::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
|
||||
};
|
||||
std::shared_ptr<Hazel::VertexBuffer> squareVB; //= VertexBuffer::Create(squareVertices, sizeof(squareVertices));
|
||||
squareVB.reset(Hazel::VertexBuffer::Create(squareVertices, sizeof(squareVertices)));
|
||||
squareVB->SetLayout(
|
||||
{
|
||||
{Hazel::ShaderDataType::Float3, "a_Postion"},
|
||||
{Hazel::ShaderDataType::Float2, "a_TexCoord"}
|
||||
});
|
||||
m_SquareVA->AddVertexBuffer(squareVB);
|
||||
|
||||
|
||||
uint32_t squareIndice[6] = {0, 1, 2, 2, 3, 0};
|
||||
std::shared_ptr<Hazel::IndexBuffer> squareIB;
|
||||
// = std::make_shared<IndexBuffer>(IndexBuffer::Create(squareIndice, sizeof(squareIndice) / sizeof(uint32_t)));
|
||||
squareIB.reset(Hazel::IndexBuffer::Create(squareIndice, sizeof(squareIndice) / sizeof(uint32_t)));
|
||||
m_SquareVA->SetIndexBuffer(squareIB);
|
||||
|
||||
std::string coloeVertexSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(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;
|
||||
|
||||
void main() {
|
||||
v_TexCoord = a_TexCoord;
|
||||
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
|
||||
}
|
||||
)";
|
||||
std::string colorFragmentSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
in vec3 v_Position;
|
||||
uniform vec4 u_Color;
|
||||
|
||||
void main() {
|
||||
color = u_Color;
|
||||
}
|
||||
)";
|
||||
m_colorShader = Hazel::Shader::Create("ColorShader", coloeVertexSrc, colorFragmentSrc);
|
||||
|
||||
|
||||
// Texture.Shader
|
||||
// m_TextureShader = Hazel::Shader::Create("assets/shaders/Texture.glsl");
|
||||
auto textureShader = m_ShaderLibrary.Load("assets/shaders/Texture.glsl");
|
||||
|
||||
|
||||
// Texture
|
||||
m_Texture = Hazel::Texture2D::Create("assets/textures/Checkerboard.png");
|
||||
m_logoTexture = Hazel::Texture2D::Create("assets/textures/iceLogo.png");
|
||||
|
||||
std::dynamic_pointer_cast<Hazel::OpenGLShader>(textureShader)->Bind();
|
||||
std::dynamic_pointer_cast<Hazel::OpenGLShader>(textureShader)->UploadUniformInt("u_Texture", 0);
|
||||
}
|
||||
|
||||
void OnUpdate(Hazel::TimeStep& ts) override
|
||||
{
|
||||
m_CameraController.OnUpdate(ts);
|
||||
|
||||
// key event
|
||||
{
|
||||
float time = ts;
|
||||
|
||||
const bool* state = SDL_GetKeyboardState(NULL);
|
||||
|
||||
if (state[SDL_SCANCODE_I])
|
||||
{
|
||||
m_SquarePosition.y += 5 * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_K])
|
||||
{
|
||||
m_SquarePosition.y -= 5 * time;
|
||||
}
|
||||
if (state[SDL_SCANCODE_J])
|
||||
{
|
||||
m_SquarePosition.x -= 5 * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_L])
|
||||
{
|
||||
m_SquarePosition.x += 5 * time;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Hazel::RendererCommand::SetClearColor(m_ScreenClearColor);
|
||||
Hazel::RendererCommand::Clear();
|
||||
|
||||
Hazel::Renderer::BeginScene(m_CameraController.GetCamera());
|
||||
|
||||
auto transform = glm::translate(glm::mat4(1.0f), m_SquarePosition);
|
||||
|
||||
auto textureShader = m_ShaderLibrary.Get("Texture");
|
||||
m_Texture->Bind();
|
||||
Hazel::Renderer::Submit(textureShader, m_SquareVA, transform);
|
||||
|
||||
m_logoTexture->Bind();
|
||||
Hazel::Renderer::Submit(textureShader, m_SquareVA);
|
||||
|
||||
Hazel::Renderer::EndScene();
|
||||
}
|
||||
|
||||
void OnEvent(SDL_Event& event) override
|
||||
{
|
||||
m_CameraController.OnEvent(event);
|
||||
}
|
||||
|
||||
void OnImGuiRender() override
|
||||
{
|
||||
ImGui::ShowDemoWindow();
|
||||
|
||||
const auto cameraRotation = m_CameraController.GetCamera().GetRotation();
|
||||
const auto cameraPosition = m_CameraController.GetCamera().GetPosition();
|
||||
ImGui::Begin("Hazel Layer");
|
||||
ImGui::Text("Rotation: %f", cameraRotation);
|
||||
ImGui::Text("Position: ( %.2f, %.2f, %.2f)", cameraPosition.x, cameraPosition.y, cameraPosition.z);
|
||||
ImGui::Text("frame: %.3f", ImGui::GetIO().Framerate);
|
||||
ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor));
|
||||
ImGui::NewLine();
|
||||
ImGui::ColorEdit4("Screen Clear Color", glm::value_ptr(m_ScreenClearColor));
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
private:
|
||||
Hazel::ShaderLibrary m_ShaderLibrary;
|
||||
Hazel::Ref<Hazel::Shader> m_Shader;
|
||||
Hazel::Ref<Hazel::VertexArray> m_VertexArray;
|
||||
|
||||
Hazel::Ref<Hazel::VertexArray> m_SquareVA;
|
||||
Hazel::Ref<Hazel::Shader> m_colorShader;
|
||||
|
||||
Hazel::Ref<Hazel::Texture2D> m_Texture, m_logoTexture;
|
||||
|
||||
Hazel::OrthographicCameraController m_CameraController;
|
||||
|
||||
glm::vec3 m_SquarePosition;
|
||||
float m_SquareMoveSpeed = 1.0f;
|
||||
|
||||
glm::vec3 m_SquareColor = {0.2f, 0.3f, 0.8f};
|
||||
|
||||
glm::vec4 m_ScreenClearColor = {0.1f, 0.1f, 0.1f, 1.0f};
|
||||
};
|
||||
*/
|
||||
// #include "DemoBox/GameLayer.h"
|
||||
// #include "Example/ExampleLayer.h"
|
||||
|
||||
|
||||
|
||||
@ -237,6 +14,7 @@ public:
|
||||
{
|
||||
// PushLayer(new ExampleLayer());
|
||||
PushLayer(new SandBox2D());
|
||||
// PushLayer(new GameLayer());
|
||||
}
|
||||
|
||||
~Sandbox();
|
||||
|
||||
Reference in New Issue
Block a user