Extra2D/include/resource/shader.h

126 lines
2.9 KiB
C
Raw Normal View History

#pragma once
#include <resource/resource.h>
#include <glad/glad.h>
#include <types/math/vec2.h>
#include <types/math/vec3.h>
#include <types/math/color.h>
#include <types/base/types.h>
#include <string>
#include <unordered_map>
namespace extra2d {
/**
* @brief
*/
enum class ShaderType : uint8 {
Vertex, // 顶点着色器
Fragment, // 片段着色器
Geometry, // 几何着色器
Compute // 计算着色器
};
/**
* @brief
*
* OpenGL ES 3.2
*/
class Shader : public Resource {
public:
Shader();
~Shader() override;
/**
* @brief
*/
ResourceType getType() const override { return ResourceType::Shader; }
/**
* @brief
* @param vsPath
* @param fsPath
* @return
*/
bool loadFromFile(const std::string& vsPath, const std::string& fsPath);
/**
* @brief
* @param vsSource
* @param fsSource
* @return
*/
bool loadFromSource(const std::string& vsSource, const std::string& fsSource);
/**
* @brief 使
*/
void use() const;
/**
* @brief 使
*/
void unuse() const;
/**
* @brief Uniform
*/
void setBool(const std::string& name, bool value);
/**
* @brief Uniform
*/
void setInt(const std::string& name, int value);
/**
* @brief Uniform
*/
void setFloat(const std::string& name, float value);
/**
* @brief Vec2 Uniform
*/
void setVec2(const std::string& name, const Vec2& value);
/**
* @brief Vec3 Uniform
*/
void setVec3(const std::string& name, const Vec3& value);
/**
* @brief Uniform
*/
void setColor(const std::string& name, const Color& value);
/**
* @brief 4x4 Uniform
*/
void setMat4(const std::string& name, const float* value);
/**
* @brief OpenGL
*/
GLuint getProgram() const { return program_; }
private:
GLuint program_ = 0; // OpenGL 程序句柄
std::unordered_map<std::string, GLint> uniformCache_; // Uniform 位置缓存
/**
* @brief
* @param type
* @param source
* @return 0
*/
GLuint compileShader(GLenum type, const std::string& source);
/**
* @brief Uniform
* @param name Uniform
* @return Uniform
*/
GLint getUniformLocation(const std::string& name);
};
} // namespace extra2d