Hi All,
Some of the programs have been commented for better understanding.
This will help you to understand to program in OpenGL and also will help in understanding concepts related to input interaction.
We hope that CAT-1 exams were good.. :)
With this mail, we are attaching few OpenGL programs on Input Interaction.
Some of the programs have been commented for better understanding.
Go through these simple programs thoroughly and try to understand the flow.
This will help you to understand to program in OpenGL and also will help in understanding concepts related to input interaction.
Reply back to this mail in case of any queries.
Thanks,
Govind N. & Vishal K.
Govind N. & Vishal K.
Program 1 :
#include<stdio.h>
#include<GL/glut.h>
int width=400, height=400; /* global varibles to hold width and height */
void myinit() /* function to initialize the attributes
and states of OpenGL */
{
/* attributes */
glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
glColor3f(1.0, 0.0, 0.0); /* draw in red */
/* set up viewing */
/* width x height window with origin lower left */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)width, 0.0, (GLdouble)height); /* setup viewing rectangle
equal to window width and height */
glMatrixMode(GL_MODELVIEW);
}
void drawPoint(int x, int y) /* user defined function to draw a point */
{
/* Draw the points using the values of x and y */
glPointSize(4.0); /* set the size of pixels to 4 */
glBegin(GL_POINTS);
glVertex2i(x, (height-y)); /* convert value of y from viewport co-ordinates
to world (screen) co-ordinates */
glEnd(); /* end the procedure */
glFlush(); /* flush contente of frame buffer to o/p device */
}
void display() /* called when window is opened */
{
glClear(GL_COLOR_BUFFER_BIT); /* clear the contents of color buffer */
glFlush(); /* flush contente of frame buffer to o/p device */
}
void myMouse(int btn, int state, int x, int y) /* called when mouse is clicked */
{
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) /* will be true when left button is clicked
and button is held down */
{
drawPoint(x, y); /* call user-defined function to draw a point on window */
}
}
int main(int argc, char** argv)
{
/* Standard GLUT initialization */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
glutInitWindowSize(width,height); /* width x height pixel window */
glutInitWindowPosition(0,0); /* place window top left on display */
glutCreateWindow("Draw Points"); /* window title */
myinit(); /* set attributes */
glutDisplayFunc(display); /* display callback invoked when window opened */
glutMouseFunc(myMouse); /* mouse callback invoked when mouse clicked */
glutMainLoop(); /* enter event loop */
return 0;
}
Program 2 :
#include<stdio.h>
#include<GL/glut.h>
float x1[10], y1[10]; /* arrays to store co-ordinates of points that are drawn
on the winodw */
int width=400, height=400; /* global varibles to hold width and height */
int click=0; /* counter to keep track of no.of points drawn on the window */
void myinit() /* function to initialize the attributes
and states of OpenGL */
{
/* attributes */
glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
glColor3f(1.0, 0.0, 0.0); /* draw in red */
/* set up viewing */
/* width x height window with origin lower left */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)width, 0.0, (GLdouble)height); /* setup viewing rectangle
equal to window width and height */
glMatrixMode(GL_MODELVIEW);
}
void drawPoint(int x, int y) /* user defined function to draw a point */
{
/* Draw the points using the values of x and y */
glPointSize(4.0); /* set the size of pixels to 4 */
glBegin(GL_POINTS);
glVertex2i(x, (height-y)); /* convert value of y from viewport co-ordinates
to world (screen) co-ordinates */
glEnd(); /* end the procedure */
glFlush(); /* flush contente of frame buffer to o/p device */
}
void display() /* called when window is opened */
{
glClear(GL_COLOR_BUFFER_BIT); /* clear the contents of color buffer */
glFlush(); /* flush contente of frame buffer to o/p device */
}
void myMouse(int btn, int state, int x, int y) /* called when mouse is clicked */
{
int i;
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) /* will be true when left button is clicked
and button is held down */
{
x1[click]=x; /* store each values of x in an array */
y1[click]=(height-y); /* sotre each values of y in an array */
click++; /* increment the counter */
drawPoint(x, y); /* draw the point on the window */
}
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) /* when right button is clicked */
{
glBegin(GL_LINE_LOOP);
for(i=0;i<click;i++) /* loop through all the points */
glVertex2i(x1[i], y1[i]); /* draw the lines between all the points */
glEnd();
glFlush();
}
if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) /* when middle button is clicked */
{
glutPostRedisplay(); /* calls display callback function, display,
which clears the screen */
click = 0; /* clear the counter */
}
}
int main(int argc, char** argv)
{
/* Standard GLUT initialization */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
glutInitWindowSize(width,height); /* width x height pixel window */
glutInitWindowPosition(0,0); /* place window top left on display */
glutCreateWindow("Draw Lines using Points"); /* window title */
glutDisplayFunc(display); /* display callback invoked when window opened */
myinit(); /* set attributes */
glutMouseFunc(myMouse); /* mouse callback invoked when mouse clicked */
glutMainLoop(); /* enter event loop */
return 0;
}
Program 3 :
#include<stdio.h>
#include<GL/glut.h>
float x1[10], y1[10]; /* arrays to store co-ordinates of points that are drawn
on the winodw */
int width=400, height=400; /* global varibles to hold width and height */
int click=0; /* counter to keep track of no.of points drawn on the window */
void myinit() /* function to initialize the attributes
and states of OpenGL */
{
/* attributes */
glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
glColor3f(1.0, 0.0, 0.0); /* draw in red */
/* set up viewing */
/* width x height window with origin lower left */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)width, 0.0, (GLdouble)height); /* setup viewing rectangle
equal to window width and height */
glMatrixMode(GL_MODELVIEW);
}
void drawPoint(int x, int y) /* user defined function to draw a point */
{
/* Draw the points using the values of x and y */
glPointSize(4.0); /* set the size of pixels to 4 */
glBegin(GL_POINTS);
glVertex2i(x, (height-y)); /* convert value of y from viewport co-ordinates
to world (screen) co-ordinates */
glEnd(); /* end the procedure */
glFlush(); /* flush contente of frame buffer to o/p device */
}
void display() /* called when window is opened */
{
glClear(GL_COLOR_BUFFER_BIT); /* clear the contents of color buffer */
glFlush(); /* flush contente of frame buffer to o/p device */
}
void myMouse(int btn, int state, int x, int y) /* called when mouse is clicked */
{
int i;
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) /* will be true when left button is clicked
and button is held down */
{
x1[click]=x; /* store each values of x in an array */
y1[click]=(height-y); /* sotre each values of y in an array */
click++; /* increment the counter */
drawPoint(x, y); /* draw the point on the window */
}
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) /* when right button is clicked */
{
glBegin(GL_LINE_LOOP);
for(i=0;i<click;i++) /* loop through all the points */
glVertex2i(x1[i], y1[i]); /* draw the lines between all the points */
glEnd();
glFlush();
}
if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) /* when middle button is clicked */
{
glutPostRedisplay(); /* calls display callback function, display,
which clears the screen */
click = 0; /* clear the counter */
}
}
int main(int argc, char** argv)
{
/* Standard GLUT initialization */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
glutInitWindowSize(width,height); /* width x height pixel window */
glutInitWindowPosition(0,0); /* place window top left on display */
glutCreateWindow("Draw Lines using Points"); /* window title */
glutDisplayFunc(display); /* display callback invoked when window opened */
myinit(); /* set attributes */
glutMouseFunc(myMouse); /* mouse callback invoked when mouse clicked */
glutMainLoop(); /* enter event loop */
return 0;
}
Program 3:
#include<stdio.h>
#include<GL/glut.h>
float x1[10], y1[10]; /* arrays to store co-ordinates of points that are drawn
on the winodw */
int width=400, height=400; /* global varibles to hold width and height */
int click=0; /* counter to keep track of no.of points drawn on the window */
void myinit()
{
/* attributes */
glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
glColor3f(1.0, 0.0, 0.0); /* draw in red */
/* set up viewing */
/* width x height window with origin lower left */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)width, 0.0, (GLdouble)height);
glMatrixMode(GL_MODELVIEW);
}
void drawPoint(int x, int y)
{
glPointSize(4.0);
glBegin(GL_POINTS);
glVertex2i(x, (height-y));
glEnd();
glFlush();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void myMouse(int btn, int state, int x, int y)
{
int i;
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
x1[click]=x;
y1[click]=(height-y);
click++;
drawPoint(x, y);
}
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
if(click<=10) /* display error if an attempt is made to draw
more than 10 points on to window */
{
glBegin(GL_LINE_LOOP);
for(i=0;i<click;i++)
{
glVertex2i(x1[i], y1[i]);
}
glEnd();
glFlush();
}
else
{
printf("Fatal Error! Points limit exceeded. Resetting the count\n");
glutPostRedisplay();
click=0;
}
}
if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
{
glutPostRedisplay();
click = 0;
}
}
int main(int argc, char** argv)
{
/* Standard GLUT initialization */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
glutInitWindowSize(width,height); /* width x height pixel window */
glutInitWindowPosition(0,0); /* place window top left on display */
glutCreateWindow("Rubberbanding"); /* window title */
glutDisplayFunc(display); /* display callback invoked when window opened */
myinit(); /* set attributes */
glutMouseFunc(myMouse); /* mouse callback invoked when mouse clicked */
glutMainLoop(); /* enter event loop */
return 0;
}
Program 4:
#include<stdio.h>
#include<GL/glut.h>
float x1[10], y1[10];
int first = 0;
int width=400, height=400;
int click=0;
void myinit()
{
/* attributes */
glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
glColor3f(1.0, 0.0, 0.0); /* draw in red */
/* set up viewing */
/* 500 x 500 window with origin lower left */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)width, 0.0, (GLdouble)height);
glMatrixMode(GL_MODELVIEW);
}
void drawPoint(int x, int y)
{
//glPointSize(4.0);
glBegin(GL_POINTS);
glVertex2i(x, (height-y));
glEnd();
glFlush();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void myMouse(int btn, int state, int x, int y)
{
int i;
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
drawPoint(x, y);
}
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
drawPoint(x, y);
}
}
void move(int x, int y)
{
drawPoint(x, y);
}
int main(int argc, char** argv)
{
/* Standard GLUT initialization */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
glutInitWindowSize(width,height); /* 500 x 500 pixel window */
glutInitWindowPosition(0,0); /* place window top left on display */
glutCreateWindow("Rubberbanding"); /* window title */
glutDisplayFunc(display); /* display callback invoked when window opened */
myinit(); /* set attributes */
glutMouseFunc(myMouse);
glutMotionFunc(move);
glutMainLoop(); /* enter event loop */
return 0;
}