using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.ComponentModel;
using System.IO;
namespace Practice
{
public class CreateDevice : Form
{
Device theDevice = null;
PresentParameters presentParams = new PresentParameters();
Mesh theMesh = null;
Material[] meshMaterials;
Texture[] meshTextures;
bool pause = false;
public CreateDevice()
{
this.ClientSize = new System.Drawing.Size(640, 480);
this.Text = "Mesh - Without Light...";
}
public void InitGraphics()
{
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
theDevice = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
theDevice.DeviceReset += new System.EventHandler(this.OnResetDevice);
this.OnResetDevice(theDevice, null);
pause = false;
}
public void OnResetDevice(object sender, EventArgs e)
{
ExtendedMaterial[] materials = null;
Device theDevice = (Device)sender;
theDevice.RenderState.ZBufferEnable = true;
theDevice.RenderState.Lighting = true;
theMesh = Mesh.FromFile("teapot.x", MeshFlags.SystemMemory, theDevice, out materials);
if (meshTextures == null)
{
meshTextures = new Texture[materials.Length];
meshMaterials = new Material[materials.Length];
for (int i = 0; i < materials.Length; ++i)
{
meshMaterials = materials.Material3D;
meshMaterials.Ambient = meshMaterials.Diffuse;
meshTextures = TextureLoader.FromFile(theDevice, materials.TextureFilename);
}
}
}
private void Render()
{
if (theDevice == null)
return;
if (pause)
return;
theDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
theDevice.BeginScene();
SetupMatrix();
SetupLights();
RenderMesh();
theDevice.EndScene();
theDevice.Present();
}
private void RenderMesh()
{
for (int i = 0; i < meshMaterials.Length; ++i)
{
theDevice.SetTexture(0, meshTextures);
theDevice.Material = meshMaterials;
theMesh.DrawSubset(i);
}
return;
}
private void SetupMatrix()
{
int theTime = Environment.TickCount % 10000;
float rotationAngle = theTime * (2.0f * (float)Math.PI) / 10000.0f;
Matrix transformationMatrix = Matrix.Scaling(0.3f, 0.3f, 0.3f);
transformationMatrix.Multiply(Matrix.Translation(-1.0f, -1.0f, 2.0f));
transformationMatrix.Multiply(Matrix.RotationAxis(new Vector3(2.0f, 11.0f, 2.0f), rotationAngle));
theDevice.Transform.World = transformationMatrix;
theDevice.Transform.View = Matrix.LookAtLH(new Vector3( 0.0f, 3.0f, -5.0f),
new Vector3( 0.0f, 0.0f, 0.0f),
new Vector3( 0.0f, 0.1f, 0.0f));
theDevice.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4,
1.0f,
1.0f,
100.0f);
}
private void SetupLights()
{
theDevice.Lights[0].Type = LightType.Point;
theDevice.Lights[0].Diffuse = System.Drawing.Color.DarkTurquoise;
theDevice.Lights[0].Direction = new Vector3(-1, -1, 3);
theDevice.Lights[0].Enabled = true;
theDevice.Lights[0].Update();
theDevice.RenderState.Ambient = System.Drawing.Color.FromArgb(0x202020);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
this.Render(); // Render on painting
}
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
this.Close(); // Esc was pressed
}
protected override void OnResize(System.EventArgs e)
{
pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
}
static void Main()
{
using (CreateDevice theWindow = new CreateDevice())
{
theWindow.InitGraphics();
theWindow.Show();
while (theWindow.Created)
{
theWindow.Render();
Application.DoEvents();
}
}
}
}
}
Lighting a Mesh
Iv been tinkering with Direct3D for a while now and have run across a problem applying Light to a Mesh. The Mesh does respond to the Ambient light that is setup but does nothing with the Directional light.
Thanks in advance
Curtis
edit: added [source] tags -SiCrane
[Edited by - SiCrane on December 4, 2004 2:18:33 AM]
Check the materials for your mesh (in mesh editor). The material for diffuse light may not be affecting the result, which means light/no light doesn't make a difference. Refer to SDK for more details.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement