how can I draw more than one line??

hi every body

i implment small program for draw line

the user specify the first and end point and he can use right button to show menu cang color ,think or pattren

the code work proprtly but

i want allowed to user draw more than one line
and change the proprites of line independent way than the others

how i can do that?

this is my code

#include <gl/glut.h>
float red=1.0,blue=0.0,green=0.0,width=1;
int con=0,x1=0,x2=0,y1=0,y2=0,patt=0;

//***********
void selectcolor (int selection )
{ switch (selection)
{ case 1: red=1.0; blue=0.0;green=1.0;
break;
case 2: red=0.0 ; blue=0.0; green=1.0;
break;
case 3: red=0.0;blue=1.0;green=0.0;
break;}
glutPostRedisplay();}

//**************
void selectionstipp (int selectstripp)
{ patt=selectstripp;
glutPostRedisplay();}

//*************
void selectwidth(int selectwidth)
{switch (selectwidth)
{case 7:width=7;
break;
case 8: width=9;
break;
case 9: width=11;
break;
}
glutPostRedisplay();}
//******************* main menu
void select (int selectmain)
{switch(selectmain)
{case 0:exit(0);}
glutPostRedisplay();}
//************************
void reshape(int w,int h)
{glViewport(0,0,300,300);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,300,0.0,300.0);
}

//*****************
void display (void)
{glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LINE_STIPPLE);
switch(patt)
{case 4:
glLineStipple(1,0xFF00);//dash
break;
case 5:glLineStipple(1,0xE4E4);//dot dash
break;
case 6: glLineStipple(1,0xFFFF);//SOLID
break;}
glLineWidth(width);

//draw line
glBegin(GL_LINES);
glColor3f(red,blue,green);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();

glDisable(GL_LINE_STIPPLE);
glFlush();
}

void init (void)
{glClearColor(1.0,1.0,1.0,0);
}
//******************
void mouse (int botton ,int state ,int x , int y)
{
if( (botton==GLUT_LEFT_BOTTON)&&(state==GLUT_DOWN))
if( con==0)
{ x1=x;x2=x;y1=300-y;y2=300-y;con=con+1;}
else {x2=x;y2=300-y; con=con-1;}

glutPostRedisplay();}

//******************
int main (int argc , char **argv)
{ glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
//CREAT WINDOW
glutInitWindowSize(300,300);
glutInitWindowPosition(0,0);
glutCreateWindow(“LINE STIPPLE”);

int menucolor=glutCreateMenu(selectcolor);//submenu for color
glutAddMenuEntry(“RED”,1);
glutAddMenuEntry(“BLUE”,2);
glutAddMenuEntry(“GREEN”,3 );

int menustrippt=glutCreateMenu(selectionstipp);
glutAddMenuEntry(“DASH”,4);
glutAddMenuEntry(“DOT_DASH”,5);
glutAddMenuEntry(“SOLID”,6);

int menuwidth=glutCreateMenu(selectwidth);

glutAddMenuEntry(“WIDTH 1”,7);
glutAddMenuEntry(“WIDTH”,8);
glutAddMenuEntry(“WIDTH”,9);

glutCreateMenu(select);
glutAddSubMenu(“COLOR”,menucolor);
glutAddSubMenu(“pattren”,menustrippt);
glutAddSubMenu(“withness”,menuwidth);
glutAddMenuEntry(“QUIT”,0);

glutAttachMenu(GLUT_RIGHT_BOTTON);
glutMouseFunc(mouse);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;}

===============
my program when user want drae another line the first line earse

regards
MOON_GIRL

[This message has been edited by MOON-GIRL (edited 12-02-2002).]

pleeeeeeeeeeeeeeeeeeeeese any body help me

The problem you are having is that you are misunderstanding how OpenGL renders. Remember that everything is drawn each time you request a redisplay. So, in your case you clear the colour buffer and then draw the line again, but with the new values!

What you need to do (as an example) is to store the coordinates of each line as they are generated let’s say in two arrays xv, and yv. You keep a count of how many lines you have stored. You may also want to store other attributes of each line as well in other arrays.

Then in your display function, you run through these arrays drawing lines, up to the number of lines you have stored.

Of course, this is a VERY simple case.

You will learn that as you go on with OpenGL programming (and graphics programming in general) the data to be displayed on screen is held in a “scene description”. There are various ways this is done and will depend of the application in question. But, in the end, the actual data drawn on screen will be done via processing of the scene description.

Hope this helps,

Good luck - an assignment I guess?

Rob.

welcome Rob

its true I mis understand open gl but I try
and a wait help from any body

=================
when i dont know how lines user want draw I use linked list?for example

and the node contine structured or not allowed??

regards
Moon_Girl

CAN you give me any complete code
i want see how its work

regards
Moon_Girl

I just got done with a 2D drawing app in windows. I did exactly what the previous person said. here is some sample code from my programs “OnDraw” function:

for (int i=0; i < pDoc->NumLines; i++)
	{
		MakePen(pDoc->LineWidth[i], pDoc->LineColor[i]);
		pDC->SelectObject(&m_ThePen);
		pDC->MoveTo(pDoc->FLinePoint[i]);
		pDC->LineTo(pDoc->LLinePoint[i]);
	}

you see, I do a for loop for how many lines there are. then make a pen that was used (width, color) when line[i] was drawn. then draw it from the x points that were stored in the array, and y points that were stored in another array. all of these arrays are updated with the right properties once the user draws a line.

[This message has been edited by KenR7A (edited 12-03-2002).]

No time to write a whole program, but maybe this will give you an idea.

typedef struct
{
int x1, y1; //Start of line
int x2, y2; //End of line
glFloat color[3];
}LINE_STRUCT;

LINE_STRUCT lines[5]; // Where 5 is the number of lines we can store in our structure, could be a larger number

if (mouse_down)
{
lines[line_count].x1 = mouse_x;
lines[line_count].y1 = mouse_y;
}

if (mouse_up)
{
lines[line_count].x2 = mouse_x;
lines[line_count].y2 = mouse_y;
line_count++;
}

display()
{

for(i=0, i < line_count, i++)
{
glColor3fv(lines[i].color)
glBegin(GL_LINE);
glVertex2i(lines[i].x1, lines[i].y1);
glVertex2i(lines[i].x2, lines[i].y2);
glEnd();
}

Originally posted by MOON-GIRL:
[b]CAN you give me any complete code
i want see how its work

regards
Moon_Girl[/b]

HI;

welcome KenR7A

are use pointer?
can i see ahole program of “OnDraw”

welcome nexusone

i understand this code

but when i dont how line i need
i want user enter as its like

CAN EXPLINE WHAT IS THIS DO
glfloat color[3]

thank you every boady
and i wait you

Moon_Girl

[This message has been edited by MOON-GIRL (edited 12-03-2002).]

its the pointer to the DOC class in my 2D drawing program. its no code to put in a GL project, I was just showing you what you had to do in order for your app to remember what lines were drawn and how to re-draw them when you need to. just an idea, not an actual solution.

welcome

thank you

but i still have problem with my program

Some person also give me coding using list until this moment dosnt work

regards
Moon_Girl

The color of your line and the [3] is for the three RGB color values:

lines[0].color[0] = red values
lines[0].color[1] = green values
lines[0].color[2] = blue values

glColor3f(lines[0].color[0],lines[0].color[1],lines[0].color[2]);

I think color also can be done like so:
glColor3fv(&lines[0].color);

Originally posted by MOON-GIRL:
[b]HI;

welcome KenR7A

are use pointer?
can i see ahole program of “OnDraw”

welcome nexusone

i understand this code

but when i dont how line i need
i want user enter as its like

CAN EXPLINE WHAT IS THIS DO
glfloat color[3]

thank you every boady
and i wait you

Moon_Girl

[This message has been edited by MOON-GIRL (edited 12-03-2002).][/b]

THANK YOU nexusone FOR EXPLINE