Marcos Dell Antonio de Souza

Hi. I'm trying to draw a vertex buffer (using PositionColored vertex) but I can't see anything.

So, reading about it on the web, I've found that I need to set two matrix: Projection and View.

I've donet it!

Take a look:

// Create the vertex buffer
private void OnVertexBufferCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;

CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
verts[0] = new CustomVertex.PositionColored(0.0f, 1.0f, 1.0f, Color.Aqua.ToArgb());
verts[1] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Black.ToArgb());
verts[2] = new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, Color.Purple.ToArgb());

buffer.SetData(verts, 0, LockFlags.None);
}


The matrix configuration:

_device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0f),
new Vector3(0, 0, 0),
new Vector3(0, 1, 0));


_device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f,
this.Width / this.Height,
1.0f,
100.0f);

My device (Smartphone Emulator) has more or less 180 x 180 pixels.

So, my question is: why can't I see my object (the triangle)

Thanks.


Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Pieter Germishuys


_device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0f),
new Vector3(0, 0, 0),
new Vector3(0, 1, 0));
Refering to your code pasted, you probably want to pull your camera back a bit, the first variable is a position for the camera.
You would want to set this to new Vector3(0.0f, 0.0f, -5.0f);

I hope this helps,
Take care.







Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Neurone

Three matrix: world, view, projection. If you don't want to use world matrix, use CustomVertex.TransformedColored insead of CustomVertex.PositionColored. Also, pay attention to Culling: you must set the render state with

device.RenderState.CullMode = Cull.None

if you want to ignore vertex creation order.

Byez!





Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Marcos Dell Antonio de Souza

Man, I haven't read anything about Culling yet. Is it really necessary

I've trayied but didn't work.

Tks




Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Marcos Dell Antonio de Souza

Hi Pieter.

I've tryied this code but didn't work.

Do I need the world matrix defined too

Thanks.




Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Punkoff

1. If you have a black background and your triangle haven't any material and lighting isnt defined then:

1.1 to enable lighting
_device.renderstate.lighting=false;
[just non-realistic ambient light (fast)]
or
_device.renderstate.lighting=true;
_device.lights(0).type=lighttype.directional
_device.lights(0).position=new vector3(X,X,X) [light position]
_device.lights(0).direction=new vector3(X,X,X) [light direction]
_device.lights(0).commit [in latest DirectX this method is not declared and is not needed to call]
[normal light (slower)]

1.2 to create material
Material polygonMat
(in declaration, i'm not sure syntax is right, i don't develop with C)

polygonMat.diffuse= [any color except black];
(in ondevicereset handler)
_device.material=polygonMat;
(between device.beginscene and device.drawuserprimitives)

2. culling
culling means that when triangle vertices are in

[device.renderstate.culling=cullmode.clockwise - clockwise
device.renderstate.culling=cullmode.counterclockwise - counterclockwise
device.renderstate.culling=cullmode.none - no culling]

order, this triangle is not being drawed in current frame.
its used to ignore invisible surfaces of 3D object

note: if your triangle is visible from its back side and culling=clockwise then it will be invisible so set culling to cullmode.none while you are experimenting with simple polygons such as triangles and return it to cullmode.clockwise when you will work with real big objects





Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Marcos Dell Antonio de Souza

Hi man.

Thanks for your post, but there are no problems with the color: the background is Color.Bisque and the triangle is Black.

I was trying to draw another shapes.. lines, for example. I couldn't believe when two lines appeared in my emulator.

Let's look the code (the same for two lines and one triangle):

// the buffer

        private void OnVertexBufferCreate(object sender, EventArgs e)
        {
            VertexBuffer buffer = (VertexBuffer)sender;

            CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
            verts[0] = new CustomVertex.PositionColored(0.0f, 0.5f, 0.0f, Color.Red.ToArgb());
            verts[1] = new CustomVertex.PositionColored(-0.5f, -0.5f, 0.0f, Color.Red.ToArgb());
            verts[2] = new CustomVertex.PositionColored(0.5f, -0.5f, 0.0f, Color.Red.ToArgb());

            buffer.SetData(verts, 0, LockFlags.None);           
        }


// to drawn the triangle (does not work)

        private void Render()
        {
            // Clear the device
            _device.Clear(ClearFlags.Target, Color.Bisque, 1.0f, 0);          
           
            // Render de scene
            _device.BeginScene();

            // Setup the camera
            // SetupCamera();

            _device.SetStreamSource(0, _vertexBuffer, 0);
            _device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

            _device.EndScene();
            _device.Present();
        }

// to draw the lines (work!)

        private void Render()
        {
            // Clear the device
            _device.Clear(ClearFlags.Target, Color.Bisque, 1.0f, 0);          
           
            // Render de scene
            _device.BeginScene();

            // Setup the camera
            // SetupCamera();

            _device.SetStreamSource(0, _vertexBuffer, 0);
            _device.DrawPrimitives(PrimitiveType.LineStrip, 0, 2); // just this line changes

            _device.EndScene();
            _device.Present();
        }

Ok man... As I'm using x, y and z less then 1 and > than -1, so it's not necessary to set the view/perspective matrix, because alread there is one default.

The lines appear, normally. But the tirangle no!

Can you help me Thanks








Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Punkoff

Try this

private void OnVertexBufferCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;

CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
verts[0] = new CustomVertex.PositionColored(0.0f, 0.5f, 0.0f, Color.Red.ToArgb());
verts[2] = new CustomVertex.PositionColored(-0.5f, -0.5f, 0.0f, Color.Red.ToArgb());
verts[1] = new CustomVertex.PositionColored(0.5f, -0.5f, 0.0f, Color.Red.ToArgb());
// array elements swapped, now vertices are in other order
buffer.SetData(verts, 0, LockFlags.None);
}

or try to experiment with _device.renderstate.culling=cullmode.none or directly go to world of mesh rendering - I've skipped drawing of triangles in my path of directx expirence and i didnt loose anything. i can recommend you to read a book "managed directx: game & graphics programming. kick start" [sams publ.]. there are lots of information that can be helpful for you and either many things about drawing triangles ;-)




Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Punkoff

Here's a sample code I've found in the depths of my HDD for drawing a single rotating triangle (C#, mdx 1.1)

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;

namespace Chapter1Code
{
public class Form1 : System.Windows.Forms.Form
{
private Device device = null;
private System.ComponentModel.Container components = null;
private float angle = 0.0f;

public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}

public void InitializeGraphics()
{
PresentParameters presentParams = new PresentParameters();

presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}

private void SetupCamera()
{
device.RenderState.CullMode = Cull.None;
device.Transform.World = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.0f), angle / ((float)Math.PI * 4.0f), angle / ((float)Math.PI * 6.0f)), angle / (float)Math.PI);
angle += 0.1f;

device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 5.0f), new Vector3(), new Vector3(0,1,0));
device.RenderState.Lighting = false;
}

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

SetupCamera();

CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
verts[0].SetPosition(new Vector3(0.0f, 1.0f, 1.0f));
verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
verts[1].SetPosition(new Vector3(-1.0f, -1.0f, 1.0f));
verts[1].Color = System.Drawing.Color.Black.ToArgb();
verts[2].SetPosition(new Vector3(1.0f, -1.0f, 1.0f));
verts[2].Color = System.Drawing.Color.Purple.ToArgb();

device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
device.EndScene();

device.Present();

this.Invalidate();
}

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

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}

static void Main()
{
using (Form1 frm = new Form1())
{
// Show our form and initialize our graphics engine
frm.Show();
frm.InitializeGraphics();
Application.Run(frm);
}
}
}
}





Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Marcos Dell Antonio de Souza

Oooooooo man! THANK YOu! :-)

Now I can see the triangle! I've just changed the array position, as you said. Very good :P

Thanks a lot.

About the book, I'm reading this book. A very good book.

Thanks again.

Bye.




Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Marcos Dell Antonio de Souza

Just to get a full answear: why my solution does not work Tks.




Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Marcos Dell Antonio de Souza

Punkoff, thanks again for your solution.

Now i'm trying to use the Index Buffer.

I wrote the following code:

private void OnVertexBufferCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;

CustomVertex.PositionColored[] verts = new CustomVertex.PositionColoredMusic;
verts[0] = new CustomVertex.PositionColored(-0.5f, 0.5f, 0.5f, Color.Purple.ToArgb());
verts[1] = new CustomVertex.PositionColored(-0.5f, -0.5f, 0.5f, Color.Red.ToArgb());
verts[2] = new CustomVertex.PositionColored(0.5f, 0.5f, 0.5f, Color.Blue.ToArgb());
verts[3] = new CustomVertex.PositionColored(0.5f, -0.5f, 0.5f, Color.Yellow.ToArgb());
verts[4] = new CustomVertex.PositionColored(-0.5f, 0.5f, -0.5f, Color.Gold.ToArgb());
verts[5] = new CustomVertex.PositionColored(0.5f, 0.5f, -0.5f, Color.Green.ToArgb());
vertsDevil = new CustomVertex.PositionColored(-0.5f, -0.5f, -0.5f, Color.Black.ToArgb());
verts[7] = new CustomVertex.PositionColored(0.5f, -0.5f, -0.5f, Color.WhiteSmoke.ToArgb());

buffer.SetData(verts, 0, LockFlags.None);
}

And this index:

private static readonly short[] indices = {
0,1,2, // Front Face
1,3,2, // Front Face
4,5,6, // Back Face
6,5,7, // Back Face
0,5,4, // Top Face
0,2,5, // Top Face
1,6,7, // Bottom Face
1,7,3, // Bottom Face
0,6,1, // Left Face
4,6,0, // Left Face
2,3,7, // Right Face
5,2,7 // Right Face
};

When I try to render this using the following View, I can't see anything.

View:

_device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

If I remove the Z coord from the vertex buffer (set it to 0), I can see a box, but not 3D :S

Can you help me

Thanks again.




Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Punkoff

Try
_device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
or
_device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

or use meshes
(to render a box)
[initializing code]
public mesh mybox;
public material boxmat;

mybox=mesh.box(1,1,1); // 1,1,1 : size

boxmat.diffuse=color.green;
boxmat.ambient=color.green;

[render code]
_device.material=boxmat;
mybox.drawsubset(0);

It's all




Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Marcos Dell Antonio

Punkff, I agreee with you: meshes are the best! :-)

But for now I'm trying to use IndexBuffers because I need to load an OBJ file (Wavefront format).

Man, let me ask you another question... if you should have to load an OBJ File, you would use VertexBuffers and IndexBuffer or could you use Meshes too

Thanks a lot! :-)

Ps: now, I understood what is Culling hehe. Thanks ;)




Re: Game Technologies: DirectX 101 How to use View and Projection matrix?

Punkoff

If you have a buffer, you may can (or no ) convert it to system.io.stream and then use<br>&nbsp;mesh.fromstream <br>to create a mesh with you vertices data<br><br>But i think better way is to download Panda DirectX Max Exporter (<a href="http://www.andytather.co.uk/Panda/directxmax_downloads.aspx">here</a>) and convert your .OBJ to .X - it'll work much faster when loading objects in your app