解释一下每一句的意思
可以知道大概在干什么
void grab(void) {
FILE* pDummyFile;
FILE* pWritingFile;
GLubyte* pPixelData; //像素数据
GLubyte BMP_Header[BMP_Header_Length]; //图片标题
GLint i, j;
GLint PixelDataLength;//像素数据长度
i = WinWidth * 3;
while (i % 4 != 0)
++i;
PixelDataLength = i * WinHeight;
pPixelData = (GLubyte*)malloc(PixelDataLength);//动态内存分配
if (pPixelData == 0)
exit(0);
pDummyFile = fopen("dummy.bmp", "rb");
if (pDummyFile == 0)
exit(0);
pWritingFile = fopen("grab.bmp", "wb");
if (pWritingFile == 0)
exit(0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glReadPixels(0, 0, WinWidth, WinHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, pPixelData);
fread(BMP_Header, sizeof(BMP_Header), 1, pDummyFile);
fwrite(BMP_Header, sizeof(BMP_Header), 1, pWritingFile);
fseek(pWritingFile, 0x0012, SEEK_SET);
i = WinWidth;
j = WinHeight;
fwrite(&i, sizeof(i), 1, pWritingFile);
fwrite(&j, sizeof(j), 1, pWritingFile);
fseek(pWritingFile, 0, SEEK_END);
fwrite(pPixelData, PixelDataLength, 1, pWritingFile);
fclose(pDummyFile); fclose(pWritingFile); free(pPixelData);
}
//power of two 二次幂
int power_of_two(int n)
{
if (n <= 0)
return 0;
return (n & (n - 1)) == 0;
}
//load texture function 加载纹理函数
GLuint load_texture(const char* file_name)
{
GLint width, height, total_bytes;
GLubyte* pixels = 0;
GLint last_texture_ID = 0;
GLuint texture_ID = 0;
FILE* pFile = fopen(file_name, "rb");
if (pFile == 0)
return 0;
fseek(pFile, 0x0012, SEEK_SET);
fread(&width, 4, 1, pFile);
fread(&height, 4, 1, pFile);
fseek(pFile, BMP_Header_Length, SEEK_SET);
{
GLint line_bytes = width * 3;
while (line_bytes % 4 != 0)
++line_bytes;
total_bytes = line_bytes * height;
} //{
pixels = (GLubyte*)malloc(total_bytes);
if (pixels == 0) {
fclose(pFile);
return 0;
} //if
if (fread(pixels, total_bytes, 1, pFile) <= 0) {
free(pixels);
fclose(pFile);
return 0;
} //if
{
GLint max;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
if (!power_of_two(width) || !power_of_two(height) || width > max || height > max) {
const GLint new_width = 256;
const GLint new_height = 256;
GLint new_line_bytes, new_total_bytes;
GLubyte* new_pixels = 0;
new_line_bytes = new_width * 3;
while (new_line_bytes % 4 != 0)
++new_line_bytes;
new_total_bytes = new_line_bytes * new_height;
new_pixels = (GLubyte*)malloc(new_total_bytes);
if (new_pixels == 0) {
free(pixels);
fclose(pFile);
return 0;
}//if
gluScaleImage(GL_RGB, width, height, GL_UNSIGNED_BYTE, pixels, new_width, new_height, GL_UNSIGNED_BYTE, new_pixels);
free(pixels);
pixels = new_pixels;
width = new_width;
height = new_height;
}//if
}//{
glGenTextures(1, &texture_ID);
if (texture_ID == 0) {
free(pixels);
fclose(pFile);
return 0;
} //if
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture_ID);
glBindTexture(GL_TEXTURE_2D, texture_ID);
//设置放大和 缩小时所采用的过滤方式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//使超出的部分都取纹理边界的值
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixels);
glBindTexture(GL_TEXTURE_2D, last_texture_ID);
free(pixels);
return texture_ID;
}