• Advertisement
Sign in to follow this  

Can't see the terrain

This topic is 2773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm doing the tutorials at http://www.riemers.net/ and i should render a 3d terrain from a raw file but i won´t see the terrain. I've tryed everything that i know but i can't manage it to work.



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.IO;
using Microsoft.DirectX.DirectInput;

namespace DirectX_Tutorial
{
partial class Form1
{

private Microsoft.DirectX.Direct3D.Device device;
private float angle = 0f;
private CustomVertex.PositionColored[] vertices;
private VertexBuffer vb;
private int[] indices;
private IndexBuffer ib;
private System.ComponentModel.Container components = null;
private int WIDTH = 64;
private int HEIGHT = 64;
private int[,] heightData;
private Microsoft.DirectX.DirectInput.Device keyb;

public void InitializeKeyboard()
{
keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
keyb.Acquire();
}

private void ReadKeyboard()
{
KeyboardState keys = keyb.GetCurrentKeyboardState();
if (keys[Key.Delete])
{
angle += 0.03f;
}
if (keys[Key.End])
{
angle -= 0.03f;
}
}


public void InitializeDevice()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, presentParams);
device.RenderState.FillMode = FillMode.WireFrame;
device.RenderState.CullMode = Cull.None;
}


public void CameraPositioning()
{
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 150f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, -40, 50), new Vector3(0, -5, 0), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;
device.RenderState.CullMode = Cull.None;
}

public void VertexDeclaration()
{
vb = new VertexBuffer(typeof(CustomVertex.PositionColored), WIDTH*HEIGHT, device, Usage.Dynamic | Usage.WriteOnly,
CustomVertex.PositionColored.Format, Pool.Default);
vertices = new CustomVertex.PositionColored[WIDTH*HEIGHT];

for (int x=0;x< WIDTH;x++) {

for (int y=0; y< HEIGHT;y++) {
vertices[x+y*WIDTH].Position = new Vector3(x, y, heightData[x,y]);
vertices[x+y*WIDTH].Color = Color.White.ToArgb();
}
}

vb.SetData(vertices, 0 ,LockFlags.None);
}

public void IndicesDeclaration()
{
ib = new IndexBuffer(typeof(int), (WIDTH - 1) * (HEIGHT - 1) * 6, device, Usage.WriteOnly, Pool.Default);
indices = new int[(WIDTH - 1) * (HEIGHT - 1) * 6];

for (int x = 0; x < WIDTH - 1; x++)
{

for (int y = 0; y < HEIGHT - 1; y++)
{
indices[(x + y * (WIDTH - 1)) * 6] = (x + 1) + (y + 1) * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 1] = (x + 1) + y * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 2] = x + y * WIDTH;

indices[(x + y * (WIDTH - 1)) * 6 + 3] = (x + 1) + (y + 1) * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 4] = x + y * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 5] = x + (y + 1) * WIDTH;
}
}
ib.SetData(indices, 0, LockFlags.None);
}

public void LoadHeightData()
{
heightData = new int[WIDTH, HEIGHT];

FileStream fs = new FileStream("hojdkarta.raw", FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);

for (int i = 0; i < HEIGHT; i++)
{

for (int y = 0; y < WIDTH; y++)
{
int height = (int)(r.ReadByte() / 50);
heightData[WIDTH - 1 - y, HEIGHT - 1 - i] = height;
}
}
r.Close();
}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);

device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;

device.SetStreamSource(0,vb,0);
device.Indices = ib;

device.Transform.World = Matrix.Translation(-HEIGHT / 2, -WIDTH / 2, 0)*Matrix.RotationZ(angle);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0,0,WIDTH*HEIGHT ,0,indices.Length/3);
device.EndScene();

device.Present();

this.Invalidate();
ReadKeyboard();
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.SuspendLayout();

// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
// this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.components = new System.ComponentModel.Container();
this.ClientSize = new System.Drawing.Size(500, 500);
this.Name = "Form1";
this.Text = "DirectX Tutorial";
this.ResumeLayout(false);

}

#endregion
}
}

// My main i use

static void Main()
{
using (Form1 our_dx_form = new Form1())
{
our_dx_form.LoadHeightData();
our_dx_form.InitializeDevice();
our_dx_form.CameraPositioning();
our_dx_form.VertexDeclaration();
our_dx_form.IndicesDeclaration();
our_dx_form.InitializeKeyboard();
our_dx_form.Show();
Application.Run(our_dx_form);
}

}




[Edited by - knogge on July 21, 2010 10:50:02 AM]

Share this post


Link to post
Share on other sites
Advertisement
Use [ source ] tags when posting code. (You can edit your post using the 'edit' button in the upper right.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement