TGE w/ Bumpmapped Interiors (using the CG runtime)
by Kevin Johnson · 09/19/2004 (5:57 pm) · 122 comments
Download Code File
Hacking in CG
First let me start off by saying: THIS is not poor man's TSE.
Do some digging for Hobiticus' platformCG implimentation if you are looking for that .. :)
The code for this resource is NOT complete, and its mostly stolen from various elders in the community. Its the code from when i just got this working. As such there are demented comments, leftover variables, commented out sections of code and stuff that probably doesn't work like i thought it did, but hey.. ya get what ya pay for... What is shown here is a relatively painless way to add CG shader support to a Torque class, please forgive the messiness.
If you mess around/extend this please stick with TSE's model of material based shaders,
as it'll make porting go quicker.
In this resource we will be adding a dot3 bumpmapping fragment shader to the interrior class.
I picked CG because TGE is an openGL engine. It would be helpful if someone were to port this to GLslang..nudge, nudge,wink,wink.
PRELIMINARIES
1. Download and install the cg toolkit from developer.nvidia.com and set up your compiler for its use and Verify everything works
2. You should have a basc idea how cg works in relation to an opengl application.I have listed some great referneces below
3. download and unzip the archive into a temporary folder. It contains a hijacked version of this resource.
4. Grab a cup of your drink of choice, and get ready for some HOT TGE shader action
LETS GO
First thing you gotta hack is the CG includes. Open cgGL.h
and change
#include
to
#include
Now do a visual merge of your codebase with the ones from the archive, I AM NOT responsible for over-written files!!
(The ones in the archive are taken from 8/24 HEAD)
dgl\materialPropertyMap.cc
dgl\materialPropertyMap.h
interiors\interior.cc
interiors\interior.h
interiors\interiorInstance.cc
interiors\interiorRender.cc
When you finially get it to compile...
In your game directory (example) directory put CG\2TUfp20dot3.cg
I also have cgc.exe in that same directory
SCRIPT HOOKS
in data\init.cs
add:
exec("./interiors/propertyMap.cs");
and add propertyMap.cs and all the normal maps to the interiors directory
the normalmaps for the starter.fps demo can be found here
then fire it up and you should be (graphic card dependancies aside) drooling over TGE bump mapping...
NOTES
*Interior::renderARB_FC in interiorrender.cc is where all the magic happens.
*The effecting lightvector is hardcoded for simplicity at interiorrender.cc line#1147
glColor4f(1,1,1,baselevel);
you can change it to something like:
glColor4f(lightDir.x,lightDir.y,lightDir.z,1.0);
where lightDir = gClientSceneGraph->getLightManager()->getLight([whateverindex]).mPos -[objectposition] (or is it the other way around??)
then normalize
* running interiors through a normal map really darkens em up....but i think it looks cool..also since the shader is dealing with the basetexture + lightmap, you can come up with some real neat effects with pulse (entity)lights..
I think thats it.. Have fun and play with the shader (CG and HLSL share alot) and get ready for TSE.. its going to change the world..
This code released on the BGL (Ben Garney License.."if you use this in your game send me a copy")
also if your game is a MMO you owe my wife a free account..lol
oh and kudos to whoever did those textures..the textures really make it..
let the frenzy begin
References
www.nvidia.com/object/cg_toolkit.html
developer.nvidia.com/object/hello_cg_tutorial.html
www.fusionindustries.com/content/lore/code/articles/fi-faq-cg.php3
nehe.gamedev.net/data/lessons/lesson.asp?lesson=47
www.3ddrome.com/articles/cgshaders.php
www.gamedev.net/reference/articles/article1903.asp
www.codesampler.com/oglsrc.htm
www.ronfrazier.net/apparition/index.asp?appmain=research/advanced_per_pixel_ligh...
Hacking in CG
First let me start off by saying: THIS is not poor man's TSE.
Do some digging for Hobiticus' platformCG implimentation if you are looking for that .. :)
The code for this resource is NOT complete, and its mostly stolen from various elders in the community. Its the code from when i just got this working. As such there are demented comments, leftover variables, commented out sections of code and stuff that probably doesn't work like i thought it did, but hey.. ya get what ya pay for... What is shown here is a relatively painless way to add CG shader support to a Torque class, please forgive the messiness.
If you mess around/extend this please stick with TSE's model of material based shaders,
as it'll make porting go quicker.
In this resource we will be adding a dot3 bumpmapping fragment shader to the interrior class.
I picked CG because TGE is an openGL engine. It would be helpful if someone were to port this to GLslang..nudge, nudge,wink,wink.
PRELIMINARIES
1. Download and install the cg toolkit from developer.nvidia.com and set up your compiler for its use and Verify everything works
2. You should have a basc idea how cg works in relation to an opengl application.I have listed some great referneces below
3. download and unzip the archive into a temporary folder. It contains a hijacked version of this resource.
4. Grab a cup of your drink of choice, and get ready for some HOT TGE shader action
LETS GO
First thing you gotta hack is the CG includes. Open cgGL.h
and change
#include
to
#include
Now do a visual merge of your codebase with the ones from the archive, I AM NOT responsible for over-written files!!
(The ones in the archive are taken from 8/24 HEAD)
dgl\materialPropertyMap.cc
dgl\materialPropertyMap.h
interiors\interior.cc
interiors\interior.h
interiors\interiorInstance.cc
interiors\interiorRender.cc
When you finially get it to compile...
In your game directory (example) directory put CG\2TUfp20dot3.cg
I also have cgc.exe in that same directory
SCRIPT HOOKS
in data\init.cs
add:
exec("./interiors/propertyMap.cs");
and add propertyMap.cs and all the normal maps to the interiors directory
the normalmaps for the starter.fps demo can be found here
then fire it up and you should be (graphic card dependancies aside) drooling over TGE bump mapping...
NOTES
*Interior::renderARB_FC in interiorrender.cc is where all the magic happens.
*The effecting lightvector is hardcoded for simplicity at interiorrender.cc line#1147
glColor4f(1,1,1,baselevel);
you can change it to something like:
glColor4f(lightDir.x,lightDir.y,lightDir.z,1.0);
where lightDir = gClientSceneGraph->getLightManager()->getLight([whateverindex]).mPos -[objectposition] (or is it the other way around??)
then normalize
* running interiors through a normal map really darkens em up....but i think it looks cool..also since the shader is dealing with the basetexture + lightmap, you can come up with some real neat effects with pulse (entity)lights..
I think thats it.. Have fun and play with the shader (CG and HLSL share alot) and get ready for TSE.. its going to change the world..
This code released on the BGL (Ben Garney License.."if you use this in your game send me a copy")
also if your game is a MMO you owe my wife a free account..lol
oh and kudos to whoever did those textures..the textures really make it..
let the frenzy begin
References
www.nvidia.com/object/cg_toolkit.html
developer.nvidia.com/object/hello_cg_tutorial.html
www.fusionindustries.com/content/lore/code/articles/fi-faq-cg.php3
nehe.gamedev.net/data/lessons/lesson.asp?lesson=47
www.3ddrome.com/articles/cgshaders.php
www.gamedev.net/reference/articles/article1903.asp
www.codesampler.com/oglsrc.htm
www.ronfrazier.net/apparition/index.asp?appmain=research/advanced_per_pixel_ligh...
#22
I think your site is getting slashdotted. I'm getting a "document does not contain data" error.
The image that does load looks great!
r/Alex
09/22/2004 (3:37 am)
@Eric.I think your site is getting slashdotted. I'm getting a "document does not contain data" error.
The image that does load looks great!
r/Alex
#23
09/26/2004 (11:29 am)
@Chris: Not true at all, all ATI cards which support vertex and fragment programs are supported by CG.
#24
09/26/2004 (8:09 pm)
I have tested this on both ATI and Nvidia and found that it is supported much better by the Nvidia cards. The ATI cards that work with it are 9500 and above. I have a 9600 xt that will do it but has a really bad flicker when you get close to the surface. So all in all Nvidia seems to support it much better but that makes sense seeing how CG was made for Nvidia in the first place.
#25
I do get some tearing across the textures, as you can see in the pic below. Any idea what is causing this?
10/04/2004 (10:59 pm)
Nice tutorial! I just got TGE today and was wondering how i could go about getting Cg into my game. This is definatley a start. I do get some tearing across the textures, as you can see in the pic below. Any idea what is causing this?
#26
Yah this seems to be an issue with ati cards..I don't have access to an ati video card so i can't debug this.. perhaps you can figure it out and i can update the tut...
10/08/2004 (12:30 pm)
@GeoffreyYah this seems to be an issue with ati cards..I don't have access to an ati video card so i can't debug this.. perhaps you can figure it out and i can update the tut...
#27
I'm having a hard time understanding your tutorial on how to add the bumpmapping. I have downloaded the CG Toolkit and your zip file and added the files from the zip. But what do I do with the CG files? Where do they go and how do I get them in the engine?
Thanks
Stephen
10/11/2004 (6:50 pm)
@KevinI'm having a hard time understanding your tutorial on how to add the bumpmapping. I have downloaded the CG Toolkit and your zip file and added the files from the zip. But what do I do with the CG files? Where do they go and how do I get them in the engine?
Thanks
Stephen
#28
You should just change the cg include file cgGL.h like so ( copied from above.. ):
First thing you gotta hack is the CG includes. Open cgGL.h
and change
#include
to
#include
Have you done this? If so make sure you copy over the cg compiler (cgc.exe ) to the directory that contains your shader. From there, if you have merged the other files ok, you should be good to go. Also, as a final note, make sure that your compiler is linking to the cg libs properly, i have done a few installs where the cg paths werent added to my ide.
@Kevin
Ill be digging into this stuff for real pretty soon. Ill let you know if i find out anything about the cg bug, or make any other progress.
10/18/2004 (11:34 pm)
@StephenYou should just change the cg include file cgGL.h like so ( copied from above.. ):
First thing you gotta hack is the CG includes. Open cgGL.h
and change
#include
to
#include
Have you done this? If so make sure you copy over the cg compiler (cgc.exe ) to the directory that contains your shader. From there, if you have merged the other files ok, you should be good to go. Also, as a final note, make sure that your compiler is linking to the cg libs properly, i have done a few installs where the cg paths werent added to my ide.
@Kevin
Ill be digging into this stuff for real pretty soon. Ill let you know if i find out anything about the cg bug, or make any other progress.
#29
10/25/2004 (5:41 am)
@Geoffrey - Kool deal.. let me know how you get along..
#30
I have Radeon9600Pro and I have the same problem like all ATI owner and CG user ;).
If you have the solution, can you please say it here?
PS: Sorry for my bad english.
EDIT: Oh. I deleted my stupid text.
10/25/2004 (11:58 am)
I did the tutorial, too. It is very nice, but it is hard for beginner. ;)I have Radeon9600Pro and I have the same problem like all ATI owner and CG user ;).
If you have the solution, can you please say it here?
PS: Sorry for my bad english.
EDIT: Oh. I deleted my stupid text.
#31
11/02/2004 (11:05 am)
Does anyone know if this will work on Mac OSX? Also, if I am reading this right, this would allow any shader you could code for it to funciton, meaning things like specular bloom would now be possible. Is that correct?
#32
-Stephen
11/02/2004 (4:52 pm)
I have also noticed that when you take a screenshot or change your resolution that the bumpmapping disappears. Anyone know whats happening?-Stephen
#33
I've got your mod up and running out of the box and the difference of atmosphere is amazing... :)
The next step, I thought, was to try to implement your code using Melv's fxrenderobject... and so I tried...
Since I'm coming from visual basic and my C++ learning curve is just at the beginning (obviuosly) I didn't succeded ..:(
I've tried also to follow Nehe's tutorial but with no luck.
I was wondering if - maybe - you have ever planned to release your resource using the fxrenderobject.
I think that this kind of approach at the problem may help us newbies to integrate in a simpler way other shaders as reflection or distortion in TGE.....
May you consider the idea ?
02/13/2005 (4:09 pm)
First, let me thank you for your extremely generous contribution.I've got your mod up and running out of the box and the difference of atmosphere is amazing... :)
The next step, I thought, was to try to implement your code using Melv's fxrenderobject... and so I tried...
Since I'm coming from visual basic and my C++ learning curve is just at the beginning (obviuosly) I didn't succeded ..:(
I've tried also to follow Nehe's tutorial but with no luck.
I was wondering if - maybe - you have ever planned to release your resource using the fxrenderobject.
I think that this kind of approach at the problem may help us newbies to integrate in a simpler way other shaders as reflection or distortion in TGE.....
May you consider the idea ?
#34
if you don't want the shader to darken your material, you can change the shader .cg file,
float3 lightVectorFinal = 2.0 * (lightVector.rgb - 0.5);
float3 bumpNormalVectorFinal = 2.0 * (tex2D(bumpTexture, bumpCoords).rgb - 0.5);
in these lines you can adjust 2.0 to get the brightness you want, about 4.0 is normal brightness
----------------------------
and another thing i would like to they for those, you were confused too why "normal" bumpmaps like for the TGE terrain do not work, the bumpmap can be generated with a tool from ATI, you can find it here: http://www.ati.com/developer/sdk/radeonSDK/html/Tools/ToolsPlugIns.html
06/01/2005 (1:44 pm)
>>* running interiors through a normal map really darkens em upif you don't want the shader to darken your material, you can change the shader .cg file,
float3 lightVectorFinal = 2.0 * (lightVector.rgb - 0.5);
float3 bumpNormalVectorFinal = 2.0 * (tex2D(bumpTexture, bumpCoords).rgb - 0.5);
in these lines you can adjust 2.0 to get the brightness you want, about 4.0 is normal brightness
----------------------------
and another thing i would like to they for those, you were confused too why "normal" bumpmaps like for the TGE terrain do not work, the bumpmap can be generated with a tool from ATI, you can find it here: http://www.ati.com/developer/sdk/radeonSDK/html/Tools/ToolsPlugIns.html
#35
Does anyone have a link or the zip file? Please email it to me at altair_john@yahoo.com
Thanks a lot!
09/17/2005 (3:31 pm)
Hello. The link to the Normal maps doesn't work anymore. :'(Does anyone have a link or the zip file? Please email it to me at altair_john@yahoo.com
Thanks a lot!
#36
I tried and made the normal maps and put it in the interiors directory (all 10 of them, right?). they are in (.tga) Targa format, right?
but the problem i had when i loaded the game was that the interiors (and the oak) are still black.
what could i be doing wrong? hmmm.
i'm using an ASUS GeForce FX 5700 256mb
I got the following in my console log:
CG:callback set
CG:interior initShader started
CG: found frag profile...
CG:interior InitShader() finished
0
regarding the propertymap.cs entries e.g. ...
addMaterialMapping("stonewall", "normal: starter.fps/data/interiors/stonewallDOT3 0.5");
what does this function mean in detail? what does the 0.5 stand for? shouldn't stonewallDOT3 be stonewallDOT3.tga? well i tried both but the interiors in the game are still black. hmmm.
Any info might help. Thanks!
09/17/2005 (5:34 pm)
Hi Kev, thanks!. I tried and made the normal maps and put it in the interiors directory (all 10 of them, right?). they are in (.tga) Targa format, right?
but the problem i had when i loaded the game was that the interiors (and the oak) are still black.
what could i be doing wrong? hmmm.
i'm using an ASUS GeForce FX 5700 256mb
I got the following in my console log:
CG:callback set
CG:interior initShader started
CG: found frag profile...
CG:interior InitShader() finished
0
regarding the propertymap.cs entries e.g. ...
addMaterialMapping("stonewall", "normal: starter.fps/data/interiors/stonewallDOT3 0.5");
what does this function mean in detail? what does the 0.5 stand for? shouldn't stonewallDOT3 be stonewallDOT3.tga? well i tried both but the interiors in the game are still black. hmmm.
Any info might help. Thanks!
#37
I converted the .jpg to .tga first and then used the ATI to convert to a DOT 3.
I got it wrong the first time coz I used the compressed version of .tga as an output so my DOT3 was messed up. So I used the uncompress version of tga and now my DOT3 is ok.
The only problem i have is that the shaded materials are still in black.
I tried all sorts of stuff but to no avail so I figured what if i try something simple like a "red" shader. So I replaced the cg shader and it worked so I'm sure Cg has no problem doing any shading.
This leaves me to the Cg program itself. It seems that the parameters are not set. The lighting parameter is commented out. Any particular reason for this? By the way, there are no cgGLSetParam... for the other variables in the shader, why is that?
Any info would be great thanks!
09/18/2005 (12:18 am)
Ok found out something interesting.I converted the .jpg to .tga first and then used the ATI to convert to a DOT 3.
I got it wrong the first time coz I used the compressed version of .tga as an output so my DOT3 was messed up. So I used the uncompress version of tga and now my DOT3 is ok.
The only problem i have is that the shaded materials are still in black.
I tried all sorts of stuff but to no avail so I figured what if i try something simple like a "red" shader. So I replaced the cg shader and it worked so I'm sure Cg has no problem doing any shading.
This leaves me to the Cg program itself. It seems that the parameters are not set. The lighting parameter is commented out. Any particular reason for this? By the way, there are no cgGLSetParam... for the other variables in the shader, why is that?
Any info would be great thanks!
#38
I'm not sure if you are still getting notifications of posts here, but you had the same problem I'm having right now, the purple overlay thing. Everything compiles fine, I get the right console log CG info, it's just there is no blending of the texture with the DOT3 image, in fact the DOT3 image appears to be a different scale compared to the texture image(?!?).
When you say you fixed it by "using the proper rendering loop", what exactly do you mean? Pardon my ignorance, but I'm not really that familiar with C++, CG and Visual Studio, I can just do enough to get by, so if you are still monitoring this thread, or anyone for that matter, I'd appreciate any input.
- Alan
Really Really Good Things Studio
04/23/2006 (9:03 pm)
@Erik& and/or Anyone who might knowI'm not sure if you are still getting notifications of posts here, but you had the same problem I'm having right now, the purple overlay thing. Everything compiles fine, I get the right console log CG info, it's just there is no blending of the texture with the DOT3 image, in fact the DOT3 image appears to be a different scale compared to the texture image(?!?).
When you say you fixed it by "using the proper rendering loop", what exactly do you mean? Pardon my ignorance, but I'm not really that familiar with C++, CG and Visual Studio, I can just do enough to get by, so if you are still monitoring this thread, or anyone for that matter, I'd appreciate any input.
- Alan
Really Really Good Things Studio
#39
04/25/2006 (7:32 am)
The only problem having is set up the cg with the compiler for its use and Verify everything works. I am using VC6 because I know that one but I am no programmer. I know enough get me by, most of time I just get luckie.
#40
I have got the errors down to 8. Does any one have idea what these remaining errors is coming from. Thank you for your help.
engine\interior\interior.cc(19) : fatal error C1083: Cannot open include file: 'sim/frameAllocator.h': No such file or directory
interiorCollision.cc
engine\interior\interiorCollision.cc(795) : error C2065: 'getFlippedPlane' : undeclared identifier
engine\interior\interiorCollision.cc(963) : error C2440: 'initializing' : cannot convert from 'int' to 'class PlaneF'
No constructor could take the source type, or constructor overload resolution was ambiguous
engine\interior\interiorInstance.cc(28) : fatal error C1083: Cannot open include file: 'sim/frameAllocator.h': No such file or directory
interiorIO.cc
engine\interior\interiorRender.cc(52) : error C2146: syntax error : missing ';' before identifier 'cgContexInterior'
engine\interior\interiorRender.cc(52) : error C2501: 'cgContext' : missing storage-class or type specifiers
engine\interior\interiorRender.cc(52) : fatal error C1004: unexpected end of file found
interiorRes.cc
engine\sim\sceneObject.cc(1962) : error C2039: 'getFlippedPlane' : is not a member of 'Interior'
../engine\interior/interior.h(151) : see declaration of 'Interior'
05/03/2006 (10:36 am)
I final got the cg file where they where need. I have got the errors down to 8. Does any one have idea what these remaining errors is coming from. Thank you for your help.
engine\interior\interior.cc(19) : fatal error C1083: Cannot open include file: 'sim/frameAllocator.h': No such file or directory
interiorCollision.cc
engine\interior\interiorCollision.cc(795) : error C2065: 'getFlippedPlane' : undeclared identifier
engine\interior\interiorCollision.cc(963) : error C2440: 'initializing' : cannot convert from 'int' to 'class PlaneF'
No constructor could take the source type, or constructor overload resolution was ambiguous
engine\interior\interiorInstance.cc(28) : fatal error C1083: Cannot open include file: 'sim/frameAllocator.h': No such file or directory
interiorIO.cc
engine\interior\interiorRender.cc(52) : error C2146: syntax error : missing ';' before identifier 'cgContexInterior'
engine\interior\interiorRender.cc(52) : error C2501: 'cgContext' : missing storage-class or type specifiers
engine\interior\interiorRender.cc(52) : fatal error C1004: unexpected end of file found
interiorRes.cc
engine\sim\sceneObject.cc(1962) : error C2039: 'getFlippedPlane' : is not a member of 'Interior'
../engine\interior/interior.h(151) : see declaration of 'Interior'
Torque Owner Alfredo Perez