ait-survey

triangles-win

Oct 15th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <assert.h>
  6. #include <GL/glut.h>
  7.  
  8. #define TITLE "array"
  9. #define WIDTH 320
  10. #define HEIGHT 240
  11.  
  12. #define N 20000
  13.  
  14. enum{
  15.     DRAW_TYPE_NORMAL,
  16.     DRAW_TYPE_DISPLAY_LIST,
  17.     DRAW_TYPE_DISPLAY_LIST2,
  18.     DRAW_TYPE_ARRAY,
  19.     DRAW_TYPE_ARRAY2,
  20. };
  21.  
  22. float getRandom(void);
  23. void drawNorma(void);
  24. void drawDisplayListInit(void);
  25. void drawDisplayList(void);
  26. void drawArrayInit(void);
  27. void drawArray(void);
  28. void display(void);
  29. void setOrtho(int w, int h);
  30. void reshape(int w, int h);
  31. void key(unsigned char key, int x, int y);
  32. void init(void);
  33.  
  34. GLfloat color[N*3][3];
  35. GLfloat vertex[N*3][3];
  36. GLuint triangleIndex[N][3];
  37. int drawn = 0;
  38. int drawType = 0;
  39.  
  40.  
  41. float getRandom(void)
  42. {
  43.     float v;
  44.  
  45.     v = (float)rand()/RAND_MAX;
  46.  
  47.     if(v < 0.5) v = (float)rand()/RAND_MAX;
  48.     else v = -(float)rand()/RAND_MAX;
  49.  
  50.     return v;
  51. }
  52.  
  53.  
  54. void drawNormal(void)
  55. {
  56.     int i, j;
  57.     float r, g, b;
  58.     float x, y;
  59.  
  60.     for(i = 0; i < N; i++){
  61.         glBegin(GL_TRIANGLES);
  62.         for(j = 0; j < 3; j++){
  63.             r = fabs(getRandom());
  64.             g = fabs(getRandom());
  65.             b = fabs(getRandom());
  66.             glColor3f(r, g, b);
  67.             x = getRandom();
  68.             y = getRandom();
  69.             glVertex2f(x, y);
  70.         }
  71.         glEnd();
  72.     }
  73. }
  74.  
  75.  
  76. void drawDisplayListInit(void)
  77. {
  78.     glNewList(1, GL_COMPILE);
  79.     drawNormal();
  80.     glEndList();
  81. }
  82.  
  83.  
  84. void drawDisplayList(void)
  85. {
  86.     glCallList(1);
  87. }
  88.  
  89.  
  90. void drawArrayInit(void)
  91. {
  92.     int i;
  93.  
  94.     for(i = 0; i < N*3; i++){
  95.         color[i][0] = fabs(getRandom());
  96.         color[i][1] = fabs(getRandom());
  97.         color[i][2] = fabs(getRandom());
  98.     }
  99.  
  100.     for(i = 0; i < N*3; i++){
  101.         vertex[i][0] = getRandom();
  102.         vertex[i][1] = getRandom();
  103.         vertex[i][2] = 0.;
  104.     }
  105.  
  106.     glColorPointer(3, GL_FLOAT, 0, color);
  107.     glVertexPointer(3, GL_FLOAT, 0, vertex);
  108.  
  109.     for(i = 0; i < N; i++){
  110.         triangleIndex[i][0] = 3*i;
  111.         triangleIndex[i][1] = 3*i + 1;
  112.         triangleIndex[i][2] = 3*i + 2;
  113.     }
  114. }
  115.  
  116.  
  117. void drawArray(void)
  118. {
  119.     glDrawElements(GL_TRIANGLES, N*3, GL_UNSIGNED_INT, triangleIndex);
  120. }
  121.  
  122.  
  123. void display(void)
  124. {
  125.     clock_t s, e;
  126.  
  127.     if(!drawn){
  128.         glClear(GL_COLOR_BUFFER_BIT);
  129.  
  130.         printf("Drawing...\n");
  131.  
  132.         switch(drawType){
  133.         case DRAW_TYPE_DISPLAY_LIST2:
  134.             printf("Initialize display list\n");
  135.             drawDisplayListInit();
  136.             break;
  137.         case DRAW_TYPE_ARRAY2:
  138.             printf("Initialize array\n");
  139.             drawArrayInit();
  140.             break;
  141.         default:
  142.             break;
  143.         }
  144.  
  145.  
  146.         s = clock();
  147.  
  148.         switch(drawType){
  149.         case DRAW_TYPE_NORMAL:
  150.             printf("Draw Type: Normal\n");
  151.             drawNormal();
  152.             break;
  153.         case DRAW_TYPE_DISPLAY_LIST:
  154.             printf("Draw Type: Display List\n");
  155.             drawDisplayListInit();
  156.             drawDisplayList();
  157.             break;
  158.         case DRAW_TYPE_DISPLAY_LIST2:
  159.             printf("Draw Type: Display List Without Initialize\n");
  160.             drawDisplayList();
  161.             break;
  162.         case DRAW_TYPE_ARRAY:
  163.             printf("Draw Type: Array\n");
  164.             drawArrayInit();
  165.             drawArray();
  166.             break;
  167.         case DRAW_TYPE_ARRAY2:
  168.             printf("Draw Type: Array Without Initialize\n");
  169.             drawArray();
  170.             break;
  171.         default:
  172.             assert(0);
  173.             break;
  174.         }
  175.  
  176.         e = clock();
  177.  
  178.         printf("Done.\n");
  179.  
  180.         printf("%.2f s\n", (float)(e - s)/CLOCKS_PER_SEC);
  181.  
  182.         drawn = 1;
  183.     }
  184.  
  185.     glutSwapBuffers();
  186. }
  187.  
  188.  
  189. void setOrtho(int w, int h)
  190. {
  191.     float aspect;
  192.  
  193.     if(w > h){
  194.         aspect = (float)w/h;
  195.         gluOrtho2D(-aspect, aspect, -1., 1.);
  196.     }else{
  197.         aspect = (float)h/w;
  198.         gluOrtho2D(-1., 1., -aspect, aspect);
  199.     }
  200. }
  201.  
  202.  
  203. void reshape(int w, int h)
  204. {
  205.     glViewport(0, 0, w, h);
  206.  
  207.     glLoadIdentity();
  208.     setOrtho(w, h);
  209.  
  210.     display();
  211. }
  212.  
  213.  
  214. void key(unsigned char key, int x, int y)
  215. {
  216.     int num;
  217.  
  218.     num = key - '0' - 1;
  219.  
  220.     switch(num){
  221.     case DRAW_TYPE_NORMAL:
  222.         drawn = 0;
  223.         drawType = DRAW_TYPE_NORMAL;
  224.         glutPostRedisplay();
  225.         break;
  226.     case DRAW_TYPE_DISPLAY_LIST:
  227.         drawn = 0;
  228.         drawType = DRAW_TYPE_DISPLAY_LIST;
  229.         glutPostRedisplay();
  230.         break;
  231.     case DRAW_TYPE_DISPLAY_LIST2:
  232.         drawn = 0;
  233.         drawType = DRAW_TYPE_DISPLAY_LIST2;
  234.         glutPostRedisplay();
  235.         break;
  236.     case DRAW_TYPE_ARRAY:
  237.         drawn = 0;
  238.         drawType = DRAW_TYPE_ARRAY;
  239.         glutPostRedisplay();
  240.         break;
  241.     case DRAW_TYPE_ARRAY2:
  242.         drawn = 0;
  243.         drawType = DRAW_TYPE_ARRAY2;
  244.         glutPostRedisplay();
  245.         break;
  246.     default:
  247.         break;
  248.     }
  249. }
  250.  
  251.  
  252. void init(void)
  253. {
  254.     glClearColor(1., 1., 1., 1.);
  255.  
  256.     srand(time(NULL));
  257.  
  258.     glEnableClientState(GL_COLOR_ARRAY);
  259.     glEnableClientState(GL_VERTEX_ARRAY);
  260.  
  261.     drawn = 1;
  262. }
  263.  
  264.  
  265. int main(int argc, char* argv[])
  266. {
  267.     int w, h;
  268.  
  269.     glutInit(&argc, argv);
  270.  
  271.     w = glutGet(GLUT_SCREEN_WIDTH);
  272.     h = glutGet(GLUT_SCREEN_HEIGHT);
  273.  
  274.     glutInitWindowSize(WIDTH, HEIGHT);
  275.     glutInitWindowPosition((w - WIDTH)/2, (h - HEIGHT)/2);
  276.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
  277.     glutCreateWindow(TITLE);
  278.  
  279.     glutDisplayFunc(display);
  280.     glutReshapeFunc(reshape);
  281.     glutKeyboardFunc(key);
  282.  
  283.     init();
  284.  
  285.     printf("Press Key\n");
  286.     printf("%d: Normal\n", DRAW_TYPE_NORMAL+1);
  287.     printf("%d: Display List\n", DRAW_TYPE_DISPLAY_LIST+1);
  288.     printf("%d: Display List Without Initialize\n", DRAW_TYPE_DISPLAY_LIST2+1);
  289.     printf("%d: Array\n", DRAW_TYPE_ARRAY+1);
  290.     printf("%d: Array Without Initialize\n", DRAW_TYPE_ARRAY2+1);
  291.  
  292.     glutMainLoop();
  293.  
  294.     return 0;
  295. }
Advertisement
Add Comment
Please, Sign In to add comment