[Cg] Environment Maps

The place for artists, modellers, level designers et al to discuss their approaches for creating content for OGRE.
Post Reply
hpesoj
Halfling
Posts: 99
Joined: Fri Dec 07, 2007 8:47 pm
Location: Bristol, UK

[Cg] Environment Maps

Post by hpesoj »

Sorry, another probably simple question regarding shaders (and materials).

I have a very simple Cg shader, which is supposed to apply a sphere map to an object:

Code: Select all

void main_vs(float4 position : POSITION,
             float3 uvCoord : TEXCOORD0,
             float3 uvCoordRef : TEXCOORD1,
             out float4 oPosition : POSITION,
             out float3 oUVCoord : TEXCOORD0,
             out float3 oUVCoordRef : TEXCOORD1,
             const uniform float4x4 wvpMat)
{
    oPosition = mul(wvpMat, position);
    oUVCoord = uvCoord;
    oUVCoordRef = uvCoordRef;
}

void main_fs(float3 uvCoord : TEXCOORD0,
             float3 uvCoordRef : TEXCOORD1,
             out float4 oColour : COLOR0,
             const uniform sampler2D diffuseMap : register(s0),
             const uniform sampler2D refMap : register(s1))
{
    float4 cubeElem = tex2D(refMap, uvCoordRef);
    oColour = cubeElem;
}
The first texture is redundant at the moment due to my testing.

Here is the corresponding pass in the material file:

Code: Select all

        // Texture pass
        pass
        {
            vertex_program_ref vs_texture
            {
            }
            
            fragment_program_ref fs_texture
            {
            }

            texture_unit
            {
                texture white.bmp
            }

            texture_unit
            {
                texture smap_bar1.png
                env_map spherical
            }
        }
And the definition of the shaders:

Code: Select all

vertex_program vs_texture cg
{
    source texture.cg
    entry_point main_vs
    profiles vs_2_0 arbvp1
    
    default_params
    {
        param_named_auto wvpMat worldviewproj_matrix
    }
}

fragment_program fs_texture cg
{
    source texture.cg
    entry_point main_fs
    profiles ps_2_0 arbfp1
}
Using this setup results in a completely black object. However, when I comment out the shader program references in the material file, I end up with a lovely sphere mapped effect (but that's no use to me!) Also, when I change uvCoordRef to reference TEXCOORD0, the sphere map texture renders nicely as a regular texture onto the object. I can thus only presume that the sphere map coordinates aren't being passed/generated correctly (this is assuming that env_map spherical is meant to automatically generate the env map coords and stick them in the first free TEXCOORD slot?)

I should also mention that I have tried to apply a cube map, with similar results.

I have searched and searched for hours through the manual, wiki, forum, example programs, and google, and have found nothing. I am posting this to make sure there is nothing obvious that I am doing wrong, or any steps that I have to take, that I am foolishly missing out (I sincerely hope this is the case).

Thanks in advance,

Joe
Image
User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 11

Post by nullsquared »

The evn_map is for the fixed function pipeline, only. It doesn't *not* calculate texture coordinates for your shader. You have to manually take in the camera position and calculate the texture coordinates based on the reflection vector(s). I'm talking out of cube mapping experience, but much the same applies to spherical maps. Check google to see if you can find some shader code (spherical maps seem a little obsolete, but there's loads of info on cube maps).
hpesoj
Halfling
Posts: 99
Joined: Fri Dec 07, 2007 8:47 pm
Location: Bristol, UK

Post by hpesoj »

Thanks for clearing that up Null.
Image
tbaldree
Kobold
Posts: 29
Joined: Wed Dec 10, 2008 7:29 am
x 1

Re: [Cg] Environment Maps

Post by tbaldree »

This seems to be the same question I had, and it sounds like there are no automatically generated coordinates for this circumstance. Out of curiosity, did anyone dig up the code for generating the cubemap coords?
User avatar
toglia
Gnome
Posts: 336
Joined: Sat Dec 08, 2007 4:28 am
Location: Canada
x 7

Re: [Cg] Environment Maps

Post by toglia »

I know this in an old post but I was looking for this too. Found the solution.

It is necessary to sample the spheremap texture with the normal in view space like this:

Code: Select all

float4 reflectedColor = tex2D(SphereMap, float2(0.5,-0.5) * N_ViewSpace.xy + float2(0.5, 0.5));
To get the normal in view space:

Code: Select all

Normal_ViewSpace = mul((float3x3)worldViewMatrix,normal);
Normally you do this in the vertex shader and pass it to the fragment (pixel) shader in a textcoord.

Its important to normalize in the fragment since we didn't do it in the vertex shader. Doing it in the vertex is lighter but the fragment gives smoother results.
Post Reply