添加贴图
This commit is contained in:
BIN
Sandbox/assets/textures/Checkerboard.png
Normal file
BIN
Sandbox/assets/textures/Checkerboard.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
BIN
Sandbox/assets/textures/container2.png
Normal file
BIN
Sandbox/assets/textures/container2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 435 KiB |
BIN
Sandbox/assets/textures/iceLogo.png
Normal file
BIN
Sandbox/assets/textures/iceLogo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 216 KiB |
@ -1,39 +1,298 @@
|
||||
#include <Hazel.h>
|
||||
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_sdl2.h"
|
||||
#include "imgui_impl_opengl3.h"
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <Platform/OpenGL/OpenGLShader.h>
|
||||
#include <Platform/OpenGL/OpenGLTexture.h>
|
||||
|
||||
#include "glm/gtc/type_ptr.hpp"
|
||||
#include "glm/gtx/transform.hpp"
|
||||
|
||||
|
||||
class ExampleLayer : public Hazel::Layer
|
||||
{
|
||||
public:
|
||||
ExampleLayer() : Layer("ExampleLayer")
|
||||
// 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))
|
||||
{
|
||||
// ------------------------------------------------------------test------------------------------------------------------------
|
||||
// Vertex Array
|
||||
m_VertexArray.reset(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.reset(Hazel::Shader::Create(vertexSrc, fragmentSrc));
|
||||
|
||||
|
||||
m_SquareVA.reset(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 bluevertexSrc = 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 bluefragmentSrc = 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.reset(Hazel::Shader::Create(bluevertexSrc, bluefragmentSrc));
|
||||
|
||||
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
void OnUpdate() override
|
||||
void OnUpdate(Hazel::TimeStep& ts) override
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_K])
|
||||
{
|
||||
m_SquarePosition.y -= m_CameraSpeed * time;
|
||||
}
|
||||
if (state[SDL_SCANCODE_J])
|
||||
{
|
||||
m_SquarePosition.x -= m_CameraSpeed * time;
|
||||
}
|
||||
else if (state[SDL_SCANCODE_L])
|
||||
{
|
||||
m_SquarePosition.x += m_CameraSpeed * time;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Hazel::RendererCommand::SetClearColor({0.1f, 0.1f, 0.1f, 1.0f});
|
||||
Hazel::RendererCommand::Clear();
|
||||
|
||||
m_Camera.SetPosition(m_CameraPosition);
|
||||
m_Camera.SetRotation(m_CameraRotation);
|
||||
|
||||
|
||||
|
||||
Hazel::Renderer::BeginScene(m_Camera);
|
||||
|
||||
auto transform = glm::translate(glm::mat4(1.0f), m_SquarePosition);
|
||||
|
||||
m_Texture->Bind();
|
||||
Hazel::Renderer::Submit(m_TextureShader, m_SquareVA, transform);
|
||||
|
||||
m_logoTexture->Bind();
|
||||
Hazel::Renderer::Submit(m_TextureShader, m_SquareVA);
|
||||
|
||||
Hazel::Renderer::EndScene();
|
||||
}
|
||||
|
||||
void OnEvent(SDL_Event& event) override
|
||||
{
|
||||
if (event.type == SDL_EVENT_KEY_DOWN)
|
||||
{
|
||||
HZ_CORE_TRACE("{}", event.key.key);
|
||||
}
|
||||
}
|
||||
|
||||
void OnImGuiRender() override
|
||||
{
|
||||
ImGui::Begin("Hazel Layer");
|
||||
ImGui::Text("Hello World");
|
||||
ImGui::Text("Rotation: %f", m_CameraRotation);
|
||||
ImGui::Text("Position: ( %.2f, %.2f, %.2f)", m_CameraPosition.x, m_CameraPosition.y, m_CameraPosition.z);
|
||||
ImGui::Text("frame: %.3f", ImGui::GetIO().Framerate);
|
||||
ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor));
|
||||
ImGui::End();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
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::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;
|
||||
|
||||
glm::vec3 m_SquarePosition;
|
||||
float m_SquareMoveSpeed = 1.0f;
|
||||
|
||||
glm::vec3 m_SquareColor = {0.2f, 0.3f, 0.8f};
|
||||
};
|
||||
|
||||
|
||||
@ -44,15 +303,16 @@ public:
|
||||
{
|
||||
PushLayer(new ExampleLayer());
|
||||
}
|
||||
|
||||
~Sandbox();
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
|
||||
Sandbox::~Sandbox() = default;
|
||||
|
||||
Hazel::Application* Hazel::CreateApplication() {
|
||||
Hazel::Application* Hazel::CreateApplication()
|
||||
{
|
||||
return new Sandbox();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user