我正在尝试使用openGL创建一个魔方。在每个单独的代码中,我想使用矩阵在x,y,z周围绘制一个立方体。我想知道使用glMultMatrix
处理后如何绘制我的立方体?谢谢!
class Piece:
def __init__(self, x, y, z, position):
self.x = x
self.y = y
self.z = z
self.matrix = position
def draw(self, axis, slice, dir):
glPushMatrix()
glMultMatrixf(self.matrix)
glBegin(GL_QUADS)
#
# What would go here to draw a cube based off of self.matrix?
#
glEnd()
glPopMatrix()
您只需以(0,0,0)
4*6=24
glVertex
调用集为中心渲染轴对齐的立方体,并具有+/-1.0
或任何不同大小的组合。您可以从此示例中提取顶点(在较新的 API 中(
- 完整的GL+GLSL+VAO/VBO C++示例
所以代码将是(C++(:
const GLfloat vao_pos[]=
{
// x y z //ix
-1.0,+1.0,-1.0, //0
+1.0,+1.0,-1.0, //1
+1.0,-1.0,-1.0, //2
-1.0,-1.0,-1.0, //3
-1.0,-1.0,+1.0, //4
+1.0,-1.0,+1.0, //5
+1.0,+1.0,+1.0, //6
-1.0,+1.0,+1.0, //7
};
const GLuint vao_ix[]=
{
0,1,2,3,
4,5,6,7,
3,2,5,4,
2,1,6,5,
1,0,7,6,
0,3,4,7,
};
glBegin(GL_QUADS);
for (int i=0;i<24;i++) glVertex3dv(vao_pos+3*vao_ix[i]);
glEnd();
或者这个:
const GLfloat vao_pos[]=
{
// x y z //ix
-1.0,+1.0,-1.0, //0
+1.0,+1.0,-1.0, //1
+1.0,-1.0,-1.0, //2
-1.0,-1.0,-1.0, //3
-1.0,-1.0,+1.0, //4
+1.0,-1.0,+1.0, //5
+1.0,+1.0,+1.0, //6
-1.0,+1.0,+1.0, //7
-1.0,-1.0,-1.0, //3
+1.0,-1.0,-1.0, //2
+1.0,-1.0,+1.0, //5
-1.0,-1.0,+1.0, //4
+1.0,-1.0,-1.0, //2
+1.0,+1.0,-1.0, //1
+1.0,+1.0,+1.0, //6
+1.0,-1.0,+1.0, //5
+1.0,+1.0,-1.0, //1
-1.0,+1.0,-1.0, //0
-1.0,+1.0,+1.0, //7
+1.0,+1.0,+1.0, //6
-1.0,+1.0,-1.0, //0
-1.0,-1.0,-1.0, //3
-1.0,-1.0,+1.0, //4
-1.0,+1.0,+1.0, //7
};
glBegin(GL_QUADS);
for (int i=0;i<24*3;i+=3) glVertex3dv(vao_pos+i);
glEnd();
所以只需将其移植到 python(我不用 python 编码(它实际上只是一个数组和一个循环......您还可以对 24 个glVertex
调用进行硬编码,而无需循环....
方可以通过 3x3x3 立方体的三维数组进行组织。旋转立方体的切片似乎很容易,但请注意,如果在切片上旋转,立方体的位置会发生变化,必须重新组织。不仅位置会发生变化,(旋转的(单个立方体的方向也会发生变化。
看看这个答案 拉比德,https://stackoverflow.com/a/54953213/7977581
它非常具有描述性。他也在Python中解释了它。