Game Development Community

Ambient Occlusion Shader (bent-normal)

by PuG · in Torque Game Engine Advanced · 02/27/2008 (12:02 am) · 9 replies

Hi, right well ive been looking into AO for TGEA quiet a bit over the last few months, and found a few alternatives apart from supporting a second UV channel (for pre-bake).

I came across as site which had a guide for an OpenGL equivalent, I emailed the guy and he kindly ported it to HLSL as best as he could. What you do is run the object through his app (or similar) and it produces an .xml file of the bent-normal for each vertex.

The original website - www.ozone3d.net/tutorials/ambient_occlusion.php


..."This GLSL shader can be quite easily ported to DX HLSL. In this shader I used a vertex attribute to store AO values but this not mandatory. Just store AO values (bent-normal and and occlusion term) in the color attribute of the vertex. Simple and efficient!

Here is the new GLSL shader:"

GLSL Vertex Shader:
void main(void)
 
{
	gl_Position = ftransform();
	gl_FrontColor = gl_Color;
}
 
 
GLSL Pixel Shader:
void main (void)
 
{
	vec4 vAmbient = vec4(0.9, 0.9, 0.9, 1.0);
	vAmbient.rgb *= gl_Color.w; // Modulate with occlusion term.
	gl_FragColor = vAmbient; }



And here is the HLSL equivalent:

float4x4 worldViewProj : WORLDVIEWPROJ; //our world view projection matrix
 
//application to vertex structure
struct a2v
{
   float4 position : POSITION0;
   float4 color : COLOR0;
};
 
//vertex to pixel shader structure
struct v2p
{
   float4 position : POSITION0;
   float4 color : COLOR0;
};
 
//pixel shader to screen
struct p2f
{
   float4 color : COLOR0;
};
 
//VERTEX SHADER
void vs( in a2v IN, out v2p OUT )
{
   //getting to position to object space
   OUT.position = mul(IN.position, worldViewProj);
   OUT.color = IN.color;
}
 
//VERTEX SHADER
void ps( in v2p IN, out p2f OUT )
{
   //output some color
 
   float 4 vAmbient = float4(0.9, 0.9, 0.9, 1.0);
   vAmbient.rgb *= IN.color.a; // Modulate with occlusion term.
    OUT.color = vAmbient;
 
}

Ive had a shot myself of getting into TGEA, fixed a few syntax errors but to be honest don't know what im doing with it.

What I assume is you can just run in the per-vertex file through an object definition or something? assuming it can work in TGEA at all.

So, is their a chance anyone here could have ago themselfs? im sure it would make allot of people happy, and with ODE in the new TGEA then pollysoup buildings will be almost their.

Best Regards,

#1
02/27/2008 (6:13 am)
The HLSL syntax is probably all correct for the most part, but you do need to start by separating the vertex and pixel information into separate HLSL files;)

Then you can look at the constant definitions to see which ones to use....hmmm Im better at futzing with something completed and turning it into something else...perhaps someone else will chime in:)
#2
02/27/2008 (11:40 am)
I think it's useless to port that shader - it obviosly does a simple gouraud diffuse mapping.
You have that mapping information ready when exporting the model, the material automatically do a procedural one. You even do not need a shader for that.
#3
02/27/2008 (11:46 am)
Im not sure I fully understand what you mean Picasso?
#4
02/27/2008 (11:50 am)
Look at the shader, look at its diffuse modulation - it is a simple filter.
I can port it for you, but it is useless.
You can use already filtered diffuse map, that needless only load up your gpu.
It is a very bad practice to do that.
#5
02/27/2008 (11:54 am)
Right, as I said, no good at HLSL :) - so how could you do static AO then? I assume it still has to be bent-vertex as theirs no second UV channel without engine adjustments?
#6
02/27/2008 (12:04 pm)
If you want to do an ambient occlusion in TGEA - it is impossible now.
You just can not take ambient information from other ambient products.
You can only have the environment ambient or create a imitation with a constant for example.
There also was pointed a question about specular occlusion - it is also impossible. Shadergen needs a serious update to do that.

I also think that the hlsl shader above has nothing to do with an ambient occlusion.
#7
02/27/2008 (12:16 pm)
All it does is adjust the diffuse on certain areas of the models to make it darker, I don't see how thats impossible todo in TGEA, considering its similar to what the Lightmap does to interiors accept pre-baked clips?
#8
02/28/2008 (12:05 am)
OK my friend.

Ambient occlusion is a method for ambient modulation, not diffuse.
In the shader i do not see where is used the ambient light.
The shader just modulate the diffuse light and it's not an ambient occlusion.
It is just a diffuse mapping. And nothing else.
#9
02/28/2008 (12:12 am)
Right, ok - I think I can see what your poking at 8-(

Quote:The AO technique (AO for Ambient Occlusion) is mainly focused on the ambient term of the lighting equation used in real time calculations. Here is the equation:
If = Ia + Id + Is

where If is the intensity of the pixel final color, Ia the intensity of the ambient color, Id the intensity of the diffuse color and Is that of the specular color. For more details regarding the calculation of the Id and Is terms, please refer to the Bump Mapping . tutorial.

A bit of a disappointment.