Domanda

Qualsiasi idea di che cosa è sbagliato con il mio codice? quando eseguo glCompressedTexImage2D () il programma appena si blocca (arriva la cosa Windows XP messaggio incidente ...)

Nel tentativo di caricare un'immagine DDS, senza mipmap, il formato dell'immagine è DDS DXT1

Mi sto perdendo un po 'di file di inclusione, o che cosa ho fatto di sbagliato? Ho scaricato i file inclusi da: http: // sourceforge. net / progetti / Glew / files / Glew / 1.5.1 / glew-1.5.1-win32.zip / scaricare

Ho glew32.dll nella stessa cartella come il mio .exe è.

Il codice sotto ha solo le parti ho cambiato per essere in grado di caricare immagini DDS:

#pragma comment(lib, "glew32.lib")
#include <GL\glew.h>
#include <GL\gl.h>


...


typedef struct {
    GLuint dwSize;
    GLuint dwFlags;
    GLuint dwFourCC;
    GLuint dwRGBBitCount;
    GLuint dwRBitMask;
    GLuint dwGBitMask;
    GLuint dwBBitMask;
    GLuint dwABitMask;
} DDS_PIXELFORMAT;

typedef struct {
    GLuint dwMagic;
    GLuint dwSize;
    GLuint dwFlags;
    GLuint dwHeight;
    GLuint dwWidth;
    GLuint dwLinearSize;
    GLuint dwDepth;
    GLuint dwMipMapCount;
    GLuint dwReserved1[11];
    DDS_PIXELFORMAT ddpf;
    GLuint dwCaps;
    GLuint dwCaps2;
    GLuint dwCaps3;
    GLuint dwCaps4;
    GLuint dwReserved2;
} DDS_HEADER;

DDS_HEADER DDS_headers;


...


FILE *fp = fopen("test.dds", "rb");
fread(&DDS_headers, 1, sizeof(DDS_headers), fp);

img_width = DDS_headers.dwWidth;
img_height = DDS_headers.dwHeight;

maxsize = (img_width*img_height)/2;
unsigned char *imgdata = (unsigned char *)malloc(maxsize);

fread(imgdata, 1, maxsize, fp);

fclose(fp);

GLuint texID;

glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_PALETTE4_R5_G6_B5_OES, img_width, img_height, 0, maxsize, imgdata);
// NOTICE:
// Ive also tried with function:
// glCompressedTexImage2DARB();
// and internalformats: GL_COMPRESSED_RGB_S3TC_DXT1_EXT and all of the possible formats... its not a format error.
È stato utile?

Soluzione

Mi ricordo scherzi con il caricatore DDS in Glew, non credo che le informazioni di intestazione non è corretta. Non ho mai potuto farlo funzionare correttamente.

Il modo migliore sarebbe quella di utilizzare i contstucts intestazione che sono DDraw.h ecco quello che ero in grado di utilizzare per DXT1,3, e 5, che se non ricordo male sono gli unici che possono lavorare in OpenGL.

    struct DDS_IMAGE_DATA
    {
           GLsizei  width;
            GLsizei  height;
            GLint    components;
            GLenum   format;
            int      numMipMaps;
            GLubyte *pixels;
    };

        DDS_IMAGE_DATA* CImage::loadDDSTextureFile( const char *filename )
        {
            DDS_IMAGE_DATA *pDDSImageData;
            DDSURFACEDESC2 ddsd;
            char filecode[4];
            FILE *pFile;
            int factor;
            int bufferSize;

            // Open the file
            pFile = fopen( filename, "rb" );

            if( pFile == NULL )
            {   
                #if DEBUG
                char str[255];
                printf( str, "loadDDSTextureFile couldn't find, or failed to load \"%s\"", filename );
                #endif
                return NULL;
            }

            // Verify the file is a true .dds file
            fread( filecode, 1, 4, pFile );

            if( strncmp( filecode, "DDS ", 4 ) != 0 )
            {
                #if DEBUG
                char str[255];
                printf( str, "The file \"%s\" doesn't appear to be a valid .dds file!", filename );
                #endif
                fclose( pFile );
                return NULL;
            }

            // Get the surface descriptor
            fread( &ddsd, sizeof(ddsd), 1, pFile );

            pDDSImageData = (DDS_IMAGE_DATA*) malloc(sizeof(DDS_IMAGE_DATA));

            memset( pDDSImageData, 0, sizeof(DDS_IMAGE_DATA) );

            //
            // This .dds loader supports the loading of compressed formats DXT1, DXT3 
            // and DXT5.
            //

            switch( ddsd.ddpfPixelFormat.dwFourCC )
            {
            case FOURCC_DXT1:
                // DXT1's compression ratio is 8:1
                pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
                factor = 2;
                break;

            case FOURCC_DXT3:
                // DXT3's compression ratio is 4:1
                pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
                factor = 4;
                break;

            case FOURCC_DXT5:
                // DXT5's compression ratio is 4:1
                pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
                factor = 4;
                break;

            default:

                #if DEBUG
                char str[255];
                printf( str, "The file \"%s\" doesn't appear to be compressed "
                    "using DXT1, DXT3, or DXT5!", filename );
                #endif
                return NULL;
            }

            //
            // How big will the buffer need to be to load all of the pixel data 
            // including mip-maps?
            //

            if( ddsd.dwLinearSize == 0 )
            {
                #if DEBUG
                printf("dwLinearSize is 0!");
                #endif
            }

            if( ddsd.dwMipMapCount > 1 )
                bufferSize = ddsd.dwLinearSize * factor;
            else
                bufferSize = ddsd.dwLinearSize;

            pDDSImageData->pixels = (unsigned char*)malloc(bufferSize * sizeof(unsigned char));

            fread( pDDSImageData->pixels, 1, bufferSize, pFile );

            // Close the file
            fclose( pFile );

            pDDSImageData->width      = ddsd.dwWidth;
            pDDSImageData->height     = ddsd.dwHeight;
            pDDSImageData->numMipMaps = ddsd.dwMipMapCount;

            if( ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT1 )
                pDDSImageData->components = 3;
            else
                pDDSImageData->components = 4;

            return pDDSImageData;
        }

void CImage::loadDDS(const char * szFilename, tTexture & texData)
{
    DDS_IMAGE_DATA *pDDSImageData = loadDDSTextureFile(szFilename);

    if( pDDSImageData != NULL )
    {
        texData.m_nHeight = pDDSImageData->height;
        texData.m_nWidth  = pDDSImageData->width;
        texData.m_nHeight = pDDSImageData->height;
        texData.m_eFormat = pDDSImageData->format;
        int nHeight = pDDSImageData->height;
        int nWidth = pDDSImageData->width;

        int nNumMipMaps = pDDSImageData->numMipMaps;
        int nBlockSize;

        if( pDDSImageData->format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT )
            nBlockSize = 8;
        else
            nBlockSize = 16;

        //glGenTextures( 1, &g_compressedTextureID );
        //glBindTexture( GL_TEXTURE_2D, g_compressedTextureID );

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

        int nSize;
        int nOffset = 0;

        // Load the mip-map levels

        for( int i = 0; i < nNumMipMaps; ++i )
        {
            if( nWidth  == 0 ) nWidth  = 1;
            if( nHeight == 0 ) nHeight = 1;

            nSize = ((nWidth+3)/4) * ((nHeight+3)/4) * nBlockSize;

            glCompressedTexImage2DARB( GL_TEXTURE_2D,
                i,
                pDDSImageData->format,
                nWidth,
                nHeight,
                0,
                nSize,
                pDDSImageData->pixels + nOffset );

            nOffset += nSize;

            // Half the image size for the next mip-map level...
            nWidth  = (nWidth  / 2);
            nHeight = (nHeight / 2);
        }
    }

    if( pDDSImageData != NULL )
    {
        if( pDDSImageData->pixels != NULL )
            free( pDDSImageData->pixels );

        free( pDDSImageData );
    }
}

Questa particolare porzione di codice fa alcune ipotesi del file DDS che stiamo cercando di caricare, prima cioè è un file compresso, sia DXT1,3, o 5, e che il file DDS ha mipmap salvati nel pre-generati essa, quindi non abbiamo per generare noi stessi.

Spero che questo è stato in grado di aiutarvi mi c'è voluto del tempo per farlo funzionare correttamente solo. Se c'è qualcosa in questo frammento di codice che sembra poco chiaro, fatemelo sapere e io ti aiuterò ulteriormente.

Altri suggerimenti

L'argomento formato che si sta passando a glCompressedTexImage2D guarda un po 'strano:

glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_PALETTE4_R5_G6_B5_OES, img_width, img_height, 0, maxsize, imgdata);

Credo che questo non è né una texture tavolozza, né un'estensione di OpenGL ES. Forse qualcosa di simile a GL_COMPRESSED_RGB_S3TC_DXT1_EXT avrebbe funzionato meglio.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top