#pragma once #include #include #include #include #include #include #include #include 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 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