Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Lighting - No lighting appearing - sampleFramework (CustomUI)

0 views
Skip to first unread message

Moo Mansun

unread,
Oct 4, 2005, 6:51:59 PM10/4/05
to
I have been looking at the DX9 sampleFramework and decided to add a
directional light. The code works perfect, however, no light actually
appears.

The Light.Count accuratly reports the correct number of lights during run
time and yes, RenderState.Lighting is set to true. A material is also setup.


The odd thing is that Microsoft's framework does not mention lighting, yet,
loaded models have an ambient light.

I am assuming that the framework is rendering the basic colors of the
vertices and mimicing a RenderState.Lighting = false scenario.

How do I change this, so that I can view my lights?

For reference use CustomUI in the DX9c SDK.


Quote for Today: 'At least I know my a** from a VertexBuffer.'

Zman

unread,
Oct 4, 2005, 7:57:27 PM10/4/05
to
Could be lots of things.

Is your light pointing at the model?
Are you looking at the right side of your model?
Did you set a drop off distance for the light and then put it too far away.

Lights do work with the sample framework. Try the initial state of my
Coding4Fun article from here
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=89812117-2b35-44b9-80bd-4b1e09909b60
It has a mesh.box, some ambient and (I think) a single directional light.

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message
news:058db9b869e94e70...@ureader.com...

Moo Mansun

unread,
Oct 4, 2005, 8:57:39 PM10/4/05
to
Thanks for the link. Everything you refer to is perfect.

Here is the scenario:

If you start up Microsoft's default CustomUI, we can start from there.

1. The scene is lit, although no ambient light source is set.

Is it processing the vertex colors only? (i.e. the same as renderstate.
lighting = false)

2. How do I modify it so that the ambient light disappears and the scene
reacts to my lights?

3. Do I need to write additional code for processing lighting calculations?


The mesh is processed from dxmutmesh.cs and the renderstates set from
dxmutgui.cs.

Remember I am creating an instance of FrameworkMesh as the loading mechanism.
So I would reference something like mesh.localmesh or mesh.
systemmemorymesh.

I examined your code and DumpInfo() looks very close, but I think this is
done by the FrameworkMesh in dxmutmesh.cs.

Perhaps the best way to approach this is to create some form of checklist
covering the pipeline, a DX Pipeline Overview from the perspective of a 3D
Engine.

It would certainly assist not only newcomers but serve as an everyday
practical tool for developers. Written in plain english, with samples for
each aspect of the pipeline, then we wouldn't have the scenario of people
not knowing of certain commands or basic/full steps to implement a feature.
By insisting on classes, in a seperate file, for samples, we could make the
whole thing drag and drop essentially.


Thank Z.

Zman

unread,
Oct 5, 2005, 12:46:51 AM10/5/05
to
Again, I refer you to the sample code from my column. I start from the
EmptyProject (which uses the framework) and adds barely 20 lines of code to
do exactly what you want. use windiff to identify them.

.localmesh and .systemmesh are just Mesh objects the same as I create .box
in the sample. dumpmesh() is purely a helper function to read vertices so
I'm not sure why you think that is helpful.

What you are asking for is enough to fill a book. Maybe one day I will
consider writing one, but for now I can jsut suggest you start with Tom's
book and go from there.

It is indeed unfortunate that there are no docs for the dxframework but
thats the way it is. 3d programming is a tough beast to crack, you have to
just keep digging in until bits of start to make sense.

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:56d6ade292ab40f0...@ureader.com...

Moo Mansun

unread,
Oct 5, 2005, 2:42:50 AM10/5/05
to
Z,

I'm asking where the light is coming from in that sample?

Not really books worth...

Your sample bypasses the frameworkMesh and most and is basically a modified
version of the teapot example.

Any straight answers?

Zman

unread,
Oct 5, 2005, 1:22:48 PM10/5/05
to
You are correct SampleUI has no lighting enabled - this is becuase it does
not use the fixed function pipeline to draw the scene behind the controls it
uses a shader program.

void VertScene( float4 Pos : POSITION,float2 Tex : TEXCOORD0,out float4 oPos
: POSITION,out float2 oTex : TEXCOORD0 )
{
oPos = mul( Pos, worldViewProjection );
oTex = Tex;
}
float4 PixScene( float2 Tex : TEXCOORD0 ) : COLOR0
{
return tex2D( g_samScene, Tex );
}

Basically this shader just says the color of the final pixel is the color of
the texture. Its the same as having white ambient lighting. If you were to
render your mesh inside the same shader, and it has texture coordinates then
you should see the same thng. If you have no texture coordinates then you
will see black since nothing but the texture is taken into account.
To draw objects using the fixed function then set up your lighting (as per
my sample, there really is no difference between how you set up the lights
for a Mesh or a .frameworkMesh) and make sure your draw calls are NOT inside
the effect.Begin, effect.End block of code, but still inside the
.BeginScene, .EndSceneBlock.

The following would be the 'book' I mentioned......


>Perhaps the best way to approach this is to create some form of checklist
>covering the pipeline, a DX Pipeline Overview from the perspective of a 3D
>Engine. It would certainly assist not only newcomers but serve as an
>everyday
>practical tool for developers. Written in plain english, with samples for
>each aspect of the pipeline, then we wouldn't have the scenario of people
>not knowing of certain commands or basic/full steps to implement a feature.

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:cb0129ea974f44bc...@ureader.com...

Moo Mansun

unread,
Oct 5, 2005, 2:56:04 PM10/5/05
to
Thanks Zman, I have been all over the web trying find that. My HLSL is not
what it should be.

Basically, the shader is just outputing the normal texture colors to a
specific co-ordinate. If I have this right, the sample application passes
the texture to the .fx file, which in turn, processes the texture directly
in the GPU. By then calling DrawSubset(i) we then draw that modified
texture.

I can now see the benefits of HLSL, very fast.

Two questions now arise:

1. How do we modify the code (see below) to remove the ambient lighting
effect?
2. How do we modify the code to account for the effects of a single light?
(I'd like to do this in HLSL, speed)

The code of CustomUI.fx

void VertScene( float4 Pos : POSITION,float2 Tex : TEXCOORD0,out float4 oPos

: POSITION,out float2 oTex : TEXCOORD0 )
{
oPos = mul( Pos, worldViewProjection );
oTex = Tex;
}
float4 PixScene( float2 Tex : TEXCOORD0 ) : COLOR0
{
return tex2D( g_samScene, Tex );
}

Once I get my head around manipulation texture color I should be alright.

Thanks again Zman.

Zman

unread,
Oct 5, 2005, 4:19:15 PM10/5/05
to
Using shaders may give you some perf increase but you have to to EVERYTHING
yourself. So you need to get a book or find some articles on the lighting
equations. Basically a directional light is a simple dot product of the
light vector with the normal of the vertex and can be done in the vertex
shader. Don't forget to take into account the light and material color and
add in any texture colors. Its really not something you can cover in a forum
reply like this.

Pieter has some HLSL tutorials here which includes a basic diffuse
directional light http://www.pieterg.com/Tutorials.php

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:8bee39dd5ebb4bf7...@ureader.com...

Moo Mansun

unread,
Oct 5, 2005, 6:22:59 PM10/5/05
to
Let me get this straight, from my app I output the light vector and the
vertex normal.

The light vector is simple enough. How do I get the normal?

I found this code snippit, however, it does not support textures. I am
using tex2D in the pixel shader, so my question is how do I integrate this
into the return?


float4x4 matWorldViewProj;
float4x4 matWorld;
float4 vecLightDir;
struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 Light : TEXCOORD0;
float3 Norm : TEXCOORD1;
};

VS_OUTPUT VS(float4 Pos : POSITION, float3 Normal : NORMAL)
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Pos = mul(Pos, matWorldViewProj); // transform Position
Out.Light = vecLightDir; // output light vector
Out.Norm = normalize(mul(Normal, matWorld)); // transform Normal
and normalize it
return Out;
}

float4 PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1) : COLOR
{

float4 diffuse = { 1.0f, 0.0f, 0.0f, 1.0f};
float4 ambient = {0.1, 0.0, 0.0, 1.0};
return ambient + diffuse * saturate(dot(Light, Norm));
}

Cheers Zman.

Moo Mansun

unread,
Oct 5, 2005, 8:21:03 PM10/5/05
to
I have had some success, in that I am able now to alter the brightness and I
can see how to integrate the above code.

I have two questions:

1. How do you output a normal?
2. Is the float4 returned by tex2d() a rgba value?
3. What range should those values fall within?

Thanks Zman, I should be able to implement point lights after this.

Zman

unread,
Oct 5, 2005, 11:45:04 PM10/5/05
to
Its a component of the vertices. It either comes from your modelling package
or you can calculate them with Mesh.ComputeNormals. (after cloning of course
to convert the vertex format to be big enough for normals)

To be honest, if you don't know what normals are you really are not going to
get very far into shaders. I would suggest getting some 3d throry books and
get the fundamentals down or go back to the fixed function pipeline for now.

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:53162d2f0de64d26...@ureader.com...

Zman

unread,
Oct 5, 2005, 11:46:01 PM10/5/05
to
1. See other post
2. Yes
3. 0.0-1.0

I assume you mean directional lights. Point lights is a whole other story.

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:099cce9d13c94edd...@ureader.com...

Moo Mansun

unread,
Oct 6, 2005, 12:13:44 AM10/6/05
to
Sorry, after each post I realised I was not reading the fx file correctly.

I have had a go at implementing that algorythm into the CustomUI sample. I
have just used a static vector for the Light.

Is the light vector static anyway, or is there computation required to
obtain that value?

I only ask because this implementation provides a 'natural' form of ambient
light and the texture look richer.

If the vector between the light source and the material had to be calculated,
would this change the outcome?


//--------------------------------------------------------------------------------------
// File: Directional_Light.fx
//
// A Directional Light HLSL file for the CustomUI sample.
// Implementing HW T&L.
//
// Copyright (c 2005) Moo Mansun. All rights reserved.
//--------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4x4 worldMatrix; // World matrix for object
float4x4 worldViewProjection; // World * View * Projection matrix
texture sceneTexture;


//Sampler state for texture lookup
sampler g_samScene =
sampler_state
{
Texture = <sceneTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

//Return variables for VertScene


struct VS_OUTPUT
{
float4 Pos : POSITION;

float3 Light : TEXCOORD2;
float3 Norm : TEXCOORD0;
float2 Tex : TEXCOORD1;
};


//Vertex Shader Routine
VS_OUTPUT VertScene( float4 Pos : POSITION, float2 Tex : TEXCOORD0, float3
Normal : Normal )
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Pos = mul( Pos, worldViewProjection );
Out.Light = (-0.78, -0.68, 0.16, 10.0); //output light vector
Out.Norm = normalize(mul(Normal, worldMatrix));
Out.Tex = Tex;

return Out;
}


//Pixel Shader Routine
float4 PixScene( float2 Tex : TEXCOORD1, float3 Light: TEXCOORD0, float3
Norm : TEXCOORD2 ) : COLOR
{
float4 tex = tex2D( g_samScene, Tex );
float4 ambient = (0.0, 0.0, 0.0, -0.02);
float4 diffuse = (1.0, 0.0, 0.0, 0.04);

return tex + ambient + diffuse * saturate(dot(Light, Norm));
}


//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------
technique RenderScene
{
pass P0
{
VertexShader = compile vs_1_1 VertScene();
PixelShader = compile ps_1_1 PixScene();
}
}

//Cheers Zman...

Moo Mansun

unread,
Oct 6, 2005, 1:15:55 AM10/6/05
to
Actually, that code works, I just had to reposition the light and export my
map without the normals flipped.

Sweet, directional light and perfect too.

I have one question:

The float4 structure,

float4 ambient = (0.0, 0.0, 0.0, -0.02);

What are each of the params, are the rgba?

Moo Mansun

unread,
Oct 6, 2005, 1:47:59 AM10/6/05
to
Sorry, didn't see that you had answered that question. Here is the
corrected version.

Just replace CustomUI.fx with this code for directional light effect.

Next up, volume shadows...


//--------------------------------------------------------------------------------------
// File: Directional_Light.fx
//
// A Directional Light HLSL file for the CustomUI sample.
// Implementing HW T&L.
//
// Copyright (c 2005) Moo Mansun. All rights reserved.

// Big thanks to Zman.
//--------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4x4 worldMatrix; // World matrix for object
float4x4 worldViewProjection; // World * View * Projection matrix
texture sceneTexture;


//Sampler state for texture lookup
sampler g_samScene =
sampler_state
{
Texture = <sceneTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

//Return variables for VertScene
struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 Light : TEXCOORD2;

float3 Norm : TEXCOORD1;
float2 Tex : TEXCOORD0;
};


//Vertex Shader Routine
VS_OUTPUT VertScene( float4 Pos : POSITION, float2 Tex : TEXCOORD0, float3
Normal : Normal )
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Pos = mul( Pos, worldViewProjection );

Out.Light = (1.0, 1.0, 1.0, 1.0); //output light vector


Out.Norm = normalize(mul(Normal, worldMatrix));
Out.Tex = Tex;

return Out;
}


//Pixel Shader Routine
float4 PixScene( float2 Tex : TEXCOORD0, float3 Light: TEXCOORD2, float3
Norm : TEXCOORD1 ) : COLOR


{
float4 tex = tex2D( g_samScene, Tex );

float4 ambient = (0.0, 0.0, 0.0, -0.02);

float4 diffuse = (1.0, 1.0, 1.0, 0.1);

Moo Mansun

unread,
Oct 6, 2005, 2:27:03 AM10/6/05
to
Experimentation produced this implementation which perfects the shading.

GeForce FX 5200+ recommended.

//--------------------------------------------------------------------------------------
// File: Directional_Light.fx v1.0
//
// A Directional Light vertex shader HLSL file for the
// CustomUI sample.
// Implementing Real-Time Vertex Shading - HW T&L.

return Out;
}

//Updated equation
return (tex + ambient + diffuse) * saturate(dot(Light, Norm));

Zman

unread,
Oct 6, 2005, 3:21:22 AM10/6/05
to
Some feedback.

1. Make sure you normalize your light vector - yours in constant and happens
to be normalized, but when you paramterize it that won't always be the case.
2. dot(Light, Norm)) only varies per vertex so don't recalculate it in the
pixel shader. Do it in the vertex shader and let the hadware ineterpolate
3. You may need to saturate the final color as it could go over 1.0
4. You are varying the ambient light color by the directional light -
ambient is color only so never varies.

Basically you should calculate the vertex color in the vertex shader with
something like

dirLightAttn=saturate(dot(transformedNormal, normalize(LightDir)))

outCol=saturate(material * (ambient + dirLightAttn * LightCol))

then add in the texture color only in the pixel shader
outCol=saturate(inCol * tex2D(.....))

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:ad8b59d268d247a0...@ureader.com...

Moo Mansun

unread,
Oct 6, 2005, 12:56:16 PM10/6/05
to
The file works perfect, I am just wondering when I output my Light vector,
how I convert it from a Vector3 to a float4.

What's the additional value in the float4 for?

In a Vector3, it is xyz.

Thanks Zman.

Moo Mansun

unread,
Oct 6, 2005, 5:06:03 PM10/6/05
to
A small note on the Directional_Light.fx v1.0.

Whilst it mimics a directional light, the source must be modified to account
for the difference in the position of the light relative to the material.

If not, the shading effect is permantly fixed from a single point.

Zman

unread,
Oct 6, 2005, 8:59:46 PM10/6/05
to
No it doesn't.

(tex + ambient + diffuse) * saturate(dot(Light, Norm));

That line is varying the ambient light by a factor based on the normal and
the light angle which is not what ambient light does.


--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:a565bfbe355c4742...@ureader.com...

Moo Mansun

unread,
Oct 7, 2005, 12:27:46 AM10/7/05
to
I was referring to the visual effect, not the mathematics of it.

I have run into a snag, I need to calculate the worldMatrix. Now, I know
this should be available as the inverse of the viewMatrix, how do I
calculate that?

Thanks Z

Zman

unread,
Oct 7, 2005, 1:57:25 AM10/7/05
to
The visual effect cannot be correct if the math isn't and its unfair to post
code in the newsgroup claiming that it is an example solution when it is
not.

World != inverse(View). Not sure what you are trying to do here. Generally
there is no relation between the two.

Typically:
World is something you create to move objects from their default position
(usually around 0,0,0) to another place and orientation in the world. You
don't so much calculate it from anything as just create it. Ususually using
a product of Matrix.Translate, .Rotate and .Scale

View is also something you create - typically it represents the position of
the camera in the world and provides a transformation of the world into a
position and orientation from the point of view of that camera. Typically
you create this yourself using one of the LookAt functions, or get it from a
Camera object (for example in the DXframework classes).

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:a5ff957bde044a16...@ureader.com...

Moo Mansun

unread,
Oct 7, 2005, 12:38:11 PM10/7/05
to
I have the viewMatrix done, I just need to calculate the worldMatrix as I
need the value. Actually, if you are generating the worldView from Matrix.
Translate, .Rotate and .Scale, then technically you are calculating it, not
creating it.

There is a relationship between viewMatrix and worldMatrix, the worldMatrix
is the inverse of the viewMatrix.

As for the code, the visual effect of that file is a projected shading of
the vertices from a fixed point. I have tested it.

Moo Mansun

unread,
Oct 7, 2005, 12:43:54 PM10/7/05
to
Computation of Inverse World Matrix
The world matrix could be represented as:

Mw = Mwr * Mwt, where Mwr is the rotation part and Mwtb is the translation
part.

http://msdn.microsoft.com/archive/default.asp?
url=/archive/en-us/dnardir3d/html/d3dlight3.asp

Zman

unread,
Oct 7, 2005, 5:32:09 PM10/7/05
to
>Actually, if you are generating the worldView from Matrix.
> Translate, .Rotate and .Scale, then technically you are calculating it,
> not
> creating it.
I guess that could be the case, however I was trying to make a point to help
you distinguish between calculating it based on an already existing matrix
and creating one from scratch.

> There is a relationship between viewMatrix and worldMatrix, the
> worldMatrix
> is the inverse of the viewMatrix.

No there is not. Apparently I am not the only one who disagrees with you -
you are being told the same thing on gamedev
http://www.gamedev.net/community/forums/topic.asp?topic_id=349940
http://www.gamedev.net/community/forums/topic.asp?topic_id=349970
and you choose not to believe it on there either.

> As for the code, the visual effect of that file is a projected shading of
> the vertices from a fixed point. I have tested it.

I'm sure it looks right, but the math is wrong doesn't matter how many times
you tell us you think it is right. People will read this thread, see your
example and assume its correct. Even worse, you put my name in it so I feel
a responsibility to correct it.

I'm not sure what reaction you expect to get by asking questions then
arguing with the correct answers as if you are the expert but generally its
not a way to find friends in forums and newsgroups. Good luck to you - end
of the thread for me.

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:13f31a574a4146ea...@ureader.com...

Moo Mansun

unread,
Oct 8, 2005, 12:03:36 AM10/8/05
to
I think that you are forgetting that it **is** an implementation. Its just
not the implementation that you wanted.

Moo Mansun

unread,
Oct 8, 2005, 12:14:02 AM10/8/05
to
I'll make you a deal, assist me to finish this and I will post the final
shader, fully working. I cannot be fairer than that.

This is my problem:

Quote:


"Typically:
World is something you create to move objects from their default position
(usually around 0,0,0) to another place and orientation in the world. You
don't so much calculate it from anything as just create it. Ususually using
a product of Matrix.Translate, .Rotate and .Scale

View is also something you create - typically it represents the position of
the camera in the world and provides a transformation of the world into a
position and orientation from the point of view of that camera. Typically
you create this yourself using one of the LookAt functions, or get it from a
Camera object (for example in the DXframework classes)."


I have a viewMatrix that represents the camera, however, creating another
instance of the Matrix will have the effect of manipulating the current
matrix.

If I try to use Matrix.Translate, .Rotate and .Scale I will alter the output
to the screen. How do I get the worldView Matrix without doing this?

Zman

unread,
Oct 8, 2005, 2:58:08 AM10/8/05
to
Like I said, I'm not gong to post more in the thread (and now I've done it
anyway), newsgroups and forums are not the place for debate. I notice you
don't have a public email on your forum posts or on the news postings. Mine
is below, I do want to help - I just don't want to argue. Drop me a line.

--
Zman (mailto:zm...@thezbuffer.com)
http://www.thezbuffer.com
News and Resources for Managed DirectX


"Moo Mansun" <urlo...@nothing.com> wrote in message

news:df8b9b6322c2439a...@ureader.com...

Moo Mansun

unread,
Oct 8, 2005, 2:49:52 PM10/8/05
to
Thanks Zman...I'll be in contact.
0 new messages