Pergunta

Estou tentando mapear texturas no meu jogo, mas a imagem não é mapeada com precisão. Eu usei uma imagem de formato .bmp

Aqui está o meu código:

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

Foi útil?

Solução

Sempre existe uma seção "cabeçalho" de um arquivo BMP com 14 bytes de tamanho, por isso é melhor começar a ler a partir do 15º byte em diante. Eu tive exatamente o mesmo problema que você há muitas luas.

Então ... use uma chamada de função como esta antes do seu fread:

 fseek ( file, 15, SEEK_SET);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top