前置:
YOTO.h:
#pragma once
//用于YOTO APP
#include "YOTO/Application.h"
#include"YOTO/Layer.h"
#include "YOTO/Log.h"
#include"YOTO/Core/Timestep.h"
#include"YOTO/Input.h"
#include"YOTO/KeyCode.h"
#include"YOTO/MouseButtonCodes.h"
#include "YOTO/OrthographicCameraController.h"
#include"YOTO/ImGui/ImGuiLayer.h"
//Renderer
#include"YOTO/Renderer/Renderer.h"
#include"YOTO/Renderer/RenderCommand.h"
#include"YOTO/Renderer/Buffer.h"
#include"YOTO/Renderer/Shader.h"
#include"YOTO/Renderer/Texture.h"
#include"YOTO/Renderer/VertexArray.h"
#include"YOTO/Renderer/OrthographicCamera.h"
//入口点
#include"YOTO/EntryPoint.h"
添加方法:
OrthographicCamera.h:
#pragma once
#include <glm/glm.hpp>
namespace YOTO {
class OrthographicCamera
{
public:
OrthographicCamera(float left, float right, float bottom, float top);
void SetProjection(float left, float right, float bottom, float top);
const glm::vec3& GetPosition()const { return m_Position; }
void SetPosition(const glm::vec3& position) {
m_Position = position;
RecalculateViewMatrix();
}
float GetRotation()const { return m_Rotation; }
void SetRotation(float rotation) {
m_Rotation = rotation;
RecalculateViewMatrix();
}
const glm::mat4& GetProjectionMatrix()const { return m_ProjectionMatrix; }
const glm::mat4& GetViewMatrix()const { return m_ViewMatrix; }
const glm::mat4& GetViewProjectionMatrix()const { return m_ViewProjectionMatrix; }
private:
void RecalculateViewMatrix();
private:
glm::mat4 m_ProjectionMatrix;
glm::mat4 m_ViewMatrix;
glm::mat4 m_ViewProjectionMatrix;
glm::vec3 m_Position = { 0.0f ,0.0f ,0.0f };
float m_Rotation = 0.0f;
};
}
OrthographicCamera.cpp:
#include "ytpch.h"
#include "OrthographicCamera.h"
#include <glm/gtc/matrix_transform.hpp>
namespace YOTO {
OrthographicCamera::OrthographicCamera(float left, float right, float bottom, float top)
:m_ProjectionMatrix(glm::ortho(left, right, bottom, top)), m_ViewMatrix(1.0f)
{
m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
}
void OrthographicCamera::SetProjection(float left, float right, float bottom, float top) {
m_ProjectionMatrix = glm::ortho(left, right, bottom, top);
m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
}
void OrthographicCamera::RecalculateViewMatrix()
{
glm::mat4 transform = glm::translate(glm::mat4(1.0f), m_Position) *
glm::rotate(glm::mat4(1.0f), glm::radians(m_Rotation), glm::vec3(0, 0, 1));
m_ViewMatrix = glm::inverse(transform);
m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
}
}
封装控制类:
OrthographicCameraController.h:
#pragma once
#include "YOTO/Renderer/OrthographicCamera.h"
#include"YOTO/Core/Timestep.h"
#include "YOTO/Event/ApplicationEvent.h"
#include "YOTO/Event/MouseEvent.h"
namespace YOTO {
class OrthographicCameraController {
public:
OrthographicCameraController(float aspectRatio, bool rotation=false );//0,1280
void OnUpdate(Timestep ts);
void OnEvent(Event& e);
OrthographicCamera& GetCamera() {
return m_Camera
;
}
const OrthographicCamera& GetCamera()const {
return m_Camera
;
}
private:
bool OnMouseScrolled(MouseScrolledEvent &e);
bool OnWindowResized(WindowResizeEvent& e);
private:
float m_AspectRatio;//横纵比
float m_ZoomLevel = 1.0f;
OrthographicCamera m_Camera;
bool m_Rotation;
glm::vec3 m_CameraPosition = {0.0f,0.0f,0.0f};
float m_CameraRotation = 0.0f;
float m_CameraTranslationSpeed = 1.0f, m_CameraRotationSpeed = 180.0f;
};
}
OrthographicCameraController.cpp:
#include "ytpch.h"
#include "OrthographicCameraController.h"
#include"YOTO/Input.h"
#include <YOTO/KeyCode.h>
namespace YOTO {
OrthographicCameraController::OrthographicCameraController(float aspectRatio, bool rotation)
:m_AspectRatio(aspectRatio),m_Camera(-m_AspectRatio*m_ZoomLevel,m_AspectRatio*m_ZoomLevel,-m_ZoomLevel,m_ZoomLevel),m_Rotation(rotation)
{
}
void OrthographicCameraController::OnUpdate(Timestep ts)
{
if (Input::IsKeyPressed(YT_KEY_A)) {
m_CameraPosition.x -= m_CameraTranslationSpeed * ts;
}
else if (Input::IsKeyPressed(YT_KEY_D)) {
m_CameraPosition.x += m_CameraTranslationSpeed * ts;
}
if (Input::IsKeyPressed(YT_KEY_S)) {
m_CameraPosition.y -= m_CameraTranslationSpeed * ts;
}
else if (Input::IsKeyPressed(YT_KEY_W)) {
m_CameraPosition.y += m_CameraTranslationSpeed * ts;
}
if (m_Rotation) {
if (Input::IsKeyPressed(YT_KEY_Q)) {
m_CameraRotation += m_CameraRotationSpeed * ts;
}
else if (Input::IsKeyPressed(YT_KEY_E)) {
m_CameraRotation -= m_CameraRotationSpeed * ts;
}
m_Camera.SetRotation(m_CameraRotation);
}
m_Camera.SetPosition(m_CameraPosition);
m_CameraTranslationSpeed = m_ZoomLevel;
}
void OrthographicCameraController::OnEvent(Event& e)
{
EventDispatcher dispatcher(e);
dispatcher.Dispatch<MouseScrolledEvent>(YT_BIND_EVENT_FN(OrthographicCameraController::OnMouseScrolled));
dispatcher.Dispatch<WindowResizeEvent>(YT_BIND_EVENT_FN(OrthographicCameraController::OnWindowResized));
}
bool OrthographicCameraController::OnMouseScrolled(MouseScrolledEvent& e)
{
m_ZoomLevel -= e.GetYOffset()*0.5f;
m_ZoomLevel = std::max(m_ZoomLevel, 0.25f);
m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);
return false;
}
bool OrthographicCameraController::OnWindowResized(WindowResizeEvent& e)
{
m_AspectRatio = (float)e.GetWidth()/(float) e.GetHeight();
m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);
return false;
}
}
SandboxApp.cpp:
#include<YOTO.h>
#include "imgui/imgui.h"
#include<stdio.h>
#include <glm/gtc/matrix_transform.hpp>
#include <Platform/OpenGL/OpenGLShader.h>
#include <glm/gtc/type_ptr.hpp>
class ExampleLayer:public YOTO::Layer
{
public:
ExampleLayer()
:Layer("Example"), m_CameraController(1280.0f/720.0f,true){
uint32_t indices[3] = { 0,1,2 };
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,
};
m_VertexArray.reset(YOTO::VertexArray::Create());
YOTO::Ref<YOTO::VertexBuffer> m_VertexBuffer;
m_VertexBuffer.reset(YOTO::VertexBuffer::Create(vertices, sizeof(vertices)));
{
YOTO::BufferLayout setlayout = {
{YOTO::ShaderDataType::Float3,"a_Position"},
{YOTO::ShaderDataType::Float4,"a_Color"}
};
m_VertexBuffer->SetLayout(setlayout);
}
m_VertexArray->AddVertexBuffer(m_VertexBuffer);
YOTO::Ref<YOTO::IndexBuffer>m_IndexBuffer;
m_IndexBuffer.reset(YOTO::IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t)));
m_VertexArray->AddIndexBuffer(m_IndexBuffer);
std::string vertexSource = R"(
#version 330 core
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec4 a_Color;
uniform mat4 u_ViewProjection;
uniform mat4 u_Transform;
out vec3 v_Position;
out vec4 v_Color;
void main(){
v_Position=a_Position;
v_Color=a_Color;
gl_Position =u_ViewProjection *u_Transform* vec4( a_Position,1.0);
}
)";
//绘制颜色
std::string fragmentSource = R"(
#version 330 core
layout(location = 0) out vec4 color;
in vec3 v_Position;
in vec4 v_Color;
void main(){
color=vec4(v_Color);
}
)";
m_Shader=(YOTO::Shader::Create("VertexPosColor", vertexSource, fragmentSource));
///测试/
m_SquareVA.reset(YOTO::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,
};
YOTO::Ref<YOTO::VertexBuffer> squareVB;
squareVB.reset(YOTO::VertexBuffer::Create(squareVertices, sizeof(squareVertices)));
squareVB->SetLayout({
{YOTO::ShaderDataType::Float3,"a_Position"},
{YOTO::ShaderDataType::Float2,"a_TexCoord"}
});
m_SquareVA->AddVertexBuffer(squareVB);
uint32_t squareIndices[6] = { 0,1,2,2,3,0 };
YOTO::Ref<YOTO::IndexBuffer> squareIB;
squareIB.reset((YOTO::IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))));
m_SquareVA->AddIndexBuffer(squareIB);
//测试:
std::string BlueShaderVertexSource = R"(
#version 330 core
layout(location = 0) in vec3 a_Position;
uniform mat4 u_ViewProjection;
uniform mat4 u_Transform;
out vec3 v_Position;
void main(){
v_Position=a_Position;
gl_Position =u_ViewProjection*u_Transform*vec4( a_Position,1.0);
}
)";
//绘制颜色
std::string BlueShaderFragmentSource = R"(
#version 330 core
layout(location = 0) out vec4 color;
in vec3 v_Position;
uniform vec3 u_Color;
void main(){
color=vec4(u_Color,1.0);
}
)";
m_BlueShader=(YOTO::Shader::Create("FlatColor", BlueShaderVertexSource, BlueShaderFragmentSource));
auto textureShader= m_ShaderLibrary.Load("assets/shaders/Texture.glsl");
m_Texture=YOTO::Texture2D::Create("assets/textures/Checkerboard.png");
m_ChernoLogo= YOTO::Texture2D::Create("assets/textures/ChernoLogo.png");
std::dynamic_pointer_cast<YOTO::OpenGLShader>(textureShader)->Bind();
std::dynamic_pointer_cast<YOTO::OpenGLShader>(textureShader)->UploadUniformInt("u_Texture", 0);
}
void OnImGuiRender() override {
ImGui::Begin("设置");
ImGui::ColorEdit3("正方形颜色", glm::value_ptr(m_SquareColor));
ImGui::End();
}
void OnUpdate(YOTO::Timestep ts)override {
//update
m_CameraController.OnUpdate(ts);
//YT_CLIENT_TRACE("delta time {0}s ({1}ms)", ts.GetSeconds(), ts.GetMilliseconds());
//Render
YOTO::RenderCommand::SetClearColor({ 0.2f, 0.2f, 0.2f, 1.0f });
YOTO::RenderCommand::Clear();
YOTO::Renderer::BeginScene(m_CameraController.GetCamera());
{
static glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
glm::vec4 redColor(0.8f, 0.3f, 0.3f, 1.0f);
glm::vec4 blueColor(0.2f, 0.3f, 0.8f, 1.0f);
/* YOTO::MaterialRef material = new YOTO::MaterialRef(m_FlatColorShader);
YOTO::MaterialInstaceRef mi = new YOTO::MaterialInstaceRef(material);
mi.setValue("u_Color",redColor);
mi.setTexture("u_AlbedoMap", texture);
squreMesh->SetMaterial(mi);*/
std::dynamic_pointer_cast<YOTO::OpenGLShader>(m_BlueShader)->Bind();
std::dynamic_pointer_cast<YOTO::OpenGLShader>(m_BlueShader)->UploadUniformFloat3("u_Color",m_SquareColor);
for (int y = 0; y < 20; y++) {
for (int x = 0; x <20; x++)
{
glm::vec3 pos(x * 0.105f,y* 0.105f, 0.0);
glm::mat4 transform = glm::translate(glm::mat4(1.0f), pos) * scale;
/* if (x % 2 == 0) {
m_BlueShader->UploadUniformFloat4("u_Color", redColor);
}
else {
m_BlueShader->UploadUniformFloat4("u_Color", blueColor);
}*/
YOTO::Renderer::Submit(m_BlueShader, m_SquareVA, transform);
}
}
auto textureShader = m_ShaderLibrary.Get("Texture");
m_Texture->Bind();
YOTO::Renderer::Submit(textureShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f)));
m_ChernoLogo->Bind();
YOTO::Renderer::Submit(textureShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f)));
//YOTO::Renderer::Submit(m_Shader, m_VertexArray);
}
YOTO::Renderer::EndScene();
//YOTO::Renderer3D::BeginScene(m_Scene);
//YOTO::Renderer2D::BeginScene(m_Scene);
}
void OnEvent(YOTO::Event& e)override {
m_CameraController.OnEvent(e);
/*if (event.GetEventType() == YOTO::EventType::KeyPressed) {
YOTO:: KeyPressedEvent& e = (YOTO::KeyPressedEvent&)event;
YT_CLIENT_TRACE("ExampleLayer:{0}",(char)e.GetKeyCode());
if (e.GetKeyCode()==YT_KEY_TAB) {
YT_CLIENT_INFO("ExampleLayerOnEvent:TAB按下了");
}}*/
//YT_CLIENT_TRACE("SandBoxApp:测试event{0}", event);
}
private:
YOTO::ShaderLibrary m_ShaderLibrary;
YOTO::Ref<YOTO::Shader> m_Shader;
YOTO::Ref<YOTO::VertexArray> m_VertexArray;
YOTO::Ref<YOTO::Shader> m_BlueShader;
YOTO::Ref<YOTO::VertexArray> m_SquareVA;
YOTO::Ref<YOTO::Texture2D> m_Texture,m_ChernoLogo;
YOTO::OrthographicCameraController m_CameraController;
glm::vec3 m_SquareColor = { 0.2f,0.3f,0.7f };
};
class Sandbox:public YOTO::Application
{
public:
Sandbox(){
PushLayer(new ExampleLayer());
//PushLayer(new YOTO::ImGuiLayer());
}
~Sandbox() {
}
private:
};
YOTO::Application* YOTO::CreateApplication() {
printf("helloworld");
return new Sandbox();
}
测试:
cool,随便写点儿,水一期