What is the preferred way to show large images in OpenGL

后端 未结 2 456
时光说笑
时光说笑 2021-02-03 14:00

I\'ve had this problem a couple of times. Let\'s say I want to display a splash-screen or something in an OpenGL context (or DirectX for that matter, it\'s more of a conceptual

相关标签:
2条回答
  • 2021-02-03 14:26

    If this is a single frame splash image with no animations, then there's no harm using glDrawPixels. If performance is crucial and you are required to use a texture, then the correct way to do it is to determine at runtime, the maximum supported texture size, using a proxy texture.

    GLint width = 0;
    while ( 0 == width ) {  /* use a better condition to prevent possible endless loop */
        glTexImage2D(GL_PROXY_TEXTURE_2D, 
                          0,                /* mip map level */
                          GL_RGBA,          /* internal format */
                          desiredWidth,     /* width of image */
                          desiredHeight,    /* height of image */
                          0,                /* texture border */
                          GL_RGBA           /* pixel data format, */ 
                          GL_UNSIGNED_BYTE, /* pixel data type */
                          NULL              /* null pointer because this a proxy texture */
        );
    
        /* the queried width will NOT be 0, if the texture format is supported */
        glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
    
        desiredWidth /= 2; desiredHeight /= 2;
    }
    

    Once you know the maximum texture size supported by the system's OpenGL driver, you have at least two options if your image doesn't fit:

    • Image tiling: use multiple quads after splitting your image into smaller supported chunks. Image tiling for something like a splash screen should not be too tricky. You can use glPixelStorei's GL_PACK_ROW_LENGTH parameter to load sections of a larger image into a texture.
    • Image resizing: resize your image to fit the maximum supported texture size. There's even a GLU helper function to do this for you, gluScaleImage.
    0 讨论(0)
  • 2021-02-03 14:45

    I don't think there is a built-in opengl function, but you might find a library (or write the function yourself) to break the image down into smaller chunks and then print to screen the smaller chunks (128x128 tiles or similar).

    I had problem with this using Slick2D for a Java game. You can check out their "BigImage" class for ideas here.

    0 讨论(0)
提交回复
热议问题