Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Comments
Paulo Augusto Kunzel 23-Oct-13 5:40am    
Have you done something already? What did you do? Can you show an example of what are you trying?

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[^]
 
Share this answer
 
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.

C++
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;
}



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!
 
Share this answer
 
Comments
haiwuya 23-Oct-13 7:34am    
Thank you!
enhzflep 23-Oct-13 7:44am    
You're welcome. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900