如何将OpenGL图像保存到BMP文件? [英] How to save OpenGL images to the BMP file?

查看:55
本文介绍了如何将OpenGL图像保存到BMP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用OpenGL画一幅画,但我不知道怎么把它保存到BMP。你能告诉我怎么做吗?

I draw a picture with OpenGL, but I don't know how to save it to a BMP. Could you tell me how to do?

推荐答案

没有更多信息很难帮助你。如果你不知道该怎么做,请看看这里:



http://www.cecs.csulb.edu/~pnguyen/cecs449/lectures/bitmaps.pdf [ ^ ]



之后你看了pdf,你可能想看看:



将24位位图的像素数据以BMP格式保存到文件中 [ ^ ]
It's hard to help you without more info. If you have no clue of what to do, have a look here:

http://www.cecs.csulb.edu/~pnguyen/cecs449/lectures/bitmaps.pdf[^]

after you read the pdf, you might want to look at:

Save a 24 bit bitmap's pixel data to File in BMP format[^]


几年前我写了下面的代码,从那时起学习并忘记了很多。 :尴尬:



我用它来创建具有不同材质属性的球体位图,然后将其插入到listView中 - 它是一个材质编辑器。



I wrote the following code a couple of years ago, having learned and forgotten plenty since then. :embarrassed:

I used it to create bitmaps of spheres with different material properties, which would then be inserted into a listView - it was a materials editor.

HBITMAP makeBmp(material_t materialToUse)
{
    HDC memDC, screenDC;
    HBITMAP result, oldBmp;
    BITMAPINFO bitmapInfo;
    char *bitmapBits;
    RECT myRect;
    HBRUSH myBrush;

    bitmapInfo.bmiHeader.biBitCount = 24;
    bitmapInfo.bmiHeader.biWidth = tileSize;
    bitmapInfo.bmiHeader.biHeight = tileSize;
    bitmapInfo.bmiHeader.biCompression = BI_RGB;
    bitmapInfo.bmiHeader.biPlanes = 1;
    bitmapInfo.bmiHeader.biSize = sizeof(bitmapInfo.bmiHeader);
    bitmapInfo.bmiHeader.biClrImportant = 0;
    bitmapInfo.bmiHeader.biClrUsed = 0;

    screenDC = GetDC(NULL);
    memDC = CreateCompatibleDC(screenDC);

    result = CreateDIBSection(memDC, &bitmapInfo, DIB_RGB_COLORS, (void**)&bitmapBits, 0, 0);

    oldBmp = (HBITMAP)SelectObject(memDC, result);
    myRect.left=0;
    myRect.top=0;
    myRect.right=bitmapInfo.bmiHeader.biWidth;
    myRect.bottom=bitmapInfo.bmiHeader.biHeight;
    //myBrush = CreateSolidBrush(RGB(r,g,b));
    //FillRect(memDC, &myRect, myBrush);
    //DeleteObject(myBrush);

    HGLRC tmpRC;
    tmpRC = EnableOpenGL(memDC);
        glViewport(0, 0, bitmapInfo.bmiHeader.biWidth, bitmapInfo.bmiHeader.biHeight);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        float aspectRatio = (float)bitmapInfo.bmiHeader.biWidth / bitmapInfo.bmiHeader.biHeight;
//        glFrustum(-aspectRatio, -aspectRatio, -1.0, 1.0, 2.0,100.0);
        gldPerspective (40, -aspectRatio, 1, 10.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glEnable(GL_LIGHT0);
        glEnable(GL_NORMALIZE);

        glEnable(GL_LIGHTING);

        const GLfloat light_ambient[]  = { 0.3f, 0.3f, 0.3f, 1.0f };
        const GLfloat light_diffuse[]  = { 0.5f, 0.5f, 0.5f, 1.0f };
        const GLfloat light_specular[] = { 0.8f, 0.8f, 0.8f, 1.0f };
        const GLfloat light_position[] = { -3.0f, -4.0f, 5.0f, 0.0f };
        glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);

        const GLfloat mat_ambient[]    = { 0.5f, 0.5f, 0.5f, 1.0f };
        const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
        const GLfloat mat_specular[]   = { 0.9f, 0.9f, 0.9f, 1.0f };
        const GLfloat high_shininess[] = { materialToUse.shininess};
        glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
        glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
        glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

        glEnable(GL_COLOR_MATERIAL);
        glClearColor(1,1,1,1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        GLfloat rr,gg,bb;
        rr=(int)materialToUse.r/255.0;
        gg=(int)materialToUse.g/255.0;
        bb=(int)materialToUse.b/255.0;

        glTranslated(0,0,-3);
        glRotated(-90,1,0,0);

        glColor3d(rr,gg,bb);

        myGlutBall(1, 32,32);

        SwapBuffers(memDC);

    wglMakeCurrent( NULL, NULL );
    wglDeleteContext( tmpRC );

    SelectObject(memDC, oldBmp);
    DeleteDC(memDC);

    return result;
}







基本上,我认为你可以剔除EnableOpenGL和SwapBuffer之间的所有代码,用你拥有的代码替换它。注意:此代码不使用除原始openGL之外的任何内容 - 不使用过剩或glaux或任何其他框架/库。我不知道你需要修改/写什么才能实现同样的目标。



如果你把这个代码与一些将HBITMAP编写成一个.BMP文件,你应该拥有你需要的东西。



您还应该继续改进并磨练您的解决问题的能力。也就是说,在解决每个问题之前,将问题分解为可管理的组件 - 或者通过google&论坛,或只是搜索您可以复制和粘贴的代码。我通常会忽略一个类似于你的请求 - 但是,我心情很好,你问我们是否可以提供帮助(而不是像许多人那样告诉我们提供帮助),而且,你没有说这很紧急 。



我辩论是否回答,以免鼓励他人提出问题而不先试图解决问题。我想,你会希望看到我自己解决问题的建议,就像他们一样。此外,一般来说,我想,想要学习的人应该得到奖励!




Basically, I think you can cull all code between EnableOpenGL and SwapBuffer, replacing it with the code you have. Note: that this code does not make use of anything other than 'raw' openGL - no use of glut or glaux or any other frameworks/libraries. I've no idea what you would need to modify/write to achieve the same with one.

If you combine this code with some that will write a HBITMAP to a .BMP file, you should have what you need.

You should also continue to improve and hone your problem-solving skills. Namely, break-down a problem into manageable components, before solving each of them - either by yourself with some help from google & forums, or by simply searching for code you can copy and paste. I would generally ignore a request similar to yours - however, I'm in a good mood, you asked if we could help (rather than telling us to help, as many do), also, you didn't say "it's urgent".

I debated whether or not to answer, lest it encourage others to ask questions without trying to solve the problem first. I figured, you'll hopefully read my advice regarding solving a problem on your own, as would they. Besides, generally speaking, someone that wants to learn should be rewarded, I think!


这篇关于如何将OpenGL图像保存到BMP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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