cubeMap转为BMP图片

 

void readCubeMapToHost_forTest(unsigned int cubeMapTexture, int width, int height, GLubyte *dataMap)
{

	GLuint CarrierFBo;
	glGenFramebuffers(1, &CarrierFBo);
	glBindFramebuffer(GL_FRAMEBUFFER, CarrierFBo);
	//cout << "here 1" << endl;
	glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMapTexture);
	for (int i = 0; i < 6; i++)
	{
		//cout << "here 2" << endl;
		//貌似是必须的,不然只能读取POSITIVE_X
		glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, cubeMapTexture, 0);
		//cout << "here 3" << endl;
		glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, &dataMap[i*width*height]);
	}
	//cout << "here 4" << endl;
	glBindTexture(GL_TEXTURE_2D, 0);
	glBindFramebuffer(GL_FRAMEBUFFER, 0);

}
int bmp_write_forTest(unsigned char *image, int xsize, int ysize, char *filename)
{
#define BMP_Header_Length 54
	FILE*    pDummyFile;
	FILE*    pWritingFile;
	GLubyte* pPixelData;
	GLubyte  BMP_Header[BMP_Header_Length];
	GLint    i, j;
	GLint    PixelDataLength;
	i = xsize * 3;
	while (i % 4 != 0)
		++i;
	PixelDataLength = i * ysize;
	unsigned char * grayimage = (unsigned char *)malloc(sizeof(unsigned char)*xsize*ysize * 3);
	/*unsigned char f2c = (unsigned char)0.5f;*/
	for (int i = 0; i<xsize; i++)
		for (int n = 0; n < ysize; n++)
		{
			grayimage[(i*ysize + n) * 3 + 0] = image[i*ysize + n] ;
			grayimage[(i*ysize + n) * 3 + 1] = image[i*ysize + n] ;
			grayimage[(i*ysize + n) * 3 + 2] = image[i*ysize + n] ;
			/*grayimage[(i*ysize + n) * 3 + 0] = f2c;
			grayimage[(i*ysize + n) * 3 + 1] = f2c;
			grayimage[(i*ysize + n) * 3 + 2] = f2c;*/
		}
	pDummyFile = fopen("dog.bmp", "rb");
	if (pDummyFile == 0)
	{
		cout << "open file fail: " << endl;
		exit(0);
	}


	pWritingFile = fopen(filename, "wb");
	if (pWritingFile == 0)
	{
		cout << "open file fail: " << endl;
		exit(0);
	}

	// 把dummy.bmp的文件头复制为新文件的文件头
	fread(BMP_Header, sizeof(BMP_Header), 1, pDummyFile);
	fwrite(BMP_Header, sizeof(BMP_Header), 1, pWritingFile);
	fseek(pWritingFile, 0x0012, SEEK_SET);
	i = xsize;
	j = ysize;
	fwrite(&i, sizeof(i), 1, pWritingFile);
	fwrite(&j, sizeof(j), 1, pWritingFile);

	// 写入像素数据
	fseek(pWritingFile, 0, SEEK_END);
	fwrite(grayimage, PixelDataLength, 1, pWritingFile);

	// 释放内存和关闭文件
	fclose(pDummyFile);
	fclose(pWritingFile);
}
void saveTextureBeBmp_forTest(bool& once, const unsigned int width, const unsigned int height, unsigned int depthCubeMap)
{
	GLubyte *tem_depthMapBuffer;
	tem_depthMapBuffer = (GLubyte*)malloc(sizeof(GLubyte*) * 6 * width * height);
	memset(tem_depthMapBuffer, 0.0, sizeof(GLubyte) * 6 * width * height);

	readCubeMapToHost_forTest(depthCubeMap, width, height, tem_depthMapBuffer);
	cout << "yes" << endl;
	char * fileName1 = "testdepth1.bmp";
	bmp_write_forTest(&tem_depthMapBuffer[0 * width * height], width, height, fileName1);
	char * fileName2 = "testdepth2.bmp";
	bmp_write_forTest(&tem_depthMapBuffer[1 * width * height], width, height, fileName2);
	char * fileName3 = "testdepth3.bmp";
	bmp_write_forTest(&tem_depthMapBuffer[2 * width * height], width, height, fileName3);
	char * fileName4 = "testdepth4.bmp";
	bmp_write_forTest(&tem_depthMapBuffer[3 * width * height], width, height, fileName4);
	char * fileName5 = "testdepth5.bmp";
	bmp_write_forTest(&tem_depthMapBuffer[4 * width * height], width, height, fileName5);
	char * fileName6 = "testdepth6.bmp";
	bmp_write_forTest(&tem_depthMapBuffer[5 * width * height], width, height, fileName6);
	cout << "saveTextureBeBmp_forTest:success" << endl;
	delete[]tem_depthMapBuffer;
	once = true;
}

bool once = false;
if (!once)
{
	saveTextureBeBmp_forTest(once, cubeMapSize[0], cubeMapSize[1], this->cubeMapTexture);
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值