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;
#281
09/07/2006 (9:53 am)
Wow. Just amazing work by some very dedicated community people. Thank you guys.

If I could pick your brains for a sec, I have 2 questions:

1) Throught this resource I am reading about frame rate impacts. I was unable to gather wether this was due to implementation errors, GPU incompatability or if FPS sacrafice is something you have to live with for such a great looking resource.

Could someone please clarify the framerate impact plz. I need the FPS as my project is an MMO and I wont sacrafice FPS for beauty ;)

2) I was going to ask about hiring someone with a solid grasp of this resource to merge it into my project but I think I will contact a few folks here directly as I can see this resource significantly tricky and I need to have a genuine expert on it =)

Thanks all. Amazing work.
#282
09/07/2006 (8:08 pm)
@Flybynight Studios
Why not do it yourself? Go make 2 builds, 1 with no water resource. and 1 with water resource. type "metrics(video);" and it will say the frame rate.
#283
09/13/2006 (5:38 am)
that error i get and other errors too
engine\terrain\waterblock.cc(61) : error C2065: 'mNormalMapTexture' : undeclared identifier
engine\terrain\waterblock.cc(84) : error C2065: 'mNormalMapName' : undeclared identifier
engine\terrain\waterblock.cc(918) : error C2660: 'fluid::SetTextures' : function does not take 7 arguments
#284
09/13/2006 (2:00 pm)
@Charles

Make sure you copied the waterblock.h as well.
#285
09/13/2006 (2:10 pm)
@Randy i manuly do the patch with file compare program
#286
09/20/2006 (1:33 am)
Since i am new at this, and REAAAALLLY want this resource, could someone please help me get this one to work on mac? i've installed the CG, but doesn'n know know the next step... :(
#287
09/20/2006 (2:01 am)
@Alex, it works om Mac just as well as on Win32. Have you ever compiled TGE on your own before? If not, then that would probably be a smart first step to familiarize yourself with how the Xcode project is built up (and get an idea where the changes mentioned here should go).

Other than that, you have to go through the steps Manoel has described, and follow some of the more summarizing posts above (I would probably recommend reading the posts of jojimbo and my colleague Trond Abusdal). Let me know if you need any more Mac-specific help (using Xcode etc.).
#288
09/20/2006 (1:09 pm)
@ Eric:
I have successfully compiled in torque earlier when the engine was stock, and even after fixing the broken passwordfield with the resource by Dreamer. What i do need is kinda tutorial made for mac, like:

1. Take this.
2. Do that.
3. Add this.
4. Compile...

Any kind of support would be more than appreciated... sorry for being such a noob, but hey, gotta start somewhere, and i am not afraid of asking when i don't know the next step.
#289
09/20/2006 (1:45 pm)
@Alex: Well, then I suppose you should start with downloading the resource (from the top of this page), and also download the Cg toolkit (and install it).

Then, the next step would be to extract the RAR file, and take the files and put into your TGE source code tree. If you have a not-too-heavily modified TGE code base, it should work pretty well to just replace your original files with the corresponding files from the resource.

Thirdly, you might want to test-compile this setup, to see if the merging lead to any problems. To set up Xcode correctly, you only have to add Cg.framework to the project (drag it into the group in the Xcode project tree called "System Frameworks" (under Frameworks), and set it to be used for both Debug and Release. However, as I mentioned in an early post here, you have to make a few more changes to the resource's files:

[snip]
1) In fluidRender.cc:16,17: Changed the backslash to a forward slash ('#include "cg/cg.h"')
2) In gameTSCtrl.cc:246: Changed 'GBitmap::BitmapFormat::RGB' to 'GBitmap::RGB'

In addition, I added the Cg.framework to the list of frameworks, but I did not have to switch the gl.h include file (as mentioned in other posts) for the patched TGE to compile.
[/snip]

And then you should follow Tuanlucky's advice:

[snip]
Step 5. Change line 383 of waterblock.cc mSurfaceTexture = TextureHandle( mNormalMapName, MeshTexture ); to mNormalMapTexture = TextureHandle( mNormalMapName, MeshTexture );

Step 6. Uncomment the code "glTexEnvi ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );" at line 480 and line 500 in fluidrender.cc to get rid of the error "GL not in canonical state"
[/snip]

If you can get this to compile, then you can try to use the new "Torque Demo.app" in the example/ directory (remember to copy in the CG folder and the other additions from the resource).

It's ok to be a newbie, and you won't learn unless you ask or study... (However, most of the info in this post is mostly a rehash of the posts above) Let me know if you're still having trouble. Maybe we should pull any further discussion off the resource discussion board to avoid filling it up even more; drop me an email if there are more details you'd like to discuss.
#290
09/21/2006 (4:44 am)
Kinda got it to compile, but stops at the fluidrender.cc, saying:

can't locate framework for; -framework Cg
Command /usr/bin/g++-3.3 failed with exit code 1

any idea what the problem is?
#291
09/21/2006 (7:42 am)
Anyone? been tearing my hair off, the little that is left, and soon i will begin my quest for ripping my ears and nose off...

EDIT:
Got it to coompile all the way to the last linking, but there it stops and warns me that CG is not prebound, then another warning telling me that prebind is ignored because MACOSX_DEPLOYMENT_TARGET envrionment variable greater or equal to 10.4, and then the big bad red cross saying that i have Undefined symbols:

_cgCreateContext
and so forth...

That's it, now my left ear is coming off...!
#292
09/21/2006 (10:36 am)
@Alex,

I wish I could help. I just went through the process and it worked for me.

I downloaded the Tiger files from Nvidia and installed them.
I added the frameworks to my project.
I downloaded and installed the files from the resource.
I did a "Clean All" and "Build".
I made the changes Eric Harg pointed out. (Three edits.)
And rebuilt it and it worked.

I running Xcode 2.4, OS X 10.4.7 and I was working with v1.4.2R1 with file torque_xcode_2_2_UB from CVS.

At least I assume it worked the water has a milky white reflection on top of it now in starter.fps. I'm not sure I like the default effect but it is doing something. I wish I could be more help other than to say it worked for me.

When you added the Cg framework did you make sure you added it to your target build specifically? I would remove and re-add the Framework again (assuming you haven't) and add it to all the builds when prompted - or at least all the torque builds.

=Tod
#293
09/22/2006 (12:15 am)
Ok, this is what i have done so far:

(I have Xcode 2.3 on my 10.4.7)
Installed 1.4.1 Cg for tiger
Added the files manually by changing the ones in the resource with the excisting ones
Dragged the Cg.framework-folder from system/library/frameworks to my Frameworks/System Frameworks in Xcode and to be used for both Debug and Release
Made the changes in fluidrender.cc and gameTSCtrl.cc, and also in waterblock.cc as mentioned by Erik
Added /System/Library/Frameworks/Cg.framework/ to "Open quickly" in the prefs when right-clicking on the torque_xcode_2_2_UB
Pressed compile....

If i did something in the order wrong, please notify me...

Edit:
Forgot to mention that i used the release from "My Account".
#294
10/09/2006 (7:31 am)
not a single person able to help me?
#295
10/09/2006 (10:00 am)
@Alex: what happened once you pressed compile. Did you check the "building result" window?

I'm victim of the "blue" world (every terrain texture is now blue, as well as shapes within the water (watertower, etc.)). So any help appreciated too.

www.jmnos.com/torque/tge_blue.jpg

STef
#296
10/09/2006 (11:07 pm)
_cgCreateContext
_cgCreateProgramFromFile
_cgGLBindProgram
_cgGLDisableProfile
_cgGLEnableProfile
_cgGLGetLatestProfile
_cgGLLoadProgram
_cgGLSetParameter1f
_cgGLSetParameter2f
_cgGLSetParameter3f
_cgGLSetStateMatrixParameter
_cgGLUnbindProgram
_cgGetError
_cgGetErrorString
_cgGetLastListing
_cgGetNamedParameter
_cgSetErrorCallback
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgCreateContext
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgCreateProgramFromFile
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLBindProgram
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLDisableProfile
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLEnableProfile
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLGetLatestProfile
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLLoadProgram
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLSetParameter1f
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLSetParameter2f
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLSetParameter3f
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLSetStateMatrixParameter
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGLUnbindProgram
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGetError
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGetErrorString
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGetLastListing
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgGetNamedParameter
/Users/alexpalmquist/Desktop/Torque SDK 2 (kopia)/pb/../example/torque_xcode_2_2_UB.build/Default/Torque-MacCarb-Release.build/Objects-normal/i386/fluidRender.o reference to undefined _cgSetErrorCallback
collect2: ld returned 1 exit status
#297
10/10/2006 (12:44 am)
@Alex: you do have a problem related to fluidrender.cc with a cg framework badly linked for some reason.
I did put all the .h files from the framework located in HD/Library/cg.framework/.../.h into engine/cg (files copied)
then I set up an alias of the engine/cg folder into the lib folder (at the Torque's root level).

There are many typos with cg\cg.h instead of cg/cg.h into the code so be sure to clear them. There will be an error once the compiler find them.

You might also clear you cg.framework and install it again, mine was corrupted (duplicated files into 2 different subfolders) when I upgraded to cg 1.5. I will revert to 1.4 as it might cause my "deep blue" problem.

I suggest you to restart from a fresh code and give it another try.
#298
10/10/2006 (12:48 am)
I don't personally use a Mac, but it looks like the CG framework is not included in your build. All the "undefined symbols" are CG related.

EDIT: Looks like I'm slow :p Follow Stephan's advice ;)
#299
10/10/2006 (1:15 am)
@Alex: I'm don't think you'd have to go through all of Stephan's steps, especially the one about moving the header files and their aliases all over the place.

Wait, did something just disappear from your post, Alex? In the notifying email I got, you had included a lot more info... Well, I assume it just didn't get through, somehow...

What I *do* think, from the email, is that you have included the Cg.framework twice, and that you have also included the Cg framework's directory in the library search path? So, what I think would be the best idea for you right now, is to follow Stephan's second advice, and remove all instances of the Cg.framework, and all references to it in the Torque Xcode project (but leave the rest of the project alone, so that you don't have to go through the trouble of merging in the water shader resource again). Then, install the Cg 1.5 from developer.nvidia.com, and add the system-wide Cg framework to your project like this (*very* step-by-step, easier methods may also be used):
- click on the triangle next to "Frameworks" in the "Groups & Files" bar of your Xcode project
- Then "System Frameworks" and "Project Frameworks" appear
- right-click (or Ctrl-click) on "System Frameworks", and
- ... hold your mouse over "Add >", and
- ... select "Existing Frameworks..."
- navigate to "/System/Library/Frameworks/" in the appearing window
- select "Cg.framework" from the file list and press OK
- in the appearing window, set "Reference Type:" to "Default"; and check both the Debug and Release targets to add the framework to
- hit "Add"
- and you're done.

Hope this helps.
#300
10/10/2006 (1:27 am)
@Stephan,

I saw your email the other day, but I've been a little too busy to reply. I'll just take it here, for the potential benefits of others - this resource comment thread is way too long anyway... :-)

It is kinda difficult to say what's wrong with your blue world without any more information... Are you sure you did nothing to the terrain rendering? And did you include the normal map? Do you get any Cg-related (or terrain- or texture-related) error messages in the console? What about the "reflectionGUI" GameTSCtrl object, did you include that? And the Cg directory in the example folder is also there? Or...? :-)

As to the hardware you told me in your email that you're using (Core Duo iMac with X1600/128MB), it should *definitely* be able to run this (I'm running a *much* heavier Cg-enhanced Torque on my 12" PowerBook G4 w/ GeForce Go5200/32MB... well, *barely* running I should say). I recommend using the OpenGL Extension Viewer to check the capabilites of your video card; check out: http://www.realtech-vr.com/glview/ (both Mac and Win32).

However, I have had some strange coloring effects on my Mac when I have enabled OpenGL lighting in the prefs (now, *where* was that...?); you might want to dig around to find that setting and test it out... Otherwise, I'd like you to check all the things I listed in the top of my post... Good luck! :-)