|
| 1 | +from array import array |
| 2 | +import pyray as pr |
| 3 | + |
| 4 | +W, H = 640, 480 |
| 5 | + |
| 6 | + |
| 7 | +def make_cube(width, height, length): |
| 8 | + vertices = array('f', [ |
| 9 | + -width / 2, -height / 2, length / 2, |
| 10 | + width / 2, -height / 2, length / 2, |
| 11 | + width / 2, height / 2, length / 2, |
| 12 | + -width / 2, height / 2, length / 2, |
| 13 | + -width / 2, -height / 2, -length / 2, |
| 14 | + -width / 2, height / 2, -length / 2, |
| 15 | + width / 2, height / 2, -length / 2, |
| 16 | + width / 2, -height / 2, -length / 2, |
| 17 | + -width / 2, height / 2, -length / 2, |
| 18 | + -width / 2, height / 2, length / 2, |
| 19 | + width / 2, height / 2, length / 2, |
| 20 | + width / 2, height / 2, -length / 2, |
| 21 | + -width / 2, -height / 2, -length / 2, |
| 22 | + width / 2, -height / 2, -length / 2, |
| 23 | + width / 2, -height / 2, length / 2, |
| 24 | + -width / 2, -height / 2, length / 2, |
| 25 | + width / 2, -height / 2, -length / 2, |
| 26 | + width / 2, height / 2, -length / 2, |
| 27 | + width / 2, height / 2, length / 2, |
| 28 | + width / 2, -height / 2, length / 2, |
| 29 | + -width / 2, -height / 2, -length / 2, |
| 30 | + -width / 2, -height / 2, length / 2, |
| 31 | + -width / 2, height / 2, length / 2, |
| 32 | + -width / 2, height / 2, -length / 2 |
| 33 | + ]) |
| 34 | + |
| 35 | + texcoords = array('f', [ |
| 36 | + 0.0, 0.0, |
| 37 | + 1.0, 0.0, |
| 38 | + 1.0, 1.0, |
| 39 | + 0.0, 1.0, |
| 40 | + 1.0, 0.0, |
| 41 | + 1.0, 1.0, |
| 42 | + 0.0, 1.0, |
| 43 | + 0.0, 0.0, |
| 44 | + 0.0, 1.0, |
| 45 | + 0.0, 0.0, |
| 46 | + 1.0, 0.0, |
| 47 | + 1.0, 1.0, |
| 48 | + 1.0, 1.0, |
| 49 | + 0.0, 1.0, |
| 50 | + 0.0, 0.0, |
| 51 | + 1.0, 0.0, |
| 52 | + 1.0, 0.0, |
| 53 | + 1.0, 1.0, |
| 54 | + 0.0, 1.0, |
| 55 | + 0.0, 0.0, |
| 56 | + 0.0, 0.0, |
| 57 | + 1.0, 0.0, |
| 58 | + 1.0, 1.0, |
| 59 | + 0.0, 1.0]) |
| 60 | + |
| 61 | + normals = array('f', [ |
| 62 | + 0.0, 0.0, 1.0, |
| 63 | + 0.0, 0.0, 1.0, |
| 64 | + 0.0, 0.0, 1.0, |
| 65 | + 0.0, 0.0, 1.0, |
| 66 | + 0.0, 0.0, -1.0, |
| 67 | + 0.0, 0.0, -1.0, |
| 68 | + 0.0, 0.0, -1.0, |
| 69 | + 0.0, 0.0, -1.0, |
| 70 | + 0.0, 1.0, 0.0, |
| 71 | + 0.0, 1.0, 0.0, |
| 72 | + 0.0, 1.0, 0.0, |
| 73 | + 0.0, 1.0, 0.0, |
| 74 | + 0.0, -1.0, 0.0, |
| 75 | + 0.0, -1.0, 0.0, |
| 76 | + 0.0, -1.0, 0.0, |
| 77 | + 0.0, -1.0, 0.0, |
| 78 | + 1.0, 0.0, 0.0, |
| 79 | + 1.0, 0.0, 0.0, |
| 80 | + 1.0, 0.0, 0.0, |
| 81 | + 1.0, 0.0, 0.0, |
| 82 | + -1.0, 0.0, 0.0, |
| 83 | + -1.0, 0.0, 0.0, |
| 84 | + -1.0, 0.0, 0.0, |
| 85 | + -1.0, 0.0, 0.0 |
| 86 | + ]) |
| 87 | + |
| 88 | + indices = array('h', |
| 89 | + sum([[4 * k, 4 * k + 1, 4 * k + 2, 4 * k, 4 * k + 2, 4 * k + 3] for k in range(0, 6)], [])) |
| 90 | + |
| 91 | + print(vertices, indices) |
| 92 | + |
| 93 | + return (pr.Mesh(24, 12, vertices, texcoords, |
| 94 | + None, normals, None, None, indices, |
| 95 | + None, None, None, None, 0, None)) |
| 96 | + |
| 97 | + |
| 98 | +pr.init_window(W, H, "Mesh creation") |
| 99 | + |
| 100 | +msh = make_cube(3, 4, 40) |
| 101 | +pr.upload_mesh(msh, False) |
| 102 | +matdefault = pr.load_material_default() |
| 103 | +eye = pr.matrix_identity() |
| 104 | + |
| 105 | +camera = pr.Camera3D((30.0, 20.0, 30.0), (0.0, 0.0, 0.0), (0.0, 1.0, 0.0), 70.0, |
| 106 | + pr.CameraProjection.CAMERA_PERSPECTIVE) |
| 107 | + |
| 108 | +# Export so we can inspect it |
| 109 | +pr.export_mesh(msh, "test-cube.obj") |
| 110 | + |
| 111 | +while not pr.window_should_close(): |
| 112 | + pr.update_camera(camera, pr.CameraMode.CAMERA_ORBITAL) |
| 113 | + pr.begin_drawing() |
| 114 | + pr.clear_background(pr.BLACK) |
| 115 | + pr.begin_mode_3d(camera) |
| 116 | + pr.draw_grid(10, 5.0) |
| 117 | + pr.draw_mesh(msh, matdefault, eye) |
| 118 | + pr.end_mode_3d() |
| 119 | + pr.end_drawing() |
| 120 | +pr.close_window() |
0 commit comments