Commit 0297c0eb authored by Méso Star's avatar Méso Star

Implement the stl to Star-3D functions

parent 120c8aec
......@@ -28,6 +28,7 @@
#include "s3dstl.h"
#include <rsys/float3.h>
#include <rsys/logger.h>
#include <rsys/mem_allocator.h>
#include <rsys/ref_count.h>
......@@ -49,6 +50,87 @@ struct s3dstl {
/*******************************************************************************
* Helper functions
******************************************************************************/
static void
get_position(const unsigned ivert, float pos[3], void* ctx)
{
const struct sstl_desc* desc = ctx;
ASSERT(ctx && pos && ivert < desc->vertices_count);
f3_set(pos, desc->vertices + ivert * 3);
}
static void
get_indices(const unsigned itri, unsigned ids[3], void* ctx)
{
const struct sstl_desc* desc = ctx;
const unsigned* indices;
ASSERT(ctx && ids && itri < desc->triangles_count);
indices = desc->indices + itri*3;
ids[0] = indices[0];
ids[1] = indices[1];
ids[2] = indices[2];
}
static void
log_error(const struct s3dstl* s3dstl, const char* msg, ...)
{
va_list vargs_list;
ASSERT(s3dstl && msg);
if(s3dstl->verbose) {
res_T res; (void)res;
va_start(vargs_list, msg);
res = logger_vprint(s3dstl->logger, LOG_ERROR, msg, vargs_list);
ASSERT(res == RES_OK);
va_end(vargs_list);
}
}
static res_T
shape_create(struct s3dstl* s3dstl, const char* filename)
{
struct s3d_vertex_data vertex_data;
struct s3d_shape* shape = NULL;
struct sstl_desc desc;
res_T res = RES_OK;
ASSERT(s3dstl && filename);
res = sstl_get_desc(s3dstl->sstl, &desc);
if(res != RES_OK) {
log_error(s3dstl, "%s: couldn't retrieve the STL descriptor.\n", filename);
goto error;
}
if(!desc.triangles_count) goto exit;
res = s3d_shape_create_mesh(s3dstl->s3d, &shape);
if(res != RES_OK) {
log_error(s3dstl, "%s: couldn't create the Star-3D shape.\n", filename);
goto error;
}
vertex_data.usage = S3D_POSITION;
vertex_data.type = S3D_FLOAT3;
vertex_data.get = get_position;
res = s3d_mesh_setup_indexed_vertices(shape, (unsigned)desc.triangles_count,
get_indices, (unsigned)desc.vertices_count, &vertex_data, 1, &desc);
if(res != RES_OK) {
log_error(s3dstl, "%s: couldn't setup the data of the Star-3D shape.\n",
filename);
goto error;
}
if(s3dstl->shape) {
S3D(shape_ref_put(s3dstl->shape));
s3dstl->shape = shape;
}
exit:
return res;
error:
if(shape) S3D(shape_ref_put(shape));
goto exit;
}
static void
release_s3dstl(ref_T* ref)
{
......@@ -153,3 +235,31 @@ s3dstl_get_sstl(struct s3dstl* s3dstl, struct sstl** sstl)
return RES_OK;
}
res_T
s3dstl_load(struct s3dstl* s3dstl, const char* filename)
{
res_T res;
if(!s3dstl || !filename) return RES_BAD_ARG;
res = sstl_load(s3dstl->sstl, filename);
if(res != RES_OK) return res;
return shape_create(s3dstl, filename);
}
res_T
s3dstl_load_stream(struct s3dstl* s3dstl, FILE* stream)
{
res_T res;
if(!s3dstl || !stream) return RES_BAD_ARG;
res = sstl_load_stream(s3dstl->sstl, stream);
if(res != RES_OK) return res;
return shape_create(s3dstl, "STREAM");
}
res_T
s3dstl_get_shape(struct s3dstl* s3dstl, struct s3d_shape** shape)
{
if(!s3dstl || !shape) return RES_BAD_ARG;
*shape = s3dstl->shape;
return RES_OK;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment