我正在尝试从形状 I 生成形状 II,并且我创建了以下代码。
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
vertices = np.array([
[0, 0, 0, 1],
[0.707, -0.408, 0, 1],
[0, -0.816, 0, 1],
[-0.707, -0.408, 0, 1],
[-0.707, 0.408, 0, 1],
[0, 0.816, 0, 1],
[0.707, 0.408, 0, 1],
[0, 0, 0, 1]
], dtype=np.float64)
edges = [
(0, 1),
(1, 2),
(2, 3),
(3, 4),
(4, 5),
(5, 6),
(6, 7),
(3, 0),
(4, 7),
(7, 2),
(6, 1),
(0, 5)
]
axis_vertices = np.array([
[0, 0, 0, 1],
[4, 0, 0, 1],
[0, 4, 0, 1],
[0, 0, 4, 1]
], dtype=np.float64)
axis_edges = [
(0, 1),
(0, 2),
(0, 3)
]
def multiply_matrix_vector(matrix, vector):
result = np.zeros(matrix.shape[0])
for i in range(matrix.shape[0]):
temp_result = np.dot(matrix[i, :], vector)
list(result)[i] = temp_result
return result
def draw_cube():
glBegin(GL_LINES)
glColor3f(1, 1, 1)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex][:3])
glEnd()
def draw_axes():
glBegin(GL_LINES)
glColor3f(1, 0, 0)
glVertex3fv(axis_vertices[0][:3])
glVertex3fv(axis_vertices[1][:3])
glColor3f(0, 1, 0)
glVertex3fv(axis_vertices[0][:3])
glVertex3fv(axis_vertices[2][:3])
glColor3f(0, 0, 1)
glVertex3fv(axis_vertices[0][:3])
glVertex3fv(axis_vertices[3][:3])
glEnd()
def set_perspective_matrix(fov, aspect, near, far):
f = 1.0 / np.tan(np.radians(fov) / 2.0)
perspective_matrix = np.array([
[f / aspect, 0, 0, 0],
[0, f, 0, 0],
[0, 0, (far + near) / (near - far), (2 * far * near) / (near - far)],
[0, 0, -1, 0]
])
glLoadMatrixd(perspective_matrix.transpose())
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
set_perspective_matrix(45, (display[0] / display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -5)
draw_cube()
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
# Translate along the x-axis
translation_matrix = np.array([
[1, 0, 0, 1.5],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
global vertices
vertices = np.dot(vertices, translation_matrix.T)
# Perform isometric projection
isometric_matrix = np.array([
[0.707, -0.408, 0.577, 0],
[0, 0.816, 0.577, 0],
[-0.707, -0.408, 0.577, 0],
[0, 0, 0, 1]
])
vertices = multiply_matrix_vector(isometric_matrix, vertices.T).T
draw_axes()
glPopMatrix()
draw_cube()
pygame.display.flip()
pygame.time.wait(10)
if __name__ == "__main__":
main()
但是,我在调整时遇到了错误:
49号线
result[i] = temp_result
~~~~~~^^^
ValueError:用序列设置数组元素。当我进行以下修改时:
list(result)[i] = temp_result
我收到一个新错误: 64 号线,
glVertex3fv(vertices[vertex][:3])
IndexError:标量变量的索引无效。
你能帮我吗?
创建通过矩阵变换数组中所有顶点的函数:
def multiply_matrix_vector(matrix, vector):
result = np.zeros(vector.shape)
for i in range(vector.shape[0]):
result[i] = vector[i] @ matrix
return result
不变换原始顶点,而是变换
draw_cube
函数的参数:
def draw_cube(vertices, edges):
glBegin(GL_LINES)
glColor3f(1, 1, 1)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex][:3])
glEnd()
如果要绘制立方体,请将顶点和边传递给函数:
draw_cube(vertices, edges)
如果你想变换立方体,复制顶点,变换顶点的副本,最后将变换后的顶点低音到
draw_cube
:
v = np.copy(vertices)
# Translate along the x-axis
translation_matrix = np.array([
[1, 0, 0, 1.5],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
v = multiply_matrix_vector(translation_matrix.T, v)
# Perform isometric projection
isometric_matrix = np.array([
[0.707, -0.408, 0.577, 0],
[0, 0.816, 0.577, 0],
[-0.707, -0.408, 0.577, 0],
[0, 0, 0, 1]
])
v = multiply_matrix_vector(isometric_matrix.T, v)
draw_cube(v, edges)