Project 3

Project 3 has been the toughest project yet. Our task was to implement per pixel shaders for diffuse, specular, and normal maps as well as a spotlight shader with a projective texture. It's been difficult for me to wrap my head around the concepts of per-pixel lighting for this project.

I have properly implemented the frag/vertex shaders for calculating the specular, diffuse colors with normal mapping. Getting these shaders working took longer than expected thus I was unable to implement the spotlight shader. Below are some screenshots showing the specular/diffuse coloring with normal mapping:

Normally, I would present screenshots of my application running in Mac, Linux, and Windows, but I was pressed for time for this project. I can put some images up later showing that it works on all platforms.

Difficulties

While trying to implement the shaders for this project, I was initially confused on how to load textures from disk into the fragment shader. After some more research, I learned that it's quite simple. At first, I was loading the images manually from disk, using Qt's image module and converting the textures to GL compatible textures, but then I read that the obj module handles loading all textures from the mtl file. This made things much easier. All I needed to do was lookup the specific texture from the obj module like so:

diffuse = obj_get_mtrl_map(m_obj, 0, OBJ_KD);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, diffuse);
ShaderProgram->setUniformValue("diffuse", 0);
 

OBJ_KD is the diffuse texture loaded by the obj module when a material file is associated with a Wavefront object file. Perhaps I don't need to rebind these textures as they're already bound when the obj module parses the mtl file, but it works none the less. I'm using Qt's QGLShaderProgram wrapper for managing compilation of shader programs, setting GLSL attributes, and setting GLSL uniforms. 

On the GLSL side:

uniform sampler2D diffuse;

void main()
{
    vec4 D = texture2D(diffuse, gl_TexCoord[0].xy); // Diffuse Texture
}

For those interested in how to use Qt's image module for loading images and converting them to GL compatible textures here's a code snippet:

QImage image;
image.load("spotlight.png");
QImage glSpotlightTexture = QGLWidget::convertToGLFormat(image);
glActiveTexture(GL_TEXTURE3);
glGenTextures(1, &spot);
glBindTexture(GL_TEXTURE_2D, spot);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, glSpotlightTexture.width(), glSpotlightTexture.height(),  GL_RGBA, GL_UNSIGNED_BYTE, glSpotlightTexture.bits() );

I had started working on the code for loading the spotlight texture from disk as it isn't included in the mtl file, but I didn't get around to finishing the code for implementing the spotlight shader.

blog comments powered by Disqus

Month List

Tag cloud