rwrgb.txt
上传用户:zbjingming
上传日期:2010-01-02
资源大小:2436k
文件大小:7k
源码类别:

OpenGL

开发平台:

Visual C++

  1. From cyy Wed Aug 11 10:03:15 1999
  2. Return-Path: <cyy>
  3. Received: by ox (SMI-8.6/SMI-SVR4)
  4. id KAA00559; Wed, 11 Aug 1999 10:03:15 +0900
  5. Date: Wed, 11 Aug 1999 10:03:15 +0900
  6. From: cyy (Chen Yanyun)
  7. Message-Id: <199908110103.KAA00559@ox>
  8. To: zhx
  9. Content-Length: 6251
  10. X-Lines: 258
  11. Status: RO
  12. /*==============================================================*
  13.  | function: write_bmp |
  14.  *==============================================================*/
  15. void write_bmp(char *filename)
  16. {       
  17. int viewx,viewy;
  18.     int x,y,i,j;
  19.     int Xlen, Ylen; 
  20.     GLint port[4];
  21. unsigned char pcrgb;
  22. BITMAPFILEHEADER head;BITMAPINFOHEADER info;
  23. FILE *fp;
  24. unsigned short *rbuf;
  25. unsigned short *gbuf;
  26. unsigned short *bbuf;
  27. unsigned char *pbuf;
  28.         
  29. glGetIntegerv(GL_VIEWPORT,port);
  30. viewx = port[2]-port[0];
  31. viewy = port[3]-port[1]; 
  32.     Xlen = viewx;
  33.     Ylen = viewy;
  34.         
  35. pbuf = (unsigned char *) malloc(viewx*viewy*3);
  36. rbuf = (unsigned short *)malloc(Xlen*sizeof(short));
  37. gbuf = (unsigned short *)malloc(Xlen*sizeof(short));
  38. bbuf = (unsigned short *)malloc(Xlen*sizeof(short));
  39. if ( (pbuf == NULL)||(rbuf == NULL)||(gbuf == NULL)||(bbuf == NULL) )
  40. {
  41. printf("Malloc bbuf mamory failure n");
  42. exit(0);
  43. }
  44. glReadPixels(0,0,viewx,viewy,GL_RGB,GL_UNSIGNED_BYTE,pbuf);
  45. fp=fopen(filename,"wb");
  46. if(fp==NULL) {printf("Can't Create New Filen");exit(-1);}
  47. head.bfType=0x4d42;
  48. head.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+
  49. Xlen*Ylen*3; 
  50. head.bfReserved1=0;head.bfReserved2=0;
  51. head.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
  52. info.biSize=sizeof(BITMAPINFOHEADER);
  53. info.biWidth=Xlen;
  54. info.biHeight=Ylen;
  55. info.biPlanes=1; 
  56. info.biBitCount=24;
  57.     info.biCompression=0; 
  58. info.biSizeImage=0; 
  59. info.biXPelsPerMeter=0; 
  60.     info.biYPelsPerMeter=0; 
  61.     info.biClrUsed=0; 
  62.     info.biClrImportant=0;
  63. fwrite(&head,sizeof(BITMAPFILEHEADER),1,fp);
  64. fwrite(&info,sizeof(BITMAPINFOHEADER),1,fp);
  65. i=0;
  66. for( y=0; y<Ylen; y++)
  67. {
  68. for(x=0; x<Xlen; x++)
  69. {
  70. rbuf[x] = (short)(pbuf[(i+x)*3]);
  71. gbuf[x] = (short)(pbuf[(i+x)*3+1]);
  72. bbuf[x] = (short)(pbuf[(i+x)*3+2]);
  73. }
  74. i += Xlen;
  75. for(j=0;j<Xlen;j++)
  76. { pcrgb=(unsigned char)bbuf[j];
  77. fwrite(&pcrgb,1,1,fp);
  78. pcrgb=(unsigned char)gbuf[j];
  79. fwrite(&pcrgb,1,1,fp);
  80. pcrgb=(unsigned char)rbuf[j];
  81. fwrite(&pcrgb,1,1,fp);
  82. }
  83. j=Xlen;
  84. if(j%4!=0) j=4-j%4;
  85. else j=0;
  86. pcrgb=0;
  87. while(j>0)
  88. { fwrite(&pcrgb,1,1,fp);
  89. j--;
  90. }/* this is because windows bmp's characteristic */
  91. }
  92. fclose(fp);
  93. free(pbuf);
  94. free(rbuf);
  95. free(gbuf);
  96. free(bbuf); 
  97. }/*____write_bmp____*/
  98. ///////////////////////////////////////////////////////////////////
  99. /*==============================================================*
  100.  | function: OpenTexture |
  101.  *==============================================================*/
  102. void OpenTexture( char *fn )
  103. {
  104. #if platform==windowsnt
  105. BITMAPFILEHEADER head;BITMAPINFOHEADER info;
  106. unsigned char *ImageData,ddd;FILE *fp;
  107. int  index;
  108. int i,j,rslp, rslx, rsly;
  109. fp=fopen(fn,"rb");
  110. if(fp==NULL) {printf("Can't Create New Filen");exit(-1);}
  111. fread(&head,sizeof(BITMAPFILEHEADER),1,fp);
  112. fread(&info,sizeof(BITMAPINFOHEADER),1,fp);
  113. rslx=info.biWidth;
  114. rsly=info.biHeight;
  115. if(rslx%4!=0) rslp=rslx+4-rslx%4;
  116. else rslp=rslx;
  117. ImageData=(unsigned char *) malloc(rslp*rsly*3);
  118. if(ImageData==NULL){
  119. printf("Malloc ImageData mamory failure n");
  120. exit(0);
  121. }
  122. fread(ImageData,1,rslp*rsly*3,fp);
  123. rslp*=3;
  124. for(i=0;i<rsly;i++){
  125. index=i*rslp;
  126. for(j=0;j<rslx;j++){
  127. ddd=ImageData[index];
  128. ImageData[index]=ImageData[index+2];
  129. ImageData[index+2]=ddd;
  130. index+=3;
  131. }
  132. }
  133. fclose(fp);
  134. #else
  135. IMAGE *image;
  136. unsigned short srcx, srcy, srcz,
  137. x, y;
  138. unsigned int i, rslp, srcp, srcp2, rslx, rsly;
  139. unsigned short *rbuf, *gbuf, *bbuf;
  140. float daltarx, daltary;
  141. char *SImageData, *ImageData;
  142. /*
  143.  * load a image
  144.  */
  145. if((image = iopen(fn, "r", RLE(1), 3, srcx, srcy, 3)) == NULL)
  146. { printf("n Can not open %sn", fn); return; }
  147. srcx = image->xsize;
  148. srcy = image->ysize;
  149. srcz = image->zsize;
  150. if( srcz < 3 )
  151. { printf("n Can not read %sn", fn); return; }
  152. SImageData = (unsigned char *)malloc(srcx*srcy*3);
  153. if(SImageData==NULL){
  154. printf("Malloc SImageData mamory failure n");
  155. exit(0);
  156. }
  157. rbuf = (unsigned short*)malloc(srcx*sizeof(short));
  158. if(rbuf==NULL){
  159. printf("Malloc rbuf mamory failure n");
  160. exit(0);
  161. }
  162.    gbuf = (unsigned short*)malloc(srcx*sizeof(short));
  163. if(gbuf==NULL){
  164. printf("Malloc gbuf mamory failure n");
  165. exit(0);
  166. }
  167.    bbuf = (unsigned short*)malloc(srcx*sizeof(short));
  168. if(bbuf==NULL){
  169. printf("Malloc bbuf mamory failure n");
  170. exit(0);
  171. }
  172. i = 0;
  173.    for( y=0; y<srcy; y++)
  174. {
  175.       getrow(image, rbuf,y,0);
  176.       getrow(image, gbuf,y,1);
  177.       getrow(image, bbuf,y,2);
  178.       for(x=0; x<srcx; x++)
  179. {
  180.          SImageData[i] = (unsigned char)rbuf[x]; i++;
  181.          SImageData[i] = (unsigned char)gbuf[x]; i++;
  182.          SImageData[i] = (unsigned char)bbuf[x]; i++;
  183.         }
  184. }
  185. iclose(image);
  186. /*
  187.  * Texture size
  188.  */ 
  189. rslx = rsly = 1;
  190. while( (rslx<<1) <= srcx ) rslx <<= 1;
  191. while( (rsly<<1) <= srcy ) rsly <<= 1;
  192. ImageData = (unsigned char *)malloc( rslx * rsly * 3 );
  193. if(ImageData==NULL){
  194. printf("Malloc ImageData mamory failure n");
  195. exit(0);
  196. }
  197. /*
  198.  * Calc. daltar
  199.  */
  200. daltarx = (float)srcx/(float)rslx;
  201. daltary = (float)srcy/(float)rsly;
  202. /*
  203.  * map source picture to texture-image
  204.  */
  205. rslp = srcp = 0;
  206. for( y = 0; y < rsly; y++ )
  207. {
  208. srcp2 = srcx * 3 * (int)( daltary*y );
  209. rslp = y * rslx * 3;
  210. for( x = 0; x < rslx; x ++ )
  211. {
  212. ImageData[ rslp ] = SImageData[ srcp ];
  213. ImageData[ rslp + 1 ] = SImageData[ srcp + 1 ];
  214. ImageData[ rslp + 2 ] = SImageData[ srcp + 2 ];
  215. rslp += 3;
  216. srcp  = srcp2 + 3 * (int)((x+1) * daltarx);
  217. }
  218. }
  219. /*
  220.  * free memory
  221.  */
  222. free( rbuf );
  223. free( gbuf );
  224. free( bbuf ); 
  225. free( SImageData );
  226. #endif
  227. /*
  228.  * Enable 2D texture
  229.  */
  230. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  231. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  232. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  233. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  234. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  235. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  236. glTexImage2D ( GL_TEXTURE_2D, 0, 3, rslx, rsly,
  237. 0,GL_RGB, GL_UNSIGNED_BYTE, ImageData );
  238. glEnable(GL_TEXTURE_2D);
  239. free( ImageData );
  240. }/*____Open Texture____*/
  241. ////////////////////////////////////////////////////////////////