Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GL/glut.h>
  2. #include <GL/gl.h>
  3. #include <GL/glu.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <math.h>
  8.  
  9. #define ESCAPE_KEY 27
  10. #define WIREFRAME_KEY 119
  11. #define INC_KEY 43
  12. #define DEC_KEY 45
  13. #define DEFAULT_TIMER 50
  14.  
  15. int window;
  16.  
  17. float xrot, yrot, zrot;
  18.  
  19. float points[45][45][3];
  20.  
  21. int wiggle_count = 0;
  22. unsigned char has_wireframe = 0;
  23. unsigned int single_shot = DEFAULT_TIMER;
  24.  
  25. int texture[1];
  26.  
  27. struct Image {
  28.     unsigned long sizeX;
  29.     unsigned long sizeY;
  30.     char *data;
  31. };
  32. typedef struct Image Image;
  33.  
  34. int ImageLoad(char *filename, Image *image) {
  35.     FILE *file;
  36.     unsigned long size;                 // size of the image in bytes.
  37.     unsigned long i;                    // standard counter.
  38.     unsigned short int planes;          // number of planes in image (must be 1)
  39.     unsigned short int bpp;             // number of bits per pixel (must be 24)
  40.     char temp;                          // temporary color storage for bgr-rgb conversion.
  41.  
  42.     if ((file = fopen(filename, "rb"))==NULL)
  43.     {
  44.         printf("File Not Found : %s\n",filename);
  45.         return 0;
  46.     }
  47.  
  48.     fseek(file, 18, SEEK_CUR);
  49.  
  50.     if ((i = fread(&image->sizeX, 4, 1, file)) != 1) {
  51.         printf("Error reading width from %s.\n", filename);
  52.     return 0;
  53.     }
  54.  
  55.     if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
  56.     printf("Error reading height from %s.\n", filename);
  57.     return 0;
  58.     }
  59.  
  60.     size = image->sizeX * image->sizeY * 3;
  61.  
  62.     if ((fread(&planes, 2, 1, file)) != 1) {
  63.     printf("Error reading planes from %s.\n", filename);
  64.     return 0;
  65.     }
  66.     if (planes != 1) {
  67.     printf("Planes from %s is not 1: %u\n", filename, planes);
  68.     return 0;
  69.     }
  70.  
  71.     if ((i = fread(&bpp, 2, 1, file)) != 1) {
  72.     printf("Error reading bpp from %s.\n", filename);
  73.     return 0;
  74.     }
  75.     if (bpp != 24) {
  76.     printf("Bpp from %s is not 24: %u\n", filename, bpp);
  77.     return 0;
  78.     }
  79.  
  80.     fseek(file, 24, SEEK_CUR);
  81.  
  82.     // read the data.
  83.     image->data = (char *) malloc(size);
  84.     if (image->data == NULL) {
  85.     printf("Error allocating memory for color-corrected image data");
  86.     return 0;  
  87.     }
  88.  
  89.     if ((i = fread(image->data, size, 1, file)) != 1) {
  90.     printf("Error reading image data from %s.\n", filename);
  91.     return 0;
  92.     }
  93.  
  94.     for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb)
  95.     temp = image->data[i];
  96.     image->data[i] = image->data[i+2];
  97.     image->data[i+2] = temp;
  98.     }
  99.    
  100.     // we're done.
  101.     return 1;
  102. }
  103.  
  104. void LoadGLTextures() {
  105.     Image *image1;
  106.  
  107.     image1 = (Image *) malloc(sizeof(Image));
  108.     if (image1 == NULL) {
  109.     printf("Error allocating space for image");
  110.     exit(0);
  111.     }
  112.  
  113.     if (!ImageLoad("Data/lesson11/tim.bmp", image1)) {
  114.     exit(1);
  115.     }
  116.  
  117.     glGenTextures(1, &texture[0]);
  118.     glBindTexture(GL_TEXTURE_2D, texture[0]);   // 2d texture (x and y size)
  119.  
  120.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
  121.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
  122.  
  123.  
  124.     glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
  125. };
  126.  
  127. void InitGL(int Width, int Height)          // We call this right after our OpenGL window is created.
  128. {
  129.     float float_x, float_y;                     // loop counters.
  130.     LoadGLTextures();               // Load The Texture(s)
  131.     glEnable(GL_TEXTURE_2D);            // Enable Texture Mapping
  132.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);   // Clear The Background Color To Blue
  133.     glClearDepth(1.0);              // Enables Clearing Of The Depth Buffer
  134.     glDepthFunc(GL_LESS);           // The Type Of Depth Test To Do
  135.     glEnable(GL_DEPTH_TEST);            // Enables Depth Testing
  136.     glShadeModel(GL_SMOOTH);            // Enables Smooth Color Shading
  137.  
  138.     glMatrixMode(GL_PROJECTION);
  139.     glLoadIdentity();               // Reset The Projection Matrix
  140.  
  141.     gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);   // Calculate The Aspect Ratio Of The Window
  142.  
  143.     glMatrixMode(GL_MODELVIEW);
  144.  
  145.     for(float_x = 0.0f; float_x < 9.0f; float_x +=  0.2f )  {
  146.     for(float_y = 0.0f; float_y < 9.0f; float_y += 0.2f)        {
  147.         points[ (int) (float_x*5) ][ (int) (float_y*5) ][0] = float_x - 4.4f;
  148.         points[ (int) (float_x*5) ][ (int) (float_y*5) ][1] = float_y - 4.4f;
  149.         points[ (int) (float_x*5) ][ (int) (float_y*5) ][2] = (float) (sin( ( (float_x*5*8)/360 ) * 3.14159 * 2 ));
  150.     }
  151.     }
  152. }
  153.  
  154. void ReSizeGLScene(int Width, int Height)
  155. {
  156.     if (Height==0)              // Prevent A Divide By Zero If The Window Is Too Small
  157.     Height=1;
  158.  
  159.     glViewport(0, 0, Width, Height);        // Reset The Current Viewport And Perspective Transformation
  160.  
  161.     glMatrixMode(GL_PROJECTION);
  162.     glLoadIdentity();
  163.  
  164.     gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
  165.     glMatrixMode(GL_MODELVIEW);
  166. }
  167.  
  168. void timer_func(int which_timer)
  169. {
  170.     glutTimerFunc(single_shot, timer_func, 0);  // re-set timer function
  171.     glutPostRedisplay();   // redraw our scene
  172. }
  173.  
  174. void DrawGLScene()
  175. {
  176.     int x, y;
  177.     float float_x, float_y, float_xb, float_yb;
  178.  
  179.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer
  180.     glLoadIdentity();               // Reset The View
  181.     glTranslatef(0.0f,0.0f,-12.0f);              // move 12 units into the screen.
  182.  
  183.     glBindTexture(GL_TEXTURE_2D, texture[0]);   // choose the texture to use.
  184.  
  185.     if (has_wireframe) {
  186.         glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
  187.     } else {
  188.         glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  189.     }
  190.  
  191.     glBegin(GL_QUADS);
  192.     for (x=0; x<44; x++) {
  193.     for (y=0; y<44; y++) {
  194.         float_x  = (float) (x)/44;
  195.         float_y  = (float) (y)/44;
  196.         float_xb = (float) (x+1)/44;
  197.         float_yb = (float) (y+1)/44;
  198.  
  199.             glTexCoord2f( float_x, float_y);
  200.             glVertex3f( points[x][y][0], points[x][y][1], points[x][y][2] );
  201.  
  202.             glTexCoord2f( float_x, float_yb );
  203.             glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2] );
  204.  
  205.             glTexCoord2f( float_xb, float_yb );
  206.             glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1][2] );
  207.  
  208.             glTexCoord2f( float_xb, float_y );
  209.             glVertex3f( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2] );
  210.     }
  211.     }
  212.     glEnd();
  213.  
  214.     if (wiggle_count == 2) { // cycle the sine values
  215.     for (y = 0; y <45; y++) {
  216.         points[44][y][2] = points[0][y][2];
  217.     }
  218.  
  219.     for( x = 0; x < 44; x++ ) {
  220.         for( y = 0; y < 45; y++) {
  221.         points[x][y][2] = points[x+1][y][2];
  222.         }
  223.     }
  224.     wiggle_count = 0;
  225.     }
  226.     wiggle_count++;
  227.  
  228.     xrot +=0.3f;
  229.     yrot +=0.2f;
  230.     zrot +=0.7f;
  231.  
  232.     glutSwapBuffers();
  233. }
  234.  
  235. void keyPressed(unsigned char key, int x, int y)
  236. {
  237.     usleep(100);
  238.  
  239.     switch (key) {
  240.     case ESCAPE_KEY:
  241.         glutDestroyWindow(window);
  242.         exit(0);
  243.     case WIREFRAME_KEY:
  244.         has_wireframe = ~has_wireframe;
  245.         break;
  246.     case INC_KEY:
  247.         single_shot += 10;
  248.         break;
  249.     case DEC_KEY:
  250.         single_shot -= 10;
  251.         break;
  252.     default:
  253.         break;
  254.     }
  255.  
  256.     printf("%d\n", single_shot);
  257. }
  258.  
  259. int main(int argc, char **argv)
  260. {
  261.     glutInit(&argc, argv);  
  262.  
  263.  
  264.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);  
  265.     glutInitWindowSize(640, 480);  
  266.     glutInitWindowPosition(0, 0);  
  267.     window = glutCreateWindow("CG");  
  268.     glutDisplayFunc(&DrawGLScene);  
  269.  
  270.     glutTimerFunc(single_shot, timer_func, 0);
  271.  
  272.     glutReshapeFunc(&ReSizeGLScene);
  273.  
  274.     glutKeyboardFunc(&keyPressed);
  275.  
  276.     InitGL(640, 480);
  277.  
  278.     glutMainLoop();
  279.  
  280.     return 1;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement