Advertisement
Ladies_Man

cg lab4 raster4 (anti-alliasing works, filling almost)

Apr 28th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.95 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_WIDTH        800
  15. #define SCREEN_HEIGHT       600
  16. #define SCREEN_POS_X        100
  17. #define SCREEN_POS_Y        100
  18.  
  19. #define DATA_FORMAT         3
  20. #define BUFFER_SIZE         SCREEN_WIDTH*SCREEN_HEIGHT*DATA_FORMAT
  21. #define PXL_INDX(X,Y,COMP) (((Y)*SCREEN_WIDTH+(X))*DATA_FORMAT+(COMP))
  22.  
  23. typedef struct point{
  24.     int x, y;
  25.     point* incident_prev;
  26.     point* incident_next;
  27.     bool extremum;
  28. } point;
  29.  
  30. typedef struct {
  31.     point a, b;
  32.     int x_min, x_max;
  33.     int y_min, y_max;
  34. }edge;
  35.  
  36. bool draw_flag, fill_flag, anti_aliacing_flag, update_flag, thick_flag, clear_flag;
  37. int x_max, x_min, y_max, y_min;
  38.  
  39. GLfloat *contour;
  40. GLfloat *intrails;
  41. GLfloat *blur;
  42.  
  43. std::vector<point> vertices;
  44. std::vector<edge> edges;
  45. std::vector<std::vector<int>> edge_table;
  46.  
  47. bool is_white(GLfloat *buff_source, int x, int y)
  48. {
  49.     if (buff_source[PXL_INDX(x, y, 0)] == 1 &&
  50.         buff_source[PXL_INDX(x, y, 1)] == 1 &&
  51.         buff_source[PXL_INDX(x, y, 2)] == 1
  52.         )
  53.         return true;
  54.     return false;
  55. }
  56.  
  57. bool is_black(GLfloat *buff_source, int x, int y)
  58. {
  59.     if (buff_source[PXL_INDX(x, y, 0)] == 0 &&
  60.         buff_source[PXL_INDX(x, y, 1)] == 0 &&
  61.         buff_source[PXL_INDX(x, y, 2)] == 0
  62.         )
  63.         return true;
  64.     return false;
  65. }
  66.  
  67. int get_color(int x, int y)
  68. {
  69.     float red = blur[PXL_INDX(x, y, 0)];
  70.     float green = blur[PXL_INDX(x, y, 1)];
  71.     float blue = blur[PXL_INDX(x, y, 2)];
  72.  
  73.     return (int)(red*1000.0) + (int)(green*100.0) + (int)(blue*10.0);
  74. }
  75.  
  76. bool even_vertex(int x, int y)
  77. {
  78.     bool connected_to_top = false;
  79.     bool connected_to_bot = false;
  80.     bool connected_to_side = false;
  81.  
  82.     if ((contour[PXL_INDX(x - 1, y + 1, 0)] == 1 && contour[PXL_INDX(x - 1, y + 1,1)] == 1 && contour[PXL_INDX(x - 1, y + 1, 2)] == 1) ||
  83.         (contour[PXL_INDX(x, y + 1, 0)] == 1 && contour[PXL_INDX(x, y + 1, 1)] == 1 && contour[PXL_INDX(x, y + 1, 2)] == 1) ||
  84.         (contour[PXL_INDX(x + 1, y + 1, 0)] == 1 && contour[PXL_INDX(x + 1, y + 1, 1)] == 1 && contour[PXL_INDX(x + 1, y + 1, 2)] == 1)) {
  85.         connected_to_top = true;
  86.     }
  87.     if ((contour[PXL_INDX(x - 1, y - 1, 0)] == 1 && contour[PXL_INDX(x - 1, y - 1, 1)] == 1 && contour[PXL_INDX(x - 1, y - 1, 2)] == 1) ||
  88.         (contour[PXL_INDX(x, y - 1, 0)] == 1 && contour[PXL_INDX(x, y - 1, 1)] == 1 && contour[PXL_INDX(x, y - 1, 2)] == 1) ||
  89.         (contour[PXL_INDX(x + 1, y - 1, 0)] == 1 && contour[PXL_INDX(x + 1, y - 1, 1)] == 1 && contour[PXL_INDX(x + 1, y - 1, 2)] == 1)) {
  90.         connected_to_bot = true;
  91.     }
  92.     if ((contour[PXL_INDX(x - 1, y, 0)] == 1 && contour[PXL_INDX(x - 1, y, 1)] == 1 && contour[PXL_INDX(x - 1, y, 2)] == 1) ||
  93.         (contour[PXL_INDX(x+1, y, 0)] == 1 && contour[PXL_INDX(x+1, y, 1)] == 1 && contour[PXL_INDX(x+1, y, 2)] == 1)) {
  94.         connected_to_side = true;
  95.     }
  96.  
  97.     if (
  98.         ((connected_to_top || connected_to_side) && (connected_to_bot || connected_to_side)) ||
  99.         (connected_to_top && connected_to_bot) ||
  100.         ((connected_to_top && connected_to_side) && (connected_to_bot && connected_to_side))
  101.         )
  102.         return true;
  103.     return false;
  104. }
  105.  
  106. void changeSize(int w, int h)
  107. {
  108.     if (0 == h) h = 1;
  109.  
  110.     float ratio = w * 1.0 / h;
  111.  
  112.     // Use the Projection Matrix
  113.     glMatrixMode(GL_PROJECTION);
  114.  
  115.     // Reset Matrix
  116.     glLoadIdentity();
  117.  
  118.     // Set the viewport to be the entire window
  119.     glViewport(0, 0, w, h);
  120.  
  121.     // Set the correct perspective.
  122.     gluOrtho2D(0, (GLdouble)w, 0, (GLdouble)h);
  123.  
  124.     // Get Back to the Modelview
  125.     glMatrixMode(GL_MODELVIEW);
  126. }
  127.  
  128. void clear_vector(std::vector<point>& vector)
  129. {
  130.     vector.clear();
  131.     vector.shrink_to_fit();
  132. }
  133.  
  134. void clear_buffers()
  135. {
  136.     for (int i = 0; i < BUFFER_SIZE; i++) {
  137.         contour[i] = 0;
  138.         intrails[i] = 0;
  139.         blur[i] = 0;
  140.     }
  141. }
  142.  
  143. void copy_buffer(GLfloat *buff_source, GLfloat *buff_dest)
  144. {
  145.     for (int i = 0; i < BUFFER_SIZE; i++) {
  146.         buff_dest[i] = buff_source[i];
  147.     }
  148. }
  149.  
  150. void create_buffers()
  151. {
  152.     contour = new GLfloat[BUFFER_SIZE];
  153.     intrails = new GLfloat[BUFFER_SIZE];
  154.     blur = new GLfloat[BUFFER_SIZE];
  155. }
  156.  
  157. void set_vertices()
  158. {
  159.     int i;
  160.  
  161.     //for 0th vertex
  162.     vertices[0].extremum = false;
  163.     vertices[0].incident_next = &vertices[1];
  164.     vertices[0].incident_prev = &vertices[vertices.size()-1];
  165.  
  166.     //for other vertices
  167.     for (i = 1; i < vertices.size() - 1; i++) {
  168.         vertices[i].incident_next = &vertices[(i + 1) % vertices.size()];
  169.         vertices[i].incident_prev = &vertices[i - 1];
  170.         vertices[i].extremum = false;
  171.     }
  172.  
  173.     //find extermums
  174.     for (i = 0; i < vertices.size(); i++) {
  175.         if (
  176.             ((*vertices[i].incident_next).y > vertices[i].y && (*vertices[i].incident_prev).y > vertices[i].y) ||
  177.             ((*vertices[i].incident_next).y < vertices[i].y && (*vertices[i].incident_prev).y < vertices[i].y)
  178.             ) {
  179.             vertices[i].extremum = true;
  180.         }
  181.  
  182.     }
  183. }
  184.  
  185. void Bresenham2(int x1, int y1, int const x2, int const y2)
  186. {
  187.     int prev, curr;
  188.     prev = y1;
  189.     curr = y1;
  190.  
  191.     int delta_x(x2 - x1);
  192.     signed char const ix((delta_x > 0) - (delta_x < 0));
  193.     delta_x = std::abs(delta_x) << 1;
  194.  
  195.     int delta_y(y2 - y1);
  196.     signed char const iy((delta_y > 0) - (delta_y < 0));
  197.     delta_y = std::abs(delta_y) << 1;
  198.  
  199.     if (0) {
  200.         edge_table[y1 - y_min].push_back(x1);
  201.         edge_table[y1 - y_min].push_back(x1);
  202.     }
  203.     else {
  204.         edge_table[y1 - y_min].push_back(x1);
  205.     }
  206.  
  207.     if (delta_x >= delta_y)
  208.     {
  209.         int error(delta_y - (delta_x >> 1));
  210.         while (x1 != x2)
  211.         {
  212.             if ((error >= 0) && (error || (ix > 0)))
  213.             {
  214.                 error -= delta_x;
  215.                 y1 += iy;
  216.                 curr = y1;
  217.             }
  218.             error += delta_y;
  219.             x1 += ix;
  220.  
  221.             if (curr > prev) {
  222.                 if (0) {
  223.                     edge_table[y1 - y_min].push_back(x1);
  224.                     edge_table[y1 - y_min].push_back(x1);
  225.                 }
  226.                 else {
  227.                     edge_table[y1 - y_min].push_back(x1);
  228.                 }
  229.             }
  230.             prev = curr;
  231.         }
  232.     }
  233.     else
  234.     {
  235.         int error(delta_x - (delta_y >> 1));
  236.         while (y1 != y2)
  237.         {
  238.             if ((error >= 0) && (error || (iy > 0)))
  239.             {
  240.                 error -= delta_y;
  241.                 x1 += ix;
  242.             }
  243.             error += delta_x;
  244.             y1 += iy;
  245.  
  246.             if (0) {
  247.                 edge_table[y1 - y_min].push_back(x1);
  248.                 edge_table[y1 - y_min].push_back(x1);
  249.             }
  250.             else {
  251.                 edge_table[y1 - y_min].push_back(x1);
  252.             }
  253.         }
  254.     }
  255. }
  256.  
  257. void Bresenham(int x1, int y1, int const x2, int const y2)
  258. {
  259.     int delta_x(x2 - x1);
  260.     // if x1 == x2, then it does not matter what we set here
  261.     signed char const ix((delta_x > 0) - (delta_x < 0));
  262.     delta_x = std::abs(delta_x) << 1;
  263.  
  264.     int delta_y(y2 - y1);
  265.     // if y1 == y2, then it does not matter what we set here
  266.     signed char const iy((delta_y > 0) - (delta_y < 0));
  267.     delta_y = std::abs(delta_y) << 1;
  268.  
  269.  
  270.  
  271.     //plot(x1, y1);
  272.     for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1, y1, i)] = 1;
  273.     //========================================
  274.     if (thick_flag) {
  275.         for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 - 1, y1 + 1, i)] = 1;
  276.         for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1, y1 + 1, i)] = 1;
  277.         for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 + 1, y1 + 1, i)] = 1;
  278.         for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 - 1, y1, i)] = 1;
  279.         for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 + 1, y1, i)] = 1;
  280.         for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 - 1, y1 - 1, i)] = 1;
  281.         for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1, y1 - 1, i)] = 1;
  282.         for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 + 1, y1 - 1, i)] = 1;
  283.     }
  284.     //========================================
  285.  
  286.  
  287.  
  288.     if (delta_x >= delta_y)
  289.     {
  290.         // error may go below zero
  291.         int error(delta_y - (delta_x >> 1));
  292.  
  293.         while (x1 != x2)
  294.         {
  295.             if ((error >= 0) && (error || (ix > 0)))
  296.             {
  297.                 error -= delta_x;
  298.                 y1 += iy;
  299.             }
  300.             // else do nothing
  301.  
  302.             error += delta_y;
  303.             x1 += ix;
  304.  
  305.  
  306.             //plot(x1, y1);
  307.             for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1, y1, i)] = 1;
  308.             //========================================
  309.             if (thick_flag) {
  310.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 - 1, y1 + 1, i)] = 1;
  311.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1, y1 + 1, i)] = 1;
  312.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 + 1, y1 + 1, i)] = 1;
  313.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 - 1, y1, i)] = 1;
  314.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 + 1, y1, i)] = 1;
  315.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 - 1, y1 - 1, i)] = 1;
  316.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1, y1 - 1, i)] = 1;
  317.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 + 1, y1 - 1, i)] = 1;
  318.             }
  319.             //========================================
  320.         }
  321.     }
  322.     else
  323.     {
  324.         // error may go below zero
  325.         int error(delta_x - (delta_y >> 1));
  326.  
  327.         while (y1 != y2)
  328.         {
  329.             if ((error >= 0) && (error || (iy > 0)))
  330.             {
  331.                 error -= delta_y;
  332.                 x1 += ix;
  333.             }
  334.             // else do nothing
  335.  
  336.             error += delta_x;
  337.             y1 += iy;
  338.  
  339.  
  340.  
  341.             //plot(x1, y1);
  342.             for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1, y1, i)] = 1;
  343.             //========================================
  344.             if (thick_flag) {
  345.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 - 1, y1 + 1, i)] = 1;
  346.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1, y1 + 1, i)] = 1;
  347.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 + 1, y1 + 1, i)] = 1;
  348.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 - 1, y1, i)] = 1;
  349.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 + 1, y1, i)] = 1;
  350.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 - 1, y1 - 1, i)] = 1;
  351.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1, y1 - 1, i)] = 1;
  352.                 for (int i = 0; i < DATA_FORMAT; i++) contour[PXL_INDX(x1 + 1, y1 - 1, i)] = 1;
  353.             }
  354.             //========================================
  355.         }
  356.     }
  357. }
  358.  
  359. void draw_complex_line2(point a, point b)
  360. {
  361.     Bresenham2(a.x, a.y, b.x, b.y);
  362. }
  363.  
  364. void set_contour()//D
  365. {
  366.     for (int i = 0; i < vertices.size() - 1; i++)
  367.         Bresenham(vertices[i].x, vertices[i].y, vertices[i + 1].x, vertices[i + 1].y);
  368. }
  369.  
  370. void draw_contour()//D
  371. {
  372.     glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_FLOAT, (void*)contour);
  373. }
  374.  
  375. void init_edges()
  376. {
  377.     int i;
  378.     edge temp_edge;
  379.  
  380.     for (i = 0; i < vertices.size(); i++) {
  381.         temp_edge.a = vertices[i];
  382.         temp_edge.b = vertices[(i + 1) % vertices.size()];
  383.  
  384.         edges.push_back(temp_edge);
  385.     }
  386. }
  387.  
  388. void set_edges()
  389. {
  390.     set_vertices();
  391.  
  392.     init_edges();
  393.  
  394.     int i;
  395.    
  396.     edge_table.resize(y_max);
  397.  
  398.     for (i = 0; i < edge_table.size(); i++)
  399.         edge_table[i].resize(0);
  400.    
  401.     for (int i = 0; i < vertices.size(); i++)
  402.         Bresenham2(vertices[i].x, vertices[i].y, vertices[(i + 1) % vertices.size()].x, vertices[(i + 1) % vertices.size()].y);
  403.  
  404.     for (i = 0; i < edge_table.size(); i++)
  405.         std::sort(edge_table[i].begin(), edge_table[i].end());
  406. }
  407.  
  408. void set_intrails_vector()//F
  409. {  
  410.     int x, y, k, l, i, j, m, a, component = 0, height;
  411.     bool inside = false, stop_not_found = false;
  412.     int start, stop;
  413.  
  414.     //copy_buffer(contour, intrails);
  415.  
  416.     int vector_size = y_max - y_min;
  417.  
  418.     edge_table.resize(vector_size);
  419.  
  420.     for (y = y_min + 2, k = 0; y <= y_max - 2; y++, k++) {
  421.         inside = false;
  422.         for (x = x_min - 1; x <= x_max + 1; x++) {
  423.             if (is_white(contour, x, y) && even_vertex(x, y)) {
  424.                 inside = true;
  425.                 while (is_white(contour, x, y)) {
  426.                     x++;
  427.                 }
  428.                 edge_table[k].push_back(x);
  429.                 while (is_black(contour, x, y) && x <= x_max+1 && inside) {
  430.                     for (a = 0; a < DATA_FORMAT; a++) {
  431.                         intrails[PXL_INDX(x, y, a)] = 1;
  432.                     }
  433.                     x++;
  434.                 }
  435.                 edge_table[k].push_back(x);
  436.                 while (is_white(contour, x, y)) {
  437.                     x++;
  438.                 }
  439.             }
  440.         }
  441.     }
  442.    
  443.     /*
  444.     for (y = y_min + 2, k = 0; y < y_max; y++, k++) {
  445.         start = x_min;
  446.         stop = x_min;
  447.         for (x = x_min - 1; x < x_max + 1; x++) {
  448.             if (is_black(contour, x, y) && is_white(contour, x+1, y)) {
  449.                 x++;
  450.                 while (is_white(contour, x, y)) {
  451.                     x++;
  452.                 }
  453.                 start = x;
  454.             }
  455.             x++;
  456.             if (is_white(contour, x, y) && is_black(contour, x + 1, y)) {
  457.                 stop = x + 1;
  458.                 x++;
  459.                 while (is_white(contour, x, y)) {
  460.                     x++;
  461.                 }
  462.             }
  463.             x++;
  464.             if (start < stop) {
  465.                 for (i = start; i < stop; i++) {
  466.                     intrails[PXL_INDX(i, y, 0)] = 1;
  467.                     intrails[PXL_INDX(i, y, 1)] = 1;
  468.                     intrails[PXL_INDX(i, y, 2)] = 1;
  469.                 }
  470.             }
  471.         }
  472.     }
  473.     */
  474.    
  475.  
  476.     /*
  477.     for (y = y_min, k = 0; y < y_max; y++, k++) {
  478.         for (x = x_min, l = 0; x < x_max - 1; x++, l++) {
  479.             stop_not_found = false;
  480.             if (contour[PXL_INDX(x, y, component)] == 0 && contour[PXL_INDX(x+1, y, component)] == 1) {
  481.                 m = x;
  482.                 while (contour[PXL_INDX(m, y, component)] == 0 && m < x_max) {
  483.                     stop_not_found = true;
  484.                     m++;
  485.                 }
  486.                 if (stop_not_found) break;
  487.                 edge_table[k].push_back(x);
  488.             }
  489.         }
  490.     }*/
  491.    
  492. }
  493.  
  494. void set_intrails_pixels()
  495. {
  496.     int i, j, x, y, z, k;
  497.     int start, stop;
  498.  
  499.     for (i = 0; i < edge_table.size(); i++) {
  500.         if (edge_table[i].size() % 2 == 0 && 0 != edge_table[i].size()) {
  501.             for (int j = 0; j < edge_table[i].size() - 1; j += 2) {
  502.                 int start = edge_table[i][j];
  503.                 int stop = edge_table[i][j + 1];
  504.                 for (z = start; z < stop; z++) {
  505.                     for (int a = 0; a < 3; a++) {
  506.                         intrails[PXL_INDX(z, i + y_min, a)] = 1;
  507.                     }
  508.                 }
  509.             }
  510.  
  511.         }
  512.     }
  513. }
  514.  
  515. void draw_intrails()//F
  516. {
  517.     glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_FLOAT, (void*)intrails);
  518. }
  519.  
  520. void postfiltration()//S
  521. {
  522.     int i, j, k, x, y, num, color;
  523.     GLfloat r, g, b;
  524.  
  525.     copy_buffer(contour, blur);
  526.  
  527.     for (y = 0; y < SCREEN_HEIGHT; y++) {
  528.         for (x = 0; x < SCREEN_WIDTH; x++) {
  529.             r = 0;
  530.             g = 0;
  531.             b=0;
  532.             num = 0;
  533.             color = 0;
  534.  
  535.             if (x + 1 < SCREEN_WIDTH && y + 1 < SCREEN_HEIGHT) {
  536.                 num++;
  537.                 color += get_color(x + 1, y + 1);
  538.                 r += intrails[PXL_INDX(x + 1, y + 1, 0)];
  539.                 g += intrails[PXL_INDX(x + 1, y + 1, 1)];
  540.                 b += intrails[PXL_INDX(x + 1, y + 1, 2)];
  541.             }
  542.             if (y + 1 < SCREEN_HEIGHT) {
  543.                 num++;
  544.                 color += get_color(x, y + 1);
  545.                 r += intrails[PXL_INDX(x, y + 1, 0)];
  546.                 g += intrails[PXL_INDX(x, y + 1, 1)];
  547.                 b += intrails[PXL_INDX(x, y + 1, 2)];
  548.             }
  549.             if (x - 1 > 0 && y + 1 < SCREEN_HEIGHT) {
  550.                 num++;
  551.                 color += get_color(x - 1, y + 1);
  552.                 r += intrails[PXL_INDX(x - 1, y + 1, 0)];
  553.                 g += intrails[PXL_INDX(x - 1, y + 1, 1)];
  554.                 b += intrails[PXL_INDX(x - 1, y + 1, 2)];
  555.             }
  556.  
  557.             if (x + 1 < SCREEN_WIDTH) {
  558.                 num++;
  559.                 color += get_color(x + 1, y);
  560.                 r += intrails[PXL_INDX(x + 1, y, 0)];
  561.                 g += intrails[PXL_INDX(x + 1, y, 1)];
  562.                 b += intrails[PXL_INDX(x + 1, y, 2)];
  563.             }
  564.             if (1) {
  565.                 num++;
  566.                 color += get_color(x, y);
  567.                 r += intrails[PXL_INDX(x, y, 0)];
  568.                 g += intrails[PXL_INDX(x, y, 1)];
  569.                 b += intrails[PXL_INDX(x, y, 2)];
  570.             }
  571.             if (x - 1 > 0) {
  572.                 num++;
  573.                 color += get_color(x - 1, y);
  574.                 r += intrails[PXL_INDX(x - 1, y, 0)];
  575.                 g += intrails[PXL_INDX(x - 1, y, 1)];
  576.                 b += intrails[PXL_INDX(x - 1, y, 2)];
  577.             }
  578.  
  579.             if (x + 1 < SCREEN_WIDTH && y - 1 > 0) {
  580.                 num++;
  581.                 color += get_color(x + 1, y - 1);
  582.                 r += intrails[PXL_INDX(x + 1, y - 1, 0)];
  583.                 g += intrails[PXL_INDX(x + 1, y - 1, 1)];
  584.                 b += intrails[PXL_INDX(x + 1, y - 1, 2)];
  585.             }
  586.             if (y - 1 > 0) {
  587.                 num++;
  588.                 color += get_color(x, y - 1);
  589.                 r += intrails[PXL_INDX(x, y - 1, 0)];
  590.                 g += intrails[PXL_INDX(x, y - 1, 1)];
  591.                 b += intrails[PXL_INDX(x, y - 1, 2)];
  592.             }
  593.             if (x - 1 > 0 && y - 1 > 0) {
  594.                 num++;
  595.                 color += get_color(x - 1, y - 1);
  596.                 r += intrails[PXL_INDX(x - 1, y - 1, 0)];
  597.                 g += intrails[PXL_INDX(x - 1, y - 1, 1)];
  598.                 b += intrails[PXL_INDX(x - 1, y - 1, 2)];
  599.             }
  600.  
  601.             //r = (float)color / 1000.0;
  602.             //g = (float)color / 100.0;
  603.             //b = (float)color / 10.0;
  604.  
  605.             blur[PXL_INDX(x, y, 0)] = r / (GLfloat)num;
  606.             blur[PXL_INDX(x, y, 1)] = g / (GLfloat)num;
  607.             blur[PXL_INDX(x, y, 2)] = b / (GLfloat)num;
  608.            
  609.         }
  610.  
  611.     }
  612.     /*
  613.     for (y = 0; y < SCREEN_HEIGHT; y++) {
  614.         for (x = 0; x < SCREEN_WIDTH; x++) {
  615.             if (contour[PXL_INDX(x, y, 0)] == 1 &&
  616.                 contour[PXL_INDX(x, y, 1)] == 1 &&
  617.                 contour[PXL_INDX(x, y, 2)] == 1
  618.                 ) {
  619.                 for (i = 0; i < 3; i++) {
  620.                     blur[PXL_INDX(x, y, i)] = 1;
  621.                 }
  622.             }
  623.         }
  624.     }*/
  625. }
  626.  
  627. void draw_smoothness()//S
  628. {
  629.     glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_FLOAT, (void*)blur);
  630. }
  631.  
  632. void figure_pos()
  633. {
  634.     int max_x = 0, min_x = SCREEN_WIDTH;
  635.     int max_y = 0, min_y = SCREEN_HEIGHT;
  636.  
  637.     for (int i = 0; i < vertices.size(); i++) {
  638.         if (vertices[i].x > max_x) max_x = vertices[i].x;
  639.         if (vertices[i].x < min_x) min_x = vertices[i].x;
  640.         if (vertices[i].y > max_y) max_y = vertices[i].y;
  641.         if (vertices[i].y < min_y) min_y = vertices[i].y;
  642.     }
  643.  
  644.     x_max = max_x;
  645.     x_min = min_x;
  646.     y_max = max_y;
  647.     y_min = min_y;
  648. }
  649.  
  650. void renderScene(void)
  651. {
  652.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  653.  
  654.     if (draw_flag) {
  655.         if (0 != vertices.size()) {
  656.             set_contour();
  657.             draw_contour();
  658.         }
  659.     }
  660.  
  661.     if (fill_flag) {
  662.         set_edges();
  663.         //set_intrails_vector();
  664.         set_intrails_pixels();
  665.         draw_intrails();
  666.     }
  667.  
  668.     if (anti_aliacing_flag) {
  669.         postfiltration();
  670.         draw_smoothness();
  671.     }
  672.     if (clear_flag) {
  673.         clear_buffers();
  674.         clear_vector(vertices);
  675.     }
  676.  
  677.     glutSwapBuffers();
  678. }
  679.  
  680. void mouseButton(int button, int state, int x, int y)
  681. {
  682.     if (GLUT_LEFT_BUTTON == button) {
  683.         point a;
  684.         if (GLUT_DOWN == state) {
  685.             a.x = x;
  686.             a.y = SCREEN_HEIGHT - y;
  687.             printf("%d %d\n", a.x, a.y);
  688.             vertices.push_back(a);
  689.             figure_pos();
  690.             glutPostRedisplay();
  691.         }
  692.         if (GLUT_UP == state) {
  693.             glutPostRedisplay();
  694.         }
  695.     }
  696. }
  697.  
  698. void keyboardButton(unsigned char key, int x, int y)
  699. {
  700.     if ('d' == key || 'D' == key) {
  701.         draw_flag = !draw_flag;
  702.         figure_pos();
  703.         printf("x_min=%d x_max=%d\ny_min=%d y_max=%d\n", x_min, x_max, y_min, y_max);
  704.         draw_flag ? printf("drawing:ON\n") : printf("drawing:OFF\n");
  705.         glutPostRedisplay();
  706.     }
  707.     if ('s' == key || 'S' == key) {
  708.         anti_aliacing_flag = !anti_aliacing_flag;
  709.         anti_aliacing_flag ? printf("mode:PS4 / XBOX one\n") : printf("mode:PC\n");
  710.         glutPostRedisplay();
  711.     }
  712.     if ('f' == key || 'F' == key) {
  713.         fill_flag = !fill_flag;
  714.         figure_pos();
  715.         fill_flag ? printf("filling:ON\n") : printf("filling:OFF\n");
  716.         glutPostRedisplay();
  717.     }
  718.     /*if ('z' == key || 'Z' == key) {
  719.         if (0 < vertices.size()) {
  720.             vertices.pop_back();
  721.             figure_pos();
  722.             glutPostRedisplay();
  723.         }
  724.     }*/
  725.     if ('a' == key || 'A' == key) {
  726.         Bresenham(vertices[vertices.size() - 1].x, vertices[vertices.size() - 1].y, vertices[0].x, vertices[0].y);
  727.         glutPostRedisplay();
  728.     }
  729.     if ('q' == key || 'Q' == key) {
  730.         thick_flag = !thick_flag;
  731.         thick_flag ? printf("thick lines:ON\n") : printf("thick lines:OFF\n");
  732.         glutPostRedisplay();
  733.     }
  734.     if ('w' == key || 'W' == key) {
  735.         clear_flag = !clear_flag;
  736.         printf("cleared\n");
  737.         glutPostRedisplay();
  738.     }
  739.     if ('t' == key || 'T' == key) {
  740.         printf("table_size=%d\n", edge_table.size());
  741.         for (int i = 0; i < edge_table.size(); i++) {
  742.             printf("[%d](%d):", i, edge_table[i].size());
  743.             for (int j = 0; j < edge_table[i].size(); j++) {
  744.                 printf("%d ", edge_table[i][j]);
  745.             }
  746.             printf(" -> ");
  747.             if (edge_table[i].size() % 2 == 0 && 0 != edge_table[i].size()) {
  748.                 for (int j = 0; j < edge_table[i].size() - 1; j += 2) {
  749.                     int start = edge_table[i][j];
  750.                     int stop = edge_table[i][j + 1];
  751.                     printf("%d..%d ", start, stop);
  752.                 }
  753.                
  754.             }
  755.             printf("\n");
  756.         }
  757.         glutPostRedisplay();
  758.     }
  759. }
  760.  
  761. void initial_state()
  762. {
  763.     glClearColor(0, 0, 0, 1);
  764.  
  765.     draw_flag = false;
  766.     fill_flag = false;
  767.     anti_aliacing_flag = false;
  768.     update_flag = false;
  769.     thick_flag = false;
  770.  
  771.     thick_flag ? printf("thick lines:ON\n") : printf("thick lines:OFF\n");
  772.  
  773.     x_max = 0;
  774.     x_min = SCREEN_WIDTH;
  775.     y_max = 0;
  776.     y_min = SCREEN_HEIGHT;
  777.  
  778.     create_buffers();
  779.     clear_buffers();
  780. }
  781.  
  782. int main(int argc, char **argv)
  783. {
  784.     glutInit(&argc, argv);
  785.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  786.     glutInitWindowPosition(SCREEN_POS_X, SCREEN_POS_Y);
  787.     glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  788.     glutCreateWindow("Anthony's Project: Lab 4");
  789.  
  790.     glutDisplayFunc(renderScene);
  791.     glutReshapeFunc(changeSize);
  792.     glutIdleFunc(renderScene);
  793.  
  794.     glutMouseFunc(mouseButton);
  795.     glutKeyboardFunc(keyboardButton);
  796.  
  797.     initial_state();
  798.  
  799.     glutMainLoop();
  800.  
  801.     return 0;
  802. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement