Advertisement
Ladies_Man

cg lab4 raster1

Apr 6th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.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.  
  13. #define SCREEN_POS_X    100
  14. #define SCREEN_POS_Y    100
  15. #define SCREEN_WIDTH    800
  16. #define SCREEN_HEIGHT   600
  17. #define DATA_FORMAT     1
  18. #define BUFFER_SIZE     SCREEN_WIDTH*SCREEN_HEIGHT*DATA_FORMAT
  19. #define PXL_INDX(X,Y,COMP) ((Y*SCREEN_WIDTH+X)*DATA_FORMAT+COMP)
  20.  
  21. typedef struct {
  22.     int x, y;
  23. } point;
  24.  
  25. bool draw_flag, fill_flag, anti_aliacing_flag, update_flag;
  26. int x_max, x_min, y_max, y_min;
  27.  
  28. GLfloat *pixels;
  29.  
  30. std::vector<point> shape;
  31. std::vector<std::vector<int>> enge_table;
  32.  
  33.  
  34.  
  35. bool myfunction(point i, point j) { return (i.y<j.y); }
  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++) pixels[i] = 0;
  65. }
  66.  
  67. void draw_simple_line(point a, point b)
  68. {
  69.     if (a.x == b.x && a.y == b.y) return;
  70.  
  71.     glColor3f(1, 0, 0);
  72.     glBegin(GL_LINES);
  73.         glVertex2f(a.x, a.y);
  74.         glVertex2f(b.x, b.y);
  75.     glEnd();
  76. }
  77.  
  78. void draw_complex_line(point a, point b)
  79. {
  80.     if (a.x == b.x && a.y == b.y) return;
  81.  
  82.     int i, j;
  83.  
  84.     int dx = abs(a.x - b.x);
  85.     int dy = abs(a.y - b.y);
  86.    
  87.    
  88. }
  89.  
  90. void draw_circuit()
  91. {
  92.     int i, j;
  93.  
  94.     for (i = 0; i < shape.size() - 1; i++) {
  95.         draw_simple_line(shape[i], shape[i + 1]);
  96.     }
  97.     draw_simple_line(shape[0], shape[shape.size()-1]);
  98.  
  99.  
  100. }
  101.  
  102. void fill_circuit()
  103. {
  104.     int i, j, component = 0;
  105.  
  106.     if (3 == DATA_FORMAT) component = 3;
  107.  
  108.     clear_buffer();
  109.  
  110.     for (i = 0; i < SCREEN_HEIGHT; i++) {
  111.         for (j = 0; j < SCREEN_WIDTH; j++) {
  112.             if (i > y_min && i < y_max && j > x_min && j < x_max) {
  113.                 pixels[PXL_INDX(j, i, component)] = 1;
  114.             }
  115.         }
  116.     }
  117.  
  118.     glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RED, GL_FLOAT, (void*)pixels);
  119. }
  120.  
  121. void smooth_circuit()
  122. {
  123.  
  124. }
  125.  
  126. void figure_pos()
  127. {
  128.     int max_x = 0, min_x = SCREEN_WIDTH;
  129.     int max_y = 0, min_y = SCREEN_HEIGHT;
  130.  
  131.     for (int i = 0; i < shape.size(); i++) {
  132.         if (shape[i].x > max_x) max_x = shape[i].x;
  133.         if (shape[i].x < min_x) min_x = shape[i].x;
  134.         if (shape[i].y > max_y) max_y = shape[i].y;
  135.         if (shape[i].y < min_y) min_y = shape[i].y;
  136.     }
  137.  
  138.     x_max = max_x;
  139.     x_min = min_x;
  140.     y_max = max_y;
  141.     y_min = min_y;
  142. }
  143.  
  144. void renderScene(void)
  145. {
  146.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  147.  
  148.     if (draw_flag) {
  149.         if (0 != shape.size())
  150.             draw_circuit();
  151.     }
  152.  
  153.     if (fill_flag) {
  154.         fill_circuit();
  155.     }
  156.  
  157.     if (anti_aliacing_flag) {
  158.         smooth_circuit();
  159.     }
  160.  
  161.     if (update_flag) {
  162.         if (draw_flag) draw_circuit();
  163.         if (fill_flag) fill_circuit();
  164.         if (anti_aliacing_flag) smooth_circuit();
  165.     }
  166.  
  167.     glutSwapBuffers();
  168. }
  169.  
  170. void mouseButton(int button, int state, int x, int y)
  171. {
  172.     if (GLUT_LEFT_BUTTON == button) {
  173.         point a;
  174.         if (GLUT_DOWN == state) {
  175.             a.x = x;
  176.             a.y = SCREEN_HEIGHT - y;
  177.             printf("%d %d\n", a.x, a.y);
  178.             shape.push_back(a);
  179.             figure_pos();
  180.             glutPostRedisplay();
  181.         }
  182.         if (GLUT_UP == state) {
  183.             glutPostRedisplay();
  184.         }
  185.     }
  186. }
  187.  
  188. void keyboardButton(unsigned char key, int x, int y)
  189. {
  190.     if ('d' == key) {
  191.         draw_flag = !draw_flag;
  192.         printf("x_min=%d x_max=%d\ny_min=%d y_max=%d\n", x_min, x_max, y_min, y_max);
  193.         printf("drawing:");
  194.         draw_flag ? printf("ON\n") : printf("OFF\n");
  195.         glutPostRedisplay();
  196.     }
  197.     if ('s' == key) {
  198.         anti_aliacing_flag = !anti_aliacing_flag;
  199.         anti_aliacing_flag ? printf("pc_mode:ON\n") : printf("console_mode:ON\n");
  200.         glutPostRedisplay();
  201.     }
  202.     if ('f' == key) {
  203.         fill_flag = !fill_flag;
  204.         printf("filling:");
  205.         fill_flag ? printf("ON\n") : printf("OFF\n");
  206.         glutPostRedisplay();
  207.     }
  208.     if ('z' == key) {
  209.         shape.pop_back();
  210.         figure_pos();
  211.         glutPostRedisplay();
  212.     }
  213. }
  214.  
  215. void initial_state()
  216. {
  217.     glClearColor(1, 1, 1, 1);
  218.  
  219.     draw_flag = true;
  220.     fill_flag = false;
  221.     anti_aliacing_flag = false;
  222.     update_flag = false;
  223.  
  224.     x_max = 0;
  225.     x_min = SCREEN_WIDTH;
  226.     y_max = 0;
  227.     y_min = SCREEN_HEIGHT;
  228.    
  229.     pixels = new GLfloat[BUFFER_SIZE];
  230.     clear_buffer();
  231. }
  232.  
  233. int main(int argc, char **argv)
  234. {
  235.     glutInit(&argc, argv);
  236.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  237.     glutInitWindowPosition(SCREEN_POS_X, SCREEN_POS_Y);
  238.     glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  239.     glutCreateWindow("Anthony's Project: Lab 4");
  240.  
  241.     glutDisplayFunc(renderScene);
  242.     glutReshapeFunc(changeSize);
  243.     glutIdleFunc(renderScene);
  244.  
  245.     glutMouseFunc(mouseButton);
  246.     glutKeyboardFunc(keyboardButton);
  247.  
  248.     initial_state();
  249.  
  250.     glutMainLoop();
  251.  
  252.     return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement