- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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/
我正在尝试在图像上绘制蒙版,蒙版是 body-pix (在 NodeJS 中)完成的处理的结果。出于性能原因,我想使用 OpenCV 来绘制蒙版,而不是 htmlcanva。 const se
在我的数据框中,我想用 nan 替换低于 1 和高于 5 的每个值。 此代码有效 persDf = persDf.mask(persDf 5)) 我不知道为什么会这样。我已经检查了手册页和关于明显类
我正在使用 Explaining and Harnessing Adversarial Examples 中的 FGSM 方法实现对抗性训练使用自定义损失函数: 使用自定义损失函数在 tf.keras
我有以下功能: def delta(r, dr): res = np.zeros(r.shape) mask1 = (r >= 0.5*dr) & (r = 0.5*dr) & (r <=
我有一个给定的数据框,我希望每一行都能够选择高于该行给定百分位数的值。 让我们考虑这个数据框: df = pd.DataFrame({'A' : [5,6,3,4, 0,5,9], 'B' : [1,
目前遇到一个技术问题,想改进之前的实现,情况是: 我有 5 个 GPIO 引脚,我需要使用这些引脚作为硬件标识符,例如: pin1: LOW pin2: LOW pin3: LOW pin4: LOW
我有一个字典,其中包含多个 pandas 掩码作为特定数据帧的字符串,但我找不到使用这些掩码的方法。 这是一个简短的可重现示例: df = pd.DataFrame({'age' : [10, 24,
我有一个 bool 掩码列表,该列表是通过将不同的搜索条件应用于数据框而获得的。这是一个包含 4 个掩码的示例列表: mask_list = [mask1, mask2, mask3, mask4]
我在 pandas 掩码子集上执行了一项任务: pdxy = pd.DataFrame(data,columns=['X','Y','C','CC']) mask = pdxy[:]['Y']==8
所以我有一个输入,默认情况下我希望它的值为 0.00。很简单。 当用户开始输入时,我想从右边开始填充。所以当他们输入“5”时,输入将显示“0.05”。然后他们输入一个零,输入变为“0.50”,依此类推
我是一名优秀的程序员,十分优秀!