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;
#201
06/14/2006 (2:40 pm)
@Areal:

Care to post your videocard? So far the water seems to work on Nvidia and ATI hardware with SM 2.0 and higher. It didn't work in Radeons 9200 (SM 1.4), because Cg failed to find a valid fragment profile on that card. I didn't test it on Nvidia SM1.4 hardware (true GF4s).

The water does checks if the card supports 4 texture units and if Cg has been properly initialized before using the shader codepath, otherwise it reverts to the old-style rendering.
#202
06/14/2006 (4:08 pm)
Is it possible to add cg shader effects to dts models also?

--Mike
#203
06/14/2006 (4:44 pm)
@Areal: A GeForce 4 won't cut it. OpenGL shaders require at least an FX series card/Radeon 9500.

@Manoel: I don't know Cg well enough to know what its looking for, but unless it's checking for a register combiner/texture shader profile, it won't run on nVidia SM1.4 hardware either. It might not run on SM1.4 hardware under OpenGL period.

@Mike: Sure, it's possible. Have fun doing it.
#204
06/14/2006 (8:40 pm)
@Mike
Yes anything is posible as long as you got the skill and knowledge for it. Esp since there also the cg dot3 bump map interior resource available. (which i am still trying to get working)
#205
06/14/2006 (11:21 pm)
hey peoples, im a total noobie when it comes to anything to do with adding resources, programming etc. Anyway i was wondering if anyone here could give me detailed step by step instructions on how to implement this (i dont have visual studio btw). I can kind of understand not wanting to do this but if anyone could i would be really, really greatful.
#206
06/15/2006 (1:55 am)
1) Get TBE, or gcc/g++.
2) get winmerge and install it. use it to merge the new code.
3) use gcc/g++ to compile it. or use TBE to compile it.

but i would really recommend getting Visual studios. the license for stand alone c++ is only $100. and Visual studios express v8 is free. Tho i would recommend getting vc 6 or vc v7.1, because they already come with the project files.
#207
06/15/2006 (2:33 am)
so i only have to merge the old files with the files from tge water.rar and then recompile the engine? i assume you mean once ive installed cg toolkit right? btw $100 dollars is a lot of money to me because im only 15 and i have to use my own money to buy everything myself. If that is all it takes then thank you so much for your help. If not, thanks for your help anyway, if you are talking to someone else and i just think youre replying to my post ignore everything i just said.
#208
06/15/2006 (10:00 am)
there a lot more to just merging the current stuff as well. if you look through the comments, you will see a bunch of bug fixes that might need to be put together. But like i said vs 8 express is free. you can use that. but i warn you unless you know what your doing. your going to have a little bit of a hard time converting the project to v8.
#209
06/21/2006 (3:08 am)
Has someone get it to work underwater now?

I have tryed different methods, but get no good result for now...

Another strange thing: Since i installed this shader, Torque crash in editor modus with a acces violation error in sound.c (oO ^^).
If i simple rename CG-Folder so the shader dont load correctly editor dont crash anymore.

Im using TGE 1.4 + TLK 1.4 (+1000 different upgrades and changes.. but it has worked ;) ).
#210
06/23/2006 (10:24 am)
Ok i'm having a bit of trouble understanding if this is supposed to be this way or not, but my CG water ends up like this in-game:

http://i4.photobucket.com/albums/y108/Plague95/screenshot_010-00001.png

It just looks odd on large bodies of water for it to only render a square around you instead of making all the water CG. Is this on purpose, or some type of error?
#211
06/26/2006 (10:29 am)
has anyone got a mac port for this yet? i really need this for my project...
#212
06/28/2006 (4:14 pm)
Man is there enough comments on here or what?
I'm gonna tag this, shaders for TGE rock :-)
#213
07/01/2006 (12:10 pm)
I've figured out what was up with the big white mess in the distance on the reflection - after lots of fiddling around I found that it was the clipplane. Disabling the clip plane when drawing the sky does the trick.

Here are images:

Broken
Fixed

Here is my code (TLK 1.4) for those who want it:

In sky.cc change the top of Sky::Render to look like this:
void Sky::render(SceneState *state)
{
   PROFILE_START(SkyRender);
   F32 banHeights[2] = {-(mSpherePt.z-1),-(mSpherePt.z-1)};
   F32 alphaBan[2] = {0.0f, 0.0f};
   F32 depthInFog = 0.0f;
   Point3F camPos;
   S32 index=0;

   if(gClientSceneGraph)
   {
      F32 currentVisDis = gClientSceneGraph->getVisibleDistanceMod();
      if(mLastVisDisMod != currentVisDis)
      {
         calcPoints();
         for(S32 i = 0; i < MAX_NUM_LAYERS; ++i)
            mCloudLayer[i].setPoints();

         mLastVisDisMod = currentVisDis;
      }

		//  krimzon - PRLD_WATER_SHADER
		if (gClientSceneGraph->mReflectPass)
			glDisable(GL_CLIP_PLANE0);
		// -krimzon
   }

And at the bottom of the same function:
//  krimzon - PRLD_WATER_SHADER
	if (gClientSceneGraph)
	{
		if (gClientSceneGraph->mReflectPass)
			glEnable(GL_CLIP_PLANE0);
	}
	// -krimzon

   PROFILE_END();
}

So YAY! Fixing this problem made my day.
#214
07/03/2006 (7:23 pm)
@Alex (Stalker) Sakablukow
You mean something like this?

www.geocities.com/tsune_l/under_water.jpgwww.geocities.com/tsune_l/under_water2.jpg
@David Laurie
Good Stuff.
#215
07/03/2006 (11:09 pm)
Yes! :)

How have you get it to work?

I have a project where the most time the player is in/under water, and this really good ressource is useless for me if i dont get it to work underwater too. I have tryed to manage it "on the fly", but it still looks not good enought and has a few strange bugs...
#216
07/04/2006 (1:52 am)
well it works stock with the code there. it still needs modification to be able to see through it. or render the terrain reflection. You can see the reflection of interiors, or meshes that are close to the water, but not really anything else. So it might be useless for you then.

I'll take at the code later, when i get the normals maps working on interiors fully working first. At minimum, the only thing thats needs to be done is set the under water to be a little transparent.
#217
07/04/2006 (4:31 am)
Is anyone else having problems with the CG 1.5 beta?
#218
07/04/2006 (9:21 am)
dont use the beta. its a beta for a reason. use the last stable version.

download.nvidia.com/developer/cg/Cg_1.4/1.4.1/Cg-1.4.1_Setup.exe
#219
07/05/2006 (7:32 pm)
good advice T Squared - flawlessly working now!
#220
07/08/2006 (9:55 pm)
Installing the source changes:

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


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.
-------------------------------------------------------------------------------------------------------------------------
sorry to sound so dumb,but i am an absolute beginner,what/where exactly must i modify the TGE project?
Are the Cg/lib and Cg/include folders the ones in the Nvidia folder?
if someone could take a little time to help me get this right i would be endebted surely.