Frage

Ich versuche, Texturen in mein Spiel abzubilden, aber das Bild ist nicht genau zugeordnet. Ich habe ein .BMP -Formatbild verwendet

Hier ist mein Code:

GLuint LoadTexture( const char * filename, int width, int height ){
    GLuint texture;
    unsigned char * data;
    FILE * file;

    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;
    data = (unsigned char *)malloc( width * height * 3 );
    fread( data, width * height * 3, 1, file );
    fclose( file );
  glGenTextures( 1, &texture ); 
    glBindTexture( GL_TEXTURE_2D, texture ); 
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); 


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

    //to the edge of our shape. 
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    free( data ); 
    return texture; 
}

here is the images

War es hilfreich?

Lösung

Es gibt immer einen "Header" -Abschnitt einer BMP -Datei mit einer Größe von 14 Bytes, daher ist es am besten, ab dem 15. Byte zu lesen. Ich hatte genau das gleiche Problem wie vor vielen Monden.

Also ... verwenden Sie einen solchen Funktionsaufruf vor Ihrem fread:

 fseek ( file, 15, SEEK_SET);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top