Shader set up for simple 3D model viewer

classic Classic list List threaded Threaded
6 messages Options
Andreas Roth Andreas Roth
Reply | Threaded
Open this post in threaded view
|

Shader set up for simple 3D model viewer

Hi,

i'm trying to create an osgViewer application to display 3D models (*.ive, *.osgb files) and use the osgEarth shaders for lighting. I only want to show the 3D model without a MapNode underneath. How do i need to set up the view/camera with the Shaders to get the models displayed with correct lighting?

Thanks,
Andreas
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|

Re: Shader set up for simple 3D model viewer

Andreas,
The easiest way is to use the PhongLightingEffect class and call attach() to activate it on a StateSet (like the camera's stateset). Hope this helps.
Glenn Waldron / Pelican Mapping
Andreas Roth Andreas Roth
Reply | Threaded
Open this post in threaded view
|

Re: Shader set up for simple 3D model viewer

Thanks Glenn for the hint!

I've created a small application to display a 3D model using the PhongLightingEffect as suggested.
osgearth_modelviewer.cpp

With the PhongLightingEffect the model gets some lighting, but it almost turn completely black. I've tried to add a LightSource as well (similar to the sun), but this didn't change lighting.

Any further ideas/hints?

Andreas
emminizer emminizer
Reply | Threaded
Open this post in threaded view
|

Re: Shader set up for simple 3D model viewer

Hello.  I noticed in your example that the sunLS is a sibling to groupPhong, both are children of root.  Is it possible that sunLS needs to be in your node tree above your model?

Otherwise everything looks fine to my untrained eye.  If in doubt, run osgEarth::Registry::shaderGenerator().run(root) -- that might make some of your explicit calls to shaderize superfluous, like the material callback.

 - Dan
Andreas Roth Andreas Roth
Reply | Threaded
Open this post in threaded view
|

Re: Shader set up for simple 3D model viewer

Hi Dan,

thanks for you're time to look into this. I've updated the source file to include your suggestions, but i'm afraid it didn't solve the issue. The rendering remains the same dark model.

osgearth_modelviewer.cpp

The position of the sunLight object should not matter since it's "only" a direction not an actual position, right? When using a MapNode the model is placed on the earth at a given position, but in the model viewer the model is loaded at 0,0,0.

Are there some "limitations" in the light shader code which only work when a globe is present?

Andreas
emminizer emminizer
Reply | Threaded
Open this post in threaded view
|

Re: Shader set up for simple 3D model viewer

Hi Andreas,

No, there are no limitations like that.  The Phong shader is pretty generic.  If you're still having issues I recommend making minor edits to PhongLighting.frag.glsl and PhongLighting.vert.glsl to track down where the problem is in your code w.r.t. lighting.

I was having problems the other day and tracked my issue down very quickly this way.  I just used StateSet's setDefine(), then a pragma import_defines on it, and ifdef'd bits of code to check where logic was breaking.  So on my object I set a define, then in the shader just updated the color to yellow and returned whenever the shader was called with that define.  Then I refined the code to track down deeper.  I eventually found I was assigning material properties incorrectly.  You can probably do something similar.

 - Dan