Game Development Community

dev|Pro Game Development Curriculum

TGE water upgrade

by Manoel Neto · 07/03/2006 (11:33 am) · 347 comments

Download Code File

This resource adds basic pixel shader and vertex shader support to the TGE waterblock, using CG, and support for drawing a reflected view of the world into a texture and using it in the waterblock. Right now it's windows-only, but there's nothing stopping you from making it work on OSX and Linux, since CG is also avaliable in those platforms.

Note: For those worrying about Cg being Nvidia-only, fear not, I've personally tested it in different ATI cards and it's compatible with no visible performance problems. Just make sure your drivers aren't ancient.

Note2:
Alex Scarborough (of the TLK DRL fame) is working on porting this resource to GLSL and making it work on OSX. Might get some news soon.

Installing the source changes:

First you'll need the CG toolkit. You can get it here:
developer.nvidia.com/object/cg_toolkit.html

After installing it you must modify the TGE project to include the Cg/lib and Cg/include folders to the library paths and the include paths, respectively.

Important: you also need to open cgGL.h in the "Cg/include" folder and replace this:
#include <GL/gl.h>
By this:
#include <dgl/dgl.h>
Otherwise you'll get loads of compile errors.

Included in the zip file are the modified source files. To install into a unmodified TGE 1.4, just replace the TGE files by the ones included. To install in modified TGE sources, use a merge tool, or copy the changes by hand (all changes are comment-tagged), then compile.

Script changes and usage:

The example folder in the ZIP contains the script files you need to actually see the water in the game. The "example/CG" folder must be copied to the same folder as your executable. It contains the vertex and fragment shaders. The filenames are very hardcoded into the source code, unfortunally.

In "example/starter.fps", you'll find a modified playGui.cs and a normal map for using with the water (it's actually one of TSE's water textures).

The modified playGui.cs is required to render the reflection texture. The GameTSCtrl control was modified and now features a flag called "isReflection". Turning it on will make the gameTsCtrl render the world using a reflected camera matrix, based on the first existing waterblock.

Waterblocks will search for a GameTSCtrl called "reflectionGui". If it's found, they'll grab the refleciton texture from it.

Waterblocks also have a new field, called NormalMaptexture. You need a suitable normal map there, otherwise the reflection texture coordinates will be badly displaced.

Known bugs/limitations:

- No refraction support;
- No specular;
- No fog (at most times it looks like the water is fogged, because the reflected scene is fogged);
- No proper support for using a static envornment map instead of a full reflection (it does use the texture, but it remains fixed, and won't rotate along the view, as the original did);
- Sometimes the reflection can go strange and "melt away". Something to do with clip planes;
#321
11/14/2006 (6:26 pm)
I'm with Graham on this. I get the same errors involving m_TerrainSize and m_TerrainBlockSize being undeclared modifiers and getSunLight and registerLights not being a part of LightManager.

Any help here would be great. (This is with TGE 1.50)

@Trenton: Any advice for getting this working with TGE 1.50?
#322
11/24/2006 (1:24 pm)
@John - You copied the resource files over the 1.5 files. These were based on 1.4. You have to merge each of the resourse files w/ the 1.5 files. Winmerge is free. I like Ultracompare.
#323
12/01/2006 (10:01 am)
@Randy,
Thanks for the advice. I tried that, still not working. It compiles, but the water doesn't look the way it's supposed to to.

If anyone could offer up a fix for us non-programmers, it'd be greatly appreciated.

@Trenton,
You seemed to have gotten it to work. Any advice or help you could offer would be fantastic!
#324
12/02/2006 (6:03 am)
There is already a port to TGE 1.5 called "dynamic gamma 1.5" here:

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=10830

posted by Paul Jan.

My suggestion is to update video card drivers, 'cause before update I face the canonical state problem, gray fog instead of water, no water at all etc.

Hope this may help
#325
12/03/2006 (6:00 pm)
@Gianfranco,
Thanks for the help. I tried that version, same thing. My video drivers are up to date (I'm an avid gamer, so I'm always checking for updates). I even downloaded and installed the latest CG Toolkit (1.5). No luck.

I get a placid looking water with reflections and a pinkish tone. If I go to the editor and select the water block, the pink look goes away and it's just a flat reflecting water. It's as if the Water noise is being ignored.

As I'm not a programmer, I don't know what to look at. I'd post a pic, but I'm unsure of how to do that too.
#326
12/04/2006 (9:47 pm)
CG Toolkit 1.4.1 only works. dont use 1.5
#327
12/04/2006 (11:06 pm)
I have to disagree, or to be more accurate with T-Squared on this one.
Mac users: use CG 1.5 it works, but rename the framework to cg.framework (instead of Cg.framework). There are compilation problems due to case sensitive system.
CG 1.4.1 is for PPC based macintosh only.
Succesfully compiled and working with TGE 1.5.
#328
12/05/2006 (11:56 am)
The pink tone is the result of rendering using the default waterblock code which is used when CG detection fails.
To fix this bug, go to line 403 in waterblock.cc and change mSurfaceTexture to mNormalMapTexture.
#329
12/05/2006 (12:07 pm)
If you want to use the standard fogging with the shader water, go to line 900 in fluidRender.cc and move the text block to line 868. That will force fogging to be used with both standard and shader water.
#330
12/07/2006 (4:38 pm)
@T Squared,
Sure enough 1.5 works great with Paul's fixes.

@Stephan,
I got it to work as well.

@Paul,
Thank you sir, both of those fixes worked like a charm!! If you worked for me, I'd give you a raise!! :-)
#331
12/20/2006 (3:41 am)
Wow this is a great resource!!!

Exactly what I was looking for.

Valeu Manoel!!!
#333
02/23/2007 (5:17 pm)
First off - great resource!

2nd - does anyone know how the CG water can be turned off? Here's my issue - on my laptop with an older ATI card, its correctly falling back to the standard water (only has 1.1 shaders so can't do the CG water), but the framerate still drops as if it were using the CG water. It must still be updating the reflection gui in the background or something. Anyone know how to turn off the CG rendering feature completely for lower end systems? I'd like to make that an option anyway even for high end users who don't care about the cool water and want the higher framerate.

Thanks in advance...
#334
02/28/2007 (9:51 pm)
Yes, what Peter was asking.
#335
03/14/2007 (6:55 pm)
@Peter & Frank

Here is a few things that you could try to improve the performance.

First of all you might want to create a global flag to tell if you want the Water resource to be used. Make the flag accessible in the OptionDlg. In the mission, you may want to take out the reflectionGUI because the reflection takes alot of processing power and that could be decided according to the flag. Remember that you could dynamically add or remove elements in missions, so you could check the flag when you load the mission and add the reflectionGUI accordingly to the flag.

Another approach you could do is not to update the reflectionGUI as often as it should. Instead of grabbing the reflection of the scene to the 2D texture, you could try using a static image instead. This could be done either by just simply not updating the texture, or you could use an external image like the NormalMapTexture, but it might be be as accurate regarding reflections.

You could also check for the profiles of the display card, and decide whether to turn on or off the options. An example code that allows you to grab the vertex profiles are below:

CGprofile g_cgVertProfile;
CGprofile g_cgFragProfile ;
g_cgVertProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
g_cgFragProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);

based on the ID you get back you can figure out what version it is by the following table (this was just some extra code I did just to debug my Cg application):

if (id==6146)
Con::printf("Your VP profile is vp20");
else if (id==6147)
Con::printf("Your FP profile is fp20");
else if (id==6148)
Con::printf("Your VP profile is vp30");
else if (id==6149)
Con::printf("Your FP profile is fp30");
else if (id==6150)
Con::printf("Your VP profile is arbvp1");
else if (id ==6151)
Con::printf("Your FP profile is fp40");
else if (id==7000)
Con::printf("Your FP profile is arbfp1");
else if (id==7001)
Con::printf("Your VP profile is vp40");
else
Con::errorf("Profile is unknown");

What you could do is to change what code is run based on the profile returned.

Sorry if it isn't that precise, but just my 10 cents on it.
#336
05/04/2007 (7:09 am)
Hi - also posted this in the constructor feedback forum cause it relates to both -
getting some very strange image jumbling and flickering on DIFS after getting the water resource patch by Manoel Neto.
Here is a pic. They look fine when turning the water shader off. Have no idea what could be causing it.
Using TGE 1.5 and exporting as legacy dif...

pacybear.dyndns.org/screenCaps/jumbled.jpg
The dock on the left is tweaking in this picture. However, depending what angle it is, every dif flickers.\
Any ideas?
Kurt
#337
08/03/2007 (5:24 am)
hey everyone,

im not sure if it already was an issue, maybe i just didnt see it whereas the water used to be much darker. but when i zoom out my water (i think changes LOD level and in that way) starts to look real cheap, if im like 50 metres above it, i will start to see plain squares in the water (and not just repetition of textures). im not sure in what bitmap these squares are located. but probably not in the bitmap which is used as surface texture, because it happens whatever i change this to.

how is this caused, what do i do, or do any of you have good replacement textures that would help?

oh and by the way, some fields in the waterblock appear to be useless, for example the surfaceopacity field does not have any result i think.

and im using 1.4 and VC8 i used a clean version for both.
#338
09/12/2007 (4:30 am)
lol i seriously feel like the last one at the party. this is amazing stuff
#339
09/30/2007 (4:37 am)
Water disapear when switching into fullscreen or changing resolution/bitdepth in fullscreen modus. Any ideas ?
#340
09/30/2007 (5:50 am)
As Manoel Neto says above, you need to reload the shaders. Try "$shaderloaded = false;" in the console.

EDIT: Typo...

-Trond