2016-07-17 74 views
-1

我在OpenGL中的立方體貼圖存在問題。當我嘗試畫一個立方體地圖,它顯示黑色:(OpenGL)CubeMap未繪製

(我不能發表圖片結果表明與角度畸變黑色立方體)

着色器是可以的,因爲立方體繪製透視畸變,當我用這個片段着色器,很顯然,着色器加載: (我不能發表圖片中顯示了降級立方體從黑到紅)

#version 330 core 

in vec3 direction; 
out vec4 outcolor; 

uniform samplerCube sampler; 

void main() { 
    outcolor = vec4(direction.x, 0.0, 0.0, 1.0); 
} 

立方體地圖VAO是確定顯然(立方體顯示)。

也許錯誤是在紋理立方體貼圖加載。下面是代碼:

GLboolean load_bmp_data(LPCSTR path, char **data, GLuint *datapos, GLuint *width, GLuint *height, GLuint *bitsperpixel) { 
    GLuint size = 0; 

    load_raw_file(path, data, &size); // It was correctly writen (100% ok) 
    if(NULL == *data) { 
     fprintf(stdout, "Error loading BMP image data from file '%s'.\n", path); 
     return GL_FALSE; 
    } 

    if('B' != (*data)[0] || 'M' != (*data)[1]) { 
     fprintf(stdout, "This image loader can only load BMP images.\n"); 
     free_raw_file(*data); 
     return GL_FALSE; 
    } 

    *datapos  = *((GLuint *)(&(*data)[0x0A])); 
    *width   = *((GLuint *)(&(*data)[0x12])); 
    *height  = *((GLuint *)(&(*data)[0x16])); 
    *bitsperpixel = *((GLuint *)(&(*data)[0x1C])) & 0x0000FFFF; 

    if(24 != *bitsperpixel) { 
     fprintf(stdout, "This BMP image loader only supports loading 24 bits images.\n"); 
     free_raw_file(*data); 
     return GL_FALSE; 
    } 

    return GL_TRUE; 
} 

void free_bmp_data(char *data) { 
    free_raw_file(data); 
} 

GLboolean load_cube_map(cubemap_t *cubemap, LPCSTR dir, LPCSTR vertpath, LPCSTR fragpath) { 
    vec3 vertices[36]; 
    GLuint vbo = 0; 
    GLboolean noerror = GL_TRUE; 
    char *path = NULL; 
    char names[6][11] = {"right.bmp", "left.bmp", "top.bmp", "bottom.bmp", "front.bmp", "back.bmp"}; 
    unsigned int size = 0; 
    GLuint i = 0; 

    char *data = NULL; 
    GLuint datapos = 0; 
    GLuint width = 0; 
    GLuint height = 0; 
    GLuint bitsperpixel = 0; 

    load_cube_positions(vertices, 1.0); 

    // Generating buffer 
    // ... Here I create the vertex buffer object and the vertex array object 
    // ... 

    // Loading shaders 
    // Here I load the shaders from files 

    // Loading faces 
    glGenTextures(1, &cubemap->texture); 
    if(0 == cubemap->texture) { 
     fprintf(stdout, "Error creating a texture object for cube map.\n"); 
     return GL_FALSE; 
    } 
    glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap->texture); 

    size = strlen(dir); // dir = "shaders/cubemap/" 
    path = malloc(size + 10); 
    if(NULL == path) { 
     fprintf(stdout, "Error allocating dynamic memory for cube map paths.\n"); 
     return GL_FALSE; 
    } 
    strcpy(path, dir); 

    for(i = 0; i < 6; i++) { 
     strcpy(path + size, names[i]); 
     if(GL_TRUE != load_bmp_data(path, &data, &datapos, &width, &height, &bitsperpixel)) { 
      fprintf(stdout, "Error loading BMP data for cube map at '%s'.\n", path); 
      return GL_FALSE; 
     } // It doesn't fail, so data were loaded correctly 
     glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data+datapos); 
     free_bmp_data(data); 
    } 

    glBindTexture(GL_TEXTURE_CUBE_MAP, 0); 
    return GL_TRUE; 
} 

我認爲可能出現的錯誤是不是在「load_bmp_data」功能,因爲我可以用這個功能加載2D圖像和它完美的作品

繪製的代碼立方圖:

void draw_cube_map(const cubemap_t *cubemap, const mat4 *rotate, const mat4 *projection) { 
    mat4 matrix = *rotate; 
    translate_mat4(&matrix, 0.0f, 0.0f, 2.0f); // Matrices functions are OK (100%) 

    glCullFace(GL_FRONT); 
    glUseProgram(cubemap->shader); 
    glUniformMatrix4fv(cubemap->model_loc, 1, GL_FALSE, (GLfloat *)matrix.elements); 
    glUniformMatrix4fv(cubemap->proj_loc, 1, GL_FALSE, (GLfloat *)projection->elements); 

    glActiveTexture(GL_TEXTURE0); 
    GLuint loc = glGetUniformLocation(cubemap->shader, "sampler"); 
    glUniform1i(loc, 0); 
    glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap->texture); 

    glBindVertexArray(cubemap->vao); 
    glDrawArrays(GL_TRIANGLES, 0, 36); 
    glCullFace(GL_BACK); 
} 
+0

圖像的大小是什麼?他們都是一樣的大小,方形? –

+0

另外,你是否在某處設置了'GL_TEXTURE_MIN_FILTER',或者生成了mipmap?否則你的紋理將不會被完成。 –

+0

在[mcve]中編輯。 – genpfault

回答

0

通常黑色紋理意味着你沒有紋理映射到你的緩衝區或壞UV。

很難說清楚沒有花費詳盡的閱讀,但它看起來像你在這裏混合了兩個不同的東西,這是不兼容的。你可以渲染一個大的立方體,它可以像這樣工作,但是你應該加載一個紋理並映射一個紋理。就像渲染一個普通的多維數據集一樣,除了你是從內部完成的。您還必須確保自己處於立方體內部,因爲您已經面臨撲滅 - 並且您的纏繞順序是正確的。

http://mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=13

本教程介紹瞭如何獲得一個立方體貼圖會迅速和容易。

+0

面部剔除不是問題,因爲該立方體是完美繪製的。當我繪製「uv」(帶有上述片段着色器)時,可以看到繪製的面孔是內部面部,因此它不可能是問題 –

0

我找到了答案(但我不明白):

我必須設置過濾器的方法:

glTerParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
glTerParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

它可以GL_NEAREST或什麼的。謝謝

+0

GL_LINEAR應該也能工作。你只需要創建mipmap,或者設置一個不使用mipmap的過濾器方法。 –

+0

它可以是GL_NEAREST或**任何**。這包括GL_LINEAR:D –

+0

不,不是。 「MIN_FILTER」的默認值是「GL_NEAREST_MIPMAP_LINEAR」,這是它不起作用的原因。如果不創建mipmap,並且紋理被縮小,則任何需要mipmap的值都將不起作用。 –