لود فایل بیت مپ با اپن جی ال+خواندن فایل تصویری و نمایش آن توسط openGL
شنبه, ۱۹ دی ۱۳۹۴، ۱۰:۲۰ ق.ظ
منبع:gameover.blog.ir
این کد بسیار کمیاب هست و بهترین کدی بود که براتون قرار میدیم:
نمایش فایل bmp:
ابتدا تابع LoadBitmapFile رو کدنویسی می کنیم:
توضیحات هر خط در کد بعد از // اومده . اگه یکم زبانتون خوب کنید متوجه میشید.
حالا با کد زیر برنامه رو تکمیل می کنیم و از تابعی که در بالا نوشتیم استفاده می کنیم:
ذخیره اسکرین شات در فایل BMP:
این کد بسیار کمیاب هست و بهترین کدی بود که براتون قرار میدیم:
نمایش فایل bmp:
ابتدا تابع LoadBitmapFile رو کدنویسی می کنیم:
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader) { FILE *filePtr; // the file pointer BITMAPFILEHEADER bitmapFileHeader; // bitmap file header unsigned char *bitmapImage; // bitmap image data int imageIdx = 0; // image index counter unsigned char tempRGB; // swap variable // open filename in "read binary" mode filePtr = fopen(filename, "rb"); if (filePtr == NULL) return NULL; // read the bitmap file header fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr); // verify that this is a bitmap by checking for the universal bitmap id if (bitmapFileHeader.bfType != BITMAP_ID) { fclose(filePtr); return NULL; } // read the bitmap information header fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr); // move file pointer to beginning of bitmap data fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET); // allocate enough memory for the bitmap image data bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage); // verify memory allocation if (!bitmapImage) { free(bitmapImage); fclose(filePtr); return NULL; } // read in the bitmap image data fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr); // make sure bitmap image data was read if (bitmapImage == NULL) { fclose(filePtr); return NULL; } // swap the R and B values to get RGB since the bitmap color format is in BGR for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3) { tempRGB = bitmapImage[imageIdx]; bitmapImage[imageIdx] = bitmapImage[imageIdx + 2]; bitmapImage[imageIdx + 2] = tempRGB; } // close the file and return the bitmap image data fclose(filePtr); return bitmapImage; }fread تابع خواندن از فایل هست. f به معنی file می باشد.
توضیحات هر خط در کد بعد از // اومده . اگه یکم زبانتون خوب کنید متوجه میشید.
حالا با کد زیر برنامه رو تکمیل می کنیم و از تابعی که در بالا نوشتیم استفاده می کنیم:
void Initialize() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // clear background to black glShadeModel(GL_SMOOTH); // use smooth shading glEnable(GL_DEPTH_TEST); // hidden surface removal glEnable(GL_CULL_FACE); // do not calculate inside of polys glFrontFace(GL_CCW); // counterclockwise polygons are out glEnable(GL_TEXTURE_2D); // enable 2D texturing // load our bitmap file bitmapData = LoadBitmapFile("checker.bmp", &bitmapInfoHeader); glGenTextures(1, &texture); // generate texture object glBindTexture(GL_TEXTURE_2D, texture); // bind the texture AN OVERVIEW OF TEXTURE MAPPING 236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // load the texture image glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); }
ذخیره اسکرین شات در فایل BMP:
void SaveScreenshot(int winWidth, int winHeight) { imageData = malloc(winWidth*winHeight*3); // allocate memory for the imageData memset(imageData, 0, winWidth*winHeight*3); // clear imageData memory contents // read the image data from the window glReadPixels(0, 0, winWidth–1, winHeight–1, GL_RGB, GL_UNSIGNED_BYTE, imageData); // write the image data to a file WriteBitmapFile("writeout.bmp", winWidth, winHeight, (unsigned char*)imageData); // free the image data memory free(imageData); }