Skip to content

Instantly share code, notes, and snippets.

@SirNate0
Created February 5, 2024 04:38
Show Gist options
  • Save SirNate0/0819112af5eff7b207af136d427a25f5 to your computer and use it in GitHub Desktop.
Save SirNate0/0819112af5eff7b207af136d427a25f5 to your computer and use it in GitHub Desktop.
PyRay Creating a Mesh from Numpy Arrays
import pyray
import pyray as pr
import raylib as rl
W,H = 640, 480
def MakeCube(width, height, length):
import numpy as np
vertices = np.array([
-width/2, -height/2, length/2,
width/2, -height/2, length/2,
width/2, height/2, length/2,
-width/2, height/2, length/2,
-width/2, -height/2, -length/2,
-width/2, height/2, -length/2,
width/2, height/2, -length/2,
width/2, -height/2, -length/2,
-width/2, height/2, -length/2,
-width/2, height/2, length/2,
width/2, height/2, length/2,
width/2, height/2, -length/2,
-width/2, -height/2, -length/2,
width/2, -height/2, -length/2,
width/2, -height/2, length/2,
-width/2, -height/2, length/2,
width/2, -height/2, -length/2,
width/2, height/2, -length/2,
width/2, height/2, length/2,
width/2, -height/2, length/2,
-width/2, -height/2, -length/2,
-width/2, -height/2, length/2,
-width/2, height/2, length/2,
-width/2, height/2, -length/2
],dtype=np.float32)
texcoords = np.array([
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0]
,dtype=np.float32)
normals = np.array([
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0,-1.0,
0.0, 0.0,-1.0,
0.0, 0.0,-1.0,
0.0, 0.0,-1.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0,-1.0, 0.0,
0.0,-1.0, 0.0,
0.0,-1.0, 0.0,
0.0,-1.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0
],dtype=np.float32)
# Mesh mesh = { 0 };
# mesh.vertices = (float *)RL_MALLOC(24*3*sizeof(float));
# memcpy(mesh.vertices, vertices, 24*3*sizeof(float));
# mesh.texcoords = (float *)RL_MALLOC(24*2*sizeof(float));
# memcpy(mesh.texcoords, texcoords, 24*2*sizeof(float));
# mesh.normals = (float *)RL_MALLOC(24*3*sizeof(float));
# memcpy(mesh.normals, normals, 24*3*sizeof(float));
# mesh.indices = (unsigned short *)RL_MALLOC(36*sizeof(unsigned short));
# int k = 0;
# // Indices can be initialized right now
# for (int i = 0; i < 36; i += 6)
# {
# mesh.indices[i] = 4*k;
# mesh.indices[i + 1] = 4*k + 1;
# mesh.indices[i + 2] = 4*k + 2;
# mesh.indices[i + 3] = 4*k;
# mesh.indices[i + 4] = 4*k + 2;
# mesh.indices[i + 5] = 4*k + 3;
# k++;
# }
# mesh.vertexCount = 24;
# mesh.triangleCount = 12;)
indices = np.concatenate([[4*k,4*k+1,4*k+2,4*k,4*k+2,4*k+3] for k in range(0,6)],dtype=np.int16)
#f = lambda a : ffi.cast("float *", a.ctypes.data)
#s = lambda a : ffi.cast("unsigned short *", a.ctypes.data)
f = lambda a : pr.ffi.from_buffer("float *", a)
s = lambda a : pr.ffi.from_buffer("unsigned short *", a)
o = pr.ffi.NULL
print(vertices,indices)
return pyray.Mesh(24,12,f(vertices),f(texcoords),
o, f(normals), o, o, s(indices),
o, o, o, o, 0, o),(vertices,texcoords,normals,indices)
if __name__ == '__main__':
pr.init_window(W,H, "Hello")
msh,buffs = MakeCube(3,4,40)
rl.UploadMesh(rl.ffi.addressof(msh),False)
matdefault = rl.LoadMaterialDefault()
eye = rl.MatrixIdentity()
camera = pr.Camera3D()
camera.position = pr.Vector3(30.0, 20.0, 30.0) # Camera position
camera.target = pr.Vector3(0.0, 0.0, 0.0) # Camera looking at point
camera.up = pr.Vector3(0.0, 1.0, 0.0) # Camera up vector (rotation towards target)
camera.fovy = 70.0 # Camera field-of-view Y
camera.projection = pyray.CAMERA_PERSPECTIVE # Camera projection type
# Export so we can inspect it
#pr.export_mesh(msh,"test-cube.obj")
rl.ExportMesh(msh,"test-cube.obj".encode('ascii'))
while not pr.window_should_close():
pr.update_camera(camera,pr.CAMERA_PERSPECTIVE)
pr.begin_drawing()
pr.clear_background(pr.BLACK)
pr.begin_mode_3d(camera)
pr.draw_grid(10, 5.0)
rl.DrawMesh(msh, matdefault, eye)
pr.end_mode_3d()
pr.end_drawing()
pr.close_window()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment