3 Copyright (C) 2003 Nuno Subtil
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <lib3ds/file.h>
30 #include <lib3ds/mesh.h>
31 #include <lib3ds/node.h>
32 #include <lib3ds/material.h>
33 #include <lib3ds/vector.h>
36 #include "linked-lists.h"
39 static const char cvsid
[] =
40 "$Id: tds.c,v 1.9 2003/07/19 01:10:18 nsubtil Exp $";
42 static struct tdsfile
*tdsfiles
= NULL
;
45 carrega um ficheiro 3ds
47 struct tdsfile
*tds_load_file(char *name
)
52 h
= lib3ds_file_load(name
);
55 printf("%s: tds_load_file: lib3ds_file_load() failed\n", name
);
59 new = (struct tdsfile
*)malloc(sizeof(struct tdsfile
));
60 new->name
= strdup(name
);
64 LLIST_ADD(struct tdsfile
, tdsfiles
, new);
69 procura um ficheiro 3ds carregado
71 struct tdsfile
*tds_find(char *name
)
78 if(strcmp(cur
->name
, name
) == 0)
87 struct tdsfile
*tds_get(char *name
)
93 ret
= tds_load_file(name
);
99 carrega o mesh de um 3ds
101 struct object
*tds_load(char *name
)
103 struct tdsfile
*file
;
107 file
= tds_get(name
);
111 lib3ds_file_eval(file
->handler
, 0.0);
112 ret
= malloc(sizeof(struct object
));
115 ret
->dlist_color
= -1;
116 ret
->dlist_nocolor
= -1;
118 p
= file
->handler
->nodes
;
121 tds_add_node_recursive(ret
, p
, file
);
128 void tds_add_node_recursive(struct object
*obj
, Lib3dsNode
*node
, struct tdsfile
*file
)
132 Lib3dsVector
*normal_list
;
139 tds_add_node_recursive(obj
, p
, file
);
143 if(node
->type
!= LIB3DS_OBJECT_NODE
)
146 /* XXX - que raio é isto ? */
147 if(strcmp(node
->name
, "$$$DUMMY") == 0)
150 if(strstr(node
->name
, "ctag"))
155 mesh
= lib3ds_file_mesh_by_name(file
->handler
, node
->name
);
158 printf("tds_add_node_recursive: in [%s]: mesh is null!\n", node
->name
);
162 normal_list
= (Lib3dsVector
*)malloc(sizeof(Lib3dsVector
) * mesh
->faces
* 3);
163 lib3ds_mesh_calculate_normals(mesh
, normal_list
);
165 for(c
= 0; c
< mesh
->faces
; c
++)
168 struct face_tri
*new;
170 new = malloc(sizeof(struct face_tri
));
172 face
= &mesh
->faceL
[c
];
174 if(face
->material
[0])
176 Lib3dsMaterial
*material
;
178 material
= lib3ds_file_material_by_name(file
->handler
, face
->material
);
179 memcpy(new->color
, material
->diffuse
, sizeof(GLfloat
) * 4);
187 new->a
= mesh
->pointL
[face
->points
[0]].pos
;
188 new->b
= mesh
->pointL
[face
->points
[1]].pos
;
189 new->c
= mesh
->pointL
[face
->points
[2]].pos
;
191 memcpy(new->na
, normal_list
[3 * c
], sizeof(GLfloat
) * 3);
192 memcpy(new->nb
, normal_list
[3 * c
+ 1], sizeof(GLfloat
) * 3);
193 memcpy(new->nc
, normal_list
[3 * c
+ 2], sizeof(GLfloat
) * 3);
195 new->color_tag
= flag
;
197 object_add_face(obj
, new);