问题描述
您好。在我的OpenGL程序中,我有多种着色器,适用于多种类型的模型,地形或灯具。到目前为止,我有一个着色器类,它能够处理所有类型的着色器。但每次我想在其中加载某些内容时,我必须找到每个变量的变量ID。然后我在多个论坛中看到glGetUniformLocation(它在着色器的源中搜索特定变量)是慢速函数。所以我决定在加载着色器时缓存变量'id。
这里来了我的问题。我决定创建一个名为StaticShader的抽象类和多个派生类,如BaseShader,TerrainShader等。每个派生类(缓存)都会加载自己的变量ID位置。
Hello. In my OpenGL program i have multiple shaders for multiple types of models,terrains or lamps. As up until now i had 1 shader class which was able to handle all of type of shaders. But i had to find the variable ID of each variable every time i wanted to load up something in it. Then i saw in multiple forums that glGetUniformLocation (which searches the shader's source for the particular variable) is slow function. So i decided to cache the variables' ids once when the shader is loaded.
Here Comes my problem. I Decided to make an Abstract Class called StaticShader and multiple derived classes such as BaseShader,TerrainShader etc. Each of the derived classes (caches up) loads up its own variable ID locations.
class StaticShader
{
public:
void StartShader();
void StopShader();
void loadAttribute(GLuint, GLuint, GLuint, GLuint);
void loadUniformMat4fv(float*, GLuint);
void loadUniform4fv(float*, GLuint);
void loadUniform3fv(float*, GLuint);
void loadUniform2fv(float*, GLuint);
void loadUniform1f(GLfloat, GLuint);
void loadUniform1i(GLenum, GLuint, GLuint, GLuint);
void loadUniform1iCube(GLenum, GLuint, GLuint, GLuint);
GLuint getProgramID();
StaticShader() {};
StaticShader(string, string);
void CleanUP();
private:
string vShader;
string fShader;
GLuint shaderProgramID;
GLuint vertexShaderID;
GLuint fragmentShaderID;
char *ShaderSource(const char*);
bool compiledStatus(GLint);
GLuint makeShader(const char*, GLenum);
GLuint makeShaderProgram(GLuint, GLuint);
protected:
virtual void loadUniformLocations() = 0;
};
class BasicShader: public StaticShader
{
public:
GLuint getPositionID();
GLuint getNormalID();
GLuint getTexCoordID();
GLuint getTranslateID();
GLuint getRotateID();
GLuint getScaleID();
GLuint getViewID();
GLuint getProjectionID();
GLuint getfakeLightID();
GLuint *getLightColorID();
GLuint *getLightID();
BasicShader(string,string);
~BasicShader();
private:
GLuint MAX_LIGHTS;
GLuint s_vPosition;
GLuint s_vNormal;
GLuint s_vTexCoord;
GLuint mTranslate;
GLuint mRotate;
GLuint mScale;
GLuint mView;
GLuint mProjection;
GLuint fakeLight;
GLuint *vLightColor;
GLuint *vLight;
virtual void loadUniformLocations() override;
};
inline void BasicShader::loadUniformLocations()
{
s_vPosition = glGetUniformLocation(getProgramID(), "s_vPosition");
s_vNormal = glGetUniformLocation(getProgramID(), "s_vNormal");
s_vTexCoord = glGetUniformLocation(getProgramID(), "s_vTexCoord");
mTranslate = glGetUniformLocation(getProgramID(), "mTranslate");
mRotate = glGetUniformLocation(getProgramID(), "mRotate");
mScale = glGetUniformLocation(getProgramID(), "mScale");
//Just an example of what the function could look like
//............................
}
这是一个简单的例子上课应该是这样的。从那里我可以很容易地像这样指向StaticShader:
That is a simple example of what each class should look like. From there i can easily make a pointer to StaticShader like that:
StaticShader *shader = new BasicShader("vShader","fShader");
我的想法就是这样做:
The idea is to then do something like:
shader->loadUniform1f(data,uniformID)
!!不使用BasicShader的单独实例,如:
!! Without using a separate instance of BasicShader like:
shader->loadUniform1f(data,basicShader.getUniformID)
我的问题是如何从抽象的StaticShader类中的基本着色器访问缓存的变量ID。我应该让那些得到虚拟的人吗?
这是否可能,如果是,即使是合法或好的,我是多态的新手,我不能说我完全理解,但有些延伸。我愿意接受建议:)提前感谢。
My Question is how would i access the cached variable IDs from the basic shader in the abstract StaticShader class. Should i make the get-ers virtual as well ?
Is that even possible and if yes is it even legal or good, i am new to Polymorphism and i cant say i understand it perfectly , but to some extends. I am open to suggestions :) thanks in advance.
推荐答案
这篇关于OpenGL处理多个着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!