1 /************************************************************************
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
38 /* load the materials */
39 void model_load_obj_mtl(char* file
)
52 colour_t c
= {255,255,255,255};
55 f
= file_load("model",file
);
59 data
= (char*)f
->data
;
65 if (!strncmp(p
, "newmtl", 6)) {
67 m
= mat_from_colour(&c
);
68 mat_options(m
,MATOPT_ALPHA_BLEND
);
72 sscanf(p
,"newmtl %s",name
);
73 }else if (strncmp(p
, "map_Kd", 6) == 0) {
74 sscanf(p
,"map_Kd %s",tmp1
);
75 m
= mat_from_image("texture",tmp1
);
76 mat_options(m
,MATOPT_ALPHA_BLEND
);
79 }else if (strncmp(p
,"Kd ",3) == 0) {
80 sscanf(p
,"Kd %f %f %f",&rf
,&gf
,&bf
);
81 c
.r
= (unsigned char)(rf
*255.0);
82 c
.g
= (unsigned char)(gf
*255.0);
83 c
.b
= (unsigned char)(bf
*255.0);
84 }else if (strncmp(p
,"d ",2) == 0) {
86 c
.a
= (unsigned char)(af
*255.0);
96 m
= mat_from_colour(&c
);
101 /* load an obj model */
102 model_t
*model_load_obj(file_t
*f
)
114 struct obj_poly_s
*poly
;
123 d
= strrchr(f
->name
,'.');
127 sprintf(file
,"%s.mtl",f
->name
);
130 mdl
= model_create();
132 model_load_obj_mtl(file
);
134 data
= (char*)f
->data
;
139 if (!strncmp(p
, "vn", 2)) {
141 }else if (!strncmp(p
, "vt", 2)) {
143 }else if (!strncmp(p
, "v", 1)) {
145 }else if (!strncmp(p
, "f", 1)) {
156 vert
= malloc(sizeof(v3_t
)*vq
);
158 tex
= malloc(sizeof(v2_t
)*tq
);
160 tex
= malloc(sizeof(v2_t
)*2);
166 norm
= malloc(sizeof(v3_t
)*nq
);
167 poly
= malloc(sizeof(struct obj_poly_s
)*pq
);
174 /* stage 1, read in the file data */
176 if (strncmp(p
, "vn", 2) == 0) {
177 sscanf(p
, "vn %f %f %f", &norm
[nq
].x
, &norm
[nq
].y
, &norm
[nq
].z
);
179 }else if (strncmp(p
, "vt", 2) == 0) {
180 sscanf(p
, "vt %f %f", &tex
[tq
].x
, &tex
[tq
].y
);
182 }else if (strncmp(p
, "v", 1) == 0) {
183 sscanf(p
, "v %f %f %f", &vert
[vq
].x
, &vert
[vq
].y
, &vert
[vq
].z
);
185 }else if (strncmp(p
, "f", 1) == 0) {
186 if (strstr(p
,"//")) {
189 "f %u//%u %u//%u %u//%u",
209 "f %u/%u/%u %u/%u/%u %u/%u/%u",
232 }else if (strncmp(p
, "usemtl", 6) == 0) {
233 sscanf(p
,"usemtl %s",tmp
);
237 m
= mesh_create_material(mat
);
238 array_push_ptr(mdl
->meshes
,m
);
247 /* stage 2, make a rosethorn model out of it */
249 for (i
=0; i
<pq
; i
++) {
250 mesh_push_polypoint(poly
[i
].m
,&vert
[poly
[i
].v1
],&tex
[poly
[i
].t1
]);
251 mesh_push_polypoint(poly
[i
].m
,&vert
[poly
[i
].v2
],&tex
[poly
[i
].t2
]);
252 mesh_push_polypoint(poly
[i
].m
,&vert
[poly
[i
].v3
],&tex
[poly
[i
].t3
]);
254 for (i
=0; i
<mdl
->meshes
->length
; i
++) {
255 m
= ((mesh_t
**)(mdl
->meshes
->data
))[i
];
256 mesh_calc_normals(m
);
259 for (i
=0; i
<pq
; i
++) {
260 mesh_push_polypoint_n(poly
[i
].m
,&vert
[poly
[i
].v1
],&norm
[poly
[i
].n1
],&tex
[poly
[i
].t1
]);
261 mesh_push_polypoint_n(poly
[i
].m
,&vert
[poly
[i
].v2
],&norm
[poly
[i
].n2
],&tex
[poly
[i
].t2
]);
262 mesh_push_polypoint_n(poly
[i
].m
,&vert
[poly
[i
].v3
],&norm
[poly
[i
].n3
],&tex
[poly
[i
].t3
]);