Advertisement
Kojima0502

☆display_BMP_openGL☆

Jan 31st, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.19 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  "depth_combine48_cunbus.bmp"
  8. #define  CHANNEL  3  // color(R, G, B)
  9. int i,j;
  10. int width;           // width of picture
  11. int height;          // height of picture
  12. int window1, window2;
  13. /*
  14. float get_wx ;
  15. float get_wy ;
  16. float get_ww ;
  17. float get_wh ;
  18.  */
  19. //float Xshift,Yshift;
  20.  
  21. GLuint texture;      // texture number
  22. GLubyte* image;      // keep the BMP data
  23.  
  24. void loadImage(const char* filepath);  // read BMP
  25.  
  26. float shiftX,shiftY;
  27. float mouseX,mouseY;
  28. float x,y;
  29. float a;
  30. float g_pressed = 0;
  31.  
  32.  
  33. void display(void)
  34.  
  35.  
  36. {
  37.     /*diaplay the picture */
  38.     glEnable(GL_TEXTURE_2D);
  39.     glClear(GL_COLOR_BUFFER_BIT);
  40.     glBindTexture(GL_TEXTURE_2D, texture);
  41.    
  42.     glTranslated(-0.5,-0.5,0);
  43.    
  44.     /*glBegin(GL_QUADS);
  45.      glTexCoord2f(0 , 0); glVertex2f(-0.9 , -0.9);
  46.      glTexCoord2f(0 , 1); glVertex2f(-0.9 , 0.9);
  47.      glTexCoord2f(1 , 1); glVertex2f(0.9 , 0.9);
  48.      glTexCoord2f(1 , 0); glVertex2f(0.9 , -0.9);
  49.      glEnd();
  50.      */
  51.     glBegin(GL_QUADS);
  52.     glTexCoord2f(1.0, 0.0);  glVertex2f(1.0, 0.0);//position
  53.     glTexCoord2f(1.0, 1.0);  glVertex2f(1.0, 1.0);
  54.     glTexCoord2f(0.0, 1.0);  glVertex2f(0.0, 1.0);
  55.     glTexCoord2f(0.0, 0.0);  glVertex2f(0.0, 0.0);
  56.    
  57.     glDisable(GL_TEXTURE_2D);
  58.    
  59.     glEnd();
  60.    
  61.    
  62.     //glClear(GL_COLOR_BUFFER_BIT);
  63.    
  64.    
  65.    
  66.    
  67.     //glColor3d(0.0, 0.0, 0.0);//color
  68.    
  69.    
  70.     glTranslated(0.5,0.5,0);
  71.     //glTranslatef(shiftX,shiftY,0);
  72.    
  73.     x=0.1;
  74.     y=0.1;
  75.    
  76.     glBegin(GL_LINE_LOOP);
  77.    /*
  78.     get_wx=glutGet( GLUT_WINDOW_X );
  79.     get_wx=glutGet( GLUT_WINDOW_Y );
  80.     get_wx=glutGet( GLUT_WINDOW_WIDTH );
  81.     get_wx=glutGet( GLUT_WINDOW_HEIGHT );
  82.     */
  83.     glVertex2d(-x+shiftX, -y+shiftY);//左下
  84.     glVertex2d(x+shiftX, -y+shiftY);//右下
  85.     glVertex2d(x+shiftX, y+shiftY);//右上
  86.     glVertex2d(-x+shiftX, y+shiftY);//カーソルの左上
  87.     printf("ShiftX1 = %f : ShiftY1 = %f\n" , -x+shiftX , -y+shiftY);//左下
  88.     printf("ShiftX2 = %f : ShiftY2 = %f\n" , x+shiftX , -y+shiftY);//
  89.     printf("ShiftX3 = %f : ShiftY3 = %f\n" , x+shiftX , y+shiftY);//
  90.     printf("ShiftX4 = %f : ShiftY4 = %f\n" , -x+shiftX , y+shiftY);//
  91.     //get_wx=glutGet( GLUT_WINDOW_X );
  92.     //get_wy=glutGet( GLUT_WINDOW_Y );
  93.     //get_ww=glutGet( GLUT_WINDOW_WIDTH );
  94.     //get_wh=glutGet( GLUT_WINDOW_HEIGHT );
  95.    
  96.    // Xshift=x+shiftX;
  97.    // Yshift=y+shiftY;
  98.    
  99.    
  100.    
  101.     //glPushMatrix();
  102.     //glTranslatef(shiftX,shiftY,0);
  103.     // glTranslatef(0.5,0.5,0);
  104.     //glPopMatrix();
  105.     glEnd();
  106.    
  107.    
  108.     glutSwapBuffers();
  109.    
  110.    
  111.    
  112.     //glFlush();
  113.    
  114. }
  115.  
  116.  
  117.  
  118. void myInit(void)
  119. {
  120.     /* necessary setting for before create window*/
  121.     //window1//
  122.     glutInitWindowSize(600, 600);                  // setting for window size
  123.     glutInitWindowPosition(0, 0);              // window position
  124.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  // setting display mode
  125.     glutCreateWindow("OpenGL");// create window
  126.    
  127.    
  128.     glClearColor (0.0, 0.0, 0.0, 1.0);             // window back color
  129.    
  130.     glutDisplayFunc(display);
  131.     //create texture
  132.     loadImage(IMGFILE);
  133.     glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
  134.     glGenTextures(1, &texture);
  135.     glBindTexture(GL_TEXTURE_2D, texture);
  136.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  137.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  138.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  139.                  width, height, 0, GL_RGB,
  140.                  GL_UNSIGNED_BYTE, image);
  141.    
  142.      
  143.    
  144.     //gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, WIDTH, TEXHEIGHT,
  145.     //        GL_RGB, GL_UNSIGNED_BYTE, texture);
  146.    
  147.     free(image);}
  148.  
  149. void keyboard(unsigned char key, int x, int y)  //keyboard setting
  150. {
  151.     switch (key) {
  152.         case 'q':
  153.         case 'Q':
  154.         case '\033':
  155.             exit(0);
  156.         default:
  157.             break;
  158.     }
  159. }
  160.  
  161.  
  162. void mouse(int btn, int state, int x, int y)  //mouze setting
  163. {
  164.    
  165.    
  166.     if(btn == GLUT_LEFT_BUTTON == state == GLUT_DOWN)  //when press the left button
  167.     {
  168.         //Xshift=x+shiftX;
  169.        // Yshift=y+shiftY;
  170.         mouseX = x;
  171.         mouseY = y;
  172.         g_pressed = 1;
  173.        
  174.     }
  175.     else if(btn == GLUT_LEFT_BUTTON == state ==GLUT_UP)  //when take off the left button
  176.         g_pressed = 0;
  177.    
  178. }
  179.  
  180.  
  181. void motion(int x, int y)  //controll the motion by the mouse
  182. {
  183.     {
  184.        
  185.         shiftX = (float)((x - mouseX)/320);
  186.         shiftY = (float)(-(y - mouseY)/320);
  187.         glutPostRedisplay();
  188.        
  189.     }
  190.    
  191.    /* printf("X = %d : Y = %d\n" , x , y);  //diplay the position of coordinate
  192.     printf("ShiftX = %f : ShiftY = %f\n" , shiftX , shiftY);  //display the amount of the shift
  193.     printf("mouseX = %f : mouseY = %f \n" , -x+shiftX , mouseY);
  194.     //printf("mouseX = %f : mouseY = %f \n" , mouseX , mouseY);  //display the mouseX and mouseY
  195.     printf("wx = %f : wy = %f \n" , get_wx , get_wy);
  196.    // printf("windowX = %f : windowY = %f \n" , get_wx , get_wy);
  197.     printf("windowW = %f : windowH = %f \n" , get_ww , get_wh);
  198.     */
  199. }
  200.  
  201. int main(int argc, char* argv[])
  202. {
  203.    
  204.    
  205.     glutInit(&argc, argv);
  206.     //glutInitDisplayMode(GLUT_RGBA);
  207.     //glutCreateWindow(argv[0]);
  208.     myInit();
  209.    
  210.    
  211.     glutDisplayFunc(display);  //when need to set about display function
  212.     glutKeyboardFunc(keyboard);  //when need keyboard function
  213.     glutMouseFunc(mouse);  //when need mouse function
  214.     glutMotionFunc(motion);  //when need mouse motion function
  215.     //glutIdleFunc(idle_func);
  216.     glutMainLoop();
  217.    
  218.     return 0;
  219. }
  220.  
  221.  
  222. void loadImage(const char* filepath)
  223. {
  224.     int i;
  225.     int size;  // size of picture (width , height, color)
  226.     FILE* fp;
  227.    
  228.     /* read file */
  229.     fp = fopen(filepath, "rb");  //read binary
  230.     if (fp == NULL)
  231.     {
  232.         fprintf(stderr, "%sfail loading\n", filepath);
  233.         exit(EXIT_FAILURE);
  234.     }
  235.    
  236.     /* get width and height  */
  237.     fseek(fp, 18, SEEK_SET);  //move fp to place where has been put width of picture
  238.     fread(&width, 4, 1, fp);
  239.     fread(&height, 4, 1, fp);
  240.    
  241.     /* read data of picture */
  242.     fseek(fp, 54, SEEK_SET);  //move fp to place where has been put dataof the picture
  243.     size = width * height * CHANNEL;
  244.     image = (GLubyte* ) malloc(size);
  245.     if (image == NULL)
  246.     {
  247.         fprintf(stderr, "fail to keep the memory\n");
  248.         exit(EXIT_FAILURE);
  249.     }
  250.    
  251.     for (i = 0; i < size; ++i)
  252.         fread(&image[i], sizeof(GLubyte), 1, fp);
  253.    
  254.     /* exit */
  255.     fclose(fp);
  256. }
  257.  
  258. /*範囲選択してからの裏でデータを読む
  259.  float *file1;
  260.  file1= (float *)malloc(画像のwidth*height*sizeof(float));
  261.  if((fpi1=fopen(argv[1],"rb"))==NULL)
  262.  {
  263.  fprintf(stderr,"input file open error\n");
  264.  exit(1);
  265.  }
  266.  fread((float *)file1,sizeof(float),[ウィンドウのwidth]*[ウィンドウのheight],fpi1);
  267.  for(i=[ウィンドウの座標(比)(x/300*width)];i<[ウィンドウのheight];i++)
  268.  {
  269.  for(j=[ウィンドウの座標(比)(y/300*height)];j<[ウィンドウのwidth];j++)
  270.  {
  271.  dn1=file2[(i-iy)*iW+(j-ix)];
  272.  }
  273.  }
  274.  free(file1);
  275.  fclose(fpi1);*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement