Sign in to follow this  

[Solved] Why no lighting?

This topic is 2345 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've tried everything. And can't find out why i dont get to lit my vertices.
I do this in C++ and it works, but not in C#. why?

I also tried set VertexFormat to different formats, but still just a black sphere (or mesh).
Tried almost everything, i wonder if there's something wrong with the transformations.
But the ambient values should work anyway, so i just dont seem why it would be wrong.

Sorry for pasting long code, but i just have to do this (its not too long anyway, just a simple app).

[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace CATool
{
public partial class MainForm : Form
{
private Device pDev = null;
private Mesh pMesh;
private Material meshMaterial;
private Matrix matRot = new Matrix();
private Matrix matRotTra = new Matrix();
private Matrix matTra = new Matrix();
private Matrix matTra2 = new Matrix();
private Matrix matWorld = new Matrix();
private Vector3 angle = new Vector3();
public int time1 = 0;

public MainForm()
{
InitializeComponent();
Initialize3DGraphics();
Initialize3DMesh();
InitializeMaterial();
InitializeLight();
Initialize3DProjections();
timer1.Tick += new EventHandler(timer_Tick);
}

public bool Initialize3DGraphics()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
//presentParams.DeviceWindowHandle = this.Handle;

//pDev = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
pDev = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);

pDev.RenderState.ZBufferEnable = true;
pDev.RenderState.CullMode = Cull.None;// Cull.CounterClockwise;
pDev.RenderState.PointSize = 4;
pDev.RenderState.FillMode = FillMode.Solid;// WireFrame;
// pDev.RenderState.ShadeMode = ShadeMode.Gouraud;//Phong;
//pDev.VertexFormat = VertexFormats.Normal | VertexFormats.Position | VertexFormats.Diffuse | VertexFormats.Specular;
//pDev.VertexFormat = CustomVertex.Transformed.Format;
//pDev.VertexFormat = CustomVertex.PositionNormalColored.Format;
return true;
}

public void Initialize3DMesh()
{
//pMesh = Mesh.Box(pDev, 1, 1, 1);
//pMesh = Mesh.Teapot(pDev);
pMesh = Mesh.Sphere(pDev, 1, 32, 16);
pMesh.ComputeNormals();
}

public void InitializeMaterial()
{
meshMaterial = new Material();
meshMaterial.Ambient = Color.FromArgb(255, 128, 128, 128);
meshMaterial.Diffuse = Color.FromArgb(255, 0, 0, 10);
meshMaterial.Specular = Color.FromArgb(255, 255, 255, 255);
//meshMaterial.Emissive = Color.FromArgb(0, 0, 0, 0);
meshMaterial.SpecularSharpness = 100.0f;// 20.0f;
pDev.Material = meshMaterial;
}

public void InitializeLight()
{
pDev.RenderState.Lighting = true;
pDev.RenderState.SpecularEnable = true;
pDev.Lights[0].Type = LightType.Point;
pDev.Lights[0].Diffuse = Color.White;// Color.DarkTurquoise;
pDev.Lights[0].AmbientColor = new ColorValue(64, 64, 64);
pDev.Lights[0].Attenuation1 = 1.0f;
pDev.Lights[0].Position = new Vector3(0.0f, 2.0f, 0.0f); //new Vector3(0.0f, 1.0f, 0.0f);
// pDev.Lights[0].Direction = new Vector3(0.0f, 4.0f, 0.0f); //new Vector3(0.0f, 1.0f, 0.0f);
pDev.Lights[0].Range = 100;
pDev.Lights[0].Enabled = true;
pDev.RenderState.Ambient = Color.FromArgb(255, 32, 32, 32);
}

public void Initialize3DProjections()
{
pDev.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);
}

protected override void OnPaint(PaintEventArgs e)
{
if (pDev == null)
{
Graphics graphics = e.Graphics;
graphics.FillRectangle(Brushes.Red, new Rectangle(0, 0, Width, Height));
return;
}

//pDev.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);
pDev.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0);
pDev.BeginScene();

pDev.VertexFormat = CustomVertex.PositionNormal.Format;
//pDev.VertexFormat = CustomVertex.PositionColored.Format;
//pDev.VertexFormat = CustomVertex.PositionNormalColored.Format;
//pMesh.Device.VertexFormat = CustomVertex.PositionNormalColored.Format;
//time1 += 1;

angle.X = (float)time1 * (1.0f * (float)Math.PI) / 100.0f;
angle.Y = (float)time1 * (1.2f * (float)Math.PI) / 100.0f;
angle.Z = (float)time1 * (1.3f * (float)Math.PI) / 100.0f;

matWorld = Matrix.RotationYawPitchRoll(angle.X, angle.Y, angle.Z) * Matrix.Translation(0, 0, 8);
pDev.SetTransform(TransformType.World, matWorld);
//pDev.Transform.World = Matrix.RotationYawPitchRoll(angle.X, angle.Y, angle.Z) * Matrix.Translation(0, 0, 8);
pDev.Material = meshMaterial;
pMesh.ComputeNormals();
pMesh.DrawSubset(0);

pDev.EndScene();
pDev.Present();
}

protected override void OnPaintBackground(PaintEventArgs e)
{
}

protected override void OnSizeChanged(EventArgs e)
{
Invalidate();
}

public void timer_Tick(object sender, EventArgs e)
{
time1++;
this.Invalidate();
}
}
}
[/code]

Share this post


Link to post
Share on other sites

This topic is 2345 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this