前置:
ytpch.h:
cpp
#pragma once
#include<iostream>
#include<memory>
#include<utility>
#include<algorithm>
#include<functional>
#include<string>
#include<vector>
#include<unordered_map>
#include<unordered_set>
#include<sstream>
#include<array>
#ifdef YT_PLATFORM_WINDOWS
#include<Windows.h>
#endif // YT_PLATFORM_WINDOWS
创建glsl文件:
Texture.glsl:
cpp
#type vertex
#version 330 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;
out vec3 v_Position;
void main(){
v_TexCoord=a_TexCoord;
v_Position=a_Position;
gl_Position =u_ViewProjection*u_Transform*vec4( a_Position,1.0);
}
#type fragment
#version 330 core
layout(location = 0) out vec4 color;
in vec3 v_Position;
in vec2 v_TexCoord;
uniform sampler2D u_Texture ;
void main(){
color = texture(u_Texture, v_TexCoord);
}
抽象:
Shader.h:
cpp
#pragma once
#include <string>
#include"YOTO/Core.h"
namespace YOTO {
class Shader {
public:
virtual~Shader()=default;
virtual void Bind()const=0;
virtual void UnBind()const=0;
virtual const std::string& GetName()const = 0;
static Ref<Shader> Create(const std::string& filepath);
static Ref<Shader> Create(const std::string&name, const std::string& vertexSrc, const std::string& fragmentSrc);
};
class ShaderLibrary {
public:
void Add(const Ref<Shader>& shader);
void Add(const std::string &name,const Ref<Shader>& shader);
Ref<Shader> Load(const std::string filepath);
Ref<Shader> Load(const std::string &name,const std::string filepath);
Ref<Shader> Get(const std::string& name);
bool Exists(const std::string& name);
private:
std::unordered_map<std::string,Ref<Shader>> m_Shaders;
};
}
Shader.cpp:
cpp
#include "ytpch.h"
#include "Shader.h"
#include"Renderer.h"
#include "Platform/OpenGL/OpenGLShader.h"
namespace YOTO {
Ref<Shader> Shader::Create(const std::string& filepath)
{
switch (Renderer::GetAPI())
{
case RendererAPI::API::None:
YT_CORE_ASSERT(false, "Shader:API为None不支持");
return nullptr;
case RendererAPI::API::OpenGL:
return std::make_shared <OpenGLShader>(filepath);
}
YT_CORE_ASSERT(false, "Buffer:未知API");
return nullptr;
}
Ref<Shader> Shader::Create(const std::string& name, const std::string& vertexSrc, const std::string& fragmentSrc)
{
switch (Renderer::GetAPI())
{
case RendererAPI::API::None:
YT_CORE_ASSERT(false, "Shader:API为None不支持");
return nullptr;
case RendererAPI::API::OpenGL:
return std::make_shared <OpenGLShader>(name,vertexSrc, fragmentSrc);
}
YT_CORE_ASSERT(false, "Buffer:未知API");
return nullptr;
}
void ShaderLibrary::Add(const Ref<Shader>& shader)
{
auto& name = shader->GetName();
Add(name, shader);
}
void ShaderLibrary::Add(const std::string& name, const Ref<Shader>& shader)
{
YT_CORE_ASSERT(!Exists(name), "ShaderLibrary:shader已经存在了")
m_Shaders[name] = shader;
}
Ref<Shader> ShaderLibrary::Load(const std::string filepath)
{
auto shader = Shader::Create(filepath);
Add(shader);
return shader;
}
Ref<Shader> ShaderLibrary::Load(const std::string& name, const std::string filepath)
{
auto shader = Shader::Create(filepath);
Add(shader);
return shader;
}
Ref<Shader> ShaderLibrary::Get(const std::string& name)
{YT_CORE_ASSERT(Exists(name),"ShaderLibrary:未找到shader")
return m_Shaders[name];
}
bool ShaderLibrary::Exists(const std::string& name)
{
return m_Shaders.find(name)!=m_Shaders.end();
}
}
实现:
OpenGLShader.h:
cpp
#pragma once
#include <string>
#include "YOTO/Renderer/Shader.h"
#include <glm/glm.hpp>
typedef unsigned int GLenum;
namespace YOTO {
class OpenGLShader:public Shader {
public:
OpenGLShader(const std::string& filepath);
OpenGLShader(const std::string &name,const std::string& vertexSrc, const std::string& fragmentSrc);
~OpenGLShader();
void Bind()const override;
void UnBind()const override;
virtual const std::string& GetName()const override { return m_Name; }
void UploadUniformMat4(const std::string& name, const glm::mat4& matrix);
void UploadUniformMat3(const std::string& name, const glm::mat3& matrix);
void UploadUniformFloat4(const std::string& name, const glm::vec4& values);
void UploadUniformFloat3(const std::string& name, const glm::vec3& values);
void UploadUniformFloat2(const std::string& name, const glm::vec2& values);
void UploadUniformFloat(const std::string& name, float values);
void UploadUniformInt(const std::string& name, int values);
private:
std::string ReadFile(const std::string filepath);
std::unordered_map<GLenum,std::string> PreProcess(const std::string& source);
void Compile(const std::unordered_map<GLenum, std::string>& shaderSources);
private:
uint32_t m_RendererID;
std::string m_Name;
}
;
}
OpenGLShader.cpp:
cpp
#include "ytpch.h"
#include "OpenGLShader.h"
#include <glad/glad.h>
#include <YOTO/Log.h>
#include<glm/gtc/type_ptr.hpp>
namespace YOTO {
static GLenum ShaderTypeFromString(const std::string& type) {
if (type == "vertex") {
return GL_VERTEX_SHADER;
}
if (type == "fragment" || type == "pixel") {
return GL_FRAGMENT_SHADER;
}
YT_CORE_ASSERT(false, "不知道的shader类型");
return 0;
}
OpenGLShader::OpenGLShader(const std::string& filepath)
{
std::string source = ReadFile(filepath);
YT_CORE_ASSERT(source.size(), "GLSL读取的字符串为空");
auto shaderSources = PreProcess(source);
Compile(shaderSources);
auto lastSlash = filepath.find_last_of("/\\");
lastSlash = lastSlash == std::string::npos ? 0 : lastSlash + 1;
auto lastDot = filepath.rfind('.');
auto count = lastDot == std::string::npos ? filepath.size() - lastSlash : lastDot - lastSlash;
m_Name=filepath.substr(lastSlash, count);
}
OpenGLShader::OpenGLShader(const std::string& name, const std::string& vertexSrc, const std::string& fragmentSrc)
:m_Name(name){
std::unordered_map<GLenum, std::string >sources;
sources[GL_VERTEX_SHADER] = vertexSrc;
sources[GL_FRAGMENT_SHADER] = fragmentSrc;
Compile(sources);
}
OpenGLShader::~OpenGLShader()
{
glDeleteProgram(m_RendererID);
}
std::string OpenGLShader::ReadFile(const std::string filepath)
{
std::string result;
std::ifstream in(filepath, std::ios::in | std::ios::binary);
if (in) {
in.seekg(0, std::ios::end); // 将指针放在最后面
result.resize(in.tellg()); // 初始化string的大小, in.tellg()返回位置
in.seekg(0, std::ios::beg); // in指回头部
in.read(&result[0], result.size()); // in读入放在result指向的内存中
}
else {
YT_CORE_ERROR("不能打开文件:{0}", filepath);
}
return result;
}
std::unordered_map<GLenum, std::string> OpenGLShader::PreProcess(const std::string& source)
{
std::unordered_map<GLenum, std::string> shaderSources;
std::string typeToken = "#type";
size_t typeTokenLen = typeToken.size();
size_t findCurPos = source.find(typeToken, 0);
size_t findNextPos = findCurPos;
while (findNextPos != std::string::npos) {
size_t curlineEndPos = source.find_first_of("\r\n", findCurPos);///r/n写错为/r/n
YT_CORE_ASSERT(curlineEndPos != std::string::npos, "解析shader失败");
size_t begin = findCurPos + typeTokenLen + 1;
std::string type = source.substr(begin, curlineEndPos - begin);// 获取到是vertex还是fragment
YT_CORE_ASSERT(ShaderTypeFromString(type), "无效的shader的类型 ");
size_t nextLinePos = source.find_first_not_of("\r\n", curlineEndPos);
findNextPos = source.find(typeToken, nextLinePos);
// 获取到具体的shader代码
shaderSources[ShaderTypeFromString(type)] = source.substr(nextLinePos, findNextPos - (nextLinePos == std::string::npos ? source.size() - 1 : nextLinePos));
findCurPos = findNextPos;
}
return shaderSources;
/*
用find,而不是find_firtst_of,因为
find返回完全匹配的字符串的的位置;
find_first_of返回被查匹配字符串中某个字符的第一次出现位置。
std::string::npos是一个非常大的数
source.substr(0, source.size() + 10000)截取到从头到末尾,不会报错
*/
}
void OpenGLShader::Compile(const std::unordered_map<GLenum, std::string>& shaderSources)
{
GLuint program = glCreateProgram();
YT_CORE_ASSERT(shaderSources.size()<=2,"OpenGLShader:shader只支持两种!")
std::array<GLenum,2>glShaderIDs;
int glShaderIDIndex=0;
for (auto& kv : shaderSources) {
GLenum type = kv.first;
const std::string& source = kv.second;
// Create an empty vertex shader handle
GLuint shader = glCreateShader(type);
// Send the vertex shader source code to GL
// Note that std::string's .c_str is NULL character terminated.
const GLchar* sourceCStr = source.c_str();
glShaderSource(shader, 1, &sourceCStr, 0);
// Compile the vertex shader
glCompileShader(shader);
GLint isCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(shader, maxLength, &maxLength, &infoLog[0]);
// We don't need the shader anymore.
glDeleteShader(shader);
// Use the infoLog as you see fit.
// In this simple program, we'll just leave
YT_CORE_ERROR("{0} ", infoLog.data());
YT_CORE_ASSERT(false, "shader 编译失败!");
break;
}
// Attach our shaders to our program
glAttachShader(program, shader);
glShaderIDs[glShaderIDIndex++]=shader;
}
// Link our program
glLinkProgram(program);
// Note the different functions here: glGetProgram* instead of glGetShader*.
GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
// We don't need the program anymore.
glDeleteProgram(program);
// Don't leak shaders either.
for (auto id : glShaderIDs) {
glDeleteShader(id);
}
// Use the infoLog as you see fit.
// In this simple program, we'll just leave
YT_CORE_ERROR("{0} ", infoLog.data());
YT_CORE_ASSERT(false, "shader link failure!");
return;
}
// Always detach shaders after a successful link.
for (auto id : glShaderIDs) {
glDetachShader(program, id);
}
m_RendererID = program;
}
void OpenGLShader::Bind() const
{
glUseProgram(m_RendererID);
}
void OpenGLShader::UnBind() const
{
glUseProgram(0);
}
void OpenGLShader::UploadUniformMat4(const std::string& name, const glm::mat4& matrix)
{
GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());
glUniformMatrix4fv(loacation, 1, GL_FALSE, glm::value_ptr(matrix));
}
void OpenGLShader::UploadUniformMat3(const std::string& name, const glm::mat3& matrix)
{
GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());
glUniformMatrix3fv(loacation, 1, GL_FALSE, glm::value_ptr(matrix));
}
void OpenGLShader::UploadUniformFloat4(const std::string& name, const glm::vec4& values)
{
GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());
glUniform4f(loacation, values.x, values.y, values.z, values.w);
}
void OpenGLShader::UploadUniformFloat3(const std::string& name, const glm::vec3& values)
{
GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());
glUniform3f(loacation, values.x, values.y, values.z);
}
void OpenGLShader::UploadUniformFloat2(const std::string& name, const glm::vec2& values)
{
GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());
glUniform2f(loacation, values.x, values.y);
}
void OpenGLShader::UploadUniformFloat(const std::string& name, float values)
{
GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());
glUniform1f(loacation, values);
}
void OpenGLShader::UploadUniformInt(const std::string& name, int values)
{
GLint loacation = glGetUniformLocation(m_RendererID, name.c_str());
glUniform1i(loacation, values);
}
}
调用:
SandboxApp.cpp:
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_Camera(-2.0f, 2.0f, -2.0f, 2.0f), m_CameraPosition(0){
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 {
//YT_CLIENT_TRACE("delta time {0}s ({1}ms)", ts.GetSeconds(), ts.GetMilliseconds());
if (YOTO::Input::IsKeyPressed(YT_KEY_LEFT)) {
m_CameraPosition.x -= m_CameraMoveSpeed* ts;
}
else if (YOTO::Input::IsKeyPressed(YT_KEY_RIGHT)) {
m_CameraPosition.x += m_CameraMoveSpeed * ts;
}
if (YOTO::Input::IsKeyPressed(YT_KEY_DOWN)) {
m_CameraPosition.y -= m_CameraMoveSpeed * ts;
}
else if (YOTO::Input::IsKeyPressed(YT_KEY_UP)) {
m_CameraPosition.y += m_CameraMoveSpeed * ts;
}
if (YOTO::Input::IsKeyPressed(YT_KEY_A)) {
m_CameraRotation += m_CameraRotationSpeed * ts;
}else if (YOTO::Input::IsKeyPressed(YT_KEY_D)) {
m_CameraRotation -= m_CameraRotationSpeed * ts;
}
YOTO::RenderCommand::SetClearColor({ 0.2f, 0.2f, 0.2f, 1.0f });
YOTO::RenderCommand::Clear();
m_Camera.SetPosition(m_CameraPosition);
m_Camera.SetRotation(m_CameraRotation);
YOTO::Renderer::BeginScene(m_Camera);
{
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();
}
}
void OnEvent(YOTO::Event& event)override {
/*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::OrthographicCamera m_Camera;
glm::vec3 m_CameraPosition;
float m_CameraMoveSpeed = 5.0f;
float m_CameraRotation = 0;
float m_CameraRotationSpeed = 180.0f;
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!