Skip to content

Commit

Permalink
lib+python+blender: Sync vertex buffers
Browse files Browse the repository at this point in the history
I still don't know how I'll get normals and texture coords, but those
can come later. For now, vertices get synced, and I see the expected
amount in the dump. Will remain to be seen if they are actually valid :D
  • Loading branch information
vkoskiv committed Dec 10, 2023
1 parent 3cd4f7d commit acd980c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
18 changes: 18 additions & 0 deletions bindings/blender_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ def to_cr_face(me, poly):
face.has_normals = 0
return cr_face

def cr_vertex_buf(scene, me):
verts = []
for v in me.vertices:
cr_vert = c_ray.cr_vector()
cr_vert.x = v.co[0]
cr_vert.y = v.co[1]
cr_vert.z = v.co[2]
verts.append(cr_vert)
normals = []
texcoords = []
vbuf = (c_ray.cr_vector * len(verts))(*verts)
nbuf = (c_ray.cr_vector * len(verts))(*normals)
tbuf = (c_ray.cr_coord * len(verts))(*texcoords)
print(len(normals))
cr_vbuf = scene.vertex_buf_new(bytearray(vbuf), len(verts), bytearray(nbuf), len(normals), bytearray(tbuf), len(texcoords))
return cr_vbuf

class CrayRender(bpy.types.RenderEngine):
bl_idname = "c-ray"
bl_label = "c-ray integration for Blender"
Expand Down Expand Up @@ -131,6 +148,7 @@ def sync_scene(self, renderer, depsgraph, b_scene):
faces.append(to_cr_face(me, poly))
facebuf = (c_ray.cr_face * len(faces))(*faces)
cr_mesh.bind_faces(bytearray(facebuf), len(faces))
cr_mesh.bind_vertex_buf(cr_vertex_buf(cr_scene, me))
return cr_scene

def render(self, depsgraph):
Expand Down
28 changes: 27 additions & 1 deletion bindings/c_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def __init__(self, scene_ptr, name):
self.cr_idx = _lib.scene_mesh_new(self.scene_ptr, self.name)

def bind_vertex_buf(self, buf):
_lib.mesh_bind_vertex_buf(self.scene_ptr, self.cr_idx, buf)
_lib.mesh_bind_vertex_buf(self.scene_ptr, self.cr_idx, buf.cr_idx)
def bind_faces(self, faces, face_count):
_lib.mesh_bind_faces(self.scene_ptr, self.cr_idx, faces, face_count)

Expand Down Expand Up @@ -262,6 +262,30 @@ def transform(self, matrix):
def bind_materials(self, material_set):
_lib.instance_bind_material_set(self.scene_ptr, self.cr_idx, material_set.ms_idx)

class cr_vector(ct.Structure):
_fields_ = [
("x", ct.c_float),
("y", ct.c_float),
("z", ct.c_float),
]

class cr_coord(ct.Structure):
_fields_ = [
("u", ct.c_float),
("v", ct.c_float)
]

class vertex_buf:
def __init__(self, scene_ptr, v, vn, n, nn, t, tn):
self.cr_ptr = scene_ptr
self.v = v
self.vn = vn
self.n = n
self.nn = nn
self.t = t
self.tn = tn
self.cr_idx = _lib.scene_vertex_buf_new(self.cr_ptr, self.v, self.vn, self.n, self.nn, self.t, self.tn)

class scene:
def __init__(self, scene_ptr):
self.cr_ptr = scene_ptr
Expand All @@ -282,6 +306,8 @@ def instance_new(self, object, type):
return instance(self.cr_ptr, object, type)
def set_background(self, material):
return _lib.scene_set_background(self.cr_ptr, material)
def vertex_buf_new(self, v, vn, n, nn, t, tn):
return vertex_buf(self.cr_ptr, v, vn, n, nn, t, tn)

class cr_cb_info(ct.Structure):
_fields_ = [
Expand Down
9 changes: 6 additions & 3 deletions bindings/cray_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ static PyObject *py_cr_scene_vertex_buf_new(PyObject *self, PyObject *args) {
PyErr_SetString(PyExc_MemoryError, "Failed to parse vec_view");
return NULL;
}
if ((vec_view.len / sizeof(struct cr_vector)) != vec_count) {
if (vec_count && (vec_view.len / sizeof(struct cr_vector)) != vec_count) {
printf("vec_count: %zu\n", vec_count);
printf("vec_view.len: %zu\n", vec_view.len);
PyErr_SetString(PyExc_MemoryError, "vec_view / sizeof(struct cr_vector) != vec_count");
return NULL;
}
Expand All @@ -239,8 +240,9 @@ static PyObject *py_cr_scene_vertex_buf_new(PyObject *self, PyObject *args) {
PyErr_SetString(PyExc_MemoryError, "Failed to parse nor_view");
return NULL;
}
if ((nor_view.len / sizeof(struct cr_vector)) != nor_count) {
if (nor_count && (nor_view.len / sizeof(struct cr_vector)) != nor_count) {
printf("nor_count: %zu\n", nor_count);
printf("nor_view.len: %zu\n", nor_view.len);
PyErr_SetString(PyExc_MemoryError, "nor_view / sizeof(struct cr_vector) != nor_count");
return NULL;
}
Expand All @@ -250,8 +252,9 @@ static PyObject *py_cr_scene_vertex_buf_new(PyObject *self, PyObject *args) {
PyErr_SetString(PyExc_MemoryError, "Failed to parse tex_view");
return NULL;
}
if ((tex_view.len / sizeof(struct cr_coord)) != tex_count) {
if (tex_count && (tex_view.len / sizeof(struct cr_coord)) != tex_count) {
printf("tex_count: %zu\n", tex_count);
printf("tex_view.len: %zu\n", tex_view.len);
PyErr_SetString(PyExc_MemoryError, "tex_view / sizeof(struct cr_coord) != tex_count");
return NULL;
}
Expand Down

0 comments on commit acd980c

Please sign in to comment.