Advertisement
KillianMills

PaintAssignment2.c

Apr 18th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.03 KB | None | 0 0
  1. /*
  2. Killian Mills, 11368701
  3.  
  4. Compile:
  5.     MinGW Windows:
  6.     gcc Assignment2.c -o run glut32.lib -lopengl32 -lglu32
  7.  
  8.     Ubuntu:
  9.     gcc -o run Assignment2.c -lglut -lGLU -lGL -lm
  10. */
  11. #include <GL/gl.h>
  12. #include <GL/glext.h>
  13. #include <GL/glut.h>
  14.  
  15. #include <stdio.h>
  16.  
  17.  
  18. GLfloat xx1,xx2,yy1,yy2,xx3,yy3;
  19. GLfloat firstx, firsty;
  20. GLfloat hh;
  21. int action;
  22. int mainView;
  23.  
  24. float transparent = 1.0;
  25. float brightness = 0.0;
  26.  
  27. float red = 1.0;
  28. float green = 1.0;
  29. float blue = 1.0;
  30.  
  31. GLfloat colourPicker[3];
  32. int xAxis[99];
  33. int yAxis[99];
  34. int polyLimit;
  35.  
  36.  
  37. void Display(void)
  38. {
  39.     glClearColor(0.0,0.0,0.0,1.0);
  40.     glColor4f(red, green, blue,transparent);
  41.     glFlush();
  42.     return;
  43. }
  44.  
  45. //Point
  46. void DisplayPoint(){
  47.     glBegin(GL_POINTS);
  48.         glVertex2f(xx1,yy1);
  49.     glEnd();
  50.  
  51.     glFlush();
  52.     return;
  53. }
  54.  
  55. //Line
  56. void DisplayLine(){
  57.     glBegin(GL_LINES);
  58.         glVertex2f(xx1,yy1);
  59.         glVertex2f(xx2,yy2);
  60.     glEnd();
  61.  
  62.     glFlush();
  63.     return;
  64. }
  65.  
  66. //Rectangle
  67. void DisplayRectangle(){
  68.     glBegin(GL_POLYGON);
  69.         glVertex2f(xx1,yy1);
  70.         glVertex2f(xx1,yy2);
  71.         glVertex2f(xx2,yy2);
  72.         glVertex2f(xx2,yy1);
  73.     glEnd();
  74.  
  75.     glFlush();
  76.     return;
  77. }
  78.  
  79. //Triangle
  80. void DisplayTriangle(){
  81.     glBegin(GL_POLYGON);
  82.         glVertex2f(xx1,yy1);
  83.         glVertex2f(xx2,yy2);
  84.         glVertex2f(xx3,yy3);
  85.     glEnd();
  86.  
  87.     glFlush();
  88.     return;
  89. }
  90.  
  91. //Polygon
  92. void DisplayPolygon(){
  93.     int counter=0;
  94.     glBegin(GL_POLYGON);
  95.     while(counter < polyLimit){ // display to limit of polygon
  96.         glVertex2f(xAxis[counter],yAxis[counter]);
  97.         counter++;
  98.     }
  99.     glEnd();
  100.  
  101.     glFlush();
  102.     return;
  103. }
  104.  
  105. // mouse listener for each shape
  106. void MouseDraw(GLint button, GLint state, GLint x, GLint y)
  107. {
  108.     static int first=1;
  109.     static int count=0;
  110.  
  111.     if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
  112.     {
  113.         //Point
  114.         if(action == 0){
  115.  
  116.             xx1=x;
  117.             yy1=hh-y;
  118.  
  119.             DisplayPoint();
  120.         }
  121.        
  122.         //Line
  123.         if(action == 1){
  124.             if (first)
  125.             {
  126.                 xx1=x;
  127.                 yy1=hh-y;
  128.             }
  129.             else
  130.             {
  131.                 xx2=x;
  132.                 yy2=hh-y;
  133.                 DisplayLine();
  134.             }
  135.             first = !first;
  136.            
  137.         }
  138.        
  139.         // Rectangle
  140.         if(action == 2){
  141.             if (first)
  142.             {
  143.                 xx1=x;
  144.                 yy1=hh-y;
  145.                
  146.             }
  147.             else
  148.             {
  149.                 xx2=x;
  150.                 yy2=hh-y;
  151.                 DisplayRectangle();
  152.             }
  153.            
  154.             first = !first;
  155.         }
  156.  
  157.         // Triangle
  158.         if(action == 3){
  159.             if(first==1){
  160.                 xx1=x;
  161.                 yy1=hh-y;
  162.                 first = 2;
  163.             }
  164.            
  165.             else if(first==2){
  166.                 xx2=x;
  167.                 yy2=hh-y;
  168.                 first = 3;
  169.             }
  170.  
  171.             else{
  172.                 xx3=x;
  173.                 yy3=hh-y;
  174.                 DisplayTriangle();
  175.                 first = 1;
  176.             }
  177.         }
  178.  
  179.         // Polygon
  180.         if(action == 4){
  181.  
  182.             xAxis[count] =x;
  183.             yAxis[count] =hh-y;
  184.             count++;
  185.             if(count == polyLimit){
  186.                 DisplayPolygon();  
  187.                 count=0;               
  188.             }
  189.         }
  190.        
  191.     }
  192.  
  193.     return;
  194. }
  195.  
  196. //pick colour using mouse location on colour window
  197. void pickColour(GLint button, GLint state, GLint x, GLint y){
  198.  
  199.     if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON){
  200.         xx1=x;
  201.         yy1=hh-y;
  202.         glReadPixels(xx1, yy1, 1, 1, GL_RGB, GL_FLOAT, colourPicker);
  203.  
  204.         glutSetWindow(mainView);
  205.         red = colourPicker[0];
  206.         green = colourPicker[1];
  207.         blue = colourPicker[2];
  208.    
  209.         glColor4f(red, green, blue, transparent);
  210.         glFlush();
  211.     }
  212.     return;
  213. }
  214.  
  215. void MyReshape(GLsizei w, GLsizei h)
  216. {
  217.     glMatrixMode(GL_PROJECTION);
  218.     glLoadIdentity();
  219.     gluOrtho2D(0.0,(GLfloat)w,0.0,(GLfloat)h);
  220.     glViewport(0,0,w,h);
  221.     hh=h;
  222.     return;
  223. }
  224.  
  225. // display colours on seperate window
  226. // from : https://en.wikibooks.org/wiki/OpenGL_Programming/Basics/Color
  227. void displayColour() {
  228.        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  229.        glBegin(GL_QUADS);
  230.                glColor3f(0.5f, 0.0f, 1.0f); // make this vertex purple
  231.                glVertex2f(-0.75, 0.75);
  232.                glColor3f(1.0f, 0.0f, 0.0f); // make this vertex red
  233.                glVertex2f(-0.75, -0.75);
  234.                glColor3f(0.0f, 1.0f, 0.0f); // make this vertex green
  235.                glVertex2f(0.75, -0.75);
  236.                glColor3f(1.0f, 1.0f, 0.0f); // make this vertex yellow
  237.                glVertex2f(0.75, 0.75);
  238.        glEnd();
  239.        glutSwapBuffers();
  240. }
  241.  
  242. // basic colours, change red, green, blue
  243. void ColourMenu(int op){
  244.     switch(op) {
  245.         case('r'):
  246.             red = 1.0;
  247.             green = 0.0;
  248.             blue = 0.0;
  249.             glColor4f(red, green, blue, transparent); // red
  250.             break;
  251.         case('g'):
  252.             red = 0.0;
  253.             green = 1.0;
  254.             blue = 0.0;
  255.             glColor4f(red, green, blue, transparent); // green
  256.             break;
  257.         case('b'):
  258.             red = 0.0;
  259.             green = 0.0;
  260.             blue = 1.0;
  261.             glColor4f(red, green, blue, transparent); // blue
  262.             break;
  263.         case('w'):
  264.             red = 1.0;
  265.             green = 1.0;
  266.             blue = 1.0;
  267.             glColor4f(red, green, blue, transparent); // white
  268.             break;
  269.         case('v'):
  270.             red = 0.0;
  271.             green = 0.0;
  272.             blue = 0.0;
  273.             glColor4f(red, green, blue, transparent); // black
  274.             break;
  275.     }
  276. }
  277.  
  278. // change transparency, keep red, green, blue same
  279. void transparentMenu(int op){
  280.     switch(op) {
  281.         case(10):
  282.             transparent = 1.0;
  283.             glColor4f(red, green, blue, transparent); // high
  284.             break;
  285.         case(20):
  286.             transparent = 0.8;
  287.             glColor4f(red, green, blue, transparent);
  288.             break;
  289.         case(30):
  290.             transparent = 0.6;
  291.             glColor4f(red, green, blue, transparent);
  292.             break;
  293.         case(40):
  294.             transparent = 0.4;
  295.             glColor4f(red, green, blue, transparent);
  296.             break;
  297.         case(50):
  298.             transparent = 0.2;
  299.             glColor4f(red, green, blue, transparent); // low
  300.             break;
  301.     }
  302. }
  303.  
  304. // change brightness, add or subtract from red green and blue
  305. void brightMenu(int op){
  306.     brightness += ((double)op)/10;
  307.     if(brightness > 1.0) {
  308.         brightness = 1.0;
  309.     }
  310.     else if(brightness < -1.0) {
  311.         brightness = -1.0;
  312.     }
  313.     glColor4f(red+brightness,green+brightness,blue+brightness,transparent); // add brightness
  314.     glFlush();
  315. }
  316.  
  317. //assign limit to polygon
  318. void polygonMenu(int op){
  319.     polyLimit = op;
  320.     action = 4;
  321. }
  322.  
  323.  
  324. //main menu options
  325. void menu(int op){
  326.    
  327.     switch(op) {
  328.         case(0):
  329.             action = 0; // point
  330.             break;
  331.         case(1):
  332.             action = 1; // line
  333.             break;
  334.         case(2):
  335.             action = 2; // rectangle
  336.             break;
  337.         case(3):
  338.             action = 3; // triangle
  339.             break;
  340.         case(6):
  341.             glClear(GL_COLOR_BUFFER_BIT); // clear
  342.             glFlush();
  343.     }
  344. }
  345.  
  346. int main(int argc, char **argv)
  347. {
  348.     // Draw Window
  349.     glutInit(&argc, argv);
  350.     glutInitWindowSize(500,500);
  351.     glutInitWindowPosition(500,200);
  352.     mainView = glutCreateWindow("Assignment2: Paint");
  353.     glutReshapeFunc(MyReshape);
  354.     glutMouseFunc(MouseDraw);
  355.     glutDisplayFunc(Display);
  356.     glClear(GL_COLOR_BUFFER_BIT);
  357.     glPointSize(5);  
  358.     glLineWidth(5);
  359.    
  360.     // Colour Menu
  361.     int colourList = glutCreateMenu(ColourMenu);
  362.     glutAddMenuEntry("Red", 'r');
  363.     glutAddMenuEntry("Green", 'g');
  364.     glutAddMenuEntry("Blue", 'b');
  365.     glutAddMenuEntry("White", 'w');
  366.     glutAddMenuEntry("Black", 'v');
  367.  
  368.     // transparent Menu
  369.     int transparentList = glutCreateMenu(transparentMenu);
  370.     glutAddMenuEntry("1.0", 10);
  371.     glutAddMenuEntry("0.8", 20);
  372.     glutAddMenuEntry("0.6", 30);
  373.     glutAddMenuEntry("0.4", 40);
  374.     glutAddMenuEntry("0.2", 50);
  375.  
  376.     // transparent Menu
  377.     int brightList = glutCreateMenu(brightMenu);
  378.     glutAddMenuEntry("Increase", 1);
  379.     glutAddMenuEntry("Decrease", -1);
  380.  
  381.     // polygon Menu
  382.     int polyList = glutCreateMenu(polygonMenu);
  383.     glutAddMenuEntry("3", 3);
  384.     glutAddMenuEntry("4", 4);
  385.     glutAddMenuEntry("5", 5);
  386.     glutAddMenuEntry("6", 6);
  387.     glutAddMenuEntry("7", 7);
  388.     glutAddMenuEntry("8", 8);
  389.     glutAddMenuEntry("9", 9);
  390.     glutAddMenuEntry("10", 10);
  391.        
  392.     // Main Menu
  393.     glutCreateMenu(menu);
  394.     glutAddMenuEntry("Point", 0);
  395.     glutAddMenuEntry("Line", 1);
  396.     glutAddMenuEntry("Rectangle", 2);
  397.     glutAddMenuEntry("Triangle", 3);
  398.     glutAddSubMenu("Polygon", polyList);
  399.     glutAddSubMenu("Colour", colourList);
  400.     glutAddSubMenu("Transparency", transparentList);
  401.     glutAddSubMenu("Brightness", brightList);
  402.     glutAddMenuEntry("Clear", 6);
  403.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  404.  
  405.     // Enable Blending
  406.     glEnable(GL_BLEND);
  407.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  408.    
  409.     // Colour Window
  410.     glutInitWindowPosition(1100,200);
  411.     glutCreateWindow("Colours");
  412.     glutDisplayFunc(displayColour);
  413.     glutMouseFunc(pickColour);
  414.    
  415.     //Start Looping
  416.     glutMainLoop();
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement