【问题标题】:texture mapping position not precise纹理映射位置不精确
【发布时间】:2013-11-20 03:13:52
【问题描述】:

我正在尝试将纹理映射到我的游戏中,但图像未精确映射。我用的是.bmp格式的图片

这是我的代码:

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; 
}

【问题讨论】:

    标签: c opengl glut texture-mapping


    【解决方案1】:

    bmp 文件的“标题”部分始终为 14 个字节,因此最好从第 15 个字节开始读取。很多个月前我和你有同样的问题。

    所以...在fread 之前使用这样的函数调用:

     fseek ( file, 15, SEEK_SET);
    

    【讨论】:

    • 标题不应被忽略。宽度和高度必须是文件中的红色,而不是通过调用方猜测。 +1 为“月亮”寿 :-D
    • 不应该完全忽略它,但是为了存储图像数据,是的!现在感觉不像是“月亮”,更像是“千年”
    猜你喜欢
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2011-05-04
    相关资源
    最近更新 更多