OpenGL ES 学习教程(十五) 从 颜色缓冲区(FBO的颜色附着)中 读取颜色数据 保存到图片(FreeImage)

游戏中需要截屏功能,GL中也有提供读取像素数据的API

glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels);

传入相关参数,然后传入像素数据指针,就可以从绑定的颜色缓冲区中读取出来像素数据,然后通过FreeImage或者其他方式,存储为图片文件。


在上一篇的基础上,从FBO的颜色附着上,即纹理,即颜色缓冲区中读取像素数据。

转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

在第一次绘制之后,添加代码

virtual void render()
{
	if (m_width == 0 || m_height == 0)
	{
		return;
	}

	glBindRenderbuffer(GL_RENDERBUFFER, m_depthRenderBuffer);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthRenderBuffer);

	glBindFramebuffer(GL_FRAMEBUFFER, m_frameBuffer);
	//用作纹理的颜色缓冲区,glReadPixels从这个颜色缓冲区中读取
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture, 0);

	GLenum tmpStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (tmpStatus == GL_FRAMEBUFFER_COMPLETE)
	{
		glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

		glViewport(0, 0, m_width, m_height);
		{
			//model;
			glm::mat4 model = glm::mat4(1.0f);

			//View
			glm::mat4 view = glm::lookAt(glm::vec3(0, 0, 10), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));

			//透视
			glm::mat4 proj = glm::perspective(glm::radians(60.0f), 1.0f, 0.3f, 1000.0f);

			proj = proj*view*model;

			m_program.begin();
			{
				glm::vec3 pos[] =
				{
					glm::vec3(-5.0f, -5.0f, 0.0f),
					glm::vec3(5.0f, -5.0f, 0.0f),
					glm::vec3(0.0f, 5.0f, 0.0f),
				};


				glm::vec4 color[] =
				{
					glm::vec4(colorX, colorY, colorZ, 1),
					glm::vec4(colorZ, colorX, colorY, 1),
					glm::vec4(colorY, colorZ, colorX, 1),
				};

				colorX += 0.001f;
				if (colorX > 1) colorX = 0;
				colorY += 0.002f;
				if (colorY > 1) colorY = 0;
				colorZ += 0.003f;
				if (colorZ > 1) colorZ = 0;

				glUniformMatrix4fv(m_program.m_mvp, 1, false, &proj[0][0]);

				glVertexAttribPointer(m_program.m_position, 2, GL_FLOAT, false, sizeof(glm::vec3), pos);
				glVertexAttribPointer(m_program.m_color, 4, GL_FLOAT, false, sizeof(glm::vec4), color);

				glDrawArrays(GL_TRIANGLES, 0, 3);
			}
			m_program.end();
		}

		int tmpPixelSize = m_textureWidth*m_textureHeight * 4;
		char* tmpPixelsBuffer = (char*)malloc(tmpPixelSize);

		//从颜色缓冲区中读取数据
		glReadPixels(0, 0, m_textureWidth, m_textureHeight,GL_RGBA, GL_UNSIGNED_BYTE, tmpPixelsBuffer);
		for (size_t i = 0; i < tmpPixelSize; i+=4)
		{
			tmpPixelsBuffer[i] ^= tmpPixelsBuffer[i + 2] ^= tmpPixelsBuffer[i] ^= tmpPixelsBuffer[i + 2];
		}
		SavePng(tmpPixelsBuffer);

		free(tmpPixelsBuffer);
		

		glBindRenderbuffer(GL_RENDERBUFFER, GL_NONE);
		glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE);

		

		glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
		glBindTexture(GL_TEXTURE_2D, m_texture);
		
		......
		
		......
	}
}

读取出来数据后,就可以通过FreeImage保存为图片文件

void SavePng(char* varPixelBuffer)
{
	FIBITMAP* tmpFIBitMap = FreeImage_Allocate(m_textureWidth, m_textureHeight, 32, 8, 8, 8);
	for (size_t y = 0; y < FreeImage_GetHeight(tmpFIBitMap); y++)
	{
		BYTE* tmpBIT = FreeImage_GetScanLine(tmpFIBitMap, y);

		for (size_t x = 0; x < FreeImage_GetWidth(tmpFIBitMap); x++)
		{
			tmpBIT[0] = varPixelBuffer[(y*m_textureWidth + x) * 4 + 0];
			tmpBIT[1] = varPixelBuffer[(y*m_textureWidth + x) * 4 + 1];
			tmpBIT[2] = varPixelBuffer[(y*m_textureWidth + x) * 4 + 2];
			tmpBIT[3] = 255;
			tmpBIT += 4;
		}
	}

	bool bSuccess = FreeImage_Save(FIF_PNG, tmpFIBitMap, "Screen.png", PNG_DEFAULT);

	FreeImage_Unload(tmpFIBitMap);
}
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

保存后的效果



示例工程下载:

http://pan.baidu.com/s/1gfMD2fT 提取码a7q3


阅读更多
版权声明:本文转自http://blog.csdn.net/huutu 转载请带上 http://www.liveslives.com/ https://blog.csdn.net/cp790621656/article/details/53497479
个人分类: OpenGL ES 学习教程
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭