Advertisement
Kojima0502

☆OpenGL_bitmap_texture2_cat☆

Jan 26th, 2014
83
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 <GLUT/glut.h>
  4.  
  5.  
  6. /* keep the picture data*/
  7. #define  IMGFILE  "cat.bmp"
  8. #define  CHANNEL  3  // color(R, G, B)
  9. int width;           // width of picture
  10. int height;          // height of picture
  11. GLuint texture;      // texture number
  12. GLubyte* image;      // keep the BMP data
  13.  
  14.  
  15. void loadImage(const char* filepath);  // read BMP
  16.  
  17. float shiftX,shiftY;
  18. float mouseX,mouseY;
  19. float x,y;
  20. float a;
  21. float g_pressed = 0;
  22.  
  23. void display(void)
  24. {
  25.    
  26.    
  27.     /*diaplay the picture */
  28.     glEnable(GL_TEXTURE_2D);
  29.     glClear(GL_COLOR_BUFFER_BIT);
  30.     glBindTexture(GL_TEXTURE_2D, texture);
  31.    
  32.     glTranslated(-0.5,-0.5,0);
  33.    
  34.     glBegin(GL_QUADS);
  35.    
  36.    
  37.     glTexCoord2f(1.0, 0.0);  glVertex2f(1.0, 0.0);//position
  38.     glTexCoord2f(1.0, 1.0);  glVertex2f(1.0, 1.0);
  39.     glTexCoord2f(0.0, 1.0);  glVertex2f(0.0, 1.0);
  40.     glTexCoord2f(0.0, 0.0);  glVertex2f(0.0, 0.0);
  41.    
  42.     //glClear(GL_COLOR_BUFFER_BIT);
  43.    
  44.     glDisable(GL_TEXTURE_2D);
  45.    
  46.     glEnd();
  47.    
  48.    
  49.     //glColor3d(0.0, 0.0, 0.0);//color
  50.    
  51.    
  52.     glTranslated(0.5,0.5,0);
  53.     glTranslatef(shiftX,shiftY,0);
  54.    
  55.     x=0.1;
  56.     y=0.1;
  57.    
  58.     glBegin(GL_LINE_LOOP);
  59.    
  60.     glVertex2d(-x+shiftX, -y+shiftY);//position
  61.     glVertex2d(x+shiftX, -y+shiftY);
  62.     glVertex2d(x+shiftX, y+shiftY);
  63.     glVertex2d(-x+shiftX, y+shiftY);
  64.    
  65.     //glPushMatrix();
  66.     //glTranslatef(shiftX,shiftY,0);
  67.     // glTranslatef(0.5,0.5,0);
  68.     //glPopMatrix();
  69.     glEnd();
  70.    
  71.    
  72.     glutSwapBuffers();
  73.    
  74.    
  75.    
  76.     //glFlush();
  77.    
  78. }
  79.  
  80.  
  81.  
  82. void myInit(void)
  83. {
  84.     /* necessary setting for before create window*/
  85.     glutInitWindowSize(800, 600);                  // setting for window size
  86.     glutInitWindowPosition(0, 0);              // window position
  87.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  // setting display mode
  88.     glutCreateWindow("OpenGL");                    // create window
  89.     glClearColor (0.0, 0.0, 0.0, 1.0);             // window back color
  90.    
  91.    
  92.     glutDisplayFunc(display);
  93.     //create texture
  94.     loadImage(IMGFILE);
  95.     glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
  96.     glGenTextures(1, &texture);
  97.     glBindTexture(GL_TEXTURE_2D, texture);
  98.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  99.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  100.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  101.                  width, height, 0, GL_RGB,
  102.                  GL_UNSIGNED_BYTE, image);
  103.     free(image);
  104. }
  105.  
  106. void keyboard(unsigned char key, int x, int y)  //keyboard setting
  107. {
  108.     switch (key) {
  109.         case 'q':
  110.         case 'Q':
  111.         case '\033':
  112.             exit(0);
  113.         default:
  114.             break;
  115.     }
  116. }
  117.  
  118.  
  119. void mouse(int btn, int state, int x, int y)  //mouze setting
  120. {
  121.    
  122.    
  123.     if(btn == GLUT_LEFT_BUTTON == state == GLUT_DOWN)  //when press the left button
  124.     {
  125.        
  126.         mouseX = x;
  127.         mouseY = y;
  128.         g_pressed = 1;
  129.     }
  130.     else if(btn == GLUT_LEFT_BUTTON == state ==GLUT_UP)  //when take off the left button
  131.     {
  132.         g_pressed = 0;
  133.        
  134.     }
  135. }
  136.  
  137. void motion(int x, int y)  //controll the motion by the mouse
  138. {
  139.     {
  140.        
  141.         shiftX = (float)((x - mouseX)/6000);
  142.         shiftY = (float)(-(y - mouseY)/6000);
  143.        
  144.         glutPostRedisplay();
  145.        
  146.     }
  147.    
  148.     printf("X = %d : Y = %d\n" , x , y);  //diplay the position of coordinate
  149.     printf("ShiftX = %f : ShiftY = %f\n" , shiftX , shiftY);  //display the amount of the shift
  150.     printf("mouseX = %f : mouseY = %f \n" , mouseX , mouseY);  //display the mouseX and mouseY
  151.    
  152. }
  153.  
  154. int main(int argc, char* argv[])
  155. {
  156.    
  157.     glutInit(&argc, argv);
  158.     //glutInitDisplayMode(GLUT_RGBA);
  159.     //glutCreateWindow(argv[0]);
  160.     myInit();
  161.     glutDisplayFunc(display);  //when need to set about display function
  162.     glutKeyboardFunc(keyboard);  //when need keyboard function
  163.     glutMouseFunc(mouse);  //when need mouse function
  164.     glutMotionFunc(motion);  //when need mouse motion function
  165.    
  166.    
  167.     glutMainLoop();
  168.    
  169.     return 0;
  170. }
  171.  
  172.  
  173.  
  174. void loadImage(const char* filepath)
  175. {
  176.     int i;
  177.     int size;  // size of picture (width , height, color)
  178.     FILE* fp;
  179.    
  180.      /* read file */
  181.     fp = fopen(filepath, "rb");  //read binary
  182.     if (fp == NULL)
  183.     {
  184.         fprintf(stderr, "%sfail loading\n", filepath);
  185.         exit(EXIT_FAILURE);
  186.     }
  187.    
  188.     /* get width and height  */
  189.     fseek(fp, 18, SEEK_SET);  //move fp to place where has been put width of picture
  190.     fread(&width, 4, 1, fp);
  191.     fread(&height, 4, 1, fp);
  192.    
  193.     /* read data of picture */
  194.     fseek(fp, 54, SEEK_SET);  //move fp to place where has been put dataof the picture
  195.     size = width * height * CHANNEL;
  196.     image = (GLubyte* ) malloc(size);
  197.     if (image == NULL)
  198.     {
  199.         fprintf(stderr, "fail to keep the memory\n");
  200.         exit(EXIT_FAILURE);
  201.     }
  202.    
  203.     for (i = 0; i < size; ++i)
  204.         fread(&image[i], sizeof(GLubyte), 1, fp);
  205.    
  206.     /* exit */
  207.     fclose(fp);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement