2012-09-09 30 views
0

我一直在得到分割錯誤每次我加載第四紋理 - 什麼類型的紋理,我的意思是文件名,並不重要。我檢查了GL_TEXTURES_STACK_SIZE的值,結果是所以比更大,不是嗎?第四紋理=分割錯誤

我這裏還有代碼片段:

功能可按從PNG

static GLuint gl_loadTexture(const char filename[]) { 
    static int iTexNum = 1; 
    GLuint texture = 0; 
    img_s *img = NULL; 

    img = img_loadPNG(filename); 
    if (img) { 
     glGenTextures(iTexNum++, &texture); 
     glBindTexture(GL_TEXTURE_2D, texture); 
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
     glTexImage2D(GL_TEXTURE_2D, 0, img->iGlFormat, img->uiWidth, img->uiHeight, 0, img->iGlFormat, GL_UNSIGNED_BYTE, img->p_ubaData); 
     img_free(img); //it may cause errors on windows 
    } else printf("Error: loading texture '%s' failed!\n", filename); 

    return texture; 
} 

實際負荷

static GLuint textures[4]; 

static void gl_init() { 
    (...) //setting up OpenGL 

    /* loading textures */ 
    textures[0] = gl_loadTexture("images/background.png"); 
    textures[1] = gl_loadTexture("images/spaceship.png"); 
    textures[2] = gl_loadTexture("images/asteroid.png"); 
    textures[3] = gl_loadTexture("images/asteroid2.png"); //this is causing SegFault no matter which file I load! 
} 

任何想法加載紋理?

+0

已回答[此處](http://gamedev.stackexchange.com/a/35760/18614)。 – ErikEsTT

+1

「我檢查了」GL_TEXTURES_STACK_SIZE「的值」該枚舉正是它所說的:紋理*堆棧*的大小。它與您可以綁定的紋理數量或您可以創建的數量無關。 –

+0

因爲這是交叉http://gamedev.stackexchange.com/questions/35758/fourth-texture-segmentation-fault並在那裏回答,我正在關閉這個變種。 –

回答

1

在生成紋理的過程中至少有一個問題。您寫道:

static GLuint gl_loadTexture(const char filename[]) { 
    static int iTexNum = 1; 
    GLuint texture = 0; 
    img_s *img = NULL; 

    img = img_loadPNG(filename); 
    if (img) { 
     glGenTextures(iTexNum++, &texture); 

的第一個參數是glGenTextures要生成紋理的數量。您只爲堆棧中的1個紋理分配空間,但每次調用此方法時,都會分配1個紋理。 (因此,在第二次調用你分配2個紋理,在第三個電話,3個紋理等),你最有可能改寫指針img一旦你過去1.調用應該是:

glGenTextures (1, &texture);