To make the code looks clean, I declare a class MaterialTexture in namespace Material to store the light attributes for OpenGL. And I want to create instance(such as metal, plastic) for this class in the same namespace. I do some googling and find that a previous discussion shows that data members can't be assigned in the namespace. And the constructor is suggested to initialize the data members.
However, there're 12 data members in my case, and it would be tedious to pass 12 arguments to construct a instance. Instead, I want to use three functions (such as setDiffuse() ) to set the value of each instance.
namespace Material
{
class MaterialTexture
{
private:
float diffuse[4];
float specular[4];
float ambient[4];
public:
// Three functions below set the values for data members above.
void setDiffuse(float R, float G, float B, float A);
void setSpecular(float R, float G, float B, float A);
void setAmbient(float R, float G, float B, float A);
/* the following function tell the OpenGL how to draw the
Texture of this kind of Material and is not important
here. */
void setMaterial();
};
void MaterialTexture::setDiffuse(float R, float G, float B, float A)
{
diffuse[0]=R; diffuse[1]=G; diffuse[2]=B; diffuse[3]=A;
}
// Create instances plastic and metal
MaterialTexture plastic;
plastic.setDiffuse(0.6, 0.0, 0.0, 1.0);
...
MaterialTexture metal;
metal.setDiffuse(...);
...
}; // end of namespace
Thus, to create a red-plastic like sphere, I only need to type following codes in the display call back:
Material::MaterialTexture::plastic.setMaterial();
glutSolidSphere(...);
Compile the above code with g++, it gives the error:
error: ‘plastic’ does not name a type
and
error: ‘metal’ does not name a type
in the line of the three setting functions (such as setDiffuse() ) of each instance.
Thus it seems not only assignment directly in namespace, but functions contain assignment are not allowed... Am I right? Is there any other way to fix this? Or, are there some other way to facilitate the OpenGL programing?