2011-08-06 174 views
3

這裏是我嘗試加載質地:奇的OpenGL紋理問題?:

enter image description here

這是當我運行我的代碼我所看到的:

enter image description here

這裏是我正在使用的代碼:

#include "sdl.h" 
#include "sdl_opengl.h" 
#include <stdio.h> 
#include <gl/GL.h> 
#include <gl/GLU.h> 

Uint32 loadTexture(char* fileName) 
{ 
    Uint32 id; 
    SDL_Surface *img = NULL; 

    //load into memory using SDL 
    img = SDL_LoadBMP(fileName); 
    //generate an id for this texture 
    glGenTextures(1, &id); 
    //use this texture 
    glBindTexture(GL_TEXTURE_2D, id); 
    //load the texture into video memory via OpenGL 
    glTexImage2D(
     GL_TEXTURE_2D, 
     0, 
     GL_RGB, 
     img->w, 
     img->h, 
     0, 
     GL_RGB, 
     GL_UNSIGNED_SHORT_5_6_5, 
     img->pixels 
     ); 

    //set mip map settings 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

    SDL_FreeSurface(img); 

    return id; 
} 

Uint32 tex; 

void init() 
{ 
    glClearColor(0.0, 0.0, 0.0, 1.0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(0.0, 1.0, 1.0, 0.0, -1.0, 1.0); 
    glMatrixMode(GL_MODELVIEW); 
    glEnable(GL_TEXTURE_2D); 

    tex = loadTexture("fireball.bmp"); 
} 
void display() 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glLoadIdentity(); 

    glBindTexture(GL_TEXTURE_2D, tex); 

    glBegin(GL_QUADS); 
    glTexCoord2f(0.0, 0.0); 
    glVertex3f(0.25, 0.25, 0.0); 


    glTexCoord2f(1.0, 0.0); 
    glVertex3f(0.5, 0.25, 0.0); 


    glTexCoord2f(1.0, 1.0); 
    glVertex3f(0.5, 0.5, 0.0); 


    glTexCoord2f(0.0, 1.0); 
    glVertex3f(0.25, 0.5, 0.0); 
    glEnd(); 
} 

int main() 
{ 
    INT32 isRunning = 1; 
    SDL_Surface *screen = NULL; 
    SDL_Event event; 
    INT32 start; 
    INT32 FPS = 30; 

    SDL_Init(SDL_INIT_EVERYTHING); 

    screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL); 

    init(); 

    while(isRunning) 
    { 
     start = SDL_GetTicks(); 

     while(SDL_PollEvent(&event)) 
     { 
      switch(event.type) 
      { 
       case SDL_QUIT: isRunning = 0; break; 
      } 
     } 

     display(); 

     SDL_GL_SwapBuffers(); 

     if(1000/FPS > SDL_GetTicks() - start) 
     { 
      SDL_Delay(1000/FPS - (SDL_GetTicks() - start)); 
     } 
    } 

    SDL_Quit(); 

    return(0); 
} 

回答

4
glTexImage2D(
    GL_TEXTURE_2D, 
    0, 
    GL_RGB, 
    img->w, 
    img->h, 
    0, 
    GL_RGB, 
    GL_UNSIGNED_SHORT_5_6_5, 
    img->pixels 
    ); 

這實際上是它的格式嗎?每2個字節是一個5/6/5 RGB像素。我並不知道Windows BMP文件可以存儲5/6/5圖像數據,但我從來沒有打算寫圖像加載代碼,所以我不確定。我認爲BMP數據只能是8/8/8 BGR格式。

那麼,即使它可以是5/6/5,你確定這個圖像是這種格式嗎?

另外,什麼是行對齊?我沒有看到您使用glPixelStorei設置GL_UNPACK_ALIGNMENT,所以您必須期望每行的大小都對齊到4個字節。這是真的?

+0

我其實不確定GL_UNPACK_ALIGNMENT和glPixelStorei是什麼。我現在要去查看OpenGL文檔,但要知道我正在關注視頻教程。這個人正在使用Linux而不是Windows。我也會嘗試改變格式,因爲你說它可能是錯誤的。 –

+1

經過與價值觀發揮各地,這裏是什麼在起作用: \t glTexImage2D( \t \t GL_TEXTURE_2D, \t \t 0, \t \t GL_RGB, \t \t img-> W, \t \t img-> H, \t \t 0, \t \t GL_BGR, \t \t GL_UNSIGNED_BYTE, \t \t img->像素 \t \t); –