Thursday, March 3, 2011

CG Event Handling Forum.......


Hi All,

PFA the code that demonstrates event handling mechanism in OpenGL.

Analyze the attached program thoroughly and get the answers for the below questions:

  • When the program is run, initially why a window with white background is displayed?
  • When middle button of the mouse is clicked, why nothing is drawn on the window?
  • When left and right buttons are clicked, why circle and Sierpinski Gasket are drawn at different positions on the window?
  • Is it possible to modify the code such that both circle and Sierpinski Gasket are drawn from the point where the mouse is clicked on the window?
  • When window is maximized, why nothing is drawn on the window?
  • What modifications in the code has to be made if the aspect ratio of the object drawn should remain the same when the window is resized/maximized?
  • What modifications in the code has to be made so that the program should be terminated if the middle button of the mouse or 'q' / 'Q' key is pressed from the keyboard?


Thanks,
Govind N. & Vishal K


HAVE YOUR SAY IN THIS DISCUSSION FORUM......


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <GL/glut.h>

void gasket();

void myinit()
{
      glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
      glColor3f(1.0, 0.0, 0.0); /* draw in red */
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
      glMatrixMode(GL_MODELVIEW);
}
    
void draw()
{
    int r=10;
    GLfloat theta;
  
    glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
for(theta=0; theta<=360; theta+=0.01)
     {
      glVertex2f(5*sin(theta*3.142/180), 5*cos(theta*3.142/180));
     }
glEnd();
     glFlush();
 }


void myMouse(int button, int state, int x,int y)
{
     if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
     {
      draw();
     }
     else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
     {
      gasket();
     }
}

void gasket()
{
     GLfloat v[3][2] = {{1.0, 1.0}, {6.0, 1.0}, {3.5, 5.5}};    // Assign co-ordinates to draw 3 points for triangle
     GLfloat p[2] = {2.5, 2.5};                                 // Take an initial point
     int j, i;
    
     glClear(GL_COLOR_BUFFER_BIT);
     glBegin(GL_POINTS);
     for(i=0; i<50000; i++)
     {
              j=rand()%3;                                       // Select a vertex randomly in each iteration
              p[0]=(p[0] + v[j][0])/2.0;                        // Compute the mid-point between P and selected vertex
              p[1]=(p[1] + v[j][1])/2.0;                        // Compute the mid-point between P and selected vertex
            
              glVertex2fv(p);                                   // Display this point on the screen
     }
     glEnd();
     glFlush();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}

void myKey(unsigned char key, int x1, int y1)
{
     if(key=='c' || key=='C')
     {
      draw();
     }
     else if(key=='g' || key=='G')
     {
      gasket();
     }          
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
    glutInitWindowSize(500,500); /* 500 x 500 pixel window */
    glutInitWindowPosition(0,0); /* place window top left on display */
    glutCreateWindow("Mouse and Key");
    glutDisplayFunc(display);
    glutMouseFunc(myMouse);  /* display callback invoked when window opened */
    glutKeyboardFunc(myKey);
    myinit(); /* set attributes */
    glutMainLoop(); /* enter event loop */
    return 0;
}

No comments:

Post a Comment