View Full Version : ميخوام چند تا خط بكشم
saed2006
چهارشنبه 03 شهریور 1389, 11:53 صبح
سلام
دو تا نقطه مختصات دارم ميخوام با direx با استفاده از انها يك خط بكشم
كد زير چرا كار نميكنه؟
saed2006
چهارشنبه 03 شهریور 1389, 13:20 عصر
اين كد بايد يه خط رسم كنه اما هيچ كاري تميكنه
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.RenderState.CullMode = Cull.None;
device.RenderState.FillMode = FillMode.WireFrame;
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -30), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;
vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 2, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
vertices = new CustomVertex.PositionColored[2];
vertices[0].Position = new Vector3(0f, 0f, 0f);
vertices[0].Color = Color.White.ToArgb();
vertices[1].Position = new Vector3(10f, 10f, 0f);
vertices[1].Color = Color.White.ToArgb();
vb.SetData(vertices, 0, LockFlags.None);
device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.SetStreamSource(0, vb, 0);
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
device.EndScene();
this.Invalidate();
khorzu
چهارشنبه 03 شهریور 1389, 13:48 عصر
public partial class Form1 : Form
{
Device device;
Line lineRenderer;
Vector3[] points = new Vector3[2];
public Form1()
{
InitializeComponent();
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -30), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
//___________________________________
//-----------------------------------
lineRenderer = new Line(device);
points[0] = new Vector3(0, 0, 0);
points[1] = new Vector3(0.5f, 0.5f, 0.5f);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
device.Clear(ClearFlags.Target, Color.DarkBlue, 1, 0);
device.BeginScene();
lineRenderer.DrawTransform(points, device.Transform.World, Color.White);
device.EndScene();
device.Present();
}
saed2006
پنج شنبه 04 شهریور 1389, 09:39 صبح
حالا اگر تعداد اين خطوط به 10 ميليون برسه اين كدي كه شما نوشتين زمان زيادي خواهد برد راه حل چيه؟
khorzu
پنج شنبه 04 شهریور 1389, 10:08 صبح
Vector3[] points = new Vector3[10000000];
به صورت LineStrip رسمشون می کنه .
saed2006
پنج شنبه 04 شهریور 1389, 10:20 صبح
ميشه كدشو بدين؟
اين دستورات كار رو با حداكثر سرعت انجرا ميده؟
saed2006
پنج شنبه 04 شهریور 1389, 10:39 صبح
اگر تعداد خط ها زياد بشه اين تيكه كد:
device.BeginScene();
lineRenderer.DrawTransform(points, device.Transform.World, Color.White);
device.EndScene();
توي حلقه قرار ميگيره كه اين زمان زيادي ميبره درسته؟
khorzu
پنج شنبه 04 شهریور 1389, 10:44 صبح
اینو جای
Vector3[] points = new Vector3[2];ریپلیس کن و اعضای آرایتو مقدار دهی کن:
points[0] = new Vector3(0, 0, 0);
points[1] = new Vector3(0.5f, 0.5f, 0.5f);
...تغییر دیگه ای نمی خواد .
این بهترین حالت نیست . اگه دارید تولست می نویسید نگران سرعت نباشید ... اما اگه برای اپلیکیشن نهایی به رسم خطوط نیاز دارید اگه بجای آرایه دات نت از بافر DX استفاده کنید حتما سرعت بیشتر می شه .
saed2006
پنج شنبه 04 شهریور 1389, 10:55 صبح
بيبينيد برنامه بايد بالاترين سرعت رو براي رسم n خط به هم مرتبط كه در يك ارايه m عضوي ريخته شده اند رو رسم كنه
من دستوراتي رو ميخوام كه بالاترين سرعت رو داشته باشه
khorzu
پنج شنبه 04 شهریور 1389, 11:07 صبح
به نظرم این کد بهترین سرعتو واسه این کار بده :
int pointsCount = 10000000;
VertexBuffer vb = new VertexBuffer(typeof(CustomVertex.PositionColored), pointsCount, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Default);
CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[pointsCount];
//
// Initing vertices
//
vb.SetData(vertices, 0, LockFlags.None);
//In RenderLoop :
device.BeginScene();
device.SetStreamSource(0, vb, 0);
device.DrawPrimitives(PrimitiveType.LineStrip, 0, pointsCount);
device.EndScene();
همچنین اگه سرعت خیلی حیاتیه انجام پروژه روی #C درست نیست .
saed2006
پنج شنبه 04 شهریور 1389, 11:17 صبح
اينجا
device.Clear(ClearFlags.Target, Color.DarkBlue, 1, 0);
device.BeginScene();
device.SetStreamSource(0, vb, 0);
device.DrawPrimitives(PrimitiveType.LineStrip, 0, 2);
device.EndScene();
device.Present();
اين خطا رو ميده
Error in the application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
CustomVertex.PositionColored[] vertices;
VertexBuffer vb;
Microsoft.DirectX.Direct3D.Device device;
Line lineRenderer;
Vector3[] points;
public Form1()
{
InitializeComponent();
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -30), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
//___________________________________
//-----------------------------------
int pointsCount = 2;
vb = new VertexBuffer(typeof(CustomVertex.PositionColored), pointsCount, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Default);
vertices = new CustomVertex.PositionColored[pointsCount];
vertices[0].X = 0;
vertices[0].Y = 0;
vertices[0].Color = 0;
vertices[1].X = 1;
vertices[1].Y = 1;
vertices[1].Color = 0;
vb.SetData(vertices, 0, LockFlags.None);
//lineRenderer = new Line(device);
//points = new Vector3[4];
//points[0] = new Vector3(0, 0, 0);
//points[1] = new Vector3(0.0f, 0.5f, 0.5f);
//points[2] = new Vector3(0.5f, .5f, 0.5f);
//points[2] = new Vector3(0.5f, .5f, 0.5f);
}
private void Form1_Paint_1(object sender, PaintEventArgs e)
{
Drwlines();
}
private void Drwlines()
{
device.Clear(ClearFlags.Target, Color.DarkBlue, 1, 0);
device.BeginScene();
device.SetStreamSource(0, vb, 0);
device.DrawPrimitives(PrimitiveType.LineStrip, 0, 2);
device.EndScene();
device.Present();
}
}
}
khorzu
پنج شنبه 04 شهریور 1389, 11:36 صبح
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.SetStreamSource(0, vb, 0);
device.DrawPrimitives(PrimitiveType.LineStrip, 0, pointsCount);
device.EndScene();
saed2006
پنج شنبه 04 شهریور 1389, 11:42 صبح
اقا اين كد كار نميكنه ميشه يه كد قابل اجرا بدين
مشكل اين كد چيه اخهههههههههههههههههههههه
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
CustomVertex.PositionColored[] vertices;
VertexBuffer vb;
Microsoft.DirectX.Direct3D.Device device;
Line lineRenderer;
Vector3[] points;
public Form1()
{
InitializeComponent();
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -30), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
//___________________________________
//-----------------------------------
int pointsCount = 2;
vb = new VertexBuffer(typeof(CustomVertex.PositionColored), pointsCount, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Default);
device.VertexFormat = CustomVertex.PositionColored.Format;
vertices = new CustomVertex.PositionColored[pointsCount];
vertices[0].Position = new Vector3(0, 0, 0);
vertices[0].X = 0;
vertices[0].Y = 0;
vertices[0].Color = 800;
vertices[1].X = 1;
vertices[1].Y = 1;
vertices[1].Color = 800;
vertices[1].Position = new Vector3(10, 10, 0);
}
private void Form1_Paint_1(object sender, PaintEventArgs e)
{
Drwlines();
}
private void Drwlines()
{
device.Clear(ClearFlags.Target, Color.DarkBlue, 1, 0);
device.BeginScene();
device.SetStreamSource(0, vb, 0);
vb.SetData(vertices, 0, LockFlags.None);
device.DrawPrimitives(PrimitiveType.LineStrip, 0, 2);
device.EndScene();
this.Invalidate();
}
}
}
khorzu
پنج شنبه 04 شهریور 1389, 11:58 صبح
____________________________________________
saed2006
پنج شنبه 04 شهریور 1389, 12:06 عصر
مشكل اين برنامه چيه اخه كه اصلا كار نميكنه
khorzu
پنج شنبه 04 شهریور 1389, 12:13 عصر
:عصبانی++:
زیر BeginScene اینو کم داره :
device.VertexFormat = CustomVertex.PositionColored.Format;
saed2006
پنج شنبه 04 شهریور 1389, 12:18 عصر
اضافه كردم درست نشد
khorzu
پنج شنبه 04 شهریور 1389, 12:19 عصر
و این اضافیه :
vb.SetData(vertices, 0, LockFlags.None);
:گریه:
saed2006
پنج شنبه 04 شهریور 1389, 12:22 عصر
هيچ فرقي نكرد حذف كردمش
khorzu
پنج شنبه 04 شهریور 1389, 12:23 عصر
پرزنت هم نکردی ... بعد از EndScene :
device.Present();
saed2006
پنج شنبه 04 شهریور 1389, 12:28 عصر
اضافه كردم ولي باز هم هيچ خطي رسم نميكنه
khorzu
پنج شنبه 04 شهریور 1389, 12:39 عصر
:عصبانی:
با آزمون و خطا که نمی شه DX رو یاد گرفت . باید بدونی داری چی کار می کنی .
پروژه ای رو که آپلود کردم روی pc من کار می کنه . امیدوارم با اون کارت راه بیفته .
saed2006
پنج شنبه 04 شهریور 1389, 12:44 عصر
من اينو نوشتم ولي كار نميكنه
khorzu
پنج شنبه 04 شهریور 1389, 13:19 عصر
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
CustomVertex.PositionColored[] vertices;
VertexBuffer vb;
Microsoft.DirectX.Direct3D.Device device;
Line lineRenderer;
Vector3[] points;
public Form1()
{
InitializeComponent();
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -30), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
>> device.RenderState.Lighting = false;
//___________________________________
//-----------------------------------
int pointsCount = 3;
vb = new VertexBuffer(typeof(CustomVertex.PositionColored), pointsCount, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Default);
vertices = new CustomVertex.PositionColored[pointsCount];
>> vertices[0].Position = new Vector3(0, 0, 0f);
vertices[0].Color = Color.Red.ToArgb();
>> vertices[1].Position = new Vector3(5, 5, 0);
vertices[1].Color = Color.Green.ToArgb();
>> vertices[2].Position = new Vector3(0, 5, 0f);
vertices[2].Color = Color.Yellow.ToArgb();
vb.SetData(vertices, 0, LockFlags.None);
}
private void Form1_Paint_1(object sender, PaintEventArgs e)
{
Drwlines();
}
private void Drwlines()
{
>> device.Clear(ClearFlags.Target, Color.Gray, 1, 0);
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.SetStreamSource(0, vb, 0);
device.DrawPrimitives(PrimitiveType.LineStrip, 0, vertices.Length - 1);
device.EndScene();
device.Present();
}
}
}
khorzu
پنج شنبه 04 شهریور 1389, 13:24 عصر
مختصات رئوس معادل مختصات پیکسل های اسکرین نیست . برای رسم رئوس در مختصات اسکرین باید ماتریس تابش دستگاه رو بجای پرسپکتویو به حالت اورتو گرافیک مقدار دهی کنید .
saed2006
پنج شنبه 04 شهریور 1389, 13:54 عصر
مختصات رئوس معادل مختصات پیکسل های اسکرین نیست . برای رسم رئوس در مختصات اسکرین باید ماتریس تابش دستگاه رو بجای پرسپکتویو به حالت اورتو گرافیک مقدار دهی کنید .
ميشه لينكي يا منبع اموزشي چيزي در اين مورد بهم بدين
و اينكه الان من چند تا مجموعه خط دارم نه يكي
مثلا يك مجموعه خط با 5 تا نقطه
و مجموعه خط هايي با نقاط مختلف
چجوري بايد مشخص كنم اين نقاط مال مجموعه نقطه 1 هست و همينطوري الي اخر
khorzu
پنج شنبه 04 شهریور 1389, 14:58 عصر
برای تابش اورتوگرافیک :
device.Transform.Projection = Matrix.OrthoLH((float)this.Width, (float)this.Height, 1f, 5000f);به جای :
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);توی این حالت یک واحد معادل یک پیکسل می شه .
برای تفکیک دسته خطوط فکر کنم راهی جز رنگ بندی گروه خطوط نباشه .:متفکر:
saed2006
پنج شنبه 04 شهریور 1389, 17:45 عصر
برای تفکیک دسته خطوط فکر کنم راهی جز رنگ بندی گروه خطوط نباشه .:متفکر:
میشه بیشتر توضیح بدین
khorzu
پنج شنبه 04 شهریور 1389, 18:20 عصر
خوب هر دسته یک رنگ . یعنی مولفه Color رئوس یک دسته خط یکی باشند .
saed2006
جمعه 05 شهریور 1389, 12:20 عصر
الان با دستوراتی که نوشتید ما یک ارایه از نقاط رو میدیم و اون تبدیل میکنه به یک سری خط ÷یوسته
سوال من اینه که:
حالا اگر ما یک ارایه ای از ارایه ای از نقاط داشته باشیم و بخوایم ارایه ای از خطوط به هم ÷یوسته ترسیم کنیم با این دستوراتی که شما نوشتین چجوری میشه این کار رو کرد؟
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.