12 #include <mrpt/config.h> 16 #include <lib3ds/file.h> 17 #include <lib3ds/background.h> 18 #include <lib3ds/camera.h> 19 #include <lib3ds/mesh.h> 20 #include <lib3ds/node.h> 21 #include <lib3ds/material.h> 22 #include <lib3ds/matrix.h> 23 #include <lib3ds/vector.h> 24 #include <lib3ds/light.h> 50 static void render_node(Lib3dsNode* node, Lib3dsFile* file);
51 static void light_update(Lib3dsLight* l, Lib3dsFile* file);
60 #if MRPT_HAS_OPENGL_GLUT 63 if (!m_3dsfile->file)
return;
65 Lib3dsFile* file = (Lib3dsFile*)m_3dsfile->file;
74 if (m_enable_extra_lighting)
77 const GLfloat a[] = {0.8f, 0.8f, 0.8f, 1.0f};
78 GLfloat c[] = {0.5f, 0.5f, 0.5f, 0.5f};
92 const GLfloat a[] = {0.1f, 0.1f, 0.1f, 1.0f};
93 GLfloat c[] = {1.0f, 1.0f, 1.0f, 1.0f};
94 GLfloat p[] = {0.0f, 0.0f, 0.0f, 1.0f};
97 for (Lib3dsLight* l = file->lights; l; l = l->next)
101 light_update(l, file);
114 float att = 1.0 / m_scale_x;
117 p[0] = l->position[0];
118 p[1] = l->position[1];
119 p[2] = l->position[2];
124 p[0] = (l->spot[0] - l->position[0]);
125 p[1] = (l->spot[1] - l->position[1]);
126 p[2] = (l->spot[2] - l->position[2]);
133 for (Lib3dsNode*
p = file->nodes;
p != 0;
p =
p->next) render_node(
p, file);
146 #define TEX_XSIZE 1024 147 #define TEX_YSIZE 1024 149 struct _player_texture
158 #if MRPT_HAS_OPENGL_GLUT 164 float scale_x, scale_y;
168 typedef struct _player_texture Player_texture;
172 const char* datapath =
".";
178 static void render_node(Lib3dsNode* node, Lib3dsFile* file)
180 #if MRPT_HAS_OPENGL_GLUT 183 for (
p = node->childs;
p != 0;
p =
p->next)
185 render_node(
p, file);
188 if (node->type == LIB3DS_OBJECT_NODE)
192 if (strcmp(node->name,
"$$$DUMMY") == 0)
197 mesh = lib3ds_file_mesh_by_name(file, node->data.object.morph);
198 if (mesh ==
nullptr) mesh = lib3ds_file_mesh_by_name(file, node->name);
213 Lib3dsVector* normalL = (Lib3dsVector*)malloc(
214 3 *
sizeof(Lib3dsVector) * mesh->faces);
215 Lib3dsMaterial* oldmat = (Lib3dsMaterial*)-1;
218 lib3ds_matrix_copy(M, mesh->matrix);
219 lib3ds_matrix_inv(M);
222 lib3ds_mesh_calculate_normals(mesh, normalL);
224 for (
p = 0;
p < mesh->faces; ++
p)
226 Lib3dsFace* f = &mesh->faceL[
p];
227 Lib3dsMaterial* mat = 0;
229 Player_texture* pt =
nullptr;
234 mat = lib3ds_file_material_by_name(file, f->material);
377 pow(2.0, 10.0 * mat->shininess));
381 static const Lib3dsRgba
a = {0.7f, 0.7f, 0.7f,
383 static const Lib3dsRgba d = {0.7f, 0.7f, 0.7f,
385 static const Lib3dsRgba
s = {1.0f, 1.0f, 1.0f,
395 else if (mat !=
nullptr && mat->texture1_map.name[0])
417 for (i = 0; i < 3; ++i)
424 mesh->texelL[f->points[i]][1] * pt->scale_x,
426 mesh->texelL[f->points[i]][0] *
449 d = &node->data.object;
469 static void light_update(Lib3dsLight* l, Lib3dsFile* file)
473 ln = lib3ds_file_node_by_name(file, l->name, LIB3DS_LIGHT_NODE);
474 sn = lib3ds_file_node_by_name(file, l->name, LIB3DS_SPOT_NODE);
478 memcpy(l->color, ln->data.light.col,
sizeof(Lib3dsRgb));
479 memcpy(l->position, ln->data.light.pos,
sizeof(Lib3dsVector));
482 if (sn !=
nullptr)
memcpy(l->spot, sn->data.spot.pos,
sizeof(Lib3dsVector));
497 writeToStreamRender(out);
500 if (m_3dsfile && m_3dsfile->file)
503 lib3ds_file_save((Lib3dsFile*)m_3dsfile->file, tmpFil.c_str());
511 out << m_enable_extra_lighting;
531 readFromStreamRender(
in);
539 if (chunk.getTotalBytesCount())
542 if (!chunk.saveBufferToFile(tmpFil))
547 loadFrom3DSFile(tmpFil);
561 in >> dummy_scale >> dummy_scale >> dummy_scale;
563 in >> m_enable_extra_lighting;
567 m_enable_extra_lighting =
false;
585 #if MRPT_HAS_OPENGL_GLUT 591 : m_bbox_min(0, 0, 0), m_bbox_max(0, 0, 0), m_enable_extra_lighting(false)
593 m_3dsfile = mrpt::make_aligned_shared<TImpl3DS>();
603 m_3dsfile = mrpt::make_aligned_shared<TImpl3DS>();
612 Lib3dsFile* file = 0;
621 "Error loading compressed file: %s", filepath.c_str())
629 file = lib3ds_file_load(tmpFil.c_str());
636 file = lib3ds_file_load(filepath.c_str());
647 for (Lib3dsMesh* mesh = file->meshes; mesh !=
nullptr;
650 Lib3dsNode* node = lib3ds_node_new_object();
651 strcpy(node->name, mesh->name);
652 node->parent_id = LIB3DS_NO_PARENT;
653 lib3ds_file_insert_node(file, node);
657 lib3ds_file_eval(file, 1.0f);
659 Lib3dsVector bmin, bmax;
660 float sx, sy, sz,
size;
663 lib3ds_file_bounding_box_of_nodes(
664 file, LIB3DS_TRUE, LIB3DS_FALSE, LIB3DS_FALSE, bmin, bmax);
666 for (
int k = 0; k < 3; k++)
672 sx = bmax[0] - bmin[0];
673 sy = bmax[1] - bmin[1];
674 sz = bmax[2] - bmin[2];
677 cx = (bmin[0] + bmax[0]) / 2;
678 cy = (bmin[1] + bmax[1]) / 2;
679 cz = (bmin[2] + bmax[2]) / 2;
683 if (file->lights ==
nullptr)
687 light = lib3ds_light_new(
"light0");
688 light->spot_light = 0;
690 light->color[0] = light->color[1] = light->color[2] = .6f;
691 light->position[0] = cx +
size * .75;
692 light->position[1] = cy -
size * 1.;
693 light->position[2] = cz +
size * 1.5;
695 light->outer_range = 100;
696 light->inner_range = 10;
697 light->multiplier = 1;
698 lib3ds_file_insert_light(file, light);
700 light = lib3ds_light_new(
"light1");
701 light->spot_light = 0;
703 light->color[0] = light->color[1] = light->color[2] = .3f;
704 light->position[0] = cx -
size;
705 light->position[1] = cy -
size;
706 light->position[2] = cz +
size * .75;
708 light->outer_range = 100;
709 light->inner_range = 10;
710 light->multiplier = 1;
711 lib3ds_file_insert_light(file, light);
713 light = lib3ds_light_new(
"light2");
714 light->spot_light = 0;
716 light->color[0] = light->color[1] = light->color[2] = .3f;
717 light->position[0] = cx;
718 light->position[1] = cy +
size;
719 light->position[2] = cz +
size;
721 light->outer_range = 100;
722 light->inner_range = 10;
723 light->multiplier = 1;
724 lib3ds_file_insert_light(file, light);
727 lib3ds_file_eval(file, 0.);
741 lib3ds_file_eval((Lib3dsFile*)
m_3dsfile->file, time_anim);
765 lib3ds_file_free((Lib3dsFile*)file);
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define GL_POLYGON_SMOOTH
std::vector< uint8_t > vector_byte
GLAPI void GLAPIENTRY glMultMatrixf(const GLfloat *m)
GLAPI void GLAPIENTRY glVertex3fv(const GLfloat *v)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI GLuint GLAPIENTRY glGenLists(GLsizei range)
std::shared_ptr< TImpl3DS > m_3dsfile
An internal pointer to the lib3ds library's object of type "Lib3dsFile".
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
#define THROW_EXCEPTION(msg)
GLAPI void GLAPIENTRY glPopMatrix(void)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
C3DSScene()
Default constructor.
void render_dl() const override
Render child objects.
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
bool vectorToBinaryFile(const vector_byte &vec, const std::string &fileName)
Saves a vector directly as a binary dump to a file:
mrpt::math::TPoint3D m_bbox_min
Scale of the object.
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) ...
GLAPI void GLAPIENTRY glTexCoord2f(GLfloat s, GLfloat t)
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
void clear()
Clear the contents of this container.
void clear()
Empty the object.
void loadFrom3DSFile(const std::string &file_name)
Loads a scene from a 3DS file (3D Studio format) into this object, from either plain ...
A renderizable object suitable for rendering with OpenGL's display lists.
GLAPI void GLAPIENTRY glBindTexture(GLenum target, GLuint texture)
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
This base provides a set of functions for maths stuff.
std::string getTempFileName()
Returns the name of a proposed temporary file name.
#define GL_LIGHT_MODEL_AMBIENT
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
GLAPI void GLAPIENTRY glNormal3fv(const GLfloat *v)
GLAPI void GLAPIENTRY glCullFace(GLenum mode)
bool decompress_gz_file(const std::string &file_path, vector_byte &buffer)
Decompress a gzip file (xxxx.gz) into a memory buffer.
std::string extractFileExtension(const std::string &filePath, bool ignore_gz=false)
Extract the extension of a filename.
A memory buffer (implements CStream) which can be itself serialized.
GLAPI void GLAPIENTRY glBegin(GLenum mode)
GLsizei const GLchar ** string
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
GLAPI void GLAPIENTRY glMaterialf(GLenum face, GLenum pname, GLfloat param)
GLAPI void GLAPIENTRY glNewList(GLuint list, GLenum mode)
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Simulation of ray-trace, given a pose.
virtual ~C3DSScene()
Private, virtual destructor: only can be deleted from smart pointers.
GLAPI void GLAPIENTRY glLightfv(GLenum light, GLenum pname, const GLfloat *params)
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
mrpt::math::TPoint3D m_bbox_max
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=nullptr, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
The namespace for 3D scene representation and rendering.
bool deleteFile(const std::string &fileName)
Deletes a single file.
GLAPI void GLAPIENTRY glEnd(void)
GLAPI void GLAPIENTRY glEndList(void)
#define GL_CONSTANT_ATTENUATION
GLAPI void GLAPIENTRY glPushMatrix(void)
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
void evaluateAnimation(double time_anim)
Evaluates the scene at a given animation time.
GLAPI void GLAPIENTRY glDisable(GLenum cap)
bool loadBufferFromFile(const std::string &file_name)
Loads the entire buffer from a file *.
GLubyte GLubyte GLubyte a
GLAPI void GLAPIENTRY glMaterialfv(GLenum face, GLenum pname, const GLfloat *params)
This element keeps a set of objects imported from a 3DStudio file (.3ds).
GLAPI void GLAPIENTRY glLightModelfv(GLenum pname, const GLfloat *params)
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
#define GL_SPOT_DIRECTION
GLAPI void GLAPIENTRY glCallList(GLuint list)