Click here to Skip to main content
15,123,367 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C++
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;
};



C++
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
        //............................
}



That is a simple example of what each class should look like. From there i can easily make a pointer to StaticShader like that:
C++
StaticShader *shader = new BasicShader("vShader","fShader");


The idea is to then do something like:
C++
shader->loadUniform1f(data,uniformID)


!! Without using a separate instance of BasicShader like:
C++
shader->loadUniform1f(data,basicShader.getUniformID)


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.
Posted
Updated 3-Apr-15 6:38am
v2

1 solution


  • you should read and about Polymorphism to use it
  • you should avoid ANY OpenGL which you can cache
  • an abstract class cant have instances, it is an interface.

Use for the shader a global instance and for your textures an so on some array or list mechanism.

Tip: read about OpenGL and how the objects are created and cached. This will get you more performance.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900