我在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);
}
圖像的大小是什麼?他們都是一樣的大小,方形? –
另外,你是否在某處設置了'GL_TEXTURE_MIN_FILTER',或者生成了mipmap?否則你的紋理將不會被完成。 –
在[mcve]中編輯。 – genpfault