2012-05-01 51 views
1

我試着使用教程提供了Linux/SDL版本編譯訥河(http://nehe.gamedev.net/tutorial/texture_mapping/12038/)lesson06使用MinGW爲Windows。MinGW的OpenGL的SDL只顯示白色紋理

立方體繪製純白色,而不是質感。 (Win7上測試,還用酒我的開發機器上)

我可以證實,紋理的SDL正確,但顯然沒有加載到OpenGL的。

我也試過生成隨機噪聲紋理(不使用SDL_image或SDL_loadBMP的),但Windows仍然建不顯示任何紋理。

我使用的代碼塊在Ubuntu下,通過本教程http://wiki.codeblocks.org/index.php?title=Code::Blocks_and_Cross_Compilers去建立我的交叉編譯器。

我相信它與我編譯的方式有關,但無法找出錯誤的來源。

+0

更新您的GFX卡驅動程序。 –

+0

沒有幫助。另外:在葡萄酒上運行應該使用與正確渲染的本地linux客戶端相同的驅動程序。 – Rock

+0

你確定圖片加載正確嗎?你檢查'textureImage-> w'是否正確?如果是,請發佈相關的渲染代碼。 –

回答

0

glEnable(GL_TEXTURE_2D)。紋理默認是禁用的。


#include <SDL/SDL.h> 
#include <GL/glew.h> 
#include <GL/gl.h> 
#include <GL/glu.h> 
#include <wchar.h> 
#include <math.h> 
#include <SDL/SDL_image.h> 

#undef main 

static int scrWidth = 800; 
static int scrHeight = 600; 

int main(int argc, char** argv){ 
    SDL_Init(SDL_INIT_VIDEO); 

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); 
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); 
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); 
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); 
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 
    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0); 

    if (SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_OPENGL) == 0){ 
     fprintf(stderr, "couldn't set mode %dx%d!\n", 640, 480); 
     SDL_Quit(); 
     return -1; 
    } 
    glewInit(); 
    IMG_Init(IMG_INIT_PNG|IMG_INIT_JPG); 

    SDL_ShowCursor(SDL_DISABLE); 

    glClearColor(0.2f, 0.2f, 0.2f, 0.2f); 
    glClearDepth(1.0f); 

    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_TEXTURE_2D); 

    GLuint texId = 0; 
    glGenTextures(1, &texId); 

    glBindTexture(GL_TEXTURE_2D, texId); 


    SDL_Surface* tex = IMG_Load("1.png"); 

    int texW = tex->w, texH = tex->h; 
    SDL_LockSurface(tex); 

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 

    int bytesPerPixel = tex->format->BytesPerPixel; 

    GLenum texFormat = GL_INVALID_ENUM, pixelFormat = GL_INVALID_ENUM; 

    switch(bytesPerPixel){ 
     case(3): 
      texFormat = GL_RGB; 
      pixelFormat = texFormat; 
      break; 
     case(4): 
      texFormat = GL_RGBA; 
      pixelFormat = texFormat; 
      break; 
     default: 
      fprintf(stderr, "invalid texture format: %d bytes per pixel", bytesPerPixel); 
      SDL_FreeSurface(tex); 
      SDL_Quit(); 
      return -1; 
      break; 
    } 

    int expectedPitch = bytesPerPixel*tex->w; 
    if (tex->pitch != expectedPitch){ 
     fprintf(stderr, "invalid surface pitch %d instead of %d", tex->pitch, expectedPitch); 
     SDL_FreeSurface(tex); 
     SDL_Quit(); 
     return -1; 
    } 

    glTexImage2D(GL_TEXTURE_2D, 0, texFormat, texW, texH, 0, pixelFormat, GL_UNSIGNED_BYTE, tex->pixels); 
    GLenum err = glGetError(); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

    SDL_UnlockSurface(tex); 

    SDL_FreeSurface(tex); 


    glBindTexture(GL_TEXTURE_2D, texId); 

    bool running = true; 
    while (running){ 
     SDL_Event event; 
     if (SDL_PollEvent(&event)){ 
      switch(event.type){ 
       case SDL_QUIT: 
        running = false; 
        break; 
      }; 
     } 

     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     gluOrtho2D(0, scrWidth, scrHeight, 0); 

     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 

     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 

     glBindTexture(GL_TEXTURE_2D, texId); 

     glBegin(GL_TRIANGLE_FAN); 
     glTexCoord2f(0, 0); 
     glVertex2f(0, 0); 
     glTexCoord2f(1, 0); 
     glVertex2f((float)texW, 0); 
     glTexCoord2f(1, 1); 
     glVertex2f((float)texW, (float)texH); 
     glTexCoord2f(0, 1); 
     glVertex2f(0, (float)texH); 
     glEnd(); 

     glDisable(GL_DEPTH_TEST); 

     glFlush(); 
     SDL_GL_SwapBuffers();  
    } 

    glDeleteTextures(1, &texId); 

    IMG_Quit(); 
    SDL_Quit(); 
    return 0; 
} 
+0

來自完全相同的源代碼的Linux構建工作。我啓用了GL_TEXTURE_2D。 – Rock

+1

@Rock:「Linux構建」這與你的問題無關。如果它起作用,這並不意味着它應該起作用。 「我有GL_TEXTURE_2D」你的代碼片段中沒有glEnable。您的代碼也不會生成mipmap。可以使用glGenerateMipmap,gluBuild2DMipmaps或glTexParameter(GL_GENERATE_MIPMAP)來完成。你爲什麼不發佈完整的小例子來證明你的問題?你當前的代碼片段甚至沒有任何渲染例程。 – SigTerm

+0

我更新了問題以提供更多信息。 – Rock

1

我發現這個問題,google搜索同樣的問題。花了一天的時間找出發生了什麼事情。

問題是調整大小的方法,可能重新創建繪圖區域左右,可能有很多程序使用相同的圖形卡,所以我的猜測是,當這個調整大小的代碼運行時卡釋放紋理。

重新加載紋理的大小調整方法有助於(我的項目)還沒有在訥河lession 6次嘗試,但它會從相同的症狀遭遇。

我把我的紋理原始數據放入緩存並重新加載到openGL中。

我希望這有助於 關心的Kristoffer。