VSModel Lib – Very Simple Model Lib
This lib is a subclass of VSResourceLib, and it provides an interface to the Open Asset Import Library (Assimp).
Assimp is able to load a rich set of file formats. This lib interfaces with the Assimp lib to be able to provide those same loading abilities and render the models with core OpenGL .
Not all of Assimp’s features are made available yet, but, as is, the lib is able to import and render all models using texture coordinates, normals, tangents and bitangents.
Requirements
This class requires the following classes from VSL:
- VSResourceLib – the superclass
- 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:
Methods and usage
This class implements the abstract methods in VSResourceLib.
Assume that the shader has the following block:
layout (std140) uniform Material { vec4 diffuse; vec4 ambient; vec4 specular; vec4 emissive; float shininess; int texCount; };
The following snippet of code shows how a model can be loaded and its uniforms initialized:
VSModelLib myModel; // load the model if (myModel1.load("models/spider.obj")) printf("%s\n", myModel.getInfo().c_str()); else { printf("%s\n", myModel.getErrors().c_str()); return; } // setting the semantics of the uniform block myModel.setMaterialBlockName("Material");
This next example shows how to replace/add the texture settings for a given texture unit, for ALL meshes of a model.
myModel.addTexture(0, "Textures/tile2.tga");
To render the model just call the render
method after glUseProgram
.
The Shaders
VSResModelLib aims at providing users with the ability to load and render 3D models, using Assimp.
As usual, in OpenGL core versions we need shaders to render stuff. Since VSResModelLib depends on VSMathLib, the shader should also support “standard” matrices such as model, view and projection, as well as derived matrices.
As an example, here is a pair of vertex and fragment shaders that can do the job perfectly.
Vertex shader:
#version 330 layout (std140) uniform Matrices { mat4 projViewModelMatrix; mat3 normalMatrix; }; in vec3 position; in vec3 normal; in vec2 texCoord; out vec2 TexCoord; out vec3 Normal; void main() { Normal = normalize(normalMatrix * normal); TexCoord = vec2(texCoord); gl_Position = projViewModelMatrix* vec4(position, 1.0); }
And here goes the fragment shader:
#version 330 layout (std140) uniform Material { vec4 diffuse; vec4 ambient; vec4 specular; vec4 emissive; float shininess; int texCount; }; uniform sampler2D texUnit; in vec3 Normal; in vec2 TexCoord; out vec4 outputF; void main() { vec4 color, emission,amb; float intensity; vec3 lightDir, n; lightDir = normalize(vec3(1.0,1.0,1.0)); n = normalize(Normal); intensity = max(dot(lightDir,n),0.0); if (texCount == 0) { color = diffuse; amb = ambient; emission = emissive; } else { color = texture(texUnit, TexCoord) * diffuse; amb = color * ambient; emission = texture(texUnit, TexCoord) * emissive; } outputF = (color * intensity) + amb + emission; }