我正在尝试使用OpenGl功能在C ++中绘制一个实体立方体,但我想在所需的位置绘制它,例如glVertex3f(20,5,-35)
。如何在给定位置绘制实心立方体?
答案 0 :(得分:2)
glTranslatef(x,y,z)。谷歌它,我认为也有2D版本。但这会将参考帧从堆栈中转换出来。如果需要设置更复杂的转换,则需要调用glpopmatrix和glpushmatrix。小心拼写和使用它们。
答案 1 :(得分:2)
要匹配您的问题并在OpenGL(兼容性配置文件)中绘制多维数据集,您可以使用以下代码:
void drawCube(float x, float y, float z)
{
glBegin(GL_QUADS);
glVertex3f(-1.0f+x, -1.0f+y, -1.0f+z);
glVertex3f(1.0f+x, -1.0f+y, -1.0f+z);
glVertex3f(1.0f+x, 1.0f+y, -1.0f+z);
glVertex3f(-1.0f+x, 1.0f+y, -1.0f+z);
glVertex3f(-1.0f+x, -1.0f+y, -1.0f+z);
glVertex3f(-1.0f+x, -1.0f+y, 1.0f+z);
glVertex3f(-1.0f+x, 1.0f+y, -1.0f+z);
glVertex3f(-1.0f+x, 1.0f+y, 1.0f+z);
glVertex3f(-1.0f+x, -1.0f+y, -1.0f+z);
glVertex3f(-1.0f+x, -1.0f+y, 1.0f+z);
glVertex3f(1.0f+x, -1.0f+y, 1.0f+z);
glVertex3f(1.0f+x, -1.0f+y, -1.0f+z);
glVertex3f(-1.0f+x, -1.0f+y, 1.0f+z);
glVertex3f(1.0f+x, -1.0f+y, 1.0f+z);
glVertex3f(1.0f+x, 1.0f+y, 1.0f+z);
glVertex3f(-1.0f+x, 1.0f+y, 1.0f+z);
glVertex3f(-1.0f+x, -1.0f+y, 1.0f+z);
glVertex3f(1.0f+x, -1.0f+y, 1.0f+z);
glVertex3f(1.0f+x, 1.0f+y, 1.0f+z);
glVertex3f(-1.0f+x, 1.0f+y, 1.0f+z);
glVertex3f(-1.0f+x, 1.0f+y, -1.0f+z);
glVertex3f(-1.0f+x, 1.0f+y, 1.0f+z);
glVertex3f(1.0f+x, 1.0f+y, 1.0f+z);
glVertex3f(1.0f+x, 1.0f+y, -1.0f+z);
glEnd();
}
我不建议使用此功能,而是使用另一个答案中描述的glTranslatef(x, y, z)
解决方案。喜欢这个功能:
void drawCube(float x, float y, float z)
{
glPushMatrix();
glTranslatef(x, y, z);
glBegin(GL_QUADS);
glVertex3f(-1.0f,-1.0f ,-1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f ,1.0f );
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glEnd();
glPopMatrix();
}
这个功能远非完美,但它应该足够了。快乐的编码!