rendered paste bodyusing System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Textures
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Device device = null;
private VertexBuffer vb = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private float angle = 0.0f;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
this.KeyDown += new KeyEventHandler(KeyInput);
}
private float off_x = 0f;
private float off_y = 0f;
private float off_z = 0f;
private void KeyInput(object sender, KeyEventArgs args)
{
if(args.KeyCode==Keys.Left)
off_x += 0.5f;
if(args.KeyCode==Keys.Right)
off_x -= 0.5f;
if(args.KeyCode==Keys.Up)
off_y += 0.5f;
if(args.KeyCode==Keys.Down)
off_y -= 0.5f;
if(args.KeyCode==Keys.W)
off_z += 0.5f;
if(args.KeyCode==Keys.S)
off_z -= 0.5f;
}
/// <summary>
/// We will initialize our graphics device here
/// </summary>
public void InitializeGraphics()
{
// Set our presentation parameters
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
// Create our device
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
LoadHeightdata();
LoadTextures(device, new Bitmap("tex06-01.bmp"));
vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), 6*32*32, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
vb.Created += new EventHandler(this.OnVertexBufferCreate);
OnVertexBufferCreate(vb, null);
}
private void OnVertexBufferCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;
CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[(32*32*6)];
int cx = 0;
int cy = 0;
/*for(int cx = 0; cx < 8; cx++)
{
for(int cy = 0; cy < 8; cy++)
{*/
for(int y = 0; y < 32; y++)
{
for(int x = 0; x < 32; x++)
{
verts[/*cy*32*6*8+cx*32*6+*/y*32*6+x*6+0] = new CustomVertex.PositionTextured(x, y, GetZ(x,y), 0.0f, 0.0f);
verts[/*cy*32*6*8+cx*32*6+*/y*32*6+x*6+1] = new CustomVertex.PositionTextured(x+1, y, GetZ(x+1,y), 1.0f, 0.0f);
verts[/*cy*32*6*8+cx*32*6+*/y*32*6+x*6+2] = new CustomVertex.PositionTextured(x, y+1, GetZ(x,y+1), 0.0f, 1.0f);
verts[/*cy*32*6*8+cx*32*6+*/y*32*6+x*6+3] = new CustomVertex.PositionTextured(x, y+1, GetZ(x,y+1), -1.0f, 0.0f);
verts[/*cy*32*6*8+cx*32*6+*/y*32*6+x*6+4] = new CustomVertex.PositionTextured(x+1, y, GetZ(x+1,y), 0.0f, -1.0f);
verts[/*cy*32*6*8+cx*32*6+*/y*32*6+x*6+5] = new CustomVertex.PositionTextured(x+1, y+1, GetZ(x+1,y+1), 0.0f, 0.0f);
}
}
// }
//}
buffer.SetData(verts, 0, LockFlags.None);
}
private int GetZ(int x, int y)
{
if(!m_heightdata.ContainsKey(y*32+x)) return 0;
return (int)((int)m_heightdata[y*32+x]*-0.1);
}
private void SetupCamera()
{
device.Transform.Projection = Matrix.PerspectiveFovRH((float)Math.PI , this.Width / this.Height, 100.0f, 300.0f);
device.Transform.View = Matrix.LookAtRH(new Vector3(24,24,24), new Vector3(24+off_x,24+off_y,0+off_z), new Vector3(0,0,1));
//device.Transform.View = Matrix.LookAtRH(new Vector3(off_x,off_y,off_z), new Vector3(0,0,0), new Vector3(0,0,1));
device.RenderState.Lighting = false;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);
SetupCamera();
device.BeginScene();
device.VertexFormat = CustomVertex.PositionTextured.Format;
device.SetStreamSource(0, vb, 0);
DrawMap();
device.EndScene();
device.Present();
this.Invalidate();
}
private void DrawMap()
{
angle += 0.01f;
device.Transform.World = Matrix.RotationYawPitchRoll(0, 0, angle/*(float)Math.PI/2, (float)Math.PI*angle/2*/) * Matrix.Translation(0, 0, 0);
for(int i = 0; i < 32*32/**64*/; i++)
{
device.SetTexture(0, GetTexture(i));
device.DrawPrimitives(PrimitiveType.TriangleList, i*6, /*32*32**/2);
}
}
private Texture GetTexture(int id)
{
return (Texture)m_textures[id];
}
private Hashtable m_textures;
private void LoadTextures(Device device, Bitmap source)
{
m_textures = new Hashtable();
for(int y = 0; y < 32; y++)
{
for(int x = 0; x < 32; x++)
{
Bitmap bmp = GetBitmap(source,x,y);
m_textures.Add(y*32+x, new Texture(device, bmp, Usage.Dynamic, Pool.Default));
}
}
}
private Hashtable m_heightdata;
private void LoadHeightdata()
{
BinaryReader reader = new BinaryReader(File.OpenRead("map.raw"));
m_heightdata = new Hashtable();
for(int i = 0; i < 32*32; i++)
{
m_heightdata[i] = (int)reader.ReadByte();
}
}protected Bitmap GetBitmap(Bitmap source, int sx, int sy)
{
Bitmap bmp = new Bitmap(8,8);
for(int hy = 0; hy < 8; hy++)
{
for(int hx = 0; hx < 8; hx++)
{
bmp.SetPixel(hx,hy,source.GetPixel(sx*8+hx,sy*8+hy));
}
}
return bmp;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new Size(800,600);
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
using (Form1 frm = new Form1())
{
// Show our form and initialize our graphics engine
frm.Show();
frm.InitializeGraphics();
Application.Run(frm);
}
}
}
}