From f0d999bd6865a03c42a2d7378fd60c7a469201f2 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Wed, 1 Apr 2020 11:00:11 +0200 Subject: [PATCH] draw-vertex-primitive-list --- Makefile.am | 1 + examples/03.CustomSceneNode.scm | 2 +- src/primitive-types.cpp | 83 +++++++++++++++++++++++++++++++++ src/primitive-types.h | 35 ++++++++++++++ src/vertex3d.cpp | 23 +++++++++ src/vertex3d.h | 3 ++ src/video-driver.cpp | 57 +++++++++++++++++++++- src/video-driver.h | 6 +++ 8 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 src/primitive-types.cpp create mode 100644 src/primitive-types.h diff --git a/Makefile.am b/Makefile.am index 0eb3672..f94c285 100644 --- a/Makefile.am +++ b/Makefile.am @@ -46,6 +46,7 @@ libguile_irrlicht_la_SOURCES = \ src/mesh.cpp \ src/mesh-scene-node.cpp \ src/position2d.cpp \ + src/primitive-types.cpp \ src/rect.cpp \ src/reference-counted.cpp \ src/scene-manager.cpp \ diff --git a/examples/03.CustomSceneNode.scm b/examples/03.CustomSceneNode.scm index ee1c67e..21e5be8 100644 --- a/examples/03.CustomSceneNode.scm +++ b/examples/03.CustomSceneNode.scm @@ -117,7 +117,7 @@ (set! frames (+ frames 1)) (when (= frames 100) (let ((fps (get-fps driver)) - (driver-name (get-video-driver-name driver))) + (driver-name (get-name driver))) (let ((caption (format #f "Irrlicht Engine [~a] FPS:~a" driver-name fps))) (set-window-caption! device caption))) diff --git a/src/primitive-types.cpp b/src/primitive-types.cpp new file mode 100644 index 0000000..2593c75 --- /dev/null +++ b/src/primitive-types.cpp @@ -0,0 +1,83 @@ +/* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine + + Copyright (C) 2020 Javier Sancho + + This file is part of guile-irrlicht. + + guile-irrlicht is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + guile-irrlicht is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with guile-irrlicht. If not, see + . +*/ + +#include +#include +#include "primitive-types.h" + +extern "C" { + + irr::scene::E_PRIMITIVE_TYPE + scm_to_primitive_type (SCM primitive_type) + { + char* type = scm_to_utf8_stringn (scm_symbol_to_string (primitive_type), NULL); + if (!strcmp (type, "points")) + { + return irr::scene::EPT_POINTS; + } + else if (!strcmp (type, "line-strip")) + { + return irr::scene::EPT_LINE_STRIP; + } + else if (!strcmp (type, "line-loop")) + { + return irr::scene::EPT_LINE_LOOP; + } + else if (!strcmp (type, "lines")) + { + return irr::scene::EPT_LINES; + } + else if (!strcmp (type, "triangle-strip")) + { + return irr::scene::EPT_TRIANGLE_STRIP; + } + else if (!strcmp (type, "triangle-fan")) + { + return irr::scene::EPT_TRIANGLE_FAN; + } + else if (!strcmp (type, "triangles")) + { + return irr::scene::EPT_TRIANGLES; + } + else if (!strcmp (type, "quad-strip")) + { + return irr::scene::EPT_QUAD_STRIP; + } + else if (!strcmp (type, "quads")) + { + return irr::scene::EPT_QUADS; + } + else if (!strcmp (type, "polygon")) + { + return irr::scene::EPT_POLYGON; + } + else if (!strcmp (type, "point-sprites")) + { + return irr::scene::EPT_POINT_SPRITES; + } + else + { + scm_error (scm_arg_type_key, NULL, "Wrong primitive type: ~S", + scm_list_1 (primitive_type), scm_list_1 (primitive_type)); + } + } + +} diff --git a/src/primitive-types.h b/src/primitive-types.h new file mode 100644 index 0000000..a3c8bb6 --- /dev/null +++ b/src/primitive-types.h @@ -0,0 +1,35 @@ +/* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine + + Copyright (C) 2020 Javier Sancho + + This file is part of guile-irrlicht. + + guile-irrlicht is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + guile-irrlicht is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with guile-irrlicht. If not, see + . +*/ + +#ifndef __GUILE_IRRLICHT_PRIMITIVE_TYPES_H_INCLUDED__ +#define __GUILE_IRRLICHT_PRIMITIVE_TYPES_H_INCLUDED__ + +#include +#include + +extern "C" { + + irr::scene::E_PRIMITIVE_TYPE + scm_to_primitive_type (SCM primitive_type); + +} + +#endif diff --git a/src/vertex3d.cpp b/src/vertex3d.cpp index 27bf941..f6adab0 100644 --- a/src/vertex3d.cpp +++ b/src/vertex3d.cpp @@ -62,4 +62,27 @@ extern "C" { return scm_from_vector3df (s3dvertex->Pos); } + irr::video::E_VERTEX_TYPE + scm_to_vertex_type (SCM vertex_type) + { + char* type = scm_to_utf8_stringn (scm_symbol_to_string (vertex_type), NULL); + if (!strcmp (type, "standard")) + { + return irr::video::EVT_STANDARD; + } + else if (!strcmp (type, "2tcoords")) + { + return irr::video::EVT_2TCOORDS; + } + else if (!strcmp (type, "tangents")) + { + return irr::video::EVT_TANGENTS; + } + else + { + scm_error (scm_arg_type_key, NULL, "Wrong vertex_type: ~S", + scm_list_1 (vertex_type), scm_list_1 (vertex_type)); + } + } + } diff --git a/src/vertex3d.h b/src/vertex3d.h index 4c033d3..2fcf4f5 100644 --- a/src/vertex3d.h +++ b/src/vertex3d.h @@ -43,6 +43,9 @@ extern "C" { SCM vertex3d_position (SCM vertex); + irr::video::E_VERTEX_TYPE + scm_to_vertex_type (SCM vertex_type); + } #endif diff --git a/src/video-driver.cpp b/src/video-driver.cpp index aa63e52..9c81120 100644 --- a/src/video-driver.cpp +++ b/src/video-driver.cpp @@ -27,9 +27,11 @@ #include "gui-environment.h" #include "material.h" #include "matrix4.h" +#include "primitive-types.h" #include "rect.h" #include "scene-manager.h" #include "texture.h" +#include "vertex3d.h" #include "video-driver.h" #include "wrapped.h" @@ -40,13 +42,15 @@ extern "C" { { init_video_driver_type (); scm_c_define_gsubr ("begin-scene", 1, 0, 1, (scm_t_subr)irr_video_beginScene); + scm_c_define_gsubr ("draw-vertex-primitive-list", 3, 0, 1, + (scm_t_subr)irr_video_drawVertexPrimitiveList); scm_c_define_gsubr ("end-scene", 1, 0, 0, (scm_t_subr)irr_video_endScene); scm_c_define_gsubr ("get-fps", 1, 0, 0, (scm_t_subr)irr_video_getFPS); scm_c_define_gsubr ("get-texture", 2, 0, 0, (scm_t_subr)irr_video_getTexture); scm_c_define_gsubr ("get-video-driver", 1, 0, 0, (scm_t_subr)irr_getVideoDriver); scm_c_define_gsubr ("set-transform!", 3, 0, 0, (scm_t_subr)irr_video_setTransform); - scm_c_export ("begin-scene", "end-scene", "get-fps", "get-texture", - "get-video-driver", "set-transform!", NULL); + scm_c_export ("begin-scene", "draw-vertex-primitive-list", "end-scene", "get-fps", + "get-texture", "get-video-driver", "set-transform!", NULL); } DEFINE_WRAPPED_TYPE (irr::video::IVideoDriver*, "video-driver", @@ -88,6 +92,55 @@ extern "C" { sourceRectAddress)); } + SCM + irr_video_drawVertexPrimitiveList (SCM wrapped_video_driver, + SCM vertices, + SCM indices, + SCM rest) + { + SCM v_type = scm_from_utf8_symbol ("standard"); + SCM p_type = scm_from_utf8_symbol ("triangles"); + + scm_c_bind_keyword_arguments ("draw-vertex-primitive-list", rest, (scm_t_keyword_arguments_flags)0, + scm_from_utf8_keyword ("vertex-type"), &v_type, + scm_from_utf8_keyword ("primitive-type"), &p_type, + SCM_UNDEFINED); + + // Build vertex array + irr::u32 vertex_count = scm_to_uint32 (scm_length (vertices)); + irr::video::S3DVertex s3d_vertices [vertex_count]; + for (int i = 0; i < vertex_count; i++) + { + irr::video::S3DVertex* vertex = unwrap_vertex3d (scm_list_ref (vertices, scm_from_int (i))); + s3d_vertices[i] = irr::video::S3DVertex (vertex->Pos, + vertex->Normal, + vertex->Color, + vertex->TCoords); + } + + // Build index array + irr::u32 index_count = scm_to_uint32 (scm_length (indices)); + SCM flat_indices = scm_apply_0 (scm_eval_string (scm_from_utf8_string ("append")), + indices); + int flat_length = scm_to_int (scm_length (flat_indices)); + irr::u32 c_indices [flat_length]; + for (int i = 0; i < flat_length; i++) + { + c_indices[i] = scm_to_uint32 (scm_list_ref (flat_indices, scm_from_int (i))); + } + + // Draw vertices + irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver); + driver->drawVertexPrimitiveList (&s3d_vertices[0], + vertex_count, + &c_indices[0], + index_count, + scm_to_vertex_type (v_type), + scm_to_primitive_type (p_type), + irr::video::EIT_32BIT); + return SCM_UNSPECIFIED; + } + SCM irr_video_endScene (SCM wrapped_video_driver) { diff --git a/src/video-driver.h b/src/video-driver.h index d697e5b..4512b96 100644 --- a/src/video-driver.h +++ b/src/video-driver.h @@ -38,6 +38,12 @@ extern "C" { irr_video_beginScene (SCM wrapped_video_driver, SCM rest); + SCM + irr_video_drawVertexPrimitiveList (SCM wrapped_video_driver, + SCM vertices, + SCM indices, + SCM rest); + SCM irr_video_endScene (SCM wrapped_video_driver); -- 2.39.5