Help end child hunger

VSResource Lib – Very Simple Resource Library

 

Loading models and textures, creating VAOs, setting the uniforms, and finally rendering with materials, all these steps require some work and are very repetitive. Furthermore, for the beginner it is nice to have some way to load some objects to test their initial incursions in OpenGL.

This class is actually an abstract class upon which the classes that deal with models, fonts and textures are built. It establishes a common interface and provides some utilities like loading RGBA textures, and implements some common code.

The subclasses implemented so far are:

Requirements

This class requires the following classes from VSL:

  • VSMathLib – although this class does not use any math, all subclasses implemented so far do, so its included in here to.
  • VSLogLib – Logs are used to keep track of loading errors and resource info
  • VSShaderLib – To be able to work with uniform variables

VSResourceLib also has the following external dependencies:

  • GLEW – to access OpenGL functions
  • DevIL – for texture loading

As of version 0.1.2 the class can be compiled without requiring DevIL, but it looses its texturing abilities.

Methods

The abstract methods are:

void clone(VSResourceLib *res)
bool load(std::string filename)
void render()
void addTexture(unsigned int unit, std::string filename)
void addCubeMap(unsigned int unit, 
		std::string posX, std::string negX, 
		std::string posY, std::string negY, 
		std::string posZ, std::string negZ)
void setTexture(unsigned int unit, unsigned int textureID,
		GLenum textureType = GL_TEXTURE_2D)

These establish a common signature for all subclasses. The first one provides a clone of the resource parameter, where as the second is for resource, be it a model or a font, loading. The third for rendering purposes. The last ones actually have a null implementation, and are defined only to provide a common way of adding and setting textures to resources.

This class also defines a structure for the material. The structure is defined as:

struct MyMaterial{

	float diffuse[4];
	float ambient[4];
	float specular[4];
	float emissive[4];
	float shininess;
	int texCount;
};

This structure is used by the subclasses to store the basic material properties. If the material is represented inside the shaders as a block, this class also provides some functions to ease the material setting.

This implies a setup phase where the semantics are defined. An enumeration was defined for this purpose:

enum MaterialSemantics {
	DIFFUSE,
	AMBIENT,
	SPECULAR,
	EMISSIVE,
	SHININESS,
	TEX_COUNT
};

The following methods are related to material setting in the setup phase:

void setMaterialBlockName(std::string)
void setUniformSemantics(MaterialSemantics field, std::string)

If using blocks, the block name must be set using setMaterialBlockName. Then two options are available. If the block matches the definition of the material structure defined in this class, then no further steps are required, and the block will be set as a whole. Otherwise, always assuming that the uniforms are defined inside a named block, the uniform values must be set one by one. In order to achieve this it is necessary to set the semantics of each uniform variable. This can be achieved with the second function.

Note: if using named blocks, then all uniforms must be either in the same named block, or in the default block. This limitation simplifies both the code of the library, as well as its usage, and it should not cause problems in the general case.

Example to setup the class to use only the “diffuse” and “ambient” uniform variables in a block named “mat”:

VSResourceLib *res;
...
res->setMaterialBlockName("mat");
res->setUniformSemantics(VSResourceLib::DIFFUSE, "diffuse");
res->setUniformSemantics(VSResourceLib::AMBIENT, "ambient");

A few more methods are shared by all subclasses:

float getScaleForUnitCube()
std::string getErrors()
std::string getInfo()

Method getScaleForUnitCube provides the uniform scale factor to render the model in a cube where the coordinates range from -1 to 1.

The getErrors method provides a string with the errors found when loading the resource.

Method getModelInfo provides a string with info on the loaded resource.

Finally, two protected method are provided to all subclasses for loading RGBA, and cube map textures:

unsigned int loadRGBATexture(std::string filename, 
			bool mipmap = true,
			bool compress = false,
			GLenum aFilter = GL_LINEAR, 
			GLenum aRepMode = GL_REPEAT);
unsigned int loadCubeMapTexture(std::string posX, std::string negX, 
				std::string posY, std::string negY, 
				std::string posZ, std::string negZ);

As can be seen from the signature of the method, it allows the setting of filters, mipmapping, and texture compression.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: