首页 > 其他分享 >OpenGL Program渲染框架

OpenGL Program渲染框架

时间:2023-02-28 15:13:49浏览次数:37  
标签:std const name 渲染 OpenGL vector Program mHandle

Program.h

#pragma once
#ifndef Program_h
#define Program_h

class Program { public: /// Constructor for a rendering program Program( const std::vector<const char*>& vertexShaderCode, const std::vector<const char*>& fragmentShaderCode); /// Constructor for a rendering program Program( const std::vector<const char*>& vertexShaderCode, const std::vector<const char*>& geometryShaderCode, const std::vector<const char*>& fragmentShaderCode); /// Constructor for a tessellation program Program( const std::vector<const char*>& vertexShaderCode, const std::vector<const char*>& controlShaderCode, const std::vector<const char*>& evaluationShaderCode, const std::vector<const char*>& geometryShaderCode, const std::vector<const char*>& fragmentShaderCode); /// Constructor for a compute program Program(const std::vector<const char*>& computeShaderCode); /// Destructor ~Program(); /// Binds this shader program void bind(); /// Unbinds this shader program void unbind(); /// Returns the location of the attribute with the provided name in the shader GLint getAttributeLocation(const std::string& name) const; /// Returns the location to the uniform with the provided name in the shader GLint getUniformLocation(const std::string& name) const; /// Returns the index to the interface block with the provided name in the shader GLint getInterfaceBlockIndex(const std::string& name) const; /// Binds the shader storage buffer to the interface block index with the provided name void setSSBO(const std::string& name, GLuint bufferHandle); /// Sets the uniform to the provided value void setUniformBool(const std::string& name, bool x); /// Sets the uniform to the provided value void setUniform1f(const std::string& name, float x); /// Sets the uniform to the provided value void setUniform1i(const std::string& name, int x); /// Sets the uniform to the provided value void setUniform2f(const std::string& name, const Eigen::Vector2f& x); /// Sets the uniform to the provided value void setUniform2fArray(const std::string& name, const std::vector<Eigen::Vector2f>& x); /// Sets the uniform to the provided value void setUniform2i(const std::string& name, const Eigen::Vector2i& x); /// Sets the uniform to the provided value void setUniform4f(const std::string& name, const Eigen::Vector4f& x); /// Sets the uniform to the provided value void setMatrix4f(const std::string& name, const Eigen::Matrix4f& m); /// Sets all camera uniforms void setCamera(const Camera& camera); /// Assigns threads to workgroups and executes the compute shader void compute1D(int numThreads); private: /// Concatenates the individual shader pieces to one string std::string makeString(const std::vector<const char*> shaderCode); /// Creates and compiles a shader object from the provided source code GLuint createShader(GLenum shaderType, const char* shaderCode) const; /// Links the given shaders into the shader program void link(); /// Acquires and updates data about the linked program using the introspection API void getIntrospectionData(); /// Retrieves the block binding index for the provided interface block GLuint getBlockBinding(GLuint interfaceBlockIndex); public: GLuint mHandle = 0u; ///< OpenGL handle of the linked program GLuint mVertexShaderHandle = 0u; ///< OpenGL handle of the compiled vertex shader, if present GLuint mControlShaderHandle = 0u; ///< OpenGL handle of the compiled tessellation control shader, if present GLuint mEvaluationShaderHandle = 0u; ///< OpenGL handle of the compiled tessellation evaluation shader, if present GLuint mGeometryShaderHandle = 0u; ///< OpenGL handle of the compiled geometry shader, if present GLuint mFragmentShaderHandle = 0u; ///< OpenGL handle of the compiled fragment shader, if present GLuint mComputeShaderHandle = 0u; ///< OpenGL handle of the compiled compute shader, if present GLuint mBlockBindingCounter = 0u; ///< Counter to provide interface block binding indices std::unordered_map<GLuint, GLuint> mBlockBindings; std::unordered_map<std::string, IntrospectionInput> mInputs; std::unordered_map<std::string, IntrospectionInterfaceBlock> mInterfaceBlocks; std::unordered_map<std::string, IntrospectionUniform> mUniforms; }; #endif

可以看到Program类一共有4个构造函数,初始化参数有:vertexShaderCode和fragmentShaderCode(构造函数1),vertexShaderCode、geometryShaderCode和fragmentShaderCode(构造函数2),vertexShaderCode、controlShaderCode、evaluationShaderCode、geometryShaderCode和fragmentShaderCode(构造函数3),computeShaderCode(构造函数4)。

Program类的实现:

//=================================================================================================
/**
*    Constructor for a rendering program.
*
*    @author    Daniel Cornel
**/
//=================================================================================================
Program::Program(
    const std::vector<const char*>& vertexShaderCode,
    const std::vector<const char*>& fragmentShaderCode)
{
    mVertexShaderHandle = createShader(GL_VERTEX_SHADER, makeString(vertexShaderCode).c_str());
    mFragmentShaderHandle = createShader(GL_FRAGMENT_SHADER, makeString(fragmentShaderCode).c_str());

    mHandle = glCreateProgram();

    glAttachShader(mHandle, mVertexShaderHandle);
    glAttachShader(mHandle, mFragmentShaderHandle);

    glBindFragDataLocation(mHandle, 0, "outColor");

    link();
}
//=================================================================================================
/**
*    Constructor for a rendering program.
*
*    @author    Daniel Cornel
**/
//=================================================================================================
Program::Program(
    const std::vector<const char*>& vertexShaderCode,
    const std::vector<const char*>& geometryShaderCode,
    const std::vector<const char*>& fragmentShaderCode)
{
    mVertexShaderHandle = createShader(GL_VERTEX_SHADER, makeString(vertexShaderCode).c_str());
    mGeometryShaderHandle = createShader(GL_GEOMETRY_SHADER, makeString(geometryShaderCode).c_str());
    mFragmentShaderHandle = createShader(GL_FRAGMENT_SHADER, makeString(fragmentShaderCode).c_str());

    mHandle = glCreateProgram();

    glAttachShader(mHandle, mVertexShaderHandle);
    glAttachShader(mHandle, mGeometryShaderHandle);
    glAttachShader(mHandle, mFragmentShaderHandle);

    glBindFragDataLocation(mHandle, 0, "outColor");

    link();
}
//=================================================================================================
/**
*    Constructor for a tessellation program.
*
*    @author    Daniel Cornel
**/
//=================================================================================================
Program::Program(
    const std::vector<const char*>& vertexShaderCode,
    const std::vector<const char*>& controlShaderCode,
    const std::vector<const char*>& evaluationShaderCode,
    const std::vector<const char*>& geometryShaderCode,
    const std::vector<const char*>& fragmentShaderCode)
{
    mVertexShaderHandle = createShader(GL_VERTEX_SHADER, makeString(vertexShaderCode).c_str());
    mControlShaderHandle = createShader(GL_TESS_CONTROL_SHADER, makeString(controlShaderCode).c_str());
    mEvaluationShaderHandle = createShader(GL_TESS_EVALUATION_SHADER, makeString(evaluationShaderCode).c_str());
    mGeometryShaderHandle = createShader(GL_GEOMETRY_SHADER, makeString(geometryShaderCode).c_str());
    mFragmentShaderHandle = createShader(GL_FRAGMENT_SHADER, makeString(fragmentShaderCode).c_str());

    mHandle = glCreateProgram();

    glAttachShader(mHandle, mVertexShaderHandle);
    glAttachShader(mHandle, mControlShaderHandle);
    glAttachShader(mHandle, mEvaluationShaderHandle);
    glAttachShader(mHandle, mGeometryShaderHandle);
    glAttachShader(mHandle, mFragmentShaderHandle);

    glBindFragDataLocation(mHandle, 0, "outColor");

    link();
}
//=================================================================================================
/**
*    Constructor for a compute program.
*
*    @author    Daniel Cornel
**/
//=================================================================================================
Program::Program(const std::vector<const char*>& computeShaderCode)
{
    mComputeShaderHandle = createShader(GL_COMPUTE_SHADER, makeString(computeShaderCode).c_str());

    mHandle = glCreateProgram();

    glAttachShader(mHandle, mComputeShaderHandle);

    link();
}

参考:Daniel Cornel, Watertight Incremental Heightfield Tessellation,http://drivenbynostalgia.com/

标签:std,const,name,渲染,OpenGL,vector,Program,mHandle
From: https://www.cnblogs.com/2008nmj/p/17164304.html

相关文章