• Advertisement
Sign in to follow this  

Trouble with input and rendering

This topic is 3002 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

So I'm having 2 issues. The first is I don't get why pressing a, s, i, and k have no effect. I'm updating the view Matrix, but not seeing any changes. My other issue is what gets rendered looks completely wrong. Does anyone notice any logical errors in my genVerts function? It's supposed to recursively break a square into 4 smaller, equal sized squares by creating 2 triangles to create the smaller squares. I'd appreciate any help on either issue. Full source:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Terrain
{
    public class TerrainMaker : Form
    {
        Device device;
        VertexBuffer vb;
        CustomVertex.TransformedColored[] m_Vertices;
        List<Vector4> vertices = new List<Vector4>();
        Matrix projection, view, world;
        Vector3 lookAt = Vector3.Empty;
        Vector3 position = new Vector3(0, 0, -30);
        Vector3 up = new Vector3(0, 1, 0);
        int speed = 5;

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar == 'i')
            {
                lookAt.Y+=speed;
                view = Matrix.LookAtLH(position, lookAt, up);
            }
            if (e.KeyChar == 'k')
            {
                lookAt.Y-=speed;
                view = Matrix.LookAtLH(position, lookAt, up);
            }
            if (e.KeyChar == 'w')
            {
                position.Z+=speed;
                view = Matrix.LookAtLH(position, lookAt, up);
            }
            if (e.KeyChar == 's')
            {
                position.Z-=speed;
                view = Matrix.LookAtLH(position, lookAt, up);
            }
            base.OnKeyPress(e);
        }

        protected override void OnClick(EventArgs e)
        {
            this.Close();
            base.OnClick(e);
        }

        public TerrainMaker()
        {
            AdapterInformation adapter = Manager.Adapters.Default;
            PresentParameters presentation = new PresentParameters();

            presentation.BackBufferFormat = adapter.CurrentDisplayMode.Format;
            presentation.BackBufferWidth = adapter.CurrentDisplayMode.Width;
            presentation.BackBufferHeight = adapter.CurrentDisplayMode.Height;
            presentation.SwapEffect = SwapEffect.Flip;
            presentation.Windowed = true;

            try
            {
                device = new Device(adapter.Adapter,
                            DeviceType.Hardware,
                            this,
                            CreateFlags.HardwareVertexProcessing,
                            presentation);
            }
            catch (DirectXException e)
            {
                MessageBox.Show(e.ErrorString);
                Environment.Exit(1);
            }

            projection = device.Transform.Projection;
            view = device.Transform.View;
            world = device.Transform.World;

            genVerts(new Vector4(10, 10, 0, 3), new Vector4(600, 10, 0, 6), new Vector4(600, 100, 0, 5), new Vector4(10, 100, 0, 12), 3);
            initVertexBuffer();
        }

        private void initVertexBuffer()
        {
            /*
            m_Vertices = new CustomVertex.TransformedColored[6];
            m_Vertices[0] = new CustomVertex.TransformedColored(new Vector4(this.Width/2, this.Height/4, 0, 1),Color.Green.ToArgb());
            m_Vertices[1] = new CustomVertex.TransformedColored(new Vector4(this.Width * 3 / 4, this.Height * 3 / 4, 0, 1),Color.Red.ToArgb());
            m_Vertices[2] = new CustomVertex.TransformedColored(new Vector4(this.Width / 4, this.Height * 3 / 4, 0,1), Color.Blue.ToArgb());
            m_Vertices[3] = new CustomVertex.TransformedColored(new Vector4(300, 50, 0, 1), Color.Blue.ToArgb());
            m_Vertices[4] = new CustomVertex.TransformedColored(new Vector4(400, 50, 0, 1), Color.Red.ToArgb());
            m_Vertices[5] = new CustomVertex.TransformedColored(new Vector4(350, 200, 0, 1), Color.Green.ToArgb());
            */

            m_Vertices = new CustomVertex.TransformedColored[vertices.Count];
            for (int i = 0; i < vertices.Count; i++ )
            {
                m_Vertices = new CustomVertex.TransformedColored(vertices, Color.Green.ToArgb());
            }
            vb = new VertexBuffer(typeof(CustomVertex.TransformedColored), 
               vertices.Count, device, 0,
              CustomVertex.TransformedColored.Format, Pool.Default);
            vb.SetData(m_Vertices, 0, 0);
        }

        private void genVerts(Vector4 ul, Vector4 ur, Vector4 lr, Vector4 ll)
        {
            Vector4 p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12;
            p1 = p2 = p3 = p4 = p5 = p6 = p7 = p8 = p9 = p10 = p11 = p12 = Vector4.Empty;
            p1.X = ul.X + ((ur.X - ul.X) / 2);
            p1.Y = ul.Y + ((ur.Y - ul.Y) / 2);
            p1.Z = 0;// (ul.W + ur.W + lr.W + ll.W) / 4.0f;
            p1.W = 1;
            p2 = ul;
            p2.W = 1;
            p3 = ll;
            p3.W = 1;

            p4.X = ul.X + ((ur.X - ul.X) / 2);
            p4.Y = ll.Y + ((lr.Y - ll.Y) / 2);
            p4.Z = 0;// (ul.W + ur.W + lr.W + ll.W) / 4.0f;
            p4.W = 1;
            p5 = p1;
            p5.W = 1;
            p6 = ll;
            p6.W = 1;

            p7 = p1;
            p7.W = 1;
            p8 = ur;
            p8.W = 1;
            p9 = lr;
            p9.W = 1;

            p10 = p4;
            p10.W = 1;
            p11 = ur;
            p11.W = 1;
            p12 = lr;
            p12.W = 1;

            vertices.Add(p1);
            vertices.Add(p2);
            vertices.Add(p3);
            vertices.Add(p4);
            vertices.Add(p5);
            vertices.Add(p6);
            vertices.Add(p7);
            vertices.Add(p8);
            vertices.Add(p9);
            vertices.Add(p10);
            vertices.Add(p11);
            vertices.Add(p12);
        }

        private void genVerts(Vector4 ul, Vector4 ur, Vector4 lr, Vector4 ll, int depth)
        {

            if (depth == 0)
            {
                genVerts(ul, ur, lr, ll);
                return;
            }
            else
            {
                Vector4 upmid = new Vector4(ul.X + ((ur.X - ul.X) / 2), 
                    ul.Y + ((ur.Y - ul.Y) / 2), 
                    ul.Z + ((ur.Z - ul.Z) / 2), 
                    ul.W + ((ur.W - ul.W) / 2));
                Vector4 mid = new Vector4(ll.X + ((lr.X - ll.X) / 2), 
                    ll.Y + ((lr.Y - ll.Y) / 2), 
                    ul.Z + ((lr.Z - ul.Z) / 2), 
                    (ul.W + ur.W + ll.W + lr.W)/4.0f);
                Vector4 leftmid = new Vector4(ul.X + ((ll.X - ul.X) / 2), 
                    ul.Y + ((ll.Y - ul.Y) / 2), 
                    ul.Z + ((ll.Z - ul.Z) / 2), 
                    ul.W + ((ll.W - ul.W) / 2));
                Vector4 lowmid = new Vector4(ll.X + ((lr.X - ll.X) / 2), 
                    ll.Y + ((lr.Y - ll.Y) / 2), 
                    ll.Z + ((lr.Z - ll.Z) / 2), 
                    ll.W + ((lr.W - ll.W) / 2));
                Vector4 rightmid = new Vector4(ur.X + ((lr.X - ur.X) / 2), 
                    ur.Y + ((lr.Y - ur.Y) / 2), 
                    ur.Z + ((lr.Z - ur.Z) / 2),
                    ur.W + ((lr.W - ur.W) / 2));
                
                //split box into 4 boxes

                //upper left box
        /*ul*/  genVerts(ul, 
        /*ur*/        upmid,
        /*lr*/        mid,
        /*ll*/        leftmid,
                      depth-1);

                //lower left box
        /*ul*/  genVerts(leftmid,
        /*ur*/        mid,
        /*lr*/        lowmid,
        /*ll*/        ll,
                      depth-1);

                //upper right box
        /*ul*/  genVerts(upmid,
        /*ur*/        ur,
        /*lr*/        rightmid,
        /*ll*/        mid,
                      depth - 1);

                //lower right box
        /*ul*/  genVerts(mid,
        /*ur*/        rightmid,
        /*lr*/        lr,
        /*ll*/        lowmid,
                      depth - 1);
            }
        }

        /// <summary>
        ///	Clear the screen with a blue color
        /// </summary>
        public void Render()
        {
            device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
            device.BeginScene();
            device.Transform.World = world;
            device.Transform.View = view;
            device.Transform.Projection = projection;
            device.VertexFormat = CustomVertex.TransformedColored.Format;
            device.SetStreamSource(0, vb, 0);
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Count / 3);
            device.EndScene();
            device.Present();
        }

        static void Main()
        {
            using (TerrainMaker tm = new TerrainMaker())
            {
                tm.Show();
                while (tm.Created)
                {
                    tm.Render();
                    Application.DoEvents();
                }
            }
        }
    }
}

Share this post


Link to post
Share on other sites
Advertisement
I'd suggest that you separate the problems. Try rendering a simple cube and navigating in that scene. Make sure using a breakpoint that OnKeyPress is called and the matrix is changed as you expect. For the object generation, I'd suggest that you try doing just one level of recursion and seeing if that works.

Share this post


Link to post
Share on other sites
Well, I can get it to render and take input correctly now. I just can't seem to get my camera to cooperate. I think it's just bad logic from not understanding the world, view, and projection matrices. This is what I have now.


using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX.Direct3D;

namespace Terrain
{
public class TerrainMaker : Form
{
Microsoft.DirectX.Direct3D.Device device;
VertexBuffer vb;
CustomVertex.TransformedColored[] m_Vertices;
List<Vector4> vertices = new List<Vector4>();
Matrix projection, view, world;
Vector3 lookAt = new Vector3(0,0,0);
Vector3 position = new Vector3(100, 1000, -450);
Vector3 up = new Vector3(0, 1, 0);
float rotSpeed = .2f;
int movSpeed = 2;
AdapterInformation adapter;
PresentParameters presentation;
Key[] kbState;
Microsoft.DirectX.DirectInput.Device keyboard;
bool update;
float xRot = 0f;
float yRot = 0f;

private void checkKeyState()
{
kbState = keyboard.GetPressedKeys();
foreach (Key key in kbState)
{
//turn
//look up down
if (key == Key.I)
{
xRot += rotSpeed;
if (xRot > 2 * Math.PI)
{
xRot = 2 * (float)Math.PI;
update = false;
return;
}
view *= Matrix.RotationX(xRot);
update = true;
}
if (key == Key.K)
{
xRot -= rotSpeed;
if (xRot < -2 * Math.PI)
{
xRot = -2 * (float)Math.PI;
update = false;
return;
}
view *= Matrix.RotationX(xRot);
update = true;
}
//look right left
if (key == Key.L)
{
yRot += rotSpeed;
view *= Matrix.RotationX(yRot);
update = true;
}
if (key == Key.J)
{
yRot -= rotSpeed;
view *= Matrix.RotationX(yRot);
update = true;
}

//move
//move forward back
if (key == Key.W)
{
position.Z += movSpeed;
view *= Matrix.Translation(position);
update = true;
}
if (key == Key.S)
{
position.Z -= movSpeed;
view *= Matrix.Translation(position);
update = true;
}
//move left right
if (key == Key.A)
{
position.X += movSpeed;
view *= Matrix.Translation(position);
update = true;
}
if (key == Key.D)
{
position.X -= movSpeed;
view *= Matrix.Translation(position);
update = true;
}
//up down
if (key == Key.R)
{
position.Y += movSpeed;
view *= Matrix.Translation(position);
update = true;
}
if (key == Key.F)
{
position.Y -= movSpeed;
view *= Matrix.Translation(position);
update = true;
}
}
}

protected override void OnClick(EventArgs e)
{
this.Close();
base.OnClick(e);
}

public TerrainMaker()
{
//allow for keyboard input
update = true;
keyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
keyboard.Acquire();

//set up graphis device
adapter = Microsoft.DirectX.Direct3D.Manager.Adapters.Default;
presentation = new PresentParameters();

presentation.BackBufferFormat = adapter.CurrentDisplayMode.Format;
presentation.BackBufferWidth = adapter.CurrentDisplayMode.Width;
presentation.BackBufferHeight = adapter.CurrentDisplayMode.Height;
presentation.SwapEffect = SwapEffect.Flip;
presentation.Windowed = true;

try
{
device = new Microsoft.DirectX.Direct3D.Device(adapter.Adapter,
Microsoft.DirectX.Direct3D.DeviceType.Hardware,
this,
CreateFlags.HardwareVertexProcessing,
presentation);
}
catch (DirectXException e)
{
MessageBox.Show(e.ErrorString);
Environment.Exit(1);
}

projection = device.Transform.Projection;
view = Matrix.LookAtLH(position, lookAt, up);
world = Matrix.PerspectiveFovLH((float)Math.PI / 4, presentation.BackBufferWidth / presentation.BackBufferHeight, 1f, 500f);

genVerts(new Vector4(10, 0, 10, 3), new Vector4(600, 0, 10, 6), new Vector4(600, 0, 300, 5), new Vector4(10, 0, 300, 12), 3);
}

private void updateVertexBuffer()
{
/*
m_Vertices = new CustomVertex.TransformedColored[6];
m_Vertices[0] = new CustomVertex.TransformedColored(new Vector4(this.Width/2, this.Height/4, 0, 1),Color.Green.ToArgb());
m_Vertices[1] = new CustomVertex.TransformedColored(new Vector4(this.Width * 3 / 4, this.Height * 3 / 4, 0, 1),Color.Red.ToArgb());
m_Vertices[2] = new CustomVertex.TransformedColored(new Vector4(this.Width / 4, this.Height * 3 / 4, 0,1), Color.Blue.ToArgb());
m_Vertices[3] = new CustomVertex.TransformedColored(new Vector4(300, 50, 0, 1), Color.Blue.ToArgb());
m_Vertices[4] = new CustomVertex.TransformedColored(new Vector4(400, 50, 0, 1), Color.Red.ToArgb());
m_Vertices[5] = new CustomVertex.TransformedColored(new Vector4(350, 200, 0, 1), Color.Green.ToArgb());
*/


if (vb != null)
{
vb = null;
}
m_Vertices = new CustomVertex.TransformedColored[vertices.Count];
for (int i = 0; i < vertices.Count; i++ )
{
m_Vertices = new CustomVertex.TransformedColored(vertices, Color.Green.ToArgb());
}
vb = new VertexBuffer(typeof(CustomVertex.TransformedColored),
vertices.Count, device, 0,
CustomVertex.TransformedColored.Format, Pool.Default);
vb.SetData(m_Vertices, 0, 0);
}

private void genVerts(Vector4 ul, Vector4 ur, Vector4 lr, Vector4 ll)
{
Vector4[] p = new Vector4[12];
for(int i = 0;i<p.Length; i++)
{
p = new Vector4(0,0,0,0);
}
p[0].X = ul.X + ((ur.X - ul.X) / 2);
p[0].Z = ul.Z + ((ur.Z - ul.Z) / 2);
p[0].Y = ul.W + ((ur.W - lr.W) / 2);
p[0].W = 1;
p[1] = ul;
p[1].W = 1;
p[2] = ll;
p[2].W = 1;

p[3].X = ll.X + ((lr.X - ll.X) / 2);
p[3].Z = ll.Z + ((lr.Z - ll.Z) / 2);
p[3].Y = ll.W + ((lr.W - lr.W) / 2);
p[3].W = 1;
p[4] = p[0];
p[4].W = 1;
p[5] = ll;
p[5].W = 1;

p[6] = p[0];
p[6].W = 1;
p[7] = ur;
p[7].W = 1;
p[8] = lr;
p[8].W = 1;

p[9] = p[3];
p[9].W = 1;
p[10] = p[0];
p[10].W = 1;
p[11] = lr;
p[11].W = 1;

foreach(Vector4 v in p)
{
vertices.Add(v);
}
updateVertexBuffer();
}

private void updateVerts(List<Vector4> v)
{
for (int i = 0; i < vertices.Count; i++)
{
Vector3 temp = new Vector3(vertices.X, vertices.Y, vertices.Z);
temp.TransformCoordinate(view);
vertices = new Vector4(temp.X, temp.Y, temp.Z, vertices.W);
}
updateVertexBuffer();
update = false;
}

private void genVerts(Vector4 ul, Vector4 ur, Vector4 lr, Vector4 ll, int depth)
{

if (depth == 0)
{
genVerts(ul, ur, lr, ll);
return;
}
else
{
Vector4 upmid = new Vector4(ul.X + ((ur.X - ul.X) / 2),
ul.Y + ((ur.Y - ul.Y) / 2),
ul.Z + ((ur.Z - ul.Z) / 2),
ul.W + ((ur.W - ul.W) / 2));
Vector4 mid = new Vector4(ll.X + ((lr.X - ll.X) / 2),
ll.Y + ((lr.Y - ll.Y) / 2),
ul.Z + ((lr.Z - ul.Z) / 2),
(ul.W + ur.W + ll.W + lr.W)/4.0f);
Vector4 leftmid = new Vector4(ul.X + ((ll.X - ul.X) / 2),
ul.Y + ((ll.Y - ul.Y) / 2),
ul.Z + ((ll.Z - ul.Z) / 2),
ul.W + ((ll.W - ul.W) / 2));
Vector4 lowmid = new Vector4(ll.X + ((lr.X - ll.X) / 2),
ll.Y + ((lr.Y - ll.Y) / 2),
ll.Z + ((lr.Z - ll.Z) / 2),
ll.W + ((lr.W - ll.W) / 2));
Vector4 rightmid = new Vector4(ur.X + ((lr.X - ur.X) / 2),
ur.Y + ((lr.Y - ur.Y) / 2),
ur.Z + ((lr.Z - ur.Z) / 2),
ur.W + ((lr.W - ur.W) / 2));

//split box into 4 boxes

//upper left box
/*ul*/ genVerts(ul,
/*ur*/ upmid,
/*lr*/ mid,
/*ll*/ leftmid,
depth-1);

//lower left box
/*ul*/ genVerts(leftmid,
/*ur*/ mid,
/*lr*/ lowmid,
/*ll*/ ll,
depth-1);

//upper right box
/*ul*/ genVerts(upmid,
/*ur*/ ur,
/*lr*/ rightmid,
/*ll*/ mid,
depth - 1);

//lower right box
/*ul*/ genVerts(mid,
/*ur*/ rightmid,
/*lr*/ lr,
/*ll*/ lowmid,
depth - 1);
}
}

/// <summary>
/// Clear the screen with a blue color
/// </summary>
public void Render()
{
checkKeyState();
if(update)
updateVerts(vertices);
device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
device.BeginScene();
//view = Matrix.LookAtLH(position, lookAt, up);
device.RenderState.CullMode = Cull.None;
device.RenderState.FillMode = FillMode.WireFrame;
//device.Transform.View = view;
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.SetStreamSource(0, vb, 0);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Count / 3);
device.EndScene();
device.Present();
}

static void Main()
{
using (TerrainMaker tm = new TerrainMaker())
{
tm.Show();
while (tm.Created)
{
tm.Render();
Application.DoEvents();
}
}
}
}
}

Share this post


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

  • Advertisement