|
|
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 BackGround_2
{
public partial class BackGround : Form
{
private Device bgDevice;
private PresentParameters presentParams;
private string TextureName;
Texture bgTexture;
VertexBuffer vertexBuffer = null;
BackGround backGround;
public void InitializeGraphics()
{
presentParams = new PresentParameters();
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.PresentationInterval = PresentInterval.One;
presentParams.Windowed = true;
bgDevice = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
backGround = new BackGround(bgDevice,"background.jpg");
}
public BackGround()
{
InitializeComponent();
}
public BackGround(Device bgDevice,string FileName)
{
TextureName = FileName;
bgTexture = TextureLoader.FromFile(bgDevice,TextureName);
vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedTextured), 4, bgDevice, 0, CustomVertex.TransformedTextured.Format, Pool.Managed);
Reset(800, 600);
}
public void Reset(int Width, int Height)
{
CustomVertex.TransformedTextured[] verts = (CustomVertex.TransformedTextured[])vertexBuffer.Lock(0, 0);
verts[0].Position = new Vector4(0, 0, 0, 1);
verts[0].Tu = 0.0f;
verts[0].Tv = 0.0f;
verts[1].Position = new Vector4(Width, 0, 0, 1);
verts[1].Tu = 1.0f;
verts[1].Tv = 0.0f;
verts[2].Position = new Vector4(Width, Height, 0, 1);
verts[2].Tu = 1.0f;
verts[2].Tv = 1.0f;
verts[3].Position = new Vector4(0, Height, 0, 1);
verts[3].Tu = 0.0f;
verts[3].Tv = 1.0f;
vertexBuffer.Unlock();
}
public void Render()
{
bgDevice.Clear(ClearFlags.ZBuffer | ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
bgDevice.BeginScene();
bgDevice.SetTexture(0, bgTexture);
bgDevice.RenderState.ZBufferEnable = false;
bgDevice.SetStreamSource(0, vertexBuffer, 0);
bgDevice.VertexFormat = CustomVertex.TransformedTextured.Format;
bgDevice.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);就是这句出错
bgDevice.RenderState.ZBufferEnable = true;
bgDevice.EndScene();
bgDevice.Present();
}
}
} |
|