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> 52 static void render_node(Lib3dsNode *node,Lib3dsFile *file);
53 static void light_update(Lib3dsLight *l,Lib3dsFile *file);
62 #if MRPT_HAS_OPENGL_GLUT 65 if (! m_3dsfile->file)
return;
67 Lib3dsFile *file = (Lib3dsFile*) m_3dsfile->file;
77 if (m_enable_extra_lighting)
80 const GLfloat a[] = {0.8f, 0.8f, 0.8f, 1.0f};
81 GLfloat c[] = {0.5f, 0.5f, 0.5f, 0.5f};
96 const GLfloat a[] = {0.1f, 0.1f, 0.1f, 1.0f};
97 GLfloat c[] = {1.0f, 1.0f, 1.0f, 1.0f};
98 GLfloat p[] = {0.0f, 0.0f, 0.0f, 1.0f};
101 for (Lib3dsLight *l=file->lights; l; l=l->next)
105 light_update(l,file);
118 float att = 1.0/m_scale_x;
121 p[0] = l->position[0];
122 p[1] = l->position[1];
123 p[2] = l->position[2];
128 p[0] = (l->spot[0] - l->position[0]);
129 p[1] = (l->spot[1] - l->position[1]);
130 p[2] = (l->spot[2] - l->position[2]);
138 for (Lib3dsNode *
p=file->nodes;
p!=0;
p=
p->next)
152 #define TEX_XSIZE 1024 153 #define TEX_YSIZE 1024 155 struct _player_texture
164 #if MRPT_HAS_OPENGL_GLUT 170 float scale_x, scale_y;
173 typedef struct _player_texture Player_texture;
177 const char *datapath=
".";
183 static void render_node(Lib3dsNode *node, Lib3dsFile *file)
185 #if MRPT_HAS_OPENGL_GLUT 188 for (
p=node->childs;
p!=0;
p=
p->next) {
192 if (node->type==LIB3DS_OBJECT_NODE) {
195 if (strcmp(node->name,
"$$$DUMMY")==0) {
199 mesh = lib3ds_file_mesh_by_name(file, node->data.object.morph);
201 mesh = lib3ds_file_mesh_by_name(file, node->name);
214 Lib3dsVector *normalL=(Lib3dsVector *)malloc(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) {
225 Lib3dsFace *f=&mesh->faceL[
p];
226 Lib3dsMaterial *mat=0;
228 Player_texture *pt = NULL;
231 if (f->material[0]) {
232 mat=lib3ds_file_material_by_name(file, f->material);
235 if( mat != oldmat ) {
338 static const Lib3dsRgba
a={0.7f, 0.7f, 0.7f, 1.0f};
339 static const Lib3dsRgba d={0.7f, 0.7f, 0.7f, 1.0f};
340 static const Lib3dsRgba
s={1.0f, 1.0f, 1.0f, 1.0f};
348 else if (mat != NULL && mat->texture1_map.name[0])
369 for (i=0; i<3; ++i) {
374 pt->scale_y - mesh->texelL[f->points[i]][0]*pt->scale_y);
396 d=&node->data.object;
416 static void light_update(Lib3dsLight *l,Lib3dsFile *file)
420 ln = lib3ds_file_node_by_name(file, l->name, LIB3DS_LIGHT_NODE);
421 sn = lib3ds_file_node_by_name(file, l->name, LIB3DS_SPOT_NODE);
424 memcpy(l->color, ln->data.light.col,
sizeof(Lib3dsRgb));
425 memcpy(l->position, ln->data.light.pos,
sizeof(Lib3dsVector));
429 memcpy(l->spot, sn->data.spot.pos,
sizeof(Lib3dsVector));
445 writeToStreamRender(out);
448 if (m_3dsfile && m_3dsfile->file)
451 lib3ds_file_save( (Lib3dsFile*) m_3dsfile->file, tmpFil.c_str() );
459 out << m_enable_extra_lighting;
479 readFromStreamRender(
in);
487 if (chunk.getTotalBytesCount())
490 if (!chunk.saveBufferToFile( tmpFil ) )
495 loadFrom3DSFile( tmpFil );
510 in >> dummy_scale >> dummy_scale >> dummy_scale;
512 in >> m_enable_extra_lighting;
516 m_enable_extra_lighting =
false;
535 #if MRPT_HAS_OPENGL_GLUT 543 m_enable_extra_lighting(false)
584 file=lib3ds_file_load(tmpFil.c_str());
591 file=lib3ds_file_load(filepath.c_str());
603 for(Lib3dsMesh *mesh = file->meshes; mesh != NULL; mesh = mesh->next)
605 Lib3dsNode *node = lib3ds_node_new_object();
606 strcpy(node->name, mesh->name);
607 node->parent_id = LIB3DS_NO_PARENT;
608 lib3ds_file_insert_node(file, node);
612 lib3ds_file_eval(file, 1.0f);
615 Lib3dsVector bmin, bmax;
616 float sx, sy, sz,
size;
619 lib3ds_file_bounding_box_of_nodes(file, LIB3DS_TRUE, LIB3DS_FALSE, LIB3DS_FALSE, bmin, bmax);
621 for (
int k=0;k<3;k++) {
626 sx = bmax[0] - bmin[0];
627 sy = bmax[1] - bmin[1];
628 sz = bmax[2] - bmin[2];
631 cx = (bmin[0] + bmax[0])/2;
632 cy = (bmin[1] + bmax[1])/2;
633 cz = (bmin[2] + bmax[2])/2;
637 if (file->lights == NULL)
641 light = lib3ds_light_new(
"light0");
642 light->spot_light = 0;
644 light->color[0] = light->color[1] = light->color[2] = .6f;
645 light->position[0] = cx +
size * .75;
646 light->position[1] = cy -
size * 1.;
647 light->position[2] = cz +
size * 1.5;
649 light->outer_range = 100;
650 light->inner_range = 10;
651 light->multiplier = 1;
652 lib3ds_file_insert_light(file, light);
654 light = lib3ds_light_new(
"light1");
655 light->spot_light = 0;
657 light->color[0] = light->color[1] = light->color[2] = .3f;
658 light->position[0] = cx -
size;
659 light->position[1] = cy -
size;
660 light->position[2] = cz +
size * .75;
662 light->outer_range = 100;
663 light->inner_range = 10;
664 light->multiplier = 1;
665 lib3ds_file_insert_light(file, light);
667 light = lib3ds_light_new(
"light2");
668 light->spot_light = 0;
670 light->color[0] = light->color[1] = light->color[2] = .3f;
671 light->position[0] = cx;
672 light->position[1] = cy +
size;
673 light->position[2] = cz +
size;
675 light->outer_range = 100;
676 light->inner_range = 10;
677 light->multiplier = 1;
678 lib3ds_file_insert_light(file, light);
681 lib3ds_file_eval(file,0.);
695 lib3ds_file_eval( (Lib3dsFile*)
m_3dsfile->file, time_anim );
722 lib3ds_file_free( (Lib3dsFile*) file);
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
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".
#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.
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
bool BASE_IMPEXP 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.
char BASE_IMPEXP * strcpy(char *dest, size_t destSize, const char *source) MRPT_NO_THROWS
An OS-independent version of strcpy.
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. This class automatically holds the cached 3x3 rotation m...
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)
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=NULL, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=NULL, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=NULL, 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...
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 BASE_IMPEXP 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 BASE_IMPEXP decompress_gz_file(const std::string &file_path, vector_byte &buffer)
Decompress a gzip file (xxxx.gz) into a memory buffer.
std::string BASE_IMPEXP 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
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.
A container for automatic deletion of lib3ds's scene when the last reference of the smart_ptr's is de...
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
Bounding box.
The namespace for 3D scene representation and rendering.
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const MRPT_OVERRIDE
Simulation of ray-trace, given a pose.
bool BASE_IMPEXP 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)
void evaluateAnimation(double time_anim)
Evaluates the scene at a given animation time.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
void render_dl() const MRPT_OVERRIDE
Render child objects.
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)
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
This element keeps a set of objects imported from a 3DStudio file (.3ds).
GLAPI void GLAPIENTRY glLightModelfv(GLenum pname, const GLfloat *params)
#define GL_SPOT_DIRECTION
GLAPI void GLAPIENTRY glCallList(GLuint list)