PDA

View Full Version : not able to draw lines btw objects already drwn



kkamal
04-26-2010, 11:33 AM
i am drawing a line between two objects when ever mouse click is done over two objects. for first set of objects the line is drawn nicely but when i do it for other set,the line drawn previously gets deleted.
plz help ..... i want to draw lines one after another without the previous one getting deleted.


//the form1.cs code follows
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Tao.FreeGlut;
using Tao.OpenGl;
using Tao.Platform.Windows;

namespace pickit2
{
public partial class Form1 : Form
{
static int wh, ww;

public Form1()
{
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();

Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_RGB | Glut.GLUT_DEPTH);
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//for Background
Gl.glColor3f(1.0f, 1.0f, 1.0f);//colour for the primitive
Gl.glPointSize(5.0f);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Gl.glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
Gl.glEnable(Gl.GL_DEPTH_TEST);
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void simpleOpenGlControl1_Load(object sender, EventArgs e)
{

resiz(this.Height, this.Width);
ww = simpleOpenGlControl1.Width;
wh = simpleOpenGlControl1.Height;
}
const float r =0.5f;
static void resiz(int h, int w)
{

Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
if (h == 0)
{
h = 1;
}

if (w <= h)
{
Gl.glOrtho(-r, r, -r * h / w, r * h / w, -r, r);
}
else
{
Gl.glOrtho(-r * w / h, r * w / h, -r, r, -r, r);
}
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();

}
static bool btn1 = false;
static bool btn2 = false;
static bool btn3 = false;
static bool connector = false;
static float x1, x2, y1, y2;

public static void draw()
{

Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

if (btn1 == true)
{
Gl.glPushMatrix();
Gl.glTranslatef(0.3f, 0, 0);
Glut.glutWireCube(0.1);

Gl.glPopMatrix();
Glut.glutPostOverlayRedisplay();
}
if (btn2 == true)
{
Gl.glPushMatrix();
Gl.glTranslatef(-0.3f, 0, 0);
Glut.glutWireCube(0.1);

Gl.glPopMatrix();
Glut.glutPostOverlayRedisplay();
}
if (btn3 == true)
{
Gl.glPushMatrix();
Gl.glTranslatef(0.0f, 0.3f, 0.0f);
Glut.glutWireCube(0.1);

Gl.glPopMatrix();
Glut.glutPostOverlayRedisplay();
}
connector_func();

}

public static void connector_func()
{

if (connector == true)
{
Gl.glPushMatrix();
Gl.glBegin(Gl.GL_LINES);
Gl.glVertex3f(x1, y1, 0);
Gl.glVertex3f(x2, y2, 0);
Gl.glEnd();
Gl.glFlush();
Gl.glPopMatrix();
Glut.glutPostOverlayRedisplay();
connector = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Refresh();
}

private void button1_Click(object sender, EventArgs e)
{
btn1 = true;
draw();
}

private void button2_Click(object sender, EventArgs e)
{
btn2 = true;
draw();
}

private void button3_Click(object sender, EventArgs e)
{
btn3 = true;
draw();
}

private void button4_Click(object sender, EventArgs e)
{
connector = true;
}
static bool start = true;
private void simpleOpenGlControl1_MouseClick(object sender, MouseEventArgs e)
{
if (connector == true)
{
if (start == true)
{
if (((e.X <= 165) &amp;&amp; (e.X >= 117)) || ((e.X <= 451) &amp;&amp; (e.X >= 405)) || ((e.X <= 309) &amp;&amp; (e.X >= 262)))
{
x1 = (((float)e.X - (ww / 2)) / 1000) * 2f;
label1.Text = x1.ToString();
}
if ((e.Y <= 300) &amp;&amp; (e.Y >= 246) || ((e.Y <= 302) &amp;&amp; (e.Y >= 246)) || ((e.Y <= 135) &amp;&amp; (e.Y >= 81)))
{
y1 = (((wh / 2) - (float)e.Y) / 1000) * 1.8f;
label2.Text = y1.ToString();
}

start = false;
}
else
{
if (((e.X <= 165) &amp;&amp; (e.X >= 117)) || ((e.X <= 451) &amp;&amp; (e.X >= 405)) || ((e.X <= 309) &amp;&amp; (e.X >= 262)))
{

x2 = (((float)e.X - (ww / 2)) / 1000) * 2f;
label3.Text = x2.ToString();
}
if ((e.Y <= 300) &amp;&amp; (e.Y >= 246) || ((e.Y <= 302) &amp;&amp; (e.Y >= 246)) || ((e.Y <= 135) &amp;&amp; (e.Y >= 81)))
{
y2 = (((wh / 2) - (float)e.Y) / 1000) * 1.8f;
label4.Text = y2.ToString();
}


start = true;
draw();
}

}

}

private void simpleOpenGlControl1_MouseMove(object sender, MouseEventArgs e)
{
label5.Text = e.X.ToString();
label6.Text = e.Y.ToString();
}

private void label6_Click(object sender, EventArgs e)
{

}


}
}

Exoide
04-26-2010, 01:24 PM
Hi kkmal,

Your problem is that you don't store the previous lines and the Draw() method only draw one line (the last one) so you need to store every line and draw all of them.

I think you can create a struct like this


struct Line
{
float x1, y1; // Start point
float x2, y2; // End point
}

List<Line> Lines; // Where the lines will be stored

And in your Draw() method draw all the lines in "Lines".