Advertisement Jump to content
Sign in to follow this  

Light issue solved :)

This topic is 4875 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

I'm using a positionTextured vertexBuffer with a single texture on it.I have some fog on there and I tryed to enable the light(point,ambient,specular all types) nothing is working. Are there any presentationParameters that set the light? I enabled lightning form the device, and enabled each light i wanted to use. Can you please guide me to a good tutorial(no msdn, i tryed that) where i could view a functional sample or something like that. Here's the code i use, it should be a ligth attached to the camera: device.RenderState.Lighting = True Dim matr As New Material matr.AmbientColor = Microsoft.DirectX.Direct3D.ColorValue.FromColor(Color.Red) matr.DiffuseColor = Microsoft.DirectX.Direct3D.ColorValue.FromColor(Color.Red) device.Lights(0).Type = LightType.Point device.Lights(0).Diffuse = Color.Red device.Lights(0).Ambient = Color.Red device.Lights(0).Specular = Color.Red device.Lights(0).Position = New Vector3(CameraCls.TranslationMatrix.M41, CameraCls.TranslationMatrix.M42, CameraCls.TranslationMatrix.M43) device.Lights(0).Attenuation0 = 1.0F device.Lights(0).Range = 1000000.0F 'i wanted to see it :) device.Lights(0).Enabled = True How can i add peace of code,..i've seen some people add that drowp down list special for codes? [Edited by - Zmax on September 13, 2005 7:46:47 AM]

Share this post

Link to post
Share on other sites
Hi there ZMax,
How are you doing buddy?

The Problem
Setting up light and some fog code in C# and Managed DirectX

The Solution
Setting up light is a very simple task ZMax,
Here is a check list
1) What light type am I going to use?
2) Depending on the light type what is the range of my light and attenuation etc..
3) Did I enable my light?
With this said, here is some simple code that sets up a directional light that shines inwards into the monitor in a left handed coordinate system

Simple Fog setup

public void RenderFog(float density, float start, float end, System.Drawing.Color fogColor)
device.RenderState.FogEnable = true;
device.RenderState.FogDensity = density;
device.RenderState.FogVertexMode = FogMode.Linear;
device.RenderState.RangeFogEnable = true;
device.RenderState.FogColor = fogColor;
device.RenderState.FogStart = start;
device.RenderState.FogEnd = end;

Simple Light setup

//Add a directional light.
device.Lights[0].Type = LightType.Directional;
//Directional lights have no attentuation, The directional vector is also normalized.
device.Lights[0].Direction = new Vector3(0.0f, 0.0f, 1.0f);
//Enable the light.
device.Lights[0].Enabled = true;

//just make sure the light renderstate for the device is enabled
device.RenderState.Lighting = true;

I hope this helps a bit buddy.

Share this post

Link to post
Share on other sites
It seems to me that your device is looking for normal information within your vertices, and when it doesn't find any it can't perform lighting calculations.

Try using PositionNormalTextured instead of PositionTextured for your vertices and add a normal vector to every one of them.

Also, if you want to place your posted code in a scrolling edit field, put the code witin [ source ][ /source ] tags. Read more about this in the general forum FAQ.

Share this post

Link to post
Share on other sites
Hi there staaf :)
How are you doing?

The Answer
Right on, It might be a problem if you render a position textured vertex formatted primitive. What you might want to consider if turning the light off. Or go with Saaf's option by adding some normals and turning the light back on.

Will render if light is turned off.
Will not render if light is turned on.

Well done saaf, thank you for pointing that out.

Share this post

Link to post
Share on other sites
Hmmm...i created vertexByffer as positionNormalTextured and set all the normals up(new vector3(0,1,0) but the lights don't appear. :(

'creates a vertexBuffer from the Verts array
Public Sub CreateVertexBuffer(ByVal device As Device, ByVal verts() As CustomVertex.PositionNormalTextured)
device.VertexFormat = CustomVertex.PositionNormalTextured.Format
vertexBuffer = New VertexBuffer(GetType(CustomVertex.PositionNormalTextured), HeightMapCls.BmpWidth * HeightMapCls.BmpHeight + 1, device, 0, CustomVertex.PositionNormalTextured.Format, Pool.Default)

vertexBuffer.SetData(verts, 0, 0)
End Sub

'returns the vertex array from a bmp image
Public Function GetMapVertexes() As CustomVertex.PositionNormalTextured()
Dim verts() As CustomVertex.PositionNormalTextured

ReDim verts(BmpWidth * BmpHeight)

For x = 0 To BmpWidth - 1 Step 1
For y = 0 To BmpHeight - 1 Step 1
If (x = 0) Or (y = 0) Or (x = BmpWidth - 2) Or (y = BmpHeight - 2) Then
verts((y + x * BmpHeight)).Position = New Vector3(x * MyScale, Bmp.GetPixel(x, y).R + 50, y * MyScale)

verts((y + x * BmpHeight)).Position = New Vector3(x * MyScale, Bmp.GetPixel(x, y).R * 2, y * MyScale)
End If
verts((y + x * BmpHeight)).Normal = New Vector3(0, 1, 0)
'this is for teh texture coordinates nothing interesting
For x = 0 To BmpWidth - 1
For y = 0 To BmpHeight - 1

'texture settings
verts(y + x * BmpHeight).Tu = x / (BmpWidth / 8)
verts(y + x * BmpHeight).Tv = y / (BmpHeight / 8)


Return verts
End Function

hmmm the lights are like u told me to do them, in the render method,sub :((

Share this post

Link to post
Share on other sites
I solved the problem and i know what was.
The light works so,..the color of the light blends with the color of the material(diffuse color) so the material color has to be a very very light one,..i used white. Hope this will help other people too :)

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!