• Advertisement
Sign in to follow this  

Lightmap fx file

This topic is 3840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm currently using C# in conjunction with the DirectX API, I need to be able to apply a lightmap texture on top of a normal texture for an object.. I was originally using:
dev.SetTexture(0, MainTexture);
dev.SetTexture(1, ltmpTexture);
But now I am switching to the programmable pipeline, and whilst i do have a working effect in place, it does not render lightmaps. Heres what I've got at the moment:
//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4 g_MaterialAmbientColor = float4(1,1,1,1);      // Material's ambient color
float4 g_MaterialDiffuseColor = float4(1,1,1,1);      // Material's diffuse color
float3 g_LightDir = float3(0.1, -1.0, 0.1);			  // Light's direction in world space
float4 g_LightDiffuse = float4(1,1,1,1);              // Light's diffuse color
texture g_MeshTexture;              // Color texture for mesh

float    g_fTime;                   // App's time in seconds
float4x4 g_mWorld;                  // World matrix for object
float4x4 g_mWorldViewProjection;    // World * View * Projection matrix



//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler MeshTextureSampler = 
sampler_state
{
    Texture = <g_MeshTexture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};


//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Position   : POSITION;   // vertex position 
    float4 Diffuse    : COLOR0;     // vertex diffuse color (note that COLOR0 is clamped from 0..1)
    float2 TextureUV  : TEXCOORD0;  // vertex texture coords 
};


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0 )
{
    VS_OUTPUT Output;
    float3 vNormalWorldSpace;
    
    // Transform the position from object space to homogeneous projection space
    Output.Position = mul(vPos, g_mWorldViewProjection);
    
    // Transform the normal from object space to world space    
    vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld)); // normal (world space)

    // Calc diffuse color    
    Output.Diffuse.rgb = g_MaterialDiffuseColor * g_LightDiffuse * max(0,dot(vNormalWorldSpace, g_LightDir)) + 
                         g_MaterialAmbientColor;   
    Output.Diffuse.a = 1.0f; 
    
    // Just copy the texture coordinate through
    Output.TextureUV = vTexCoord0; 
    
    return Output;    
}


//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};


//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In ) 
{ 
    PS_OUTPUT Output;

    // Lookup mesh texture and modulate it with diffuse
    Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;

    return Output;
}


//--------------------------------------------------------------------------------------
// Renders scene 
//--------------------------------------------------------------------------------------
technique RenderScene
{
    pass P0
    {          
        VertexShader = compile vs_1_1 RenderSceneVS();
        PixelShader  = compile ps_1_1 RenderScenePS(); 
    }
}
So basically, if someone could expand upon this to support lightmaps, that would be most appreciated :) [Edited by - Prey074 on July 14, 2007 3:27:10 PM]

Share this post


Link to post
Share on other sites
Advertisement
hmm Try this:

LightmapSampler = a texturemap with grayscale values like this one below:

grayscale image

//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4 g_MaterialAmbientColor = float4(1,1,1,1); // Material's ambient color
float4 g_MaterialDiffuseColor = float4(1,1,1,1); // Material's diffuse color
float3 g_LightDir = float3(0.1, -1.0, 0.1); // Light's direction in world space
float4 g_LightDiffuse = float4(1,1,1,1); // Light's diffuse color
texture g_MeshTexture; // Color texture for mesh
texture g_LightTexture;

float g_fTime : Time; // App's time in seconds
float4x4 g_mWorld : World; // World matrix for object
float4x4 g_mWorldViewProjection : worldviewprojection; // World * View * Projection matrix



//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler MeshTextureSampler =
sampler_state
{
Texture = <g_MeshTexture>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

sampler LightmapSampler =
sampler_state
{
Texture = <g_LightTexture>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
float4 Position : POSITION; // vertex position
float4 Diffuse : COLOR0; // vertex diffuse color (note that COLOR0 is clamped from 0..1)
float2 TextureUV : TEXCOORD0; // vertex texture coords
float2 TextureLightmap : TEXCOORD1;
};


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION,
float3 vNormal : NORMAL,
float2 vTexCoord0 : TEXCOORD0,
float2 vTextureLightmap : TEXCOORD1 )
{
VS_OUTPUT Output;
float3 vNormalWorldSpace;

// Transform the position from object space to homogeneous projection space
Output.Position = mul(vPos, g_mWorldViewProjection);

// Transform the normal from object space to world space
vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld)); // normal (world space)

// Calc diffuse color
Output.Diffuse.rgb = g_MaterialDiffuseColor * g_LightDiffuse * max(0,dot(vNormalWorldSpace, g_LightDir)) +
g_MaterialAmbientColor;
Output.Diffuse.a = 1.0f;

// Just copy the texture coordinate through
Output.TextureUV = vTexCoord0;
Output.TextureLightmap = vTextureLightmap;
return Output;
}


//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
//struct PS_OUTPUT
//{
// float4 RGBColor : COLOR0; // Pixel color
//};


//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
float4 RenderScenePS( VS_OUTPUT In ): COLOR0
{
//PS_OUTPUT Output;

// Lookup mesh texture and modulate it with diffuse
float4 MeshTex = tex2D(MeshTextureSampler , In.TextureUV);
float4 LightMapTex = tex2D(LightmapSampler , In.TextureLightmap);
return In.Diffuse * MeshTex * LightMapTex;

//return Output;
}


//--------------------------------------------------------------------------------------
// Renders scene
//--------------------------------------------------------------------------------------
technique RenderScene
{
pass P0
{
VertexShader = compile vs_1_1 RenderSceneVS();
PixelShader = compile ps_1_1 RenderScenePS();
}
}



Share this post


Link to post
Share on other sites
Hmm, it seemed to mess up slightly in that i got some black squares dotted around the place. I've also just realized the original effect isn't even working for me anyway >_>

For now, i would like to go back to my original method above (dev.SetTexture(...)). But the thing is if i comment out the effect begin/end() etc then nothing is rendered at all. I am using the empty project found in the DirectX SDK, I was thinking they must have turned something off or..? Anyone have any info on this?

[Edited by - Prey074 on July 19, 2007 11:19:00 AM]

Share this post


Link to post
Share on other sites
Ah I realize now that you can't set parameters once .BeginPass() has been called, so I did get mine and yours effects working again, still getting those black patches though :-/

I've reverted back to just using the fixed pipeline for now, I found what my error was with nothing being rendered; It seemed the devices view and projection matrices were never being set :P

So, can you shed any light on the black patches I seem to be getting?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement