Discussion:
Lighting not working with Textured triangles.
(too old to reply)
Akshay Srinivasan
2007-10-06 05:12:01 UTC
Permalink
Hi,

I am exploring the basics of DirectX3D and have come accross a problem.
When I use PositionNormalColored I get lighting to work. When I switch to
PositionNormalTextured the triangle faces go black which means lighting
problem. If I switch off lighting I see the textured triangle faces. Here
is the code you can check the problem out with. I am not sure what I am
doing wrong.

public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.Opaque, true);
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, pp);
device.Clear(ClearFlags.Target, System.Drawing.Color.Aquamarine,
1.0f, 0);
device.Transform.Projection = Matrix.PerspectiveFovLH(100,
this.Width / this.Height, 1f, 1000f);
device.RenderState.Lighting = true;
device.Transform.View = Matrix.LookAtLH(new Vector3(0f, 0f,
-10f), new Vector3(0f, 0f, 0f), new Vector3(0, 1f, 0));
previousX = this.Width / 2;
previousY = this.Height / 2;
t = new Texture(device, new Bitmap(@"c:\cubefacetexture.bmp"),
Usage.None, Pool.Managed);
}

private void Draw()
{
device.Clear(ClearFlags.Target, System.Drawing.Color.Aquamarine,
1.0f, 0);
CustomVertex.PositionNormalTextured[] vs = new
CustomVertex.PositionNormalTextured[12];
vs[0].Position = new Vector3(0f, 0f, 0f);
vs[0].Normal = new Vector3(0f, 0f, -1f);
//vs[0].Color = System.Drawing.Color.Green.ToArgb();
vs[0].Tu = 1f;
vs[0].Tv = 0f;
vs[1].Position = new Vector3(0f, 10f, 0f);
vs[1].Normal = new Vector3(0f, 0f, -1f);
//vs[1].Color = System.Drawing.Color.Green.ToArgb();
vs[1].Tu = 0f;
vs[1].Tv = 0f;
vs[2].Position = new Vector3(10f, 0f, 0f);
vs[2].Normal = new Vector3(0f, 0f, -1f);
//vs[2].Color = System.Drawing.Color.Green.ToArgb();
vs[2].Tu = 1f;
vs[2].Tv = 1f;
vs[3].Position = new Vector3(10f, 0f, 0f);
vs[3].Normal = new Vector3(0f, 0f, -1f);
//vs[3].Color = System.Drawing.Color.Green.ToArgb();
vs[3].Tu = 1;
vs[3].Tv = 1;
vs[4].Position = new Vector3(0f, 10f, 0f);
vs[4].Normal = new Vector3(0f, 0f, -1f);
//vs[4].Color = System.Drawing.Color.Green.ToArgb();
vs[5].Tu = 0;
vs[5].Tv = 0;
vs[5].Position = new Vector3(10f, 10f, 0f);
vs[5].Normal = new Vector3(0f, 0f, -1f);
//vs[5].Color = System.Drawing.Color.Green.ToArgb();
vs[5].Tu = 0;
vs[5].Tv = 1;
vs[6].Position = new Vector3(0f, 0f, 0f);
vs[6].Normal = new Vector3(-1f, 0f, 0f);
vs[7].Position = new Vector3(0f, 0f, 10f);
vs[7].Normal = new Vector3(-1f, 0f, 0f);
vs[8].Position = new Vector3(0f, 10f, 10f);
vs[8].Normal = new Vector3(-1f, 0f, 0f);
vs[9].Position = new Vector3(0f, 0f, 0f);
vs[9].Normal = new Vector3(-1f, 0f, 0f);
vs[10].Position = new Vector3(0f, 10f, 10f);
vs[10].Normal = new Vector3(-1f, 0f, 0f);
vs[11].Position = new Vector3(0f, 10f, 0f);
vs[11].Normal = new Vector3(-1f, 0f, 0f);
device.BeginScene();
device.Lights[0].Type = LightType.Point;
device.Lights[0].Diffuse = System.Drawing.Color.White;
device.Lights[0].Position = new Vector3(0, 0, -20);
device.Lights[0].Attenuation0 = 0.2f;
device.Lights[0].Range = 10000f;
device.Lights[0].Enabled = true;
device.Lights[0].Update();
device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
device.SetTexture(0, t);
device.DrawUserPrimitives(PrimitiveType.TriangleList, 4, vs);
device.EndScene();
device.Present();
}

protected override void OnPaint(PaintEventArgs e)
{
Draw();
}
--
Life is about joy!
Akshay Srinivasan
2007-10-06 05:31:00 UTC
Permalink
Problem was solved by adding a Material definition to the device and the
surface is displayed.
--
Life is about joy!
Post by Akshay Srinivasan
Hi,
I am exploring the basics of DirectX3D and have come accross a problem.
When I use PositionNormalColored I get lighting to work. When I switch to
PositionNormalTextured the triangle faces go black which means lighting
problem. If I switch off lighting I see the textured triangle faces. Here
is the code you can check the problem out with. I am not sure what I am
doing wrong.
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.Opaque, true);
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, pp);
device.Clear(ClearFlags.Target, System.Drawing.Color.Aquamarine,
1.0f, 0);
device.Transform.Projection = Matrix.PerspectiveFovLH(100,
this.Width / this.Height, 1f, 1000f);
device.RenderState.Lighting = true;
device.Transform.View = Matrix.LookAtLH(new Vector3(0f, 0f,
-10f), new Vector3(0f, 0f, 0f), new Vector3(0, 1f, 0));
previousX = this.Width / 2;
previousY = this.Height / 2;
Usage.None, Pool.Managed);
}
private void Draw()
{
device.Clear(ClearFlags.Target, System.Drawing.Color.Aquamarine,
1.0f, 0);
CustomVertex.PositionNormalTextured[] vs = new
CustomVertex.PositionNormalTextured[12];
vs[0].Position = new Vector3(0f, 0f, 0f);
vs[0].Normal = new Vector3(0f, 0f, -1f);
//vs[0].Color = System.Drawing.Color.Green.ToArgb();
vs[0].Tu = 1f;
vs[0].Tv = 0f;
vs[1].Position = new Vector3(0f, 10f, 0f);
vs[1].Normal = new Vector3(0f, 0f, -1f);
//vs[1].Color = System.Drawing.Color.Green.ToArgb();
vs[1].Tu = 0f;
vs[1].Tv = 0f;
vs[2].Position = new Vector3(10f, 0f, 0f);
vs[2].Normal = new Vector3(0f, 0f, -1f);
//vs[2].Color = System.Drawing.Color.Green.ToArgb();
vs[2].Tu = 1f;
vs[2].Tv = 1f;
vs[3].Position = new Vector3(10f, 0f, 0f);
vs[3].Normal = new Vector3(0f, 0f, -1f);
//vs[3].Color = System.Drawing.Color.Green.ToArgb();
vs[3].Tu = 1;
vs[3].Tv = 1;
vs[4].Position = new Vector3(0f, 10f, 0f);
vs[4].Normal = new Vector3(0f, 0f, -1f);
//vs[4].Color = System.Drawing.Color.Green.ToArgb();
vs[5].Tu = 0;
vs[5].Tv = 0;
vs[5].Position = new Vector3(10f, 10f, 0f);
vs[5].Normal = new Vector3(0f, 0f, -1f);
//vs[5].Color = System.Drawing.Color.Green.ToArgb();
vs[5].Tu = 0;
vs[5].Tv = 1;
vs[6].Position = new Vector3(0f, 0f, 0f);
vs[6].Normal = new Vector3(-1f, 0f, 0f);
vs[7].Position = new Vector3(0f, 0f, 10f);
vs[7].Normal = new Vector3(-1f, 0f, 0f);
vs[8].Position = new Vector3(0f, 10f, 10f);
vs[8].Normal = new Vector3(-1f, 0f, 0f);
vs[9].Position = new Vector3(0f, 0f, 0f);
vs[9].Normal = new Vector3(-1f, 0f, 0f);
vs[10].Position = new Vector3(0f, 10f, 10f);
vs[10].Normal = new Vector3(-1f, 0f, 0f);
vs[11].Position = new Vector3(0f, 10f, 0f);
vs[11].Normal = new Vector3(-1f, 0f, 0f);
device.BeginScene();
device.Lights[0].Type = LightType.Point;
device.Lights[0].Diffuse = System.Drawing.Color.White;
device.Lights[0].Position = new Vector3(0, 0, -20);
device.Lights[0].Attenuation0 = 0.2f;
device.Lights[0].Range = 10000f;
device.Lights[0].Enabled = true;
device.Lights[0].Update();
device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
device.SetTexture(0, t);
device.DrawUserPrimitives(PrimitiveType.TriangleList, 4, vs);
device.EndScene();
device.Present();
}
protected override void OnPaint(PaintEventArgs e)
{
Draw();
}
--
Life is about joy!
Loading...