写这篇文章的目的就是想学习OpenGL的实践方法,以便在后面的项目中扩展渲染引擎的功能。
VTK怎样管理着色器shader
上来就粘代码虽然看起来和没啥水平,但是作为理解别人写的程序的最好的方法,还是先看看他写了什么接口比较好总览全局。
class VTKRENDERINGOPENGL2_EXPORT vtkShader : public vtkObject
{
public:
static vtkShader* New();
vtkTypeMacro(vtkShader, vtkObject);
void PrintSelf(ostream& os, vtkIndent indent) override;
/** Available shader types. */
enum Type
{
Vertex, /**< Vertex shader */
Fragment, /**< Fragment shader */
Geometry, /**< Geometry shader */
Unknown /**< Unknown (default) */
};
/** Set the shader type. */
void SetType(Type type);
/** Get the shader type, typically Vertex or Fragment. */
Type GetType() const { return this->ShaderType; }
/** Set the shader source to the supplied string. */
void SetSource(const std::string& source);
/** Get the source for the shader. */
std::string GetSource() const { return this->Source; }
/** Get the error message (empty if none) for the shader. */
std::string GetError() const { return this->Error; }
/** Get the handle of the shader. */
int GetHandle() const { return this->Handle; }
/** Compile the shader.
* @note A valid context must to current in order to compile the shader.
*/
bool Compile();
/** Delete the shader.
* @note This should only be done once the ShaderProgram is done with the
* Shader.
*/
void Cleanup();
class ReplacementSpec
{
public:
std::string OriginalValue;
vtkShader::Type ShaderType;
bool ReplaceFirst;
bool operator<(const ReplacementSpec& v1) const
{
if (this->OriginalValue != v1.OriginalValue)
{
return this->OriginalValue < v1.OriginalValue;
}
if (this->ShaderType != v1.ShaderType)
{
return this->ShaderType < v1.ShaderType;
}
return (this->ReplaceFirst < v1.ReplaceFirst);
}
bool operator>(const ReplacementSpec& v1) const
{
if (this->OriginalValue != v1.OriginalValue)
{
return this->OriginalValue > v1.OriginalValue;
}
if (this->ShaderType != v1.ShaderType)
{
return this->ShaderType > v1.ShaderType;
}
return (this->ReplaceFirst > v1.ReplaceFirst);
}
};
class ReplacementValue
{
public:
std::string Replacement;
bool ReplaceAll;
};
protected:
vtkShader();
~vtkShader() override;
Type ShaderType;
int Handle;
bool Dirty;
std::string Source;
std::string Error;
private:
vtkShader(const vtkShader&) = delete;
void operator=(const vtkShader&) = delete;
};
逐个分析这些接口的功能,在我看来大可不必。如果读者学过一些简单的OpenGL的接口,那上面的接口大概一看就知道什么用处了。
我在写这篇文章的时候,也只是略读了蓝宝书的几个基础的章节,所以我只能谈谈“现在”我对上面代码封装的接口的理解。(以下提到的着色器程序指的是OpenGL着色器代码或者着色器对象)
- 在OpenGL中着色器程序是分类型的,可编程的着色器程序主要有三类,顶点,几何和片段,所以需要定义Type枚举来标识类型;
- 在OpenGL中着色器程序是一段用C语言风格写的程序(它的功能主要告诉OpenGl怎样处理每个渲染阶段的数据和状态,更详细的作用后续再来补充),它在形式上属于一个字符串,所以需要用string类型来存储这个字符串;
- 在OpenGL中编译着色器程序时会返回一些信息,如错误信息,所以也需要一个Error string来出存储它;
- 在OpenGL中创建着色器程序时会返回一个int类型的值,标识这个着色器程序,所以也要有个Handle来寸它;
- 其他的东西就和OpenGL关系不大了,这些应该是为了优雅的封装OpenGL所需要的东西了
本文介绍了VTK库中的vtkShader类,详细解释了其主要接口功能,包括设置着色器类型、源代码、错误信息和编译过程,针对OpenGL中不同类型的着色器(如顶点、片段、几何)进行了说明。
1461






