如何在 C++ 中将位图绘制为 OpenGL 纹理?

发布于 2024-12-02 18:01:42 字数 92 浏览 2 评论 0原文

我有一个位图及其句柄(Win32 HBITMAP)。关于如何在 OpenGL 四边形上绘制此位图(缩放并​​拉动位图的 4 个角以适合四边形的 4 个顶点)有什么建议吗?

I have a bitmap, and its handle (Win32 HBITMAP). Any suggestion of how to draw this bitmap on an OpenGL quad (with scaling and pulling the 4 corners of the bitmap to fit the 4 vertexes of the quad)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

|煩躁 2024-12-09 18:01:42

您需要检索 HBITMAP 中包含的数据,请参阅 http://msdn.microsoft.com/en-us/library/dd144879(v=vs.85).aspx 然后您可以使用以下命令将 DIB 数据上传到 OpenGL glTexImage2DglTexSubImage2D

创建纹理后,您可以像往常一样应用它(启用纹理,为四边形的每个角指定一个纹理坐标)。

由于评论而编辑

这段(未经测试!)代码应该可以解决问题

GLuint load_bitmap_to_texture(
    HDC device_context, 
    HBITMAP bitmap_handle, 
    bool flip_image) /* untested */
{
    const int BytesPerPixel = sizeof(DWORD);

    SIZE bitmap_size;
    if( !GetBitmapDimensionEx(bitmap_handle, &bitmap_size) )
        return 0;

    ssize_t bitmap_buffer_size = bitmap_size.cx * bitmap_size.cy * BytesPerPixel;

#ifdef USE_DWORD
    DWORD *bitmap_buffer;
#else
    void *bitmap_buffer;
#endif
    bitmap_buffer = malloc(bitmap_buffer_size);

    if( !bitmap_buffer )
        return 0;


    BITMAPINFO bitmap_info;
    memset(&bitmap_info, 0, sizeof(bitmap_info));
    bitmap_info.bmiHeader.biSize = sizeof(bitmap_info.bmiHeader);
    bitmap_info.bmiHeader.biWidth  = bitmap_size.cx;
    bitmap_info.bmiHeader.biHeight = bitmap_size.cy;
    bitmap_info.bmiHeader.biPlanes = 1;
    bitmap_info.bmiHeader.biBitCount = BitsPerPixel;
    bitmap_info.bmiHeader.biCompression = BI_RGB;

    if( flip_image ) /* this tells Windows where to set the origin (top or bottom) */
        bitmap_info.bmiHeader.biHeight *= -1;

    if( !GetDIBits(device_context, 
                   bitmap_handle, 
                   0, bitmap_size.cy, 
                   bitmap_buffer, 
                   &bitmap_info,
                   DIB_RGB_COLORS /* irrelevant, but GetDIBits expects a valid value */ )
     ) {
        free(bitmap_buffer);
        return 0;
    }

    GLuint texture_name;
    glGenTextures(1, &texture_name);
    glBindTexture(GL_TEXTURE_2D, texture_name);

    glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
    glPixelStorei(GL_UNPACK_LSB_FIRST,  GL_TRUE);
    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
    glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                 bitmap_size.cx, bitmap_size.cy, 0,
                 GL_RGBA,
#ifdef USE_DWORD
                 GL_UNSIGNED_INT_8_8_8_8,
#else
                 GL_UNSIGNED_BYTE, 
#endif
                 bitmap_buffer);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    free(bitmap_buffer);

    return texture_name;
}

You need to retrieve the data contained in the HBITMAP, see http://msdn.microsoft.com/en-us/library/dd144879(v=vs.85).aspx Then you can upload the DIB data to OpenGL using glTexImage2D or glTexSubImage2D

With a texture being created you can apply this like usual (enable texturing, give each corner of the quad a texture coordinate).

EDIT due to comment

This (untested!) code should do the trick

GLuint load_bitmap_to_texture(
    HDC device_context, 
    HBITMAP bitmap_handle, 
    bool flip_image) /* untested */
{
    const int BytesPerPixel = sizeof(DWORD);

    SIZE bitmap_size;
    if( !GetBitmapDimensionEx(bitmap_handle, &bitmap_size) )
        return 0;

    ssize_t bitmap_buffer_size = bitmap_size.cx * bitmap_size.cy * BytesPerPixel;

#ifdef USE_DWORD
    DWORD *bitmap_buffer;
#else
    void *bitmap_buffer;
#endif
    bitmap_buffer = malloc(bitmap_buffer_size);

    if( !bitmap_buffer )
        return 0;


    BITMAPINFO bitmap_info;
    memset(&bitmap_info, 0, sizeof(bitmap_info));
    bitmap_info.bmiHeader.biSize = sizeof(bitmap_info.bmiHeader);
    bitmap_info.bmiHeader.biWidth  = bitmap_size.cx;
    bitmap_info.bmiHeader.biHeight = bitmap_size.cy;
    bitmap_info.bmiHeader.biPlanes = 1;
    bitmap_info.bmiHeader.biBitCount = BitsPerPixel;
    bitmap_info.bmiHeader.biCompression = BI_RGB;

    if( flip_image ) /* this tells Windows where to set the origin (top or bottom) */
        bitmap_info.bmiHeader.biHeight *= -1;

    if( !GetDIBits(device_context, 
                   bitmap_handle, 
                   0, bitmap_size.cy, 
                   bitmap_buffer, 
                   &bitmap_info,
                   DIB_RGB_COLORS /* irrelevant, but GetDIBits expects a valid value */ )
     ) {
        free(bitmap_buffer);
        return 0;
    }

    GLuint texture_name;
    glGenTextures(1, &texture_name);
    glBindTexture(GL_TEXTURE_2D, texture_name);

    glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
    glPixelStorei(GL_UNPACK_LSB_FIRST,  GL_TRUE);
    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
    glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                 bitmap_size.cx, bitmap_size.cy, 0,
                 GL_RGBA,
#ifdef USE_DWORD
                 GL_UNSIGNED_INT_8_8_8_8,
#else
                 GL_UNSIGNED_BYTE, 
#endif
                 bitmap_buffer);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    free(bitmap_buffer);

    return texture_name;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文