Search code examples
c++openglassemblygraphics64-bit

Error when using Assembly code to render a video in OpenGL


I followed this tutorial from Nehe in order to render a video in OpenGL. I'm trying to build it on x64, but it gives me an error when compiling the code and it points to flipIt(void* buffer) function. Is it not written well or I need to import a library?

void Video::flipIt(void* buffer)
{
    void* b = buffer;
    __asm
    {
        mov ecx, 256 * 256
        mov ebx, b
        label :
        mov al, [ebx + 0]
            mov ah, [ebx + 2]
            mov[ebx + 2], al
            mov[ebx + 0], ah

            add ebx, 3
            dec ecx
            jnz label
    }
}

Solution

  • Microsoft Developer Network (MSDN) has 24 bit bitmaps which are RGB, so on WINDOWS, RGBs data are actually stored backwards (BGRs), but in OpenGL, RGB is just RGB.

    The solution Nehe uses is to write Assembly code which is a bad idea, in my opinion, because Visual C++ does not support inline assembly for x64, so you can't swap the bytes using ASM code. What can you do right now is to modify the texture generation code to use GL_BGR instead of GL_RGB, but be carefull, some OpenGL drivers have problems with GL_BGR.

    So remove the _asm function and change the GL_RGB to GL_BGR in glTexSubImage2D(...) function:

    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 256, GL_BGR, GL_UNSIGNED_BYTE, data);