Advertisement
Ladies_Man

cg lab4 raster2 (draw+fill)

Apr 8th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.65 KB | None | 0 0
  1. // glfw_30.cpp : Defines the entry point for the console application.
  2.  
  3. #include "stdafx.h"
  4. #include <time.h>
  5. #define _USE_MATH_DEFINES
  6. #include <cmath>
  7. #include <GL/glew.h>
  8. #include <GL\glut.h>
  9. #include <GLFW/glfw3.h>
  10. #include <vector>
  11. #include <algorithm>
  12. #include <cstdlib>
  13.  
  14. #define SCREEN_POS_X        100
  15. #define SCREEN_POS_Y        100
  16. #define SCREEN_WIDTH        800
  17. #define SCREEN_HEIGHT       600
  18. #define DATA_FORMAT     1
  19. #define BUFFER_SIZE     SCREEN_WIDTH*SCREEN_HEIGHT*DATA_FORMAT
  20. #define PXL_INDX(X,Y,COMP)  ((Y*SCREEN_WIDTH+X)*DATA_FORMAT+COMP)
  21.  
  22. typedef struct {
  23.     int x, y;
  24. } point;
  25.  
  26.  
  27. bool draw_flag, fill_flag, anti_aliacing_flag, update_flag;
  28. int x_max, x_min, y_max, y_min;
  29.  
  30. GLfloat *pixels;
  31. GLfloat *intrails;
  32.  
  33. std::vector<point> shape;
  34. std::vector<std::vector<int>> edge_table;
  35.  
  36.  
  37. void changeSize(int w, int h)
  38. {
  39.     // Prevent a divide by zero, when window is too short
  40.     // (you cant make a window of zero width).
  41.     if (0 == h)
  42.         h = 1;
  43.  
  44.     float ratio = w * 1.0 / h;
  45.  
  46.     // Use the Projection Matrix
  47.     glMatrixMode(GL_PROJECTION);
  48.  
  49.     // Reset Matrix
  50.     glLoadIdentity();
  51.  
  52.     // Set the viewport to be the entire window
  53.     glViewport(0, 0, w, h);
  54.  
  55.     // Set the correct perspective.
  56.     gluOrtho2D(0, (GLdouble)w, 0, (GLdouble)h);
  57.  
  58.     // Get Back to the Modelview
  59.     glMatrixMode(GL_MODELVIEW);
  60. }
  61.  
  62. void clear_buffer()
  63. {
  64.     for (int i = 0; i < BUFFER_SIZE; i++) {
  65.         pixels[i] = 0;
  66.         intrails[i] = 0;
  67.     }
  68. }
  69.  
  70. void copy_buffer(GLfloat *buff_source, GLfloat *buff_dest)
  71. {
  72.     for (int i = 0; i < BUFFER_SIZE; i++) {
  73.         buff_dest[i] = buff_source[i];
  74.     }
  75. }
  76.  
  77. void create_buffer()
  78. {
  79.     pixels = new GLfloat[BUFFER_SIZE];
  80.     intrails = new GLfloat[BUFFER_SIZE];
  81. }
  82.  
  83. void draw_simple_line(point a, point b)
  84. {
  85.     if (a.x == b.x && a.y == b.y) return;
  86.  
  87.     glColor3f(1, 0, 0);
  88.     glBegin(GL_LINES);
  89.         glVertex2f(a.x, a.y);
  90.         glVertex2f(b.x, b.y);
  91.     glEnd();
  92. }
  93.  
  94. void Bresenham(int x1, int y1, int const x2, int const y2)
  95. {
  96.     int delta_x(x2 - x1);
  97.     // if x1 == x2, then it does not matter what we set here
  98.     signed char const ix((delta_x > 0) - (delta_x < 0));
  99.     delta_x = std::abs(delta_x) << 1;
  100.  
  101.     int delta_y(y2 - y1);
  102.     // if y1 == y2, then it does not matter what we set here
  103.     signed char const iy((delta_y > 0) - (delta_y < 0));
  104.     delta_y = std::abs(delta_y) << 1;
  105.  
  106.     //plot(x1, y1);
  107.     pixels[PXL_INDX(x1, y1, 0)] = 1;
  108.  
  109.     if (delta_x >= delta_y)
  110.     {
  111.         // error may go below zero
  112.         int error(delta_y - (delta_x >> 1));
  113.  
  114.         while (x1 != x2)
  115.         {
  116.             if ((error >= 0) && (error || (ix > 0)))
  117.             {
  118.                 error -= delta_x;
  119.                 y1 += iy;
  120.             }
  121.             // else do nothing
  122.  
  123.             error += delta_y;
  124.             x1 += ix;
  125.  
  126.             //plot(x1, y1);
  127.             pixels[PXL_INDX(x1, y1, 0)] = 1;
  128.         }
  129.     }
  130.     else
  131.     {
  132.         // error may go below zero
  133.         int error(delta_x - (delta_y >> 1));
  134.  
  135.         while (y1 != y2)
  136.         {
  137.             if ((error >= 0) && (error || (iy > 0)))
  138.             {
  139.                 error -= delta_y;
  140.                 x1 += ix;
  141.             }
  142.             // else do nothing
  143.  
  144.             error += delta_x;
  145.             y1 += iy;
  146.  
  147.             //plot(x1, y1);
  148.             pixels[PXL_INDX(x1,y1,0)] = 1;
  149.         }
  150.     }
  151. }
  152.  
  153. void draw_complex_line(point a, point b)
  154. {
  155.     if (a.x == b.x && a.y == b.y) return;
  156.  
  157.     Bresenham(a.x, a.y, b.x, b.y);
  158. }
  159.  
  160. void set_contour()//D
  161. {
  162.     for (int i = 0; i < shape.size() - 1; i++)
  163.         draw_complex_line(shape[i], shape[i + 1]);
  164. }
  165.  
  166. void draw_contour()//D
  167. {
  168.     glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RED, GL_FLOAT, (void*)pixels);
  169.  
  170.     //for (int i = 0; i < shape.size() - 1; i++) draw_simple_line(shape[i], shape[i + 1]);
  171.     //draw_simple_line(shape[0], shape[shape.size() - 1]);
  172. }
  173.  
  174. void set_intrails_vector()//F
  175. {
  176.    
  177.     int x, y, k, component = 0, height;
  178.     bool inside;
  179.  
  180.     if (3 == DATA_FORMAT) component = 3;
  181.  
  182.     copy_buffer(pixels, intrails);
  183.  
  184.     edge_table.resize(SCREEN_HEIGHT);
  185.  
  186.     for (y = y_min + 2, k = 0; y <= y_max - 1; y++, k++) {
  187.         inside = false;
  188.         for (x = x_min - 1; x <= x_max + 1; x++) {
  189.             if (pixels[PXL_INDX(x, y, component)] == 1) {
  190.                 inside = true;
  191.                 while (pixels[PXL_INDX(x, y, component)] == 1){
  192.                     edge_table[k].push_back(x);
  193.                     x++;
  194.                 }
  195.                 while (pixels[PXL_INDX(x, y, component)] == 0 && x < x_max && inside) {
  196.                     intrails[PXL_INDX(x, y, component)] = 1;
  197.                     x++;
  198.                 }
  199.                 while (pixels[PXL_INDX(x, y, component)] == 1){
  200.                     edge_table[k].push_back(x);
  201.                     x++;
  202.                 }
  203.             }
  204.         }
  205.     }
  206.    
  207. }
  208.  
  209. void set_intrails_pixels()
  210. {
  211.     int x, y, i;
  212.     /*
  213.     for (y = 0; y < SCREEN_HEIGHT; y++) {
  214.         for (x = 0; x < SCREEN_WIDTH; x++) {
  215.             for (i = 0; i < edge_table[y].size(); i++) {
  216.                 if (edge_table[y][i] == x) {
  217.                     intrails[PXL_INDX(x, y, 0)] = 1;
  218.                 }
  219.                 else {
  220.                     intrails[PXL_INDX(x, y, 0)] = 0;
  221.                 }
  222.             }
  223.         }
  224.     }*/
  225. }
  226.  
  227. void draw_intrails()//F
  228. {
  229.     glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RED, GL_FLOAT, (void*)intrails);
  230. }
  231.  
  232. void smooth_circuit()//S
  233. {
  234.    
  235. }
  236.  
  237. void figure_pos()
  238. {
  239.     int max_x = 0, min_x = SCREEN_WIDTH;
  240.     int max_y = 0, min_y = SCREEN_HEIGHT;
  241.  
  242.     for (int i = 0; i < shape.size(); i++) {
  243.         if (shape[i].x > max_x) max_x = shape[i].x;
  244.         if (shape[i].x < min_x) min_x = shape[i].x;
  245.         if (shape[i].y > max_y) max_y = shape[i].y;
  246.         if (shape[i].y < min_y) min_y = shape[i].y;
  247.     }
  248.  
  249.     x_max = max_x;
  250.     x_min = min_x;
  251.     y_max = max_y;
  252.     y_min = min_y;
  253. }
  254.  
  255. void renderScene(void)
  256. {
  257.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  258.  
  259.     if (draw_flag) {
  260.         if (0 != shape.size()) {
  261.             set_contour();
  262.             draw_contour();
  263.         }
  264.     }
  265.  
  266.     if (fill_flag) {
  267.         set_intrails_vector();
  268.         set_intrails_pixels();
  269.         draw_intrails();
  270.     }
  271.  
  272.     if (anti_aliacing_flag) {
  273.         smooth_circuit();
  274.     }
  275.  
  276.     if (update_flag) {
  277.         if (draw_flag) {
  278.             set_contour();
  279.             draw_contour();
  280.         }
  281.         if (fill_flag) {
  282.             set_intrails_vector();
  283.             set_intrails_pixels();
  284.             draw_intrails();
  285.         }
  286.         if (anti_aliacing_flag) smooth_circuit();
  287.     }
  288.  
  289.     glutSwapBuffers();
  290. }
  291.  
  292. void mouseButton(int button, int state, int x, int y)
  293. {
  294.     if (GLUT_LEFT_BUTTON == button) {
  295.         point a;
  296.         if (GLUT_DOWN == state) {
  297.             a.x = x;
  298.             a.y = SCREEN_HEIGHT - y;
  299.             printf("%d %d\n", a.x, a.y);
  300.             shape.push_back(a);
  301.             figure_pos();
  302.             glutPostRedisplay();
  303.         }
  304.         if (GLUT_UP == state) {
  305.             glutPostRedisplay();
  306.         }
  307.     }
  308. }
  309.  
  310. void keyboardButton(unsigned char key, int x, int y)
  311. {
  312.     if ('d' == key || 'D' == key) {
  313.         draw_flag = !draw_flag;
  314.         printf("x_min=%d x_max=%d\ny_min=%d y_max=%d\n", x_min, x_max, y_min, y_max);
  315.         printf("drawing:");
  316.         draw_flag ? printf("ON\n") : printf("OFF\n");
  317.         glutPostRedisplay();
  318.     }
  319.     if ('s' == key || 'S' == key) {
  320.         anti_aliacing_flag = !anti_aliacing_flag;
  321.         anti_aliacing_flag ? printf("pc_mode:ON\n") : printf("console_mode:ON\n");
  322.         glutPostRedisplay();
  323.     }
  324.     if ('f' == key || 'F' == key) {
  325.         fill_flag = !fill_flag;
  326.         printf("filling:");
  327.         fill_flag ? printf("ON\n") : printf("OFF\n");
  328.         glutPostRedisplay();
  329.     }
  330.     if ('z' == key || 'Z' == key) {
  331.         if (0 < shape.size()) {
  332.             shape.pop_back();
  333.             figure_pos();
  334.             glutPostRedisplay();
  335.         }
  336.     }
  337.     if ('a' == key || 'A' == key) {
  338.         draw_complex_line(shape[shape.size()-1], shape[0]);
  339.         glutPostRedisplay();
  340.     }
  341. }
  342.  
  343. void initial_state()
  344. {
  345.     glClearColor(0, 0, 0, 1);
  346.  
  347.     draw_flag = false;
  348.     fill_flag = false;
  349.     anti_aliacing_flag = false;
  350.     update_flag = false;
  351.  
  352.     x_max = 0;
  353.     x_min = SCREEN_WIDTH;
  354.     y_max = 0;
  355.     y_min = SCREEN_HEIGHT;
  356.    
  357.     create_buffer();
  358.     clear_buffer();
  359. }
  360.  
  361. int main(int argc, char **argv)
  362. {
  363.     glutInit(&argc, argv);
  364.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  365.     glutInitWindowPosition(SCREEN_POS_X, SCREEN_POS_Y);
  366.     glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  367.     glutCreateWindow("Anthony's Project: Lab 4");
  368.  
  369.     glutDisplayFunc(renderScene);
  370.     glutReshapeFunc(changeSize);
  371.     glutIdleFunc(renderScene);
  372.  
  373.     glutMouseFunc(mouseButton);
  374.     glutKeyboardFunc(keyboardButton);
  375.  
  376.     initial_state();
  377.  
  378.     glutMainLoop();
  379.  
  380.     return 0;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement