3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2010 Pavel Herrmann (morpheus.ibis@gmail.com)
27 #define ISWHITE(x) ((x==' ')||(x=='\t'))
34 #include "objloader.h"
44 int addstring( vector
<string
>& v
,string s
)
48 if (s
.compare(v
[i
])==0)
57 int loadOBJ(char* filename
, triangle_s
** out_tri
, int * trinum
, char*** mtllist
, int * mtlnum
, char** mtlfile
)
62 vector
<triangle_s
> tris
;
65 file
.open(filename
,std::ios::in
);
76 readpoint
=(char*) line
.c_str();
77 while (ISWHITE(readpoint
[0]))
87 retval
=sscanf(readpoint
+2,"%f %f %f",&(v
.x
),&(v
.y
),&(v
.z
));
94 retval
=sscanf(readpoint
+3,"%f %f %f",&(vn
.x
),&(vn
.y
),&(vn
.z
));
101 retval
=sscanf(readpoint
+3,"%f %f",&(vt
.x
),&(vt
.y
));
110 vertex firstvtx
,lastvtx
,currvtx
;
116 while (ISWHITE(readpoint
[0]))
118 while (readpoint
[0]!=0)
120 for (int i
=0;i
<3;i
++)
123 while((!ISWHITE(readpoint
[0]))&&(readpoint
[0]!='/'))
127 t
=t
*10+(readpoint
[0]-48);
131 while((ISWHITE(readpoint
[0]))||(readpoint
[0]=='/'))
141 currvtx
.space
=vs
.at(currvertex
[0]-1);
142 currvtx
.texture
=vts
.at(currvertex
[1]-1);
143 currvtx
.normal
=vns
.at(currvertex
[2]-1);
148 temptri
.spaceCoords
[0]=firstvtx
.space
;
149 temptri
.textureCoords
[0]=firstvtx
.texture
;
150 temptri
.normalCoords
[0]=firstvtx
.normal
;
151 temptri
.spaceCoords
[1]=lastvtx
.space
;
152 temptri
.textureCoords
[1]=lastvtx
.texture
;
153 temptri
.normalCoords
[1]=lastvtx
.normal
;
154 temptri
.spaceCoords
[2]=currvtx
.space
;
155 temptri
.textureCoords
[2]=currvtx
.texture
;
156 temptri
.normalCoords
[2]=currvtx
.normal
;
157 temptri
.objectIndex
=currentmtl
;
158 tris
.push_back(temptri
);
164 currentmtl
=addstring(mtls
,string(readpoint
+7));
167 sscanf(readpoint
+6, "%as", mtlfile
);
173 *trinum
= tris
.size();
174 *out_tri
= (triangle_s
*) malloc(sizeof(triangle_s
)*(*trinum
));
175 copy(tris
.begin(),tris
.end(),*out_tri
);
176 *mtlnum
= mtls
.size();
177 *mtllist
= (char**) malloc(sizeof(char*)*(*mtlnum
));
178 for (int i
=0;i
<(*mtlnum
);i
++)
180 (*mtllist
)[i
] = (char*) malloc(sizeof(char)*(mtls
.at(i
).size()+1));
181 strcpy((*mtllist
)[i
],mtls
.at(i
).c_str());
186 int loadMTL (char* filename
, int mtlnum
, char** mtllist
, struct mtl_def_s
** out
)
188 FILE * f
= fopen(filename
, "r");
199 *out
= (struct mtl_def_s
*) malloc(sizeof(struct mtl_def_s
)*mtlnum
);
200 while ( (read
=getline(&line
,&length
,f
))!=-1)
202 sscanf(line
, "%as %d %n", &mtlname
, &texcount
, &pos
);
205 for (int i
=0;i
<mtlnum
;i
++)
207 if (!strcmp(mtlname
,mtllist
[i
]))
212 (*out
)[mtlidx
].texcount
=texcount
;
213 (*out
)[mtlidx
].texnames
=(char**) malloc(sizeof(char*)*texcount
);
214 for (int j
=0;j
<texcount
;j
++)
216 sscanf(line
, "%as%n", &((*out
)[mtlidx
].texnames
[j
]), &pos
);