📄 lighting.cs
字号:
Vector3 vecUp = new Vector3(0, 1, 0);
Matrix matWorldInv;
matWorldInv = Matrix.LookAtLH(vecFrom, vecAt, vecUp);
matWorld = Matrix.Invert(matWorldInv);
device.SetTransform(TransformType.World, matWorld);
coneMesh.DrawSubset(0);
}
// Output statistics
fpsTimer.Render();
device.EndScene();
device.Present();
fpsTimer.StopFrame();
}
/// <summary>
/// The device has been created. Resources that are not lost on
/// Reset() can be created here.
/// </summary>
public void InitializeDeviceObjects()
{
// initializes the fps timer
fpsTimer = new FpsTimerTool(device);
}
/// <summary>
/// The device exists, but may have just been Reset(). Resources
/// and any other device state that persists during
/// rendering should be set here. Render states, matrices, textures,
/// etc., that don't change during rendering can be set once here to
/// avoid redundant state setting during Render() or FrameMove().
/// </summary>
void RestoreDeviceObjects(System.Object sender,
System.EventArgs e)
{
MyVertex[] v;
Mesh pWallMeshTemp;
// Create a square grid numberVertsX*numberVertsZ for rendering
// the wall
pWallMeshTemp = new Mesh(numberTriangles, numberTriangles * 3,
0, MyVertex.Format, device);
// Fill in the grid vertex data
v = (MyVertex[])pWallMeshTemp.VertexBuffer.Lock(0,
typeof(MyVertex), 0, numberTriangles * 3);
float dX = 1.0f / (numberVertsX - 1);
float dZ = 1.0f / (numberVertsZ - 1);
uint k = 0;
for (uint z = 0; z < (numberVertsZ - 1); z++)
{
for (uint x = 0; x < (numberVertsX - 1); x++)
{
v[k].p = new Vector3(10 * x * dX, 0.0f, 10 * z * dZ);
v[k].n = new Vector3(0.0f, 1.0f, 0.0f);
k++;
v[k].p = new Vector3(10 * x * dX, 0.0f, 10 * (z+1) * dZ);
v[k].n = new Vector3(0.0f, 1.0f, 0.0f);
k++;
v[k].p =
new Vector3(10 * (x+1) * dX, 0.0f, 10 * (z+1) * dZ);
v[k].n = new Vector3(0.0f, 1.0f, 0.0f);
k++;
v[k].p = new Vector3(10 * x * dX, 0.0f, 10 * z * dZ);
v[k].n = new Vector3(0.0f, 1.0f, 0.0f);
k++;
v[k].p =
new Vector3(10 * (x+1) * dX, 0.0f, 10 * (z+1) * dZ);
v[k].n = new Vector3(0.0f, 1.0f, 0.0f);
k++;
v[k].p = new Vector3(10 * (x+1) * dX, 0.0f, 10 * z * dZ);
v[k].n = new Vector3(0.0f, 1.0f, 0.0f);
k++;
}
}
pWallMeshTemp.VertexBuffer.Unlock();
// Fill in index data
ushort[] pIndex;
pIndex = (ushort[])pWallMeshTemp.IndexBuffer.Lock(0,
typeof(ushort), 0, numberTriangles * 3);
for (ushort iIndex = 0; iIndex < numberTriangles * 3; iIndex++)
pIndex[iIndex] = iIndex;
pWallMeshTemp.IndexBuffer.Unlock();
// Eliminate redundant vertices
int[] pdwAdjacency = new int[3 * numberTriangles];
pWallMeshTemp.GenerateAdjacency(0.01f, pdwAdjacency);
// Optimize the mesh
wallMesh = pWallMeshTemp.Optimize(MeshFlags.OptimizeCompact |
MeshFlags.OptimizeVertexCache | MeshFlags.VbDynamic |
MeshFlags.VbWriteOnly, pdwAdjacency);
pWallMeshTemp = null;
pdwAdjacency = null;
// Create sphere and cone meshes to represent the lights
sphereMesh = Mesh.Sphere(device, 0.25f, 8, 8);
coneMesh = Mesh.Cylinder(device, 0.0f, 0.25f, 0.5f, 8, 8);
// Set up a material
Microsoft.WindowsMobile.DirectX.Direct3D.Material mtrl =
new Material();
mtrl.Ambient = mtrl.Diffuse = System.Drawing.Color.White;
device.Material = mtrl;
// Set miscellaneous render states
device.RenderState.DitherEnable = false;
device.RenderState.SpecularEnable = false;
device.TextureState[0].ColorOperation = TextureOperation.Disable;
device.TextureState[0].AlphaOperation = TextureOperation.Disable;
// Set the world matrix
Matrix matIdentity = Matrix.Identity;
device.SetTransform(TransformType.World, matIdentity);
// Set the view matrix.
Matrix matView;
Vector3 vFromPt = new Vector3(-10, 10, -10);
Vector3 vLookatPt = new Vector3(0.0f, 0.0f, 0.0f);
Vector3 vUpVec = new Vector3(0.0f, 1.0f, 0.0f);
matView = Matrix.LookAtLH(vFromPt, vLookatPt, vUpVec);
device.SetTransform(TransformType.View, matView);
// Set the projection matrix
Matrix matProj;
float fAspect =
((float)device.PresentationParameters.BackBufferWidth) /
device.PresentationParameters.BackBufferHeight;
matProj = Matrix.PerspectiveFovLH((float)Math.PI/4,
fAspect, 1.0f, 100.0f);
device.SetTransform(TransformType.Projection, matProj);
// Turn on lighting.
device.RenderState.Lighting = true;
// Enable ambient lighting to a dim, grey light, so objects that
// are not lit by the other lights are not completely black
device.RenderState.Ambient = System.Drawing.Color.Gray;
// Set light #0 to be a simple, faint grey directional light so
// the walls and floor are slightly different shades of grey
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Direction = new Vector3(0.3f, -0.5f, 0.2f);
device.Lights[0].Diffuse =
System.Drawing.Color.FromArgb(64, 64, 64);
device.Lights[0].Update();
// Set light #1 to be a simple, bright directional light to use
// on the mesh representing light #2
device.Lights[1].Type = LightType.Directional;
device.Lights[1].Direction = new Vector3(0.5f, -0.5f, 0.5f);
device.Lights[1].Diffuse = System.Drawing.Color.Blue;
device.Lights[1].Update();
// Light #2 will be the light used to light the floor and walls.
// It will be set up in FrameMove() since it changes every frame.
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (tickStart == 0)
tickStart = Environment.TickCount - 1;
appTime = ((float)(Environment.TickCount - tickStart)) / 1000.0f;
this.FrameMove();
this.Render(); // Render on painting
this.Invalidate(); // Render again
}
protected override void OnPaintBackground(
System.Windows.Forms.PaintEventArgs e)
{
}
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
try
{
LightingForm d3dApp = new LightingForm();
System.Windows.Forms.Application.Run(d3dApp);
}
catch(NotSupportedException)
{
MessageBox.Show("Your device does not have the needed 3d " +
"support to run this sample");
}
catch(DriverUnsupportedException)
{
MessageBox.Show("Your device does not have the needed 3d " +
"support to run this sample");
}
catch(Exception e)
{
MessageBox.Show("The sample has run into an error and needs" +
"to close: " + e.Message);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -