Polyline using Mouse Interaction


/*
* Title: Creating a polyline using mouse interactions
* Name: Akshay Thakare
*/

#include <GL/glut.h>

struct GLintPoint
{ GLint x,y;
};

int Height=650, Width= 650;
void myMouse(int button, int state, int x, int y );

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush(); // Send all output to display
}

void myinit() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Set background as black
glColor3f(1.0, 1.0, 0.0); // Draw in Yellow
glMatrixMode(GL_PROJECTION); // Establish the coordinate system
glLoadIdentity();
gluOrtho2D(0.0, 650.0, 0.0, 650.0);
}
void myKeyboard(unsigned char key, int mouseX, int mouseY )
{
switch (key) {
case 27:
exit(0);
}
}
void myMouse(int button, int state, int x, int y )
{
static GLintPoint vertex [1];
static int pt = 0;
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if (pt == 0)
{
vertex [pt].x = x;
vertex [pt].y = Height- y;
pt++;
}
else
{
glBegin(GL_LINE_STRIP);
glVertex2i(vertex [0].x, vertex[0].y);
glVertex2i(x, Height- y);
glEnd();
vertex [0].x = x;
vertex [0].y = Height- y;
}
}
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); // Initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); // Set display mode
glutInitWindowSize(500, 500); // Set window size
glutInitWindowPosition(100,100); // Set window position on the screen
// Open the screen window
glutCreateWindow("Draw Polyline using Interaction using OpenGL");
glutDisplayFunc(display); // Register redraw function
glutKeyboardFunc(myKeyboard); // Register keyboard function
glutMouseFunc(myMouse); // Register mouse function
myinit();
glutMainLoop(); // go into a perpetual loop
return 0;
}

&nbsp;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s