/// <summary> /// Called once per frame, the call is the entry point for /// animating the scene. /// </summary> public void FrameMove() { lightData = device.LightsFixed[2]; // Rotate through the various light types if (((int)appTime % 20) < 10) { device.LightsFixed[2].Type = LightType.Point; } else { device.LightsFixed[2].Type = LightType.Directional; } // Make sure the light type is supported by the device. If // VertexProcessingCaps.PositionAllLights is not set, the // device does not support point or spot lights, so change // light #2's type to a directional light. if (!device.DeviceCaps.VertexProcessingCaps.SupportsPositionalLights) { if (device.LightsFixed[2].Type == LightType.Point) { device.LightsFixed[2].Type = LightType.Directional; } } // Values for the light position, direction, and color float x = (float)Math.Sin(appTime * 2.000f); float y = (float)Math.Sin(appTime * 2.246f); float z = (float)Math.Sin(appTime * 2.640f); byte r = (byte)((0.5f + 0.5f * x) * 0xff); byte g = (byte)((0.5f + 0.5f * y) * 0xff); byte b = (byte)((0.5f + 0.5f * z) * 0xff); device.LightsFixed[2].DiffuseColor = ColorValueFixed.FromColor(System.Drawing.Color.FromArgb(r, g, b)); device.LightsFixed[2].Range = 100.0f; switch (device.LightsFixed[2].Type) { case LightType.Point: device.LightsFixed[2].Position = new Vector3Fixed(4.5f * x, 4.5f * y, 4.5f * z); device.LightsFixed[2].Attenuation1 = 0.4f; break; case LightType.Directional: device.LightsFixed[2].Direction = new Vector3Fixed(x, y, z); break; } device.LightsFixed[2].Update(); }
/// <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 = null; // 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 MatrixFixed matIdentity = MatrixFixed.Identity; device.Transform.WorldFixed = matIdentity; // Set the view matrix. MatrixFixed 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 = new MatrixFixed(Matrix.LookAtLH(vFromPt, vLookatPt, vUpVec)); device.Transform.ViewFixed = matView; // Set the projection matrix MatrixFixed matProj; float fAspect = ((float)device.PresentationParameters.BackBufferWidth) / device.PresentationParameters.BackBufferHeight; matProj = new MatrixFixed( Matrix.PerspectiveFovLH((float)Math.PI / 4, fAspect, 1.0f, 100.0f)); device.Transform.ProjectionFixed = 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.LightsFixed[0].Type = LightType.Directional; device.LightsFixed[0].Direction = new Vector3Fixed(0.3f, -0.5f, 0.2f); device.LightsFixed[0].DiffuseColor = ColorValueFixed.FromColor(System.Drawing.Color.FromArgb(64, 64, 64)); device.LightsFixed[0].Update(); // Set light #1 to be a simple, bright directional light to use // on the mesh representing light #2 device.LightsFixed[1].Type = LightType.Directional; device.LightsFixed[1].Direction = new Vector3Fixed(0.5f, -0.5f, 0.5f); device.LightsFixed[1].DiffuseColor = ColorValueFixed.FromColor((System.Drawing.Color.Blue)); device.LightsFixed[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. }