Advertisement
Ladies_Man

cg lab4 raster3 (almost postfiltration 3x3)

Apr 14th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.21 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         3
  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 *contour;
  31. GLfloat *intrails;
  32. GLfloat *blur;
  33.  
  34. std::vector<point> shape;
  35. std::vector<std::vector<int>> edge_table;
  36.  
  37. bool is_white(GLfloat *buff_source, int x, int y)
  38. {
  39.     if (buff_source[PXL_INDX(x, y, 0)] == 1 &&
  40.         buff_source[PXL_INDX(x, y, 1)] == 1 &&
  41.         buff_source[PXL_INDX(x, y, 2)] == 1
  42.         )
  43.         return true;
  44.     return false;
  45. }
  46.  
  47. bool is_black(GLfloat *buff_source, int x, int y)
  48. {
  49.     if (buff_source[PXL_INDX(x, y, 0)] == 0 &&
  50.         buff_source[PXL_INDX(x, y, 1)] == 0 &&
  51.         buff_source[PXL_INDX(x, y, 2)] == 0
  52.         )
  53.         return true;
  54.     return false;
  55. }
  56.  
  57. void changeSize(int w, int h)
  58. {
  59.     // Prevent a divide by zero, when window is too short
  60.     // (you cant make a window of zero width).
  61.     if (0 == h)
  62.         h = 1;
  63.  
  64.     float ratio = w * 1.0 / h;
  65.  
  66.     // Use the Projection Matrix
  67.     glMatrixMode(GL_PROJECTION);
  68.  
  69.     // Reset Matrix
  70.     glLoadIdentity();
  71.  
  72.     // Set the viewport to be the entire window
  73.     glViewport(0, 0, w, h);
  74.  
  75.     // Set the correct perspective.
  76.     gluOrtho2D(0, (GLdouble)w, 0, (GLdouble)h);
  77.  
  78.     // Get Back to the Modelview
  79.     glMatrixMode(GL_MODELVIEW);
  80. }
  81.  
  82. void clear_buffer()
  83. {
  84.     for (int i = 0; i < BUFFER_SIZE; i++) {
  85.         contour[i] = 0;
  86.         intrails[i] = 0;
  87.         blur[i] = 0;
  88.     }
  89. }
  90.  
  91. void copy_buffer(GLfloat *buff_source, GLfloat *buff_dest)
  92. {
  93.     for (int i = 0; i < BUFFER_SIZE; i++) {
  94.         buff_dest[i] = buff_source[i];
  95.     }
  96. }
  97.  
  98. void create_buffer()
  99. {
  100.     contour = new GLfloat[BUFFER_SIZE];
  101.     intrails = new GLfloat[BUFFER_SIZE];
  102.     blur = new GLfloat[BUFFER_SIZE];
  103. }
  104.  
  105. void draw_simple_line(point a, point b)
  106. {
  107.     if (a.x == b.x && a.y == b.y) return;
  108.  
  109.     glColor3f(1, 0, 0);
  110.     glBegin(GL_LINES);
  111.         glVertex2f(a.x, a.y);
  112.         glVertex2f(b.x, b.y);
  113.     glEnd();
  114. }
  115.  
  116. void Bresenham(int x1, int y1, int const x2, int const y2)
  117. {
  118.     int delta_x(x2 - x1);
  119.     // if x1 == x2, then it does not matter what we set here
  120.     signed char const ix((delta_x > 0) - (delta_x < 0));
  121.     delta_x = std::abs(delta_x) << 1;
  122.  
  123.     int delta_y(y2 - y1);
  124.     // if y1 == y2, then it does not matter what we set here
  125.     signed char const iy((delta_y > 0) - (delta_y < 0));
  126.     delta_y = std::abs(delta_y) << 1;
  127.  
  128.     //plot(x1, y1);
  129.     contour[PXL_INDX(x1, y1, 0)] = 1;
  130.  
  131.     if (delta_x >= delta_y)
  132.     {
  133.         // error may go below zero
  134.         int error(delta_y - (delta_x >> 1));
  135.  
  136.         while (x1 != x2)
  137.         {
  138.             if ((error >= 0) && (error || (ix > 0)))
  139.             {
  140.                 error -= delta_x;
  141.                 y1 += iy;
  142.             }
  143.             // else do nothing
  144.  
  145.             error += delta_y;
  146.             x1 += ix;
  147.  
  148.             //plot(x1, y1);
  149.             for (int i = 0; i < DATA_FORMAT; i++) {
  150.                 contour[PXL_INDX(x1, y1, i)] = 1;
  151.             }
  152.            
  153.         }
  154.     }
  155.     else
  156.     {
  157.         // error may go below zero
  158.         int error(delta_x - (delta_y >> 1));
  159.  
  160.         while (y1 != y2)
  161.         {
  162.             if ((error >= 0) && (error || (iy > 0)))
  163.             {
  164.                 error -= delta_y;
  165.                 x1 += ix;
  166.             }
  167.             // else do nothing
  168.  
  169.             error += delta_x;
  170.             y1 += iy;
  171.  
  172.             //plot(x1, y1);
  173.             for (int i = 0; i < DATA_FORMAT; i++) {
  174.                 contour[PXL_INDX(x1, y1, i)] = 1;
  175.             }
  176.         }
  177.     }
  178. }
  179.  
  180. void draw_complex_line(point a, point b)
  181. {
  182.     if (a.x == b.x && a.y == b.y) return;
  183.  
  184.     Bresenham(a.x, a.y, b.x, b.y);
  185. }
  186.  
  187. void set_contour()//D
  188. {
  189.     for (int i = 0; i < shape.size() - 1; i++)
  190.         draw_complex_line(shape[i], shape[i + 1]);
  191. }
  192.  
  193. void draw_contour()//D
  194. {
  195.     glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_FLOAT, (void*)contour);
  196. }
  197.  
  198. void set_intrails_vector()//F
  199. {
  200.     //glReadpixels() //проверить буыферы - во внешний/внтренний записывается/считывается???
  201.    
  202.     int x, y, k, l, i, j, m, a, component = 0, height;
  203.     bool inside = false, stop_not_found = false;
  204.  
  205.     if (3 == DATA_FORMAT) component = 3;
  206.  
  207.     //copy_buffer(contour, intrails);
  208.  
  209.     int vector_size = y_max - y_min;
  210.  
  211.     edge_table.resize(vector_size);
  212.    
  213.     for (y = y_min + 2, k = 0; y <= y_max - 1; y++, k++) {
  214.         inside = false;
  215.         for (x = x_min - 1; x <= x_max + 1; x++) {
  216.             if (is_white(contour, x, y)) {
  217.                 inside = true;
  218.                 while (is_white(contour, x, y)) {
  219.                     edge_table[k].push_back(x);
  220.                     x++;
  221.                 }
  222.                 while (is_black(contour, x, y) && x < x_max && inside) {
  223.                     for (a = 0; a < DATA_FORMAT; a++) {
  224.                         intrails[PXL_INDX(x, y, a)] = 1;
  225.                     }
  226.                     x++;
  227.                 }
  228.                 while (is_white(contour, x, y)) {
  229.                     edge_table[k].push_back(x);
  230.                     x++;
  231.                 }
  232.             }
  233.         }
  234.     }
  235.     /*
  236.     for (y = y_min, k = 0; y < y_max; y++, k++) {
  237.         for (x = x_min, l = 0; x < x_max - 1; x++, l++) {
  238.             stop_not_found = false;
  239.             if (contour[PXL_INDX(x, y, component)] == 0 && contour[PXL_INDX(x+1, y, component)] == 1) {
  240.                 m = x;
  241.                 while (contour[PXL_INDX(m, y, component)] == 0 && m < x_max) {
  242.                     stop_not_found = true;
  243.                     m++;
  244.                 }
  245.                 if (stop_not_found) break;
  246.                 edge_table[k].push_back(x);
  247.             }
  248.         }
  249.     }*/
  250.    
  251. }
  252.  
  253. void set_intrails_pixels()
  254. {
  255.    
  256. }
  257.  
  258. void draw_intrails()//F
  259. {
  260.     glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_FLOAT, (void*)intrails);
  261. }
  262.  
  263. void postfiltration3x3()//S
  264. {
  265.     int i, j, k, x, y, num;
  266.     float r, g, b;
  267.  
  268.     //copy_buffer(intrails, blur);
  269.     //x = 0;
  270.     //y = 2;
  271.     for (y = 0; y < SCREEN_HEIGHT; y++) {
  272.         printf("string:%d\n", y);
  273.         for (x = 0; x < SCREEN_WIDTH; x++) {
  274.             r = 0;
  275.             g = 0;
  276.             b = 0;
  277.             num = 0;
  278.  
  279.             if (x - 1 > 0 && y + 1 < SCREEN_HEIGHT) {
  280.                 num++;
  281.                 printf("(7)%d;%d;%d ", x - 1, y + 1, PXL_INDX(x - 1, y + 1, 0));
  282.                 r += contour[PXL_INDX(x - 1, y + 1, 0)];
  283.                 g += contour[PXL_INDX(x - 1, y + 1, 1)];
  284.                 b += contour[PXL_INDX(x - 1, y + 1, 2)];
  285.             }
  286.             if (y + 1 < SCREEN_HEIGHT) {
  287.                 num++;
  288.                 printf("(8)%d;%d;%d ", x, y + 1, PXL_INDX(x, y + 1, 0));
  289.                 r += contour[PXL_INDX(x, y + 1, 0)];
  290.                 g += contour[PXL_INDX(x, y + 1, 1)];
  291.                 b += contour[PXL_INDX(x, y + 1, 2)];
  292.             }
  293.             if (x + 1 < SCREEN_WIDTH && y + 1 < SCREEN_HEIGHT) {
  294.                 num++;
  295.                 printf("(9)%d;%d;%d ", x + 1, y + 1, PXL_INDX(x + 1, y + 1, 0));
  296.                 r += contour[PXL_INDX(x + 1, y + 1, 0)];
  297.                 g += contour[PXL_INDX(x + 1, y + 1, 1)];
  298.                 b += contour[PXL_INDX(x + 1, y + 1, 2)];
  299.             }
  300.  
  301.             if (x - 1 > 0) {
  302.                 num++;
  303.                 printf("(4)%d;%d;%d ", x - 1, y, PXL_INDX(x + 1, y, 0));
  304.                 r += contour[PXL_INDX(x - 1, y, 0)];
  305.                 g += contour[PXL_INDX(x - 1, y, 1)];
  306.                 b += contour[PXL_INDX(x - 1, y, 2)];
  307.             }
  308.             if (1) {
  309.                 num++;
  310.                 printf("(5)%d;%d;%d ", x, y, PXL_INDX(x, y, 0));
  311.                 r += contour[PXL_INDX(x, y, 0)];
  312.                 g += contour[PXL_INDX(x, y, 1)];
  313.                 b += contour[PXL_INDX(x, y, 2)];
  314.             }
  315.             if (x + 1 < SCREEN_WIDTH) {
  316.                 num++;
  317.                 printf("(6)%d;%d;%d ", x + 1, y, PXL_INDX(x + 1, y, 0));
  318.                 r += contour[PXL_INDX(x + 1, y, 0)];
  319.                 g += contour[PXL_INDX(x + 1, y, 1)];
  320.                 b += contour[PXL_INDX(x + 1, y, 2)];
  321.             }
  322.  
  323.             if (x - 1 > 0 && y - 1 > 0) {
  324.                 num++;
  325.                 printf("(1)%d;%d;%d ", x - 1, y - 1, PXL_INDX(x - 1, y - 1, 0));
  326.                 r += contour[PXL_INDX(x - 1, y - 1, 0)];
  327.                 g += contour[PXL_INDX(x - 1, y - 1, 1)];
  328.                 b += contour[PXL_INDX(x - 1, y - 1, 2)];
  329.             }
  330.             if (y - 1 > 0) {
  331.                 num++;
  332.                 printf("(2)%d;%d;%d ", x, y - 1, PXL_INDX(x, y - 1, 0));
  333.                 r += contour[PXL_INDX(x, y - 1, 0)];
  334.                 g += contour[PXL_INDX(x, y - 1, 1)];
  335.                 b += contour[PXL_INDX(x, y - 1, 2)];
  336.             }
  337.             if (x + 1 < SCREEN_WIDTH && y - 1 > 0) {
  338.                 num++;
  339.                 printf("(3)%d;%d;%d ", x + 1, y - 1, PXL_INDX(x + 1, y - 1, 0));
  340.                 r += contour[PXL_INDX(x + 1, y - 1, 0)];
  341.                 g += contour[PXL_INDX(x + 1, y - 1, 1)];
  342.                 b += contour[PXL_INDX(x + 1, y - 1, 2)];
  343.             }
  344.            
  345.  
  346.             blur[PXL_INDX(x, y, 0)] = r / num;
  347.             blur[PXL_INDX(x, y, 1)] = g / num;
  348.             blur[PXL_INDX(x, y, 2)] = b / num;
  349.            
  350.         }
  351.         printf("\n");
  352.     }
  353. }
  354.  
  355. void draw_smoothness()//S
  356. {
  357.     glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RED, GL_FLOAT, (void*)blur);
  358. }
  359.  
  360. void figure_pos()
  361. {
  362.     int max_x = 0, min_x = SCREEN_WIDTH;
  363.     int max_y = 0, min_y = SCREEN_HEIGHT;
  364.  
  365.     for (int i = 0; i < shape.size(); i++) {
  366.         if (shape[i].x > max_x) max_x = shape[i].x;
  367.         if (shape[i].x < min_x) min_x = shape[i].x;
  368.         if (shape[i].y > max_y) max_y = shape[i].y;
  369.         if (shape[i].y < min_y) min_y = shape[i].y;
  370.     }
  371.  
  372.     x_max = max_x;
  373.     x_min = min_x;
  374.     y_max = max_y;
  375.     y_min = min_y;
  376. }
  377.  
  378. void renderScene(void)
  379. {
  380.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  381.  
  382.     if (draw_flag) {
  383.         if (0 != shape.size()) {
  384.             set_contour();
  385.             draw_contour();
  386.         }
  387.     }
  388.  
  389.     if (fill_flag) {
  390.         set_intrails_vector();
  391.         set_intrails_pixels();
  392.         draw_intrails();
  393.     }
  394.  
  395.     if (anti_aliacing_flag) {
  396.         postfiltration3x3();
  397.         draw_smoothness();
  398.     }
  399.  
  400.     if (update_flag) {
  401.         if (draw_flag) {
  402.             set_contour();
  403.             draw_contour();
  404.         }
  405.         if (fill_flag) {
  406.             set_intrails_vector();
  407.             set_intrails_pixels();
  408.             draw_intrails();
  409.         }
  410.         if (anti_aliacing_flag) {
  411.             postfiltration3x3();
  412.             draw_smoothness();
  413.         }
  414.     }
  415.  
  416.     glutSwapBuffers();
  417. }
  418.  
  419. void mouseButton(int button, int state, int x, int y)
  420. {
  421.     if (GLUT_LEFT_BUTTON == button) {
  422.         point a;
  423.         if (GLUT_DOWN == state) {
  424.             a.x = x;
  425.             a.y = SCREEN_HEIGHT - y;
  426.             printf("%d %d\n", a.x, a.y);
  427.             shape.push_back(a);
  428.             figure_pos();
  429.             glutPostRedisplay();
  430.         }
  431.         if (GLUT_UP == state) {
  432.             glutPostRedisplay();
  433.         }
  434.     }
  435. }
  436.  
  437. void keyboardButton(unsigned char key, int x, int y)
  438. {
  439.     if ('d' == key || 'D' == key) {
  440.         draw_flag = !draw_flag;
  441.         printf("x_min=%d x_max=%d\ny_min=%d y_max=%d\n", x_min, x_max, y_min, y_max);
  442.         printf("drawing:");
  443.         draw_flag ? printf("ON\n") : printf("OFF\n");
  444.         glutPostRedisplay();
  445.     }
  446.     if ('s' == key || 'S' == key) {
  447.         anti_aliacing_flag = !anti_aliacing_flag;
  448.         anti_aliacing_flag ? printf("pc_mode:ON\n") : printf("console_mode:ON\n");
  449.         glutPostRedisplay();
  450.     }
  451.     if ('f' == key || 'F' == key) {
  452.         fill_flag = !fill_flag;
  453.         printf("filling:");
  454.         fill_flag ? printf("ON\n") : printf("OFF\n");
  455.         glutPostRedisplay();
  456.     }
  457.     if ('z' == key || 'Z' == key) {
  458.         if (0 < shape.size()) {
  459.             shape.pop_back();
  460.             figure_pos();
  461.             glutPostRedisplay();
  462.         }
  463.     }
  464.     if ('a' == key || 'A' == key) {
  465.         draw_complex_line(shape[shape.size()-1], shape[0]);
  466.         glutPostRedisplay();
  467.     }
  468. }
  469.  
  470. void initial_state()
  471. {
  472.     glClearColor(0, 0, 0, 1);
  473.  
  474.     draw_flag = false;
  475.     fill_flag = false;
  476.     anti_aliacing_flag = false;
  477.     update_flag = false;
  478.  
  479.     x_max = 0;
  480.     x_min = SCREEN_WIDTH;
  481.     y_max = 0;
  482.     y_min = SCREEN_HEIGHT;
  483.    
  484.     create_buffer();
  485.     clear_buffer();
  486. }
  487.  
  488. int main(int argc, char **argv)
  489. {
  490.     glutInit(&argc, argv);
  491.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  492.     glutInitWindowPosition(SCREEN_POS_X, SCREEN_POS_Y);
  493.     glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  494.     glutCreateWindow("Anthony's Project: Lab 4");
  495.  
  496.     glutDisplayFunc(renderScene);
  497.     glutReshapeFunc(changeSize);
  498.     glutIdleFunc(renderScene);
  499.  
  500.     glutMouseFunc(mouseButton);
  501.     glutKeyboardFunc(keyboardButton);
  502.  
  503.     initial_state();
  504.  
  505.     glutMainLoop();
  506.  
  507.     return 0;
  508. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement