İbrahim Ersoy

i need urgent help about drawing a rectangle.which classes shall i use

Thanks a lot




Re: Game Technologies: DirectX 101 How To Draw Rectangle?

windozer_

ibrahim,

Could you give a little more details on what you are trying to achieve Is this 2D (GDI) or 3D (DirectX) Do you want to use managed DirectX or DirectX9 I can post a code snippet for whatever you are trying to achieve. Thanks.

Cale





Re: Game Technologies: DirectX 101 How To Draw Rectangle?

?brahim Ersoy

windozer_,

it will be 3d but i think rectangle cant be a 3d shape .I m using Managed DirectX-C# 2.0

i have drawen a triangle but i couldnt draw a rectangle.what must i do

thanks a lot






Re: Game Technologies: DirectX 101 How To Draw Rectangle?

ivanik

hi Ibrahim..

when u draw triangle u need 3 vertice, when u draw rectangle u need 6 vertice but if u draw with indexBuffer u only need 4 vertice (if iam not wrong )

so when u create the vertex the code like this :

ex :

CustomVertex.PositionTextured[] vert = new CustomVertex.PositionTextured[ 6 ];

vert[0].X = -1.0f; vert[0].Y = 1.0f; vert[0].Z = 1.0f;

vert[1].X = 1.0f; vert[1].Y = 1.0f; vert[1].Z = 1.0f;

vert[2].X = -1.0f; vert[2].Y = -1.0f; vert[2].Z = 1.0f;

vert[3].X = -1.0f; vert[3].Y = -1.0f; vert[3].Z = 1.0f;

vert[4].X = 1.0f; vert[4].Y = 1.0f; vert[4].Z = 1.0f;

vert[5].X = 1.0f; vert[5].Y = -1.0f; vert[5].Z = 1.0f;

 

when u want to add texture coordinate u can write like this :

vert[0].Tu = 0.0f; vert[0].Tv = 0.0f;

vert[1].Tu = 1.0f; vert[1].Tv = 0.0f;

vert[2].Tu = 0.0f; vert[2].Tv = 1.0f;

vert[3].Tu = 0.0f; vert[3].Tv = 1.0f;

vert[4].Tu = 1.0f; vert[4].Tv = 0.0f;

vert[5].Tu = 1.0f; vert[5].Tv = 1.0f;

hope windozer_ tell u more..

 





Re: Game Technologies: DirectX 101 How To Draw Rectangle?

?brahim Ersoy

thanks a lot i will try the code






Re: Game Technologies: DirectX 101 How To Draw Rectangle?

ivanik

remember all 3D object created from triangle, the sample code i write is create 2 triangle with different position/size, so when u render this vertex the code like this :

device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);// the last parameter tell the device that we want draw 2 triangle





Re: Game Technologies: DirectX 101 How To Draw Rectangle?

IBRAHIM ERSOY

thanks a lot i was able to draw a rectangle and im giving the code;

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 Proje
{
public partial class Vertices : Form
{
Device device = null;
VertexBuffer vertex = null;


public Vertices()
{
this.ClientSize = new Size(400, 300);
this.Text = "Working With Vertices";

}

public bool InitializeGraphics()
{
try
{
PresentParameters par = new PresentParameters();
par.Windowed = true;
par.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, par);
this.OnCreateDevice(device, null);
return true;
}
catch (DirectXException)
{
return false;
}

}

public void OnCreateDevice(object sender, EventArgs e)
{
Device dev =(Device)sender;
vertex = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, dev, 0, CustomVertex.PositionTextured.Format, Pool.Default);
vertex.Created += new EventHandler(this.OnCreateVertexBuffer);
this.OnCreateVertexBuffer(vertex, null);


}

public void OnCreateVertexBuffer(object sender, EventArgs e)
{
VertexBuffer vb = (VertexBuffer)sender;
GraphicsStream str = vb.Lock(0, 0, 0);
CustomVertex.PositionTextured[] quad = new CustomVertex.PositionTextured[4];
quad[0] = new CustomVertex.PositionTextured(-1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
quad[1] = new CustomVertex.PositionTextured(-1.0f, 2.0f, 0.0f, 0.0f, 0.0f);
quad[2] = new CustomVertex.PositionTextured(1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
quad[3] = new CustomVertex.PositionTextured(1.0f, 2.0f, 0.0f, 1.0f, 0.0f);

str.Write(quad);
vb.Unlock();

}

private void Render()
{
if (device == null)
return;

device.Clear(ClearFlags.Target, Color.Chocolate, 1.0f, 0);
device.BeginScene();
device.SetStreamSource(0, vertex, 0);
device.VertexFormat = CustomVertex.PositionTextured.Format;
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);


device.EndScene();
device.Present();
}

protected override void OnPaint(PaintEventArgs e)
{
this.Render();

}

protected override void OnKeyPress(KeyPressEventArgs e)
{
if ((int)(byte)e.KeyChar ==(int) Keys.Escape)
this.Close();

}

static void Main()
{
using (Vertices frm = new Vertices())
{
if (!frm.InitializeGraphics())
{
MessageBox.Show("Direct3d is unable to load");
return;
}

frm.Show();

while (frm.Created)
{
frm.Render();
Application.DoEvents();
}
}

}


private void InitializeComponent()
{
this.SuspendLayout();
this.ClientSize = new Size(300, 400);
this.Name = "Vertices";
this.Load += new EventHandler(this.Vertices_Load);
this.ResumeLayout(false);


}

private void Vertices_Load(object sender, EventArgs e)
{

}
}
}