修复例子测试小bug
This commit is contained in:
@ -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
|
||||
112
Sandbox/src/SandBox2D/ParticleSystem.cpp
Normal file
112
Sandbox/src/SandBox2D/ParticleSystem.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
//
|
||||
// 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(m_PoolIndex--);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
bool isParticule = false;
|
||||
for (auto& particle : m_particlePool)
|
||||
{
|
||||
if (particle.Active)
|
||||
{
|
||||
isParticule = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isParticule)
|
||||
{
|
||||
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.LifeTime;
|
||||
particle.SizeBegin = particuleProps.SizeBegin + particuleProps.SizeVariation * (Random::Float() - 0.5f);
|
||||
particle.SizeEnd = particuleProps.SizeEnd;
|
||||
|
||||
m_PoolIndex = --m_PoolIndex % m_particlePool.size();
|
||||
}
|
||||
50
Sandbox/src/SandBox2D/ParticleSystem.h
Normal file
50
Sandbox/src/SandBox2D/ParticleSystem.h
Normal file
@ -0,0 +1,50 @@
|
||||
//
|
||||
// Created by sfd on 25-5-21.
|
||||
//
|
||||
|
||||
#ifndef PARTICLESYSTEM_H
|
||||
#define PARTICLESYSTEM_H
|
||||
|
||||
#include <Hazel.h>
|
||||
#include <glad/glad.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 = 5000;
|
||||
};
|
||||
|
||||
|
||||
#endif //PARTICLESYSTEM_H
|
||||
@ -9,7 +9,7 @@
|
||||
#include "glm/gtc/type_ptr.hpp"
|
||||
|
||||
SandBox2D::SandBox2D()
|
||||
: Layer("SandBox2D"), m_CameraController(1280.f /720.f)
|
||||
: Layer("SandBox2D"), m_CameraController((float)Hazel::Application::Get().GetWindow().GetWidth() / (float)Hazel::Application::Get().GetWindow().GetHeight())
|
||||
{
|
||||
}
|
||||
|
||||
@ -17,6 +17,15 @@ void SandBox2D::OnAttach()
|
||||
{
|
||||
HZ_PROFILE_FUNCTION();
|
||||
m_LogoTexture = Hazel::Texture2D::Create("assets/textures/iceLogo.png");
|
||||
|
||||
m_Particle.ColorBegin = {0 / 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,53 +35,94 @@ 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);
|
||||
|
||||
{
|
||||
HZ_PROFILE_SCOPE("Renderer Prep");
|
||||
Hazel::RendererCommand::SetClearColor({0.2f, 0.2f, 0.2f, 1.0f});
|
||||
// Hazel::RendererCommand::SetClearColor({0.2f, 0.2f, 0.2f, 1.0f});
|
||||
Hazel::RendererCommand::SetClearColor(m_BackgroundColor);
|
||||
Hazel::RendererCommand::Clear();
|
||||
}
|
||||
{
|
||||
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);
|
||||
ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor));
|
||||
ImGui::ColorEdit4("Square Color", glm::value_ptr(m_BackgroundColor));
|
||||
|
||||
|
||||
ImGui::NewLine();
|
||||
ImGui::ColorEdit4("begin Color", glm::value_ptr(m_Particle.ColorBegin));
|
||||
ImGui::ColorEdit4("End Color", glm::value_ptr(m_Particle.ColorEnd));
|
||||
|
||||
ImGui::NewLine();
|
||||
for (auto& profileResult : m_ProfileResults)
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#define SANDBOX2D_H
|
||||
#include <Hazel.h>
|
||||
|
||||
#include "ParticleSystem.h"
|
||||
|
||||
class SandBox2D : public Hazel::Layer{
|
||||
public:
|
||||
@ -24,7 +25,7 @@ private:
|
||||
Hazel::Ref<Hazel::Texture2D> m_Texture;
|
||||
Hazel::Ref<Hazel::Texture2D> m_LogoTexture;
|
||||
|
||||
glm::vec4 m_SquareColor = {0.2f, 0.3f, 0.8f, 1.0f};
|
||||
glm::vec4 m_BackgroundColor = {0.2f, 0.2f, 0.2f, 1.0f};
|
||||
|
||||
struct ProfileResult
|
||||
{
|
||||
@ -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