我正在尝试制作一个能够绘制具有指定大小和纹理的立方体的方法。问题是,当我调整立方体的大小时,纹理不会随之重新调整大小。例如,如果我使用大小.5F绘制,则只有8x8像素的16x16纹理绘制。我做错了什么?
static void drawCube(float size, Texture tex, int x, int y, int z) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTranslatef(x, y, z);
tex.bind();
glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_QUADS);
glTexCoord2d(0.0f * size, 0.0f * size);
glVertex3f(-1.0f * size, -1.0f * size, 1.0f * size);
glTexCoord2d(1.0f * size, 0.0f * size);
glVertex3f(1.0f * size, -1.0f * size, 1.0f * size);
glTexCoord2d(1.0f * size, 1.0f * size);
glVertex3f(1.0f * size, 1.0f * size, 1.0f * size);
glTexCoord2d(0.0f * size, 1.0f * size);
glVertex3f(-1.0f * size, 1.0f * size, 1.0f * size);
glEnd();
glBegin(GL_QUADS);
glTexCoord2d(1.0f * size, 0.0f * size);
glVertex3f(-1.0f * size, -1.0f * size, -1.0f * size);
glTexCoord2d(1.0f * size, 1.0f * size);
glVertex3f(-1.0f * size, 1.0f * size, -1.0f * size);
glTexCoord2d(0.0f * size, 1.0f * size);
glVertex3f(1.0f * size, 1.0f * size, -1.0f * size);
glTexCoord2d(0.0f * size, 0.0f * size);
glVertex3f(1.0f * size, -1.0f * size, -1.0f * size);
glEnd();
glBegin(GL_QUADS);
glTexCoord2d(0.0f * size, 1.0f * size);
glVertex3f(-1.0f * size, 1.0f * size, -1.0f * size);
glTexCoord2d(0.0f * size, 0.0f * size);
glVertex3f(-1.0f * size, 1.0f * size, 1.0f * size);
glTexCoord2d(1.0f * size, 0.0f * size);
glVertex3f(1.0f * size, 1.0f * size, 1.0f * size);
glTexCoord2d(1.0f * size, 1.0f * size);
glVertex3f(1.0f * size, 1.0f * size, -1.0f * size);
glEnd();
glBegin(GL_QUADS);
glTexCoord2d(1.0f * size, 1.0f * size);
glVertex3f(-1.0f * size, -1.0f * size, -1.0f * size);
glTexCoord2d(0.0f * size, 1.0f * size);
glVertex3f(1.0f * size, -1.0f * size, -1.0f * size);
glTexCoord2d(0.0f * size, 0.0f * size);
glVertex3f(1.0f * size, -1.0f * size, 1.0f * size);
glTexCoord2d(1.0f * size, 0.0f * size);
glVertex3f(-1.0f * size, -1.0f * size, 1.0f * size);
glEnd();
glBegin(GL_QUADS);
glTexCoord2d(1.0f * size, 0.0f * size);
glVertex3f(1.0f * size, -1.0f * size, -1.0f * size);
glTexCoord2d(1.0f * size, 1.0f * size);
glVertex3f(1.0f * size, 1.0f * size, -1.0f * size);
glTexCoord2d(0.0f * size, 1.0f * size);
glVertex3f(1.0f * size, 1.0f * size, 1.0f * size);
glTexCoord2d(0.0f * size, 0.0f * size);
glVertex3f(1.0f * size, -1.0f * size, 1.0f * size);
glEnd();
glBegin(GL_QUADS);
glTexCoord2d(0.0f * size, 0.0f * size);
glVertex3f(-1.0f * size, -1.0f * size, -1.0f * size);
glTexCoord2d(1.0f * size, 0.0f * size);
glVertex3f(-1.0f * size, -1.0f * size, 1.0f * size);
glTexCoord2d(1.0f * size, 1.0f * size);
glVertex3f(-1.0f * size, 1.0f * size, 1.0f * size);
glTexCoord2d(0.0f * size, 1.0f * size);
glVertex3f(-1.0f * size, 1.0f * size, -1.0f * size);
glEnd();
glTranslatef(-x, -y, -z);
}
答案 0 :(得分:2)
纹理坐标始终使用[0..1]坐标系。如果要绘制完整纹理,请不要根据大小缩放纹理坐标,即
glTexCoord2d(1.0f * size, 0.0f * size); // Not good! (For this purpose anyway)
glTexCoord2d(1.0f, 0.0f); // Good!