Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Tuesday, 19 November 2019

Mouse Event Program

nclude<stdio.h>
#include<math.h>
#include<GL/glut.h>
int width=400,height=400;
void myInit()
{
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,10.0,0.0,10.0);
    glMatrixMode(GL_MODELVIEW);
}
void drawSquare()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_POLYGON);
    glVertex2i(1,1);
    glVertex2i(9,1);
    glVertex2i(9,9);
    glVertex2i(1,9);
    glEnd();
    glutSwapBuffers();
    glFlush();
}
void drawTriangle()
{
    glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,1.0);
    glBegin(GL_POLYGON);
    glVertex2i(1,1);
    glVertex2i(5,9);
    glVertex2i(9,1);
    glEnd();
    glutSwapBuffers();
    glFlush();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glutSwapBuffers();
    glFlush();
}
void myMouse(int btn,int state,int x,int y)
{
     if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
    {
        drawTriangle();
    }
    if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
    {
        drawSquare();
    }
    if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
    {
        exit(0);
    }

}
void main(int argc,char **argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    //glutInitWindowSize(width,height);
  glutInitWindowSize(400,400);
   glutInitWindowPosition(100,100);
   glutCreateWindow("draw objects");
   myInit();
   glutDisplayFunc(display);
   glutMouseFunc(myMouse);
   glutMainLoop();
   return 0;
}

0 comments:

Post a Comment