增加窗口自由缩放
This commit is contained in:
38
Sandbox/assets/shaders/Texture.glsl
Normal file
38
Sandbox/assets/shaders/Texture.glsl
Normal file
@ -0,0 +1,38 @@
|
||||
#type vertex
|
||||
#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);
|
||||
}
|
||||
|
||||
#type fragment
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
in vec2 v_TexCoord;
|
||||
|
||||
uniform sampler2D u_Texture;
|
||||
|
||||
void main() {
|
||||
vec4 tmpColor = texture(u_Texture, v_TexCoord);
|
||||
if(gl_FragCoord.x < 635){
|
||||
color = tmpColor;
|
||||
}
|
||||
else{
|
||||
if(tmpColor.a < 0.1){
|
||||
discard;
|
||||
}else{
|
||||
color = vec4(v_TexCoord, 0.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,7 @@
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <Hazel/OrthographicCameraController.h>
|
||||
#include <Platform/OpenGL/OpenGLShader.h>
|
||||
#include <Platform/OpenGL/OpenGLTexture.h>
|
||||
|
||||
@ -17,7 +17,7 @@ class ExampleLayer : public Hazel::Layer
|
||||
public:
|
||||
// ExampleLayer() : Layer("ExampleLayer"), m_Camera(-1.0f, 1.0f, -1.0f, 1.0f), m_CameraPosition(0.0f)
|
||||
|
||||
ExampleLayer() : Layer("ExampleLayer"), m_Camera(-1.6f, 1.6f, -0.9f, 0.9f), m_CameraPosition(0.0f), m_SquarePosition(glm::vec3(0.0f))
|
||||
ExampleLayer() : Layer("ExampleLayer"), m_CameraController(1280.0f / 720.0f), m_SquarePosition(glm::vec3(0.0f))
|
||||
{
|
||||
// ------------------------------------------------------------test------------------------------------------------------------
|
||||
// Vertex Array
|
||||
@ -76,7 +76,7 @@ public:
|
||||
color = vec4(v_Color, 1.0f);
|
||||
}
|
||||
)";
|
||||
m_Shader.reset(Hazel::Shader::Create(vertexSrc, fragmentSrc));
|
||||
m_Shader = Hazel::Shader::Create("demoShader", vertexSrc, fragmentSrc);
|
||||
|
||||
|
||||
m_SquareVA.reset(Hazel::VertexArray::Create());
|
||||
@ -103,7 +103,7 @@ public:
|
||||
squareIB.reset(Hazel::IndexBuffer::Create(squareIndice, sizeof(squareIndice) / sizeof(uint32_t)));
|
||||
m_SquareVA->SetIndexBuffer(squareIB);
|
||||
|
||||
std::string bluevertexSrc = R"(
|
||||
std::string coloeVertexSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) in vec3 a_Position;
|
||||
@ -119,7 +119,7 @@ public:
|
||||
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
|
||||
}
|
||||
)";
|
||||
std::string bluefragmentSrc = R"(
|
||||
std::string colorFragmentSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
@ -131,168 +131,107 @@ public:
|
||||
color = u_Color;
|
||||
}
|
||||
)";
|
||||
m_colorShader.reset(Hazel::Shader::Create(bluevertexSrc, bluefragmentSrc));
|
||||
m_colorShader = Hazel::Shader::Create("ColorShader", coloeVertexSrc, colorFragmentSrc);
|
||||
|
||||
|
||||
std::string textureVertexSrc = 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 textureFragmentSrc = R"(
|
||||
#version 460 core
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
in vec2 v_TexCoord;
|
||||
|
||||
uniform sampler2D u_Texture;
|
||||
|
||||
void main() {
|
||||
vec4 tmpColor = texture(u_Texture, v_TexCoord);
|
||||
if(gl_FragCoord.x < 635){
|
||||
color = tmpColor;
|
||||
}
|
||||
else{
|
||||
if(tmpColor.a < 0.1){
|
||||
discard;
|
||||
}else{
|
||||
color = vec4(v_TexCoord, 0.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
m_TextureShader.reset(Hazel::Shader::Create(textureVertexSrc, textureFragmentSrc));
|
||||
// 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>(m_TextureShader)->Bind();
|
||||
std::dynamic_pointer_cast<Hazel::OpenGLShader>(m_TextureShader)->UploadUniformInt("u_Texture", 0);
|
||||
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_A])
|
||||
{
|
||||
m_CameraPosition.x -= m_CameraSpeed * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_D])
|
||||
{
|
||||
m_CameraPosition.x += m_CameraSpeed * time;
|
||||
}
|
||||
if (state[SDL_SCANCODE_W])
|
||||
{
|
||||
m_CameraPosition.y += m_CameraSpeed * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_S])
|
||||
{
|
||||
m_CameraPosition.y -= m_CameraSpeed * time;
|
||||
}
|
||||
if (state[SDL_SCANCODE_Q])
|
||||
{
|
||||
m_CameraRotation += m_CameraRotationSpeed * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_E])
|
||||
{
|
||||
m_CameraRotation -= m_CameraRotationSpeed * time;
|
||||
}
|
||||
|
||||
if (state[SDL_SCANCODE_I])
|
||||
{
|
||||
m_SquarePosition.y += m_CameraSpeed * time;
|
||||
m_SquarePosition.y += 5 * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_K])
|
||||
{
|
||||
m_SquarePosition.y -= m_CameraSpeed * time;
|
||||
m_SquarePosition.y -= 5 * time;
|
||||
}
|
||||
if (state[SDL_SCANCODE_J])
|
||||
{
|
||||
m_SquarePosition.x -= m_CameraSpeed * time;
|
||||
m_SquarePosition.x -= 5 * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_L])
|
||||
{
|
||||
m_SquarePosition.x += m_CameraSpeed * time;
|
||||
m_SquarePosition.x += 5 * time;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Hazel::RendererCommand::SetClearColor({0.1f, 0.1f, 0.1f, 1.0f});
|
||||
Hazel::RendererCommand::SetClearColor(m_ScreenClearColor);
|
||||
Hazel::RendererCommand::Clear();
|
||||
|
||||
m_Camera.SetPosition(m_CameraPosition);
|
||||
m_Camera.SetRotation(m_CameraRotation);
|
||||
|
||||
|
||||
|
||||
Hazel::Renderer::BeginScene(m_Camera);
|
||||
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(m_TextureShader, m_SquareVA, transform);
|
||||
Hazel::Renderer::Submit(textureShader, m_SquareVA, transform);
|
||||
|
||||
m_logoTexture->Bind();
|
||||
Hazel::Renderer::Submit(m_TextureShader, m_SquareVA);
|
||||
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", m_CameraRotation);
|
||||
ImGui::Text("Position: ( %.2f, %.2f, %.2f)", m_CameraPosition.x, m_CameraPosition.y, m_CameraPosition.z);
|
||||
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, m_TextureShader;
|
||||
Hazel::Ref<Hazel::Shader> m_colorShader;
|
||||
|
||||
Hazel::Ref<Hazel::Texture2D> m_Texture, m_logoTexture;
|
||||
|
||||
Hazel::OrthographicCamera m_Camera;
|
||||
|
||||
glm::vec3 m_CameraPosition;
|
||||
float m_CameraSpeed = 1.0f;
|
||||
|
||||
float m_CameraRotation = 0.0f;
|
||||
float m_CameraRotationSpeed = 30.f;
|
||||
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};
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user