在OpenGL中渲染具有不同纹理的立方体

时间:2015-04-19 19:48:58

标签: c opengl textures

我使用精灵表然后使用纹理坐标制作三个数组,用于立方体的边,顶部和底部。我的问题是,无论出于何种原因,我的块的每一面都是使用tex_bottom数组中的纹理绘制的。

这是加载纹理的函数:

int load_gl_texture(const char *filename, unsigned char *image, unsigned width, unsigned height) {
    int status = 0;

    while(!lodepng_decode32_file(&image, &width, &height, filename)) {
        GLint texSize;
        glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);

        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA,
            GL_UNSIGNED_BYTE, image);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        status = 1;
        break;
    }
    if (image)
        free(image);

    return status;

}

以下是绘制多维数据集的代码:

    glBegin(GL_QUADS);

    // Front Face
    glTexCoord2fv(tex_sides[0]); glVertex3fv(vertices[1]);  // Bottom Left Of The Texture and Quad
    glTexCoord2fv(tex_sides[1]); glVertex3fv(vertices[0]);  // Bottom Right Of The Texture and Quad
    glTexCoord2fv(tex_sides[2]); glVertex3fv(vertices[4]);  // Top Right Of The Texture and Quad
    glTexCoord2fv(tex_sides[3]); glVertex3fv(vertices[5]);  // Top Left Of The Texture and Quad

    // Back Face
    glTexCoord2fv(tex_sides[1]); glVertex3fv(vertices[2]);  // Bottom Right Of The Texture and Quad
    glTexCoord2fv(tex_sides[2]); glVertex3fv(vertices[6]);  // Top Right Of The Texture and Quad
    glTexCoord2fv(tex_sides[3]); glVertex3fv( vertices[7]);  // Top Left Of The Texture and Quad
    glTexCoord2fv(tex_sides[0]); glVertex3fv( vertices[3]);  // Bottom Left Of The Texture and Quad

    // Top Face
    glTexCoord2fv(tex_top[3]); glVertex3fv(vertices[7]);  // Top Left Of The Texture and Quad
    glTexCoord2fv(tex_top[0]); glVertex3fv(vertices[5]);  // Bottom Left Of The Texture and Quad
    glTexCoord2fv(tex_top[1]); glVertex3fv( vertices[4]);  // Bottom Right Of The Texture and Quad
    glTexCoord2fv(tex_top[2]); glVertex3fv( vertices[6]);  // Top Right Of The Texture and Quad

    // Bottom Face
    glTexCoord2fv(tex_bottom[2]); glVertex3fv(vertices[0]);  // Top Right Of The Texture and Quad
    glTexCoord2fv(tex_bottom[3]); glVertex3fv( vertices[2]);  // Top Left Of The Texture and Quad
    glTexCoord2fv(tex_bottom[0]); glVertex3fv( vertices[3]);  // Bottom Left Of The Texture and Quad
    glTexCoord2fv(tex_bottom[1]); glVertex3fv(vertices[1]);  // Bottom Right Of The Texture and Quad

    // Right face
    glTexCoord2fv(tex_sides[1]); glVertex3fv( vertices[2]);  // Bottom Right Of The Texture and Quad
    glTexCoord2fv(tex_sides[2]); glVertex3fv( vertices[6]);  // Top Right Of The Texture and Quad
    glTexCoord2fv(tex_sides[3]); glVertex3fv( vertices[4]);  // Top Left Of The Texture and Quad
    glTexCoord2fv(tex_sides[0]); glVertex3fv( vertices[0]);  // Bottom Left Of The Texture and Quad

    // Left Face
    glTexCoord2fv(tex_sides[0]); glVertex3fv(vertices[3]);  // Bottom Left Of The Texture and Quad
    glTexCoord2fv(tex_sides[1]); glVertex3fv(vertices[1]);  // Bottom Right Of The Texture and Quad
    glTexCoord2fv(tex_sides[2]); glVertex3fv(vertices[5]);  // Top Right Of The Texture and Quad
    glTexCoord2fv(tex_sides[3]); glVertex3fv(vertices[7]);  // Top Left Of The Texture and Quad

    glEnd();

导致此问题的原因是什么?如何解决?

1 个答案:

答案 0 :(得分:0)

我弄明白了这个问题。我将将块id转换为纹理坐标的函数存在缺陷,因为它返回了一个在函数内部使用的内存地址,而不是返回实际值。一旦没有保存相应存储器地址的变量,这导致值被垃圾收集。为了解决这个问题,我做了以下更改:

旧:

float* block_id_to_coordinates(int id) {
    GLfloat tex = {(id % 16)/16.0, (id / 16)/16.0};
    return tex;
} 

新:

void block_id_to_coordinates(int id, GLfloat tex[]) {
    tex[0] = (id % 16)/16.0;
    tex[1] = (id / 16)/16.0;
} 

新函数使用draw_block函数中声明的变量,以便它们可以在整个绘图过程中使用。所以像这样:

float type_top[2];
block_id_to_coordinates(top, type_top); // Initialize array using the "constructor" function

float type_sides[2];
block_id_to_coordinates(sides, type_sides);

float type_bottom[2];
block_id_to_coordinates(bottom, type_bottom);