# OpenGLShader OpenGL 着色器实现,封装 shader program。 ## 头文件 ```cpp #include ``` ## 继承关系 ``` RHIShader (interface) └── OpenGLShader (implementation) ``` ## 公共成员函数 ### 构造函数与析构函数 #### `OpenGLShader()` #### `~OpenGLShader() override` ### RHI 接口实现 #### `bool CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) override` 从文件编译(适配 RHI 接口,`target` 被忽略)。 #### `bool Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target) override` 从内存编译。 #### `void Shutdown() override` ### OpenGL 特有编译方法 #### `bool CompileFromFile(const char* vertexPath, const char* fragmentPath)` 编译顶点+片元着色器。 #### `bool CompileFromFile(const char* vertexPath, const char* fragmentPath, const char* geometryPath)` 编译 VS + GS + FS。 #### `bool Compile(const char* vertexSource, const char* fragmentSource)` 从源码编译 VS + FS。 #### `bool Compile(const char* vertexSource, const char* fragmentSource, const char* geometrySource)` #### `bool CompileCompute(const char* computeSource)` 编译计算着色器。 #### `bool Compile(const char* source, ShaderType type)` 按类型编译单个着色器。 ### 绑定 #### `void Use() const` 使用此 program。 #### `void Bind() override { Use(); }` #### `void Unbind() override` ### Uniform 设置 #### `void SetInt(const char* name, int value) override` #### `void SetIntArray(const char* name, const int* values, unsigned int count)` #### `void SetFloat(const char* name, float value) override` #### `void SetFloatArray(const char* name, const float* values, unsigned int count)` #### `void SetVec3(const char* name, float x, float y, float z) override` #### `void SetVec3(const char* name, const float* values)` #### `void SetVec4(const char* name, float x, float y, float z, float w) override` #### `void SetVec4(const char* name, const float* values)` #### `void SetMat2(const char* name, const float* value)` #### `void SetMat3(const char* name, const float* value)` #### `void SetMat4(const char* name, const float* value) override` #### `void SetMat4Array(const char* name, const float* values, unsigned int count)` ### 属性 #### `int GetUniformLocation(const char* name) const` 获取 uniform location。 #### `unsigned int GetID() const` 获取 GL program ID。 #### `void* GetNativeHandle() override` #### `bool IsValid() const override` 检查 program 是否有效。 #### `ShaderType GetType() const override` ## 内部成员 | 成员 | 类型 | 描述 | |------|------|------| | `m_program` | `unsigned int` | GL program ID | | `m_type` | `ShaderType` | 着色器类型 | ## 私有方法 #### `bool CheckCompileErrors(unsigned int shader, const char* type)` 检查单个 shader 编译错误。 #### `bool CheckLinkErrors(unsigned int program)` 检查 program 链接错误。 ## 使用示例 ```cpp // Simple shader OpenGLShader shader; shader.Compile(R"( #version 450 core layout(location=0) in vec3 aPos; void main() { gl_Position = vec4(aPos, 1.0); } )", R"( #version 450 core out vec4 FragColor; void main() { FragColor = vec4(1.0); } )", "vs", "fs"); shader.Use(); shader.SetMat4("u_model", glm::value_ptr(model)); shader.SetVec3("u_color", 1.0f, 0.0f, 0.0f); shader.SetInt("u_texture", 0); // From files OpenGLShader shader2; shader2.CompileFromFile("shaders/Default.vert", "shaders/Default.frag"); ``` ## 备注 - 使用 GLSL 450 core profile - 编译/链接错误通过日志输出 - `GetUniformLocation` 返回 -1 表示 uniform 不存在