如何将openGl屏幕外数据写入JPG图像 [英] How to write openGl offscreen data in to JPG image

查看:192
本文介绍了如何将openGl屏幕外数据写入JPG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是openGl的新手,我有一个任务是在openGl Offscreen中渲染一些并将其写入JPG或Bmp图像,我不想要将渲染数据显示到openGl窗口中我只需要写入图像(jpg或BMP)。



我谷歌它但无法找到正确的解决方案。 ..我很喜欢这个任何人请帮我完成这个任务...



提前谢谢。

Hi,

I am new to openGl, i have one task to render some in openGl Offscreen and write the same into JPG or Bmp image here i dont want to show the render data into a openGl window i just need to write into an Image(jpg or BMP).

I google it but unable to find the right solution... i am strucked on this any one please help me on this task...

Thanks in advance.

推荐答案

我希望FrameBuffer中的图像数据可用,然后你可以使用glReadPixels()来读取帧缓冲区的像素(来自像素缓冲区的RGB数据)。



glReadPixels(左,上,宽,高,GL_BGR_EXT,GL_UNSIGNED_BYTE,pbyData); // pbyData应分配3 * Width * Height以从帧缓冲区接收RGB数据。



然后将此缓冲区保存到带有BMP头的文件中。



这是一个示例代码,在此应用程序中,帧缓冲区内容保存为BMP文件。

http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=236394

请参阅BMPLoader :: SaveBMP ()和ZoomInterpolationDlg :: OnBnClickedButtonSave()
I hope image data available in a FrameBuffer, then you can use glReadPixels() to read pixels of the frame buffer( RGB data from the pixel buffer).

glReadPixels( Left, Top, Width, Height, GL_BGR_EXT, GL_UNSIGNED_BYTE, pbyData ); // pbyData should be allocated with 3 * Width * Height to receive RGB data from frame buffer.

Then save this buffer to a file with BMP header.

Here is a sample code, in this application frame buffer contents are saved to a BMP file.
http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=236394
Please refer BMPLoader::SaveBMP() and ZoomInterpolationDlg::OnBnClickedButtonSave()


#include <stdlib.h> 
#include <iostream>
//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include 
#include 
#else
#include 
#endif
#include "bitmap_image.hpp"
using namespace std;


//Draws the 3D scene
void drawScene()
{
	//Clear information from last draw
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective	

	glEnable(GL_DEPTH_TEST);

	static unsigned char texture[3 * 400 * 400];
	static unsigned int texture_id;

	// Texture setting
	glEnable(GL_TEXTURE_2D);
	glGenTextures(1, &texture_id);
	glBindTexture(GL_TEXTURE_2D, texture_id);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 400, 400, 0, GL_RGB,	GL_UNSIGNED_BYTE, texture);
	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_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	glLoadIdentity();
	glTranslatef(0, 0, -10);	

	/* Define a view-port adapted to the texture */
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(20, 1, 5, 15);
	glViewport(0, 0, 400, 400);
	glMatrixMode(GL_MODELVIEW);

	/* Render to buffer */
	glClearColor(1, 1, 1, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	
	glColor3f(1.0,0.0,0.0);

	glBegin(GL_TRIANGLES);
	
	glVertex3f( 0.0f, 1.0f, 0.0f);             

	glVertex3f(-1.0f,-1.0f, 0.0f);             

	glVertex3f( 1.0f,-1.0f, 0.0f);             

	glEnd();  
	///glFlush();
	// Image Writing	
	unsigned char* imageData = (unsigned char *)malloc((int)(400*400*(3)));
	glReadPixels(0, 0, 400, 400, GL_RGB, GL_UNSIGNED_BYTE, imageData);
	//write 
	bitmap_image image(400,400);
	image_drawer draw(image);

	for (unsigned int i = 0; i < image.width(); ++i)
	{
		for (unsigned int j = 0; j < image.height(); ++j)
		{
			image.set_pixel(i,j,*(++imageData),*(++imageData),*(++imageData));		
		}
	}

	image.save_image("Trangle_image.bmp");
	///glutSwapBuffers(); 
}

int main(int argc, char** argv)
{
	//Initialize GLUT
	//glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(400, 400); //Set the window size
	//Create the window
	glutCreateWindow("Test");	
	drawScene();
	return 0; //This line is never reached
}</iostream></stdlib.h>







最后我完成了任务,工作正常。




Finally i achived the task, which is working fine.


你可以使用屏幕副本并用GDI +粘贴到jpg或bmp格式的主体。
you can use copy of screen and paste in to body of jpg or bmp format with GDI+.


这篇关于如何将openGl屏幕外数据写入JPG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆