gpt4 book ai didi

c - 在 OpenGL 中渲染每面具有不同纹理的立方体

转载 作者:行者123 更新时间:2023-11-30 19:43:19 24 4
gpt4 key购买 nike

我使用 Sprite 表,然后使用立方体侧面、顶部和底部的纹理坐标创建三个数组。我的问题是,无论出于何种原因,我的 block 的每一面都是使用 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();

是什么导致了这个问题以及如何解决它?

最佳答案

我发现了问题所在。我将 block 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);

关于c - 在 OpenGL 中渲染每面具有不同纹理的立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29735013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com