Nvidia Nature Demo

Return to Code Snippets

Postby ynjh_jo » Sat Mar 03, 2007 1:06 pm

Ooops, wrong answer. It happened to me too, only when looking from the hills, right ? I haven't able to fix it.

[EDIT]
alright, it's definitely transparency problem. The waterplane is simply a geometry created by cardmaker, so it only has 2 triangles, and if compared to the grasses' triangles, it's so damn huge.
I used my own very well tesselated plane and no more problem.
http://ynjh.panda3dprojects.com | http://ynjh.p3dp.com
Intel P4Prescott 2.8GHz HT | ATI Radeon HD4670 1GB GDDR3
User avatar
ynjh_jo
 
Posts: 1795
Joined: Tue Apr 18, 2006 12:41 am
Location: Malang, Indonesia

Postby enn0x » Sun Mar 04, 2007 5:31 am

Yes, a problem with z ordering caused by the 2 triangle plane.

If spending more polygons for the water plane then it should be possible to modify the water shader, and replace the transparency factor that is read from the water depth-map with a fresnel term, for more realistic water reflections.

Hmm... when I have some time I will add this too. But I am afraid this won't be before in two or three weeks.

enn0x
enn0x
 
Posts: 1267
Joined: Wed Nov 08, 2006 1:39 am
Location: Germany, Munich

Postby ThomasEgi » Sun Mar 04, 2007 6:32 am

there are lots ordering issues inside the gras/ground. switching the transparency to "DUAL" solves this but makes the grass looking pretty ugly :P.
User avatar
ThomasEgi
 
Posts: 2147
Joined: Fri Jul 28, 2006 10:43 am
Location: Germany,Koblenz

Postby pleopard » Mon Mar 05, 2007 3:31 pm

This is quite possibly the most important example I have seen on these forums yet. I am studying it trying to understand how it all works. I have run across a few snags ... hope someone can answer these questions for me:

1) In the following code there are 3 arguments to setTexture. Obviously the arguments are (textureStage, texture, priority) however, I am unable to find any documentation with more than 2 arguments in it. I experimented with setting the priorities to 0, -1, and other values but it had no effect. What is going on here?
Code: Select all
    def _setupTerrain( self ):
        tex0 = loader.loadTexture( 'models/dirt.png' )
        tex1 = loader.loadTexture( 'models/fungus.png' )
        tex2 = loader.loadTexture( 'models/grass.png' )

        ts0 = TextureStage( 'dirt' )
        ts1 = TextureStage( 'fungus' )
        ts2 = TextureStage( 'grass' )

        np = loader.loadModel( 'models/terrain' )
        np.reparentTo( render )
        np.setPos( 0, 0, 0 )
        np.setTexture( ts0, tex0, 10 )
        np.setTexture( ts1, tex1, 20 )
        np.setTexture( ts2, tex2, 30 )
        np.setTag( 'Normal', 'True' )
        np.setTag( 'Clipped', 'True' )
        np.setShaderInput( 'terrain', _terrain )


2) In the above code segment, what is the logic for determining which level the textures are assigned to? It appears that the highest level (>66) is loaded first, the lowest level (<16) second, and the mid level (16 to 66) third.

3) What do the 'setTag(...)' calls do? In the documentation it says that setTag allows the user to set tags to be stored by a node but that tese values are meaningless to Panda. I search the code and find nothing referencing these tags but if I comment them out, I get nothing rendered.

4) In the following code (in _setupCamera()) can someone explain to me what is going on?
Code: Select all
        cam1.setTagStateKey( 'Clipped' )
        cam1.setTagState( 'True', RenderState.make( sa ) )


Thanks!
Paul
User avatar
pleopard
 
Posts: 202
Joined: Tue Apr 25, 2006 1:33 pm

Postby enn0x » Mon Mar 05, 2007 7:23 pm

I try to explain:

(1)
This has been my first experience with shaders, and I did work with try and error at first. Right in the beginning I had problems with passing multiple textures to a shader, so I tried several thing, among them setting texture priorities. But the problem has been somewhere else.

The priorities are still from this try & error phase, and they are useless. What is important is the order of adding texture stages. This manual page gives some info on what priorities are good for:
http://panda3d.org/wiki/index.php/Texture_Order

(2)
I admit it would have been nicer to add the textures in one oder or another, e.g. in the oder of the elevation levels they are used for.

Please have a look at the terrain shader code:
Code: Select all
...
              in uniform sampler2D tex_0 : TEXUNIT0,
              in uniform sampler2D tex_1 : TEXUNIT1,
              in uniform sampler2D tex_2 : TEXUNIT2,

              out float4 o_color : COLOR )
{
    float4 dirtSample = tex2D( tex_0, l_texcoord0 );
    float4 fungusSample = tex2D( tex_1, l_texcoord0 );
    float4 grassSample = tex2D( tex_2, l_texcoord0 );

    // texture blending
    o_color = dirtSample;
    o_color = o_color * l_blend.y + ( 1.0 - l_blend.y ) * grassSample;
    o_color = o_color * l_blend.x + ( 1.0 - l_blend.x ) * fungusSample;
...


The first texture ("dirt.png") is used in the shader as variable "tex_0" (bound to TEXUNIT0). Then I sample this texture I use explicit names again: "dirtSample". And so on for the other two textures.

Then I blend the three samples, and since I have explicit names I didn't realize that the textures are not in the same oder as the elevation ranges.

Hmm... by the way, now that I see the code again I think using lerp( ) would be faster than blending by hand.

(3)
Now this is a bit tricky. At first the water reflections had some flaws, among them the fact that parts of the terrain that are below the water surface have been reflected too.

My first approach to this problem has been to use an additional clip plane on the camera that renders the reflection. This would have worked, if I hadn't used shaders for the terrain. ynij_jo found out that when using shaders clipping seems to happen at geom level and not at pixel level.

So if I want to clip away the underwater parts of the terrain I would have to divide the mesh into two parts, one over-water and one under-water. Not nice :-(

The work-around is in the shaders again. There are two shaders for the terrain: "terrainNormal.sha" and "terrainClipped.sha". The only difference are these lines in "shaderClipped":
Code: Select all
    // clipping
    if ( l_mpos.z < 36.0f ) discard;

If the interpolated position of a pixel is below z=36 then it is not rendered. (Hmm... again something that should be fixed: hard-coding the water level in the shader to 36.0f is bad)

Fine so far, but now I have another problem: The main camera has to render the terrain using "shaderNormal.sha", and the reflection camera has to render the terrain using "terrainClipped.sha". This is what the tags are for.

If you look at the part of the demo where the terrain NodePath is created you will notice that I assign no shader to this NodePath, like I did for grassNO, skyNP and so on. But I assign two tags, "normal" and "clipped". Now, whenever a camera sees the terrain NodePath, it assigns the render state that is associated with these tags. And in _setupCamera( ) is equip the two cameras with proper render states. The main camera (cam0) gets a render state containing the shader "terrainNormal.sha" associated with the tag "normal", and the reflection camera (cam1) gets another render state containing the shader "terrainClipped.sha" associated to the tag "clipped".

A very mighty instrument in my opinion. This manual page explains it too:
http://panda3d.org/wiki/index.php/Multi-Pass_Rendering

I hope I was able to help with understanding the code.
enn0x
enn0x
 
Posts: 1267
Joined: Wed Nov 08, 2006 1:39 am
Location: Germany, Munich

Postby pleopard » Tue Mar 06, 2007 9:47 am

Yes, that helps a lot. Thanks for taking the time to explain it.

I now understand the ordering of the textures. I changed the python code to this:
Code: Select all
      tex0 = loader.loadTexture( 'Resources/level0.png' )
      tex1 = loader.loadTexture( 'Resources/level1.png' )
      tex2 = loader.loadTexture( 'Resources/level2.png' )

      ts0 = TextureStage( 'level0' )
      ts1 = TextureStage( 'level1' )
      ts2 = TextureStage( 'level2' )


And the shader code to this :

Code: Select all
    float4 level0 = tex2D( tex_0, l_texcoord0 );
    float4 level1 = tex2D( tex_1, l_texcoord0 );
    float4 level2 = tex2D( tex_2, l_texcoord0 );

    // texture blending
    o_color = level2;
    o_color = o_color * l_blend.y + ( 1.0 - l_blend.y ) * level1;
    o_color = o_color * l_blend.x + ( 1.0 - l_blend.x ) * level0;


Now I can rename the textures accordingly and it makes sense (level0.png is the fungus, level1.png is the grass, level2.png is the dirt)


Thx,
Paul
User avatar
pleopard
 
Posts: 202
Joined: Tue Apr 25, 2006 1:33 pm

Postby birukoff » Thu Nov 08, 2007 7:08 am

In my case this demo doesn't work. It is very pity since many people on the forum refer to it. I have Panda 1.4.2, PyPE 2.8.8, Python 2.4.4, wxPython 2.8.6.1, PyXML 0.8.4, PIL 1.1.6.
Here is the log:
Code: Select all
Encoding set to UTF8
DirectStart: Starting the game.
Warning: DirectNotify: category 'Interval' already exists
Known pipe types:
  wglGraphicsPipe
(3 aux display modules not yet loaded.)
:util(warning): Adjusting global clock's real time by 1.04179 seconds.
Traceback (most recent call last):
  File "Demo-Nature.py", line 302, in ?
    world = World( )
  File "Demo-Nature.py", line 64, in __init__
    self._setupBase( )
  File "Demo-Nature.py", line 104, in _setupBase
    props.setCursorHidden( True )
TypeError: Cannot call WindowProperties.setCursorHidden() on a const object.

**** End of process output ****
User avatar
birukoff
 
Posts: 424
Joined: Thu Nov 08, 2007 7:03 am
Location: Russia, Moscow

Postby enn0x » Thu Nov 08, 2007 8:48 am

Right, doesn't work anymore. This demo has been written for an earlier version of Panda3D.

To make it work again you have to change this method:
Code: Select all
    def _setupBase( self ):
        self.centerx = base.win.getProperties( ).getXSize( ) / 2
        self.centery = base.win.getProperties( ).getYSize( ) / 2
        base.win.movePointer( 0, self.centerx, self.centery )

        props = WindowProperties( )
        props.setCursorHidden( True )
        base.win.requestProperties( props )

        base.disableMouse( )
        base.setBackgroundColor( 0, 0, 0 )

enn0x
enn0x
 
Posts: 1267
Joined: Wed Nov 08, 2006 1:39 am
Location: Germany, Munich

Postby mindstormss » Thu Nov 08, 2007 11:28 am

It doesn't seem to work for me, erroring both on the props.setCursorHidden(True) and on the loader.loadModel( 'models/sky' ). Is this because the bam file was made in a different version of panda?
All human beings should try to learn before they die what they are running from, and to, and why.
  - James Thurber
User avatar
mindstormss
 
Posts: 203
Joined: Mon Jul 30, 2007 9:21 pm
Location: California

Postby enn0x » Thu Nov 08, 2007 12:21 pm

This could be. I used .bam because of the smaller size. Not a wise choice.

But notice that there also has been a change in how Panda3D finds the models. You now have to give the filename including extension. Otherwise Panda3D will search for the default extension only (.egg):
Code: Select all
        self.skyNP = loader.loadModel( 'models/sky.bam' )

Also for anything else loaded via "loader.loadXYZ". Well, the code is rather old and obsolete by now.

enn0x
enn0x
 
Posts: 1267
Joined: Wed Nov 08, 2006 1:39 am
Location: Germany, Munich

Postby birukoff » Thu Nov 08, 2007 12:50 pm

Aha, now it works. I am amazed!!! enn0x, will you update it with some new features like improved water, reflections and so on? It could be great tutorial for all, I think.
User avatar
birukoff
 
Posts: 424
Joined: Thu Nov 08, 2007 7:03 am
Location: Russia, Moscow

Postby rdb » Wed Jan 23, 2008 10:43 am

Code: Select all
Assertion failed: !((To *)(this->_void_ptr))->empty() at line 324 of built/include/pointerToArray.I


Is it me messing up my panda installation, or a bug in panda?
I have no clue why it is occurring, and the assertionerror doesn't give me one either.
I rarely respond to PMs
rdb
 
Posts: 8565
Joined: Mon Dec 04, 2006 5:58 am
Location: Netherlands

Postby Laurens » Wed Jan 23, 2008 4:10 pm

I just tried it with panda 1.4.2. I get a different error, but maybe somehow related. The thing is that the model "sky" is there only as a bam file, so prolly my newer panda version can't handle it.
Laurens
 
Posts: 145
Joined: Sat Sep 23, 2006 5:08 am

Postby ynjh_jo » Thu Jan 24, 2008 1:50 am

Could be another broken piece on Linux. What call is causing that ?
http://ynjh.panda3dprojects.com | http://ynjh.p3dp.com
Intel P4Prescott 2.8GHz HT | ATI Radeon HD4670 1GB GDDR3
User avatar
ynjh_jo
 
Posts: 1795
Joined: Tue Apr 18, 2006 12:41 am
Location: Malang, Indonesia

Postby rdb » Thu Jan 24, 2008 2:34 pm

@Laurens: You need to change every loader.loadModel call and add .bam.pz to every model name, then it works (at least here). Oh, you will also need to fix that const windowsproperties thing to make it work.

@ynjh_jo, its the loadModel call of the grass loading stuff. The model path is correct -- when I change it to something wrong it fails with a NoneType error. But when the path is correct it just plain fails loading with this assertion error.
I rarely respond to PMs
rdb
 
Posts: 8565
Joined: Mon Dec 04, 2006 5:58 am
Location: Netherlands

Postby enn0x » Sat Jan 26, 2008 1:29 pm

Hmmm... this demo is quite old, done for Panda3D 1.3.2 if I still remember right. Several things have changed by now, for example the way you have to specify a file for loading a model. Using .bam files for a demo has been a mistake, since .bam files are version-dependant. You guys are right, it is time to update the code.

On the other hand, there are several things in this demo which I would do different by now. For example creating all the grass geometry in Python and then saving them to .bam files. I will see to it, but I am afraid it will be a few weeks before I find enough time (quite busy with my new job).

enn0x
enn0x
 
Posts: 1267
Joined: Wed Nov 08, 2006 1:39 am
Location: Germany, Munich

Postby jhocking » Sun Mar 15, 2009 5:59 pm

aw the downloads are gone? I accidentally deleted my copy of this demo, and now the download links aren't working.

If anyone who has this demo could post it here to download or PM me to send it to me that'd be great!
-Joe Hocking
www.newarteest.com
User avatar
jhocking
 
Posts: 206
Joined: Sun Dec 21, 2008 10:22 am
Location: Second City

Postby ynjh_jo » Wed Mar 18, 2009 11:03 pm

http://ynjh.panda3dprojects.com | http://ynjh.p3dp.com
Intel P4Prescott 2.8GHz HT | ATI Radeon HD4670 1GB GDDR3
User avatar
ynjh_jo
 
Posts: 1795
Joined: Tue Apr 18, 2006 12:41 am
Location: Malang, Indonesia

Postby rdb » Thu Mar 19, 2009 2:17 am

The bams are too old for Panda to laod. Could you maybe convert the bam to egg?
I rarely respond to PMs
rdb
 
Posts: 8565
Joined: Mon Dec 04, 2006 5:58 am
Location: Netherlands

Postby jhocking » Mon Mar 23, 2009 10:26 am

Thanks people who sent me the demo, and thanks ynjh for posting a new download here!

As pro-rsoft mentioned, while the demo looks great in Panda 1.5.4 on Windows, the .bam files won't load on any other platform. If the original files aren't posted as eggs by the time my spring commitments edge off, I might just redo the models myself as a way of practicing working with .egg files.
-Joe Hocking
www.newarteest.com
User avatar
jhocking
 
Posts: 206
Joined: Sun Dec 21, 2008 10:22 am
Location: Second City

Postby rdb » Mon Mar 23, 2009 10:30 am

Hmm, if they load fine on your Windows XP machine, you can bam2egg the stuff there and copy the .eggs to your Linux machine.
I rarely respond to PMs
rdb
 
Posts: 8565
Joined: Mon Dec 04, 2006 5:58 am
Location: Netherlands

Postby jhocking » Mon Mar 23, 2009 10:31 am

oh there's a bam2egg? Didn't know that, I'll try it later.

ADDITION: Just converted all the models and put them here
http://www.box.net/shared/fkke9b1lsc

I must say, it's a good thing I know how to write a script to run bam2egg automatically, because apparently each bit of grass is a separate model that needed to be converted.

Obviously you need to edit the python code in a couple places for the demo to load the correct models.


Incidentally, as long as I'm talking about editing the demo code, how do you adjust the sky color? I figured that's set by _sky near the top of the code, but adjusting that isn't working the way I expect; clearly it's not as simple as the RGB value for the color of the sky, more like a multiple that's used by the shader somehow. The sky is too saturated right now, so I want to see what it looks like with a color more like RGB 200,200,255
-Joe Hocking
www.newarteest.com
User avatar
jhocking
 
Posts: 206
Joined: Sun Dec 21, 2008 10:22 am
Location: Second City

Postby benicourt81 » Fri Aug 06, 2010 2:13 am

thanks a lot, nice job, very fast.
Panda3D and Blender... my favourite toys !
benicourt81
 
Posts: 60
Joined: Tue Aug 03, 2010 2:44 am

Postby abdoo47 » Tue Sep 07, 2010 12:25 pm

:shock: ooh man that's very awesome , very wonderfull
helped a lot , thank you
abdoo47
 
Posts: 32
Joined: Fri Aug 27, 2010 4:35 am
Location: Cairo , Egypt

Previous

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest