rlk / obj

Wavefront OBJ library in C with an OpenGL Core Profile renderer
MIT License
93 stars 19 forks source link

OBJ renderer does not seem to use Material constants #3

Open timmyd7777 opened 3 years ago

timmyd7777 commented 3 years ago

This is a brilliant piece of code!

However, the OpenGL renderer does not seem to use the material constants, even though they are being read from the OBJ file. As a test, I commented out all of the texture maps in the example Chest.mtl file, leaving only the material constants Kd, Ka, Ks, Ke. For extra confirmation I changed the "diffuse" constant Kd to 0.0 1.0 0.0, to make the treasure chest appears in bright green. In every other OBJ viewer I have tried, this has the intended effect: the treasure chest appears bright (non-textured) green. However, in the example renderer provided here, the treasure chest appears featureless black.

Looking into the code, it seems that the material constants are not being passed into the vertex or fragment shaders anywhere. This is a problem for us because we have to render models that contain a mixture of textured and non-textured surfaces.

Can you provide some guidance on how to use the material constants Kd, Ka, Ks, Ke in the shaders? Or even better, add the code needed to pass them into the shaders, so they can be used for surfaces which are not textured?

Thanks again for a fantastically useful piece of work.

rlk commented 3 years ago

You're absolutely right.

First, how this came to be: The original implementation of this library was written in the era of the OpenGL fixed-function pipeline (it's pretty old). At that time, the OBJ material model mapped easily onto the OpenGL material model. When that model was deprecated, I implemented the bare minimum GLSL necessary to accomplish what I needed at the time, and I neglected to include the materials.

It would be fairly straightforward to fix. Fortunately, the values in question are set per-material instead of per-vertex. This means that there's no material interpolation and the vertex shader doesn't need to change. They'd just need to be added as uniforms, accessible to the fragment shader, handled similarly to the AmbientLight uniform.

timmyd7777 commented 3 years ago

OK. Could I convince you to add these uniforms to the fragment shader (and also add code to pass them in from the obj's material representation) in a future release? I may take a crack at this myself over the weekend, but I'm guessing you will do a better job of this, in less time, than I will :-)

rlk commented 3 years ago

TBH, my last release of this code was 7 years ago. My next release is unlikely to occur before the weekend.