/// <summary> /// Called once per frame, the call is the entry point for 3d rendering. This /// function sets up render states, clears the viewport, and renders the scene. /// </summary> protected override void Render() { // Clear the viewport device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Black.ToArgb(), 1.0f, 0); device.BeginScene(); // Render the waves device.SetTexture(0, bumpTex); device.SetTexture(1, background); device.TextureState[0].TextureCoordinateIndex = 0; device.TextureState[0].ColorOperation = TextureOperation.BumpEnvironmentMap; device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; device.TextureState[0].ColorArgument2 = TextureArgument.Current; device.TextureState[1].TextureCoordinateIndex = 1; device.TextureState[1].ColorOperation = TextureOperation.SelectArg1; device.TextureState[1].ColorArgument1 = TextureArgument.TextureColor; device.TextureState[1].ColorArgument2 = TextureArgument.Current; device.TextureState[2].ColorOperation = TextureOperation.Disable; device.VertexFormat = BumpVertex.Format; device.SetStreamSource(0, waterBuffer, 0); // Verify that the texture operations are possible on the device ValidateDeviceParams validParams = device.ValidateDevice(); if (validParams.Result != 0) { // The right thing to do when device validation fails is to try // a different rendering technique. This sample just warns the user. deviceValidationFailed = true; } device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); // Output statistics font.DrawText(2, 1, System.Drawing.Color.Yellow, frameStats); font.DrawText(2, 20, System.Drawing.Color.Yellow, deviceStats); if (deviceValidationFailed) { font.DrawText(2, 40, System.Drawing.Color.Yellow, "Warning: Device validation failed. Rendering may not look right."); } device.EndScene(); }
/// <summary> /// Called once per frame, the call is the entry point for 3d rendering. This /// function sets up render states, clears the viewport, and renders the scene. /// </summary> protected override void Render() { // Clear the viewport device.BeginScene(); // Render the background device.SetTexture(0, backTex); device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; device.TextureState[1].ColorOperation = TextureOperation.Disable; device.VertexFormat = CustomVertex.TransformedColoredTextured.Format; device.SetStreamSource(0, background, 0); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); // Render the lens device.SetTexture(0, bumpTex); device.SetTexture(1, backTex); device.TextureState[0].ColorOperation = TextureOperation.BumpEnvironmentMap; device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; device.TextureState[0].ColorArgument2 = TextureArgument.Current; device.TextureState[0].BumpEnvironmentMaterial00 = 0.2f; device.TextureState[0].BumpEnvironmentMaterial01 = 0.0f; device.TextureState[0].BumpEnvironmentMaterial10 = 0.0f; device.TextureState[0].BumpEnvironmentMaterial11 = 0.2f; device.TextureState[0].BumpEnvironmentLuminanceScale = 1.0f; device.TextureState[0].BumpEnvironmentLuminanceOffset = 0.0f; device.TextureState[1].ColorOperation = TextureOperation.SelectArg1; device.TextureState[1].ColorArgument1 = TextureArgument.TextureColor; device.TextureState[1].ColorArgument2 = TextureArgument.Current; // Generate texture coords depending on objects camera space position Matrix mat = new Matrix(); mat.M11 = 0.5f; mat.M12 = 0.0f; mat.M21 = 0.0f; mat.M22 = -0.5f; mat.M31 = 0.0f; mat.M32 = 0.0f; mat.M41 = 0.5f; mat.M42 = 0.5f; // Scale-by-z here Matrix matView, matProj; matView = device.Transform.View; matProj = device.Transform.Projection; Vector3 vEyePt = new Vector3(matView.M41, matView.M42, matView.M43); float z = vEyePt.Length(); mat.M11 *= (matProj.M11 / (matProj.M33 * z + matProj.M34)); mat.M22 *= (matProj.M22 / (matProj.M33 * z + matProj.M34)); device.Transform.Texture1 = mat; device.TextureState[1].TextureTransform = TextureTransform.Count2; device.TextureState[1].TextureCoordinateIndex = (int)TextureCoordinateIndex.CameraSpacePosition | 1; // Position the lens Matrix matWorld = Matrix.Translation(0.7f * (1000.0f - 256.0f) * lensX, 0.7f * (1000.0f - 256.0f) * lensY, 0.0f); device.Transform.World = matWorld; device.VertexFormat = BumpVertex.Format; device.SetStreamSource(0, lens, 0); // Verify that the texture operations are possible on the device ValidateDeviceParams validParams = device.ValidateDevice(); if (validParams.Result != 0) { // The right thing to do when device validation fails is to try // a different rendering technique. This sample just warns the user. deviceValidationFailed = true; } // Render the lens device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); // Output statistics font.DrawText(2, 1, System.Drawing.Color.Yellow, frameStats); font.DrawText(2, 20, System.Drawing.Color.Yellow, deviceStats); if (deviceValidationFailed) { font.DrawText(2, 40, System.Drawing.Color.Yellow, "Warning: Device validation failed. Rendering may not look right."); } device.EndScene(); }
/// <summary> /// The device has been created. Resources that are not lost on /// Reset() can be created here -- resources in Pool.Managed, /// Pool.Scratch, or Pool.SystemMemory. Image surfaces created via /// CreateImageSurface are never lost and can be created here. Vertex /// shaders and pixel shaders can also be created here as they are not /// lost on Reset(). /// </summary> protected override void InitializeDeviceObjects() { drawingFont.InitializeDeviceObjects(device); try { // Load the texture for the background image background = GraphicsUtility.CreateTexture(device, "lake.bmp", Format.R5G6B5); // Create the bumpmap. bumpMap = CreateBumpMap(256, 256, Format.V8U8); if (bumpMap == null) { throw new InvalidOperationException(); } // Create a square for rendering the background if ((backgroundVertex == null) || (backgroundVertex.Disposed)) { // We only need to create this buffer once backgroundVertex = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default); backgroundVertex.Created += new System.EventHandler(this.BackgroundVertexCreated); // Call it manually the first time this.BackgroundVertexCreated(backgroundVertex, null); } // See if EMBM and projected vertices are supported at the same time // in the fixed-function shader. If not, switch to using a vertex shader. isUsingVertexShader = false; SetEMBMStates(); device.VertexShader = null; device.VertexFormat = CustomVertex.PositionTextured.Format; ValidateDeviceParams validParams = device.ValidateDevice(); if (validParams.Result != 0) { isUsingVertexShader = true; } // If TextureCaps.Projected is set, projected textures are computed // per pixel, so this sample will work fine with just a quad for the water // model. If it's not set, textures are projected per vertex rather than // per pixel, so distortion will be visible unless we use more vertices. if (Caps.TextureCaps.SupportsProjected && !isUsingVertexShader) { numVertx = 2; // Number of vertices in the ground grid along X numVertz = 2; // Number of vertices in the ground grid along Z } else { numVertx = 8; // Number of vertices in the ground grid along X numVertz = 8; // Number of vertices in the ground grid along Z } numTriangles = (numVertx - 1) * (numVertz - 1) * 2; // Number of triangles in the ground // Create a square for rendering the water if ((waterBuffer == null) || (waterBuffer.Disposed)) { // We only need to create this buffer once waterBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 3 * numTriangles, device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default); waterBuffer.Created += new System.EventHandler(this.WaterBufferCreated); // Call it manually the first time this.WaterBufferCreated(waterBuffer, null); } } catch { SampleException e = new MediaNotFoundException(); HandleSampleException(e, ApplicationMessage.ApplicationMustExit); throw e; } }