/lenasys/0.1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/0.1

« back to all changes in this revision

Viewing changes to trunk/ObjConv/objtojson.c

  • Committer: gustav.hartvigsson at gmail
  • Date: 2013-04-02 13:00:31 UTC
  • mfrom: (4.7.8 GammaBear)
  • Revision ID: gustav.hartvigsson@gmail.com-20130402130031-442y89s0cfzmw3r2
Merged stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#include <STDLIB.H>
3
3
#include <STRING.H>
4
4
 
5
 
void parseface(char *facedata,int *varray,int *narray,int *tarray,int position, int hasuv, int hasnormals)
6
 
{
 
5
void parseface(char *facedata,int *varray,int *narray,int *tarray,int position, int hasuv, int hasnormals) {
7
6
        char workstr[10];
8
 
 
9
 
        int readpos=0;
10
 
 
 
7
        int readpos = 0;
11
8
        int i;
12
 
        for(i=0;i<3;i++){
13
 
                int pos=0;
14
 
                
15
 
                while(readpos<strlen(facedata)&&facedata[readpos]!='/'){
16
 
                        workstr[pos]=facedata[readpos];
 
9
        for(i = 0; i < 3; i++) {
 
10
                int pos = 0;
 
11
                while(readpos < strlen(facedata) && facedata[readpos] != '/') {
 
12
                        workstr[pos] = facedata[readpos];
17
13
                        pos++;
18
14
                        readpos++;
19
15
                }
20
 
                workstr[pos]=0;
21
 
 
22
 
                pos=0;
 
16
                workstr[pos] = 0;
 
17
                pos = 0;
23
18
                readpos++;
24
 
                
 
19
 
25
20
                // -1 to compensate for wavefront file starting on 1 and not 0
26
 
                if (i==0){
27
 
                        varray[position]=atoi(workstr)-1;
28
 
                }else if((i==1)&&(hasuv>0)){
29
 
                        tarray[position]=atoi(workstr)-1;               
30
 
                }else if((i==2)&&(hasnormals>0)){
31
 
                        narray[position]=atoi(workstr)-1;
 
21
                if(i == 0) {
 
22
                        varray[position] = atoi(workstr) - 1;
 
23
                } else if((i == 1) && (hasuv > 0)) {
 
24
                        tarray[position] = atoi(workstr) - 1;
 
25
                } else if((i == 2) && (hasnormals > 0)) {
 
26
                        narray[position] = atoi(workstr) - 1;
32
27
                }
33
28
        }
34
29
}
35
30
 
36
 
 
37
 
void loadobj(char *objfilename, char *openobj, char *outputfile)
38
 
{
 
31
void loadobj(char *objfilename, char *openobj, char *outputfile) {
39
32
        FILE * pFile;
40
 
        pFile = fopen (objfilename,"r");
41
 
        
42
 
        
 
33
        pFile = fopen(objfilename, "r");
43
34
        char *buf;
44
 
        buf=(char*)malloc(1024);
 
35
        buf = (char*)malloc(1024);
45
36
        char *result;
46
 
        
 
37
 
47
38
        // foundobj is true if an object with a certain name is found
48
39
        // Set foundobj to true if we are loading all objects
49
 
        int foundobj=0;
50
 
        if (strcmp(openobj,"ALL")==0) foundobj=1;
51
 
 
52
 
        int vertcount=0;        
53
 
        int normalcount=0;      
54
 
        int texturecount=0;
55
 
        int facecount=0;
56
 
 
57
 
        int polycount=0;
58
 
        int quadcount=0;
59
 
        int trianglecount=0;
 
40
        int foundobj = 0;
 
41
        if(strcmp(openobj, "ALL") == 0) {
 
42
                foundobj = 1;
 
43
        }
 
44
        int vertcount = 0;
 
45
        int normalcount = 0;
 
46
        int texturecount = 0;
 
47
        int facecount = 0;
 
48
        int polycount = 0;
 
49
        int quadcount = 0;
 
50
        int trianglecount = 0;
60
51
 
61
52
        // Global Variables for Object
62
53
        float *vertexlist;
64
55
        float *normallist;
65
56
        int *trianglelist;
66
57
        int *quadlist;
67
 
 
68
 
        char *resultp1,*resultp2,*resultp3,*resultp4,*resultp5;
69
 
 
70
 
        if(pFile){
71
 
                printf("Msg: Success opening: %s!\n",objfilename);
72
 
 
73
 
                while ( fgets(buf,1024,pFile) != NULL ){
74
 
                        if(buf[0]=='#'){
75
 
                                // Comment - Do Nothing                         
76
 
                        }else{
77
 
                                result=strtok(buf," \n\t");
78
 
                                if(result!=NULL){
79
 
                                        if(strcmp(result,"v")==0){
80
 
                                                if(foundobj){
81
 
                                                        // Increase vertex counter
82
 
                                                        vertcount++;                                            
 
58
        char *resultp1, *resultp2, *resultp3, *resultp4, *resultp5;
 
59
 
 
60
        if(pFile) {
 
61
                printf("Msg: Success opening: %s!\n", objfilename);
 
62
                while(fgets(buf, 1024, pFile) != NULL) {
 
63
                        if(buf[0] == '#') {
 
64
                                // Comment - Do Nothing
 
65
                        } else {
 
66
                                result = strtok(buf, " \n\t");
 
67
                                if(result != NULL) {
 
68
                                        if(strcmp(result, "v") == 0) {
 
69
                                                if(foundobj) {
 
70
                                                        //Increase vertex counter
 
71
                                                        vertcount++;
83
72
                                                }
84
 
                                        }else if(strcmp(result,"vt")==0){
85
 
                                                if(foundobj){
 
73
                                        } else if(strcmp(result, "vt") == 0) {
 
74
                                                if(foundobj) {
86
75
                                                        // Increase texture coordinate counter
87
 
                                                        texturecount++;                                         
 
76
                                                        texturecount++;
88
77
                                                }
89
 
                                                
90
 
                                        }else if(strcmp(result,"vn")==0){
91
 
                                                if(foundobj){
 
78
                                        } else if(strcmp(result, "vn") == 0) {
 
79
                                                if(foundobj) {
92
80
                                                        // Increase normal counter
93
 
                                                        normalcount++;                                          
94
 
                                                }                                               
95
 
                                        }else if(strcmp(result,"f")==0){
96
 
                                                if(foundobj){
 
81
                                                        normalcount++;
 
82
                                                }
 
83
                                        } else if(strcmp(result, "f") == 0) {
 
84
                                                if(foundobj) {
97
85
                                                        // Increase face counter
98
 
                                                        facecount++;                                            
99
 
                                                        resultp1=strtok(NULL," \n\t");
100
 
                                                        resultp2=strtok(NULL," \n\t");
101
 
                                                        resultp3=strtok(NULL," \n\t");
102
 
                                                        resultp4=strtok(NULL," \n\t");
103
 
                                                        resultp5=strtok(NULL," \n\t");
104
 
                                                        if((resultp4!=NULL)&&(resultp5==NULL)){
 
86
                                                        facecount++;
 
87
                                                        resultp1 = strtok(NULL, " \n\t");
 
88
                                                        resultp2 = strtok(NULL, " \n\t");
 
89
                                                        resultp3 = strtok(NULL, " \n\t");
 
90
                                                        resultp4 = strtok(NULL, " \n\t");
 
91
                                                        resultp5 = strtok(NULL, " \n\t");
 
92
                                                        if((resultp4 != NULL)&&(resultp5 == NULL)) {
105
93
                                                                quadcount++;
106
 
                                                        }else if((resultp3!=NULL)&&(resultp4==NULL)){
 
94
                                                        } else if((resultp3 != NULL)&&(resultp4 == NULL)) {
107
95
                                                                trianglecount++;
108
 
                                                        }else{
 
96
                                                        } else {
109
97
                                                                polycount++;
110
98
                                                        }
111
 
                                                }                                               
112
 
                                        }else if(strcmp(result,"g")==0){
113
 
                                                if(result=strtok(NULL," \n\t")){
114
 
                                                        if(!strcmp(openobj,result)){
115
 
                                                                foundobj=1;
 
99
                                                }
 
100
                                        } else if(strcmp(result, "g") == 0) {
 
101
                                                if(result=strtok(NULL, " \n\t")) {
 
102
                                                        if(!strcmp(openobj, result)) {
 
103
                                                                foundobj = 1;
116
104
                                                        }
117
 
                                                }else{
118
 
                                                        printf("Error: Failed to read object name\n");  
 
105
                                                } else {
 
106
                                                        printf("Error: Failed to read object name\n");
119
107
                                                }
120
 
                                        }else{
121
 
//                                              printf("O: %s \n",buf);
 
108
                                        } else {
 
109
                                                //printf("O: %s \n",buf);
122
110
                                        }
123
111
                                }
124
112
                        }
125
113
                }
126
114
 
127
 
                if (polycount>0){
128
 
                        printf("Warning: Object %s in file %s contains faces that will be ignored, with more than 4 vertices.!\n",objfilename,openobj);         
 
115
                if(polycount > 0) {
 
116
                        printf("Warning: Object %s in file %s contains faces that will be ignored, with more than 4 vertices.!\n", objfilename, openobj);
129
117
                }
130
 
                
131
 
                if(((trianglecount+quadcount)>0)){
132
118
 
 
119
                if(((trianglecount + quadcount) > 0)) {
133
120
                        // Set foundobj to true if we are loading all objects
134
 
                        if (strcmp(openobj,"ALL")) foundobj=1;
 
121
                        if(strcmp(openobj, "ALL")) {
 
122
                                foundobj = 1;
 
123
                        }
135
124
 
136
125
                        // Seek to beginning again
137
 
                        fseek( pFile, 0, SEEK_SET);
 
126
                        fseek(pFile, 0, SEEK_SET);
138
127
 
139
128
                        // Allocate local buffers
140
 
                        float *localvertexlist=(float *)malloc(sizeof(float)*vertcount*3);
141
 
                        float *localnormallist=(float *)malloc(sizeof(float)*normalcount*3);
142
 
                        float *localuvlist=(float *)malloc(sizeof(float)*texturecount*2);
143
 
                        int *localquadlist=(int *)malloc(sizeof(int)*quadcount*4);
144
 
                        int *localquadlistuv=(int *)malloc(sizeof(int)*quadcount*4);
145
 
                        int *localquadlistnormal=(int *)malloc(sizeof(int)*quadcount*4);
146
 
                        int *localtrianglelist=(int *)malloc(sizeof(int)*trianglecount*3);
147
 
                        int *localtrianglelistuv=(int *)malloc(sizeof(int)*trianglecount*3);
148
 
                        int *localtrianglelistnormal=(int *)malloc(sizeof(int)*trianglecount*3);
 
129
                        float *localvertexlist = (float *) malloc (sizeof(float) * vertcount * 3);
 
130
                        float *localnormallist = (float *) malloc (sizeof(float) * normalcount * 3);
 
131
                        float *localuvlist = (float *) malloc (sizeof(float) * texturecount * 2);
 
132
                        int *localquadlist = (int *) malloc (sizeof(int) * quadcount * 4);
 
133
                        int *localquadlistuv = (int *) malloc (sizeof(int) * quadcount * 4);
 
134
                        int *localquadlistnormal = (int *) malloc (sizeof(int) * quadcount * 4);
 
135
                        int *localtrianglelist = (int *) malloc (sizeof(int) * trianglecount * 3);
 
136
                        int *localtrianglelistuv = (int *) malloc (sizeof(int) * trianglecount * 3);
 
137
                        int *localtrianglelistnormal = (int *) malloc (sizeof(int) * trianglecount * 3);
149
138
 
150
139
                        // Reset counters for second pass
151
 
                        trianglecount=0;
152
 
                        quadcount=0;
153
 
                        normalcount=0;
154
 
                        vertcount=0;
155
 
                        texturecount=0;
156
 
 
157
 
                        while ( fgets(buf,1024,pFile) != NULL ){
158
 
 
159
 
                                if(buf[0]=='#'){
160
 
                                        // Comment - Do Nothing                         
161
 
                                }else{
162
 
 
163
 
                                        //-------------------------
164
 
                                        result = strtok(buf," \n\t");
165
 
                                        if(result!=NULL){
166
 
                                                if(strcmp(result,"v")==0){
167
 
                                                        if(foundobj){
168
 
                                                                result=strtok(NULL," \n\t");
169
 
                                                                localvertexlist[(vertcount*3)]=atof(result);
170
 
                                                                result=strtok(NULL," \n\t");
171
 
                                                                localvertexlist[(vertcount*3)+1]=atof(result);
172
 
                                                                result=strtok(NULL," \n\t");
173
 
                                                                localvertexlist[(vertcount*3)+2]=atof(result);
 
140
                        trianglecount = 0;
 
141
                        quadcount = 0;
 
142
                        normalcount = 0;
 
143
                        vertcount = 0;
 
144
                        texturecount = 0;
 
145
 
 
146
                        while(fgets(buf, 1024, pFile) != NULL) {
 
147
                                if(buf[0] == '#') {
 
148
                                        // Comment - Do Nothing
 
149
                                } else {
 
150
                                        result = strtok(buf, " \n\t");
 
151
                                        if(result != NULL) {
 
152
                                                if(strcmp(result, "v") == 0) {
 
153
                                                        if(foundobj) {
 
154
                                                                result = strtok(NULL, " \n\t");
 
155
                                                                localvertexlist[(vertcount * 3)] = atof(result);
 
156
                                                                result = strtok(NULL, " \n\t");
 
157
                                                                localvertexlist[(vertcount * 3) + 1] = atof(result);
 
158
                                                                result = strtok(NULL, " \n\t");
 
159
                                                                localvertexlist[(vertcount * 3) + 2] = atof(result);
174
160
                                                                vertcount++;
175
161
                                                        }
176
 
                                                }else if(strcmp(result,"vt")==0){
177
 
                                                        if(foundobj){
178
 
                                                                result=strtok(NULL," \n\t");
179
 
                                                                localuvlist[(texturecount*2)]=atof(result);
180
 
                                                                result=strtok(NULL," \n\t");
181
 
                                                                localuvlist[(texturecount*2)+1]=atof(result);
182
 
                                                                texturecount++;                                         
183
 
                                                        }                                                       
184
 
                                                }else if(strcmp(result,"vn")==0){
185
 
                                                        if(foundobj){
186
 
                                                                result=strtok(NULL," \n\t");
187
 
                                                                localnormallist[(normalcount*3)]=atof(result);
188
 
                                                                result=strtok(NULL," \n\t");
189
 
                                                                localnormallist[(normalcount*3)+1]=atof(result);
190
 
                                                                result=strtok(NULL," \n\t");
191
 
                                                                localnormallist[(normalcount*3)+2]=atof(result);
 
162
                                                } else if(strcmp(result, "vt") == 0) {
 
163
                                                        if(foundobj) {
 
164
                                                                result = strtok(NULL, " \n\t");
 
165
                                                                localuvlist[(texturecount * 2)] = atof(result);
 
166
                                                                result = strtok(NULL, " \n\t");
 
167
                                                                localuvlist[(texturecount * 2) + 1] = atof(result);
 
168
                                                                texturecount++;
 
169
                                                        }
 
170
                                                } else if(strcmp(result, "vn") == 0) {
 
171
                                                        if(foundobj) {
 
172
                                                                result = strtok(NULL, " \n\t");
 
173
                                                                localnormallist[(normalcount * 3)] = atof(result);
 
174
                                                                result = strtok(NULL, " \n\t");
 
175
                                                                localnormallist[(normalcount * 3) + 1] = atof(result);
 
176
                                                                result = strtok(NULL, " \n\t");
 
177
                                                                localnormallist[(normalcount * 3) + 2] = atof(result);
192
178
                                                                normalcount++;
193
 
                                                        }                                               
194
 
 
195
 
                                                }else if(strcmp(result,"f")==0){
196
 
                                                        if(foundobj){                                                           
197
 
                                                                resultp1=strtok(NULL," \n\t");
198
 
                                                                resultp2=strtok(NULL," \n\t");
199
 
                                                                resultp3=strtok(NULL," \n\t");
200
 
                                                                resultp4=strtok(NULL," \n\t");
201
 
                                                                resultp5=strtok(NULL," \n\t");
202
 
                                                                if((resultp4!=NULL)&&(resultp5==NULL)){
 
179
                                                        }
 
180
                                                } else if(strcmp(result, "f") == 0) {
 
181
                                                        if(foundobj) {
 
182
                                                                resultp1 = strtok(NULL, " \n\t");
 
183
                                                                resultp2 = strtok(NULL, " \n\t");
 
184
                                                                resultp3 = strtok(NULL, " \n\t");
 
185
                                                                resultp4 = strtok(NULL, " \n\t");
 
186
                                                                resultp5 = strtok(NULL, " \n\t");
 
187
                                                                if((resultp4 != NULL)&&(resultp5 == NULL)) {
203
188
                                                                        // Parse all 4 points in quad
204
 
                                                                        parseface(resultp1,localquadlist,localquadlistnormal,localquadlistuv,quadcount*4,texturecount,normalcount);
205
 
                                                                        parseface(resultp2,localquadlist,localquadlistnormal,localquadlistuv,(quadcount*4)+1,texturecount,normalcount);
206
 
                                                                        parseface(resultp3,localquadlist,localquadlistnormal,localquadlistuv,(quadcount*4)+2,texturecount,normalcount);
207
 
                                                                        parseface(resultp4,localquadlist,localquadlistnormal,localquadlistuv,(quadcount*4)+3,texturecount,normalcount);
208
 
 
 
189
                                                                        parseface(resultp1, localquadlist, localquadlistnormal, localquadlistuv, quadcount * 4, texturecount, normalcount);
 
190
                                                                        parseface(resultp2, localquadlist, localquadlistnormal, localquadlistuv, (quadcount * 4) + 1, texturecount, normalcount);
 
191
                                                                        parseface(resultp3, localquadlist, localquadlistnormal, localquadlistuv, (quadcount * 4) + 2, texturecount, normalcount);
 
192
                                                                        parseface(resultp4, localquadlist, localquadlistnormal, localquadlistuv, (quadcount * 4) + 3, texturecount, normalcount);
209
193
                                                                        quadcount++;
210
 
 
211
 
                                                                }else if((resultp3!=NULL)&&(resultp4==NULL)){
212
 
                
 
194
                                                                } else if((resultp3 != NULL)&&(resultp4 == NULL)) {
213
195
                                                                        // Parse all 3 points in triangle
214
 
                                                                        parseface(resultp1,localtrianglelist,localtrianglelistnormal,localtrianglelistuv,trianglecount*3,texturecount,normalcount);
215
 
                                                                        parseface(resultp2,localtrianglelist,localtrianglelistnormal,localtrianglelistuv,(trianglecount*3)+1,texturecount,normalcount);
216
 
                                                                        parseface(resultp3,localtrianglelist,localtrianglelistnormal,localtrianglelistuv,(trianglecount*3)+2,texturecount,normalcount);
217
 
 
218
 
                                                                        trianglecount++;                                                                
 
196
                                                                        parseface(resultp1, localtrianglelist, localtrianglelistnormal, localtrianglelistuv, trianglecount * 3, texturecount, normalcount);
 
197
                                                                        parseface(resultp2, localtrianglelist, localtrianglelistnormal, localtrianglelistuv, (trianglecount * 3) + 1, texturecount, normalcount);
 
198
                                                                        parseface(resultp3, localtrianglelist, localtrianglelistnormal, localtrianglelistuv, (trianglecount * 3) + 2, texturecount, normalcount);
 
199
                                                                        trianglecount++;
219
200
                                                                }
220
 
                                                        }                                               
221
 
                                                }else if(strcmp(result,"g")==0){
222
 
                                                        if(result=strtok(NULL," \n\t")){
223
 
                                                                if(!strcmp(openobj,result)){
224
 
                                                                        foundobj=1;
 
201
                                                        }
 
202
                                                } else if(strcmp(result, "g") == 0) {
 
203
                                                        if(result = strtok(NULL, " \n\t")) {
 
204
                                                                if(!strcmp(openobj, result)) {
 
205
                                                                        foundobj = 1;
225
206
                                                                }
226
207
                                                        }
227
208
                                                }
228
 
                                        }                                       
229
 
                                        //-------------------------
230
 
 
 
209
                                        }
231
210
                                }
232
 
 
233
211
                        }
234
 
 
235
 
                        int i=0;
236
 
 
 
212
                        int i = 0;
237
213
                        // Allocate buffers
238
 
                        vertexlist=(float *)malloc(sizeof(float)*vertcount*3);
239
 
                        normallist=(float *)malloc(sizeof(float)*vertcount*3);
240
 
                        uvlist=(float *)malloc(sizeof(float)*vertcount*2);
241
 
                        trianglelist=(int *)malloc(sizeof(int)*trianglecount*3);
242
 
                        quadlist=(int *)malloc(sizeof(int)*quadcount*4);
243
 
                
 
214
                        vertexlist = (float *) malloc (sizeof(float) * vertcount * 3);
 
215
                        normallist = (float *) malloc (sizeof(float) * vertcount * 3);
 
216
                        uvlist = (float *) malloc (sizeof(float) * vertcount * 2);
 
217
                        trianglelist = (int *) malloc (sizeof(int) * trianglecount * 3);
 
218
                        quadlist = (int *) malloc (sizeof(int) * quadcount * 4);
 
219
 
244
220
                        // Copy data from Per Face model to Per Vertex model first for triangles and then for quads
245
 
 
246
221
                        // If there are triangle normals, copy triangle normal data 
247
 
                        if(normalcount>0){
248
 
                                printf("Msg: Copying Triangle Normals: %i / %i !\n",normalcount,trianglecount);
249
 
                                for(i=0;i<trianglecount*3;i++){
250
 
                                        // Normal X
251
 
                                        normallist[localtrianglelist[i]*3]=localnormallist[localtrianglelistnormal[i]*3];
252
 
                                        // Normal Y
253
 
                                        normallist[(localtrianglelist[i]*3)+1]=localnormallist[(localtrianglelistnormal[i]*3)+1];
254
 
                                        // Normal Z
255
 
                                        normallist[(localtrianglelist[i]*3)+2]=localnormallist[(localtrianglelistnormal[i]*3)+2];
256
 
                                }
257
 
                                for(i=0;i<quadcount*4;i++){
258
 
                                        // Normal X
259
 
                                        normallist[localquadlist[i]*3]=localnormallist[localquadlistnormal[i]*3];
260
 
                                        // Normal Y
261
 
                                        normallist[(localquadlist[i]*3)+1]=localnormallist[(localquadlistnormal[i]*3)+1];
262
 
                                        // Normal Z
263
 
                                        normallist[(localquadlist[i]*3)+2]=localnormallist[(localquadlistnormal[i]*3)+2];
264
 
                                }
265
 
                        }else{
 
222
                        if(normalcount > 0) {
 
223
                                printf("Msg: Copying Triangle Normals: %i / %i !\n", normalcount, trianglecount);
 
224
                                for(i = 0; i < trianglecount * 3; i++) {
 
225
                                        // Normal X
 
226
                                        normallist[localtrianglelist[i] * 3] = localnormallist[localtrianglelistnormal[i] * 3];
 
227
                                        // Normal Y
 
228
                                        normallist[(localtrianglelist[i] * 3 ) + 1 ] = localnormallist[(localtrianglelistnormal[i] * 3) + 1 ];
 
229
                                        // Normal Z
 
230
                                        normallist[(localtrianglelist[i] * 3) + 2] = localnormallist[(localtrianglelistnormal[i] * 3) + 2];
 
231
                                }
 
232
                                for(i = 0; i < quadcount * 4; i++) {
 
233
                                        // Normal X
 
234
                                        normallist[localquadlist[i] * 3] = localnormallist[localquadlistnormal[i] * 3];
 
235
                                        // Normal Y
 
236
                                        normallist[(localquadlist[i] * 3) + 1] = localnormallist[(localquadlistnormal[i] * 3) + 1];
 
237
                                        // Normal Z
 
238
                                        normallist[(localquadlist[i] * 3) + 2] = localnormallist[(localquadlistnormal[i] * 3) + 2];
 
239
                                }
 
240
                        } else {
266
241
                                printf("Msg: No Triangle Normals to Copy\n");
267
242
                        }
268
 
                        
269
 
                        // If there are triangle uv coordinates, copy uv coordinate data 
270
 
                        if(texturecount>0){
271
 
                                printf("Msg: Copying Triangle UV Coordinates: %i / %i !\n",texturecount,trianglecount);
272
 
                                for(i=0;i<trianglecount*3;i++){
273
 
                                        // U Coordinate
274
 
                                        uvlist[localtrianglelist[i]*3]=localuvlist[localtrianglelistuv[i]*3];
275
 
                                        // V Coordinate
276
 
                                        uvlist[(localtrianglelist[i]*3)+1]=localuvlist[(localtrianglelistuv[i]*3)+1];
277
 
                                }
278
 
                                for(i=0;i<quadcount*4;i++){
279
 
                                        // U Coordinate
280
 
                                        uvlist[localquadlist[i]*3]=localuvlist[localquadlistuv[i]*3];
281
 
                                        // V Coordinate
282
 
                                        uvlist[(localquadlist[i]*3)+1]=localuvlist[(localquadlistuv[i]*3)+1];
283
 
                                }
284
 
                        }else{
 
243
 
 
244
                        // If there are triangle uv coordinates, copy uv coordinate data
 
245
                        if(texturecount > 0) {
 
246
                                printf("Msg: Copying Triangle UV Coordinates: %i / %i !\n", texturecount, trianglecount);
 
247
                                for(i = 0; i < trianglecount * 3; i++) {
 
248
                                        // U Coordinate
 
249
                                        uvlist[localtrianglelist[i] * 3] = localuvlist[localtrianglelistuv[i] * 3];
 
250
                                        // V Coordinate
 
251
                                        uvlist[(localtrianglelist[i] * 3) + 1] = localuvlist[(localtrianglelistuv[i] * 3) + 1];
 
252
                                }
 
253
                                for(i = 0;i < quadcount * 4; i++) {
 
254
                                        // U Coordinate
 
255
                                        uvlist[localquadlist[i] * 3] = localuvlist[localquadlistuv[i] * 3];
 
256
                                        // V Coordinate
 
257
                                        uvlist[(localquadlist[i] * 3) + 1] = localuvlist[(localquadlistuv[i] * 3) + 1];
 
258
                                }
 
259
                        } else {
285
260
                                printf("Msg: No Triangle UV Coordinates to Copy\n");
286
261
                        }
287
262
 
288
 
                        // Same for Quads                       
289
 
 
290
 
 
 
263
                        // Same for Quads
291
264
                        printf("--------------------------------\n");
292
265
                        printf("Saving JSON Object file: %s\n", outputfile);
293
266
                        printf("--------------------------------\n");
294
267
 
295
268
                        // Process data to JSON
296
269
                        printf("{\n");
297
 
 
298
270
                        printf("\"vertexPositions\" : [");
299
 
                        for(i=0;i<(vertcount*3);i++){
300
 
                                if(i<((vertcount*3)-1)) {
301
 
                                                printf("%f,",localvertexlist[i]);
302
 
                                }else{
303
 
                                                printf("%f",localvertexlist[i]);
 
271
                        for(i = 0; i < (vertcount * 3); i++) {
 
272
                                if(i < ((vertcount * 3) - 1)) {
 
273
                                        printf("%f,", localvertexlist[i]);
 
274
                                } else {
 
275
                                        printf("%f", localvertexlist[i]);
304
276
                                }
305
277
                        }
306
278
                        printf("],\n");
307
 
 
308
279
                        printf("\"vertexNormals\" : [");
309
 
                        if(normalcount>0){
310
 
                                        for(i=0;i<(vertcount*3);i++){
311
 
                                                if(i<((vertcount*3)-1)) {
312
 
                                                                printf("%f,",normallist[i]);
313
 
                                                }else{
314
 
                                                                printf("%f",normallist[i]);
315
 
                                                }
316
 
                                        }                       
 
280
                        if(normalcount > 0) {
 
281
                                for(i = 0; i < (vertcount * 3); i++) {
 
282
                                        if(i < ((vertcount * 3) - 1)) {
 
283
                                                printf("%f,", normallist[i]);
 
284
                                        } else {
 
285
                                                printf("%f", normallist[i]);
 
286
                                        }
 
287
                                }
317
288
                        }
318
289
                        printf("],\n");
319
 
 
320
290
                        printf("\"vertexTextureCoords\" : [");
321
 
                        if(texturecount>0){
322
 
                                        for(i=0;i<(vertcount*2);i++){
323
 
                                                if(i<((vertcount*2)-1)) {
324
 
                                                                printf("%f,",uvlist[i]);
325
 
                                                }else{
326
 
                                                                printf("%f",uvlist[i]);
327
 
                                                }
328
 
                                        }                                               
 
291
                        if(texturecount > 0) {
 
292
                                for(i = 0; i < (vertcount * 2); i++){
 
293
                                        if(i < ((vertcount * 2) - 1)) {
 
294
                                                printf("%f,", uvlist[i]);
 
295
                                        } else {
 
296
                                                printf("%f", uvlist[i]);
 
297
                                        }
 
298
                                }
329
299
                        }
330
300
                        printf("],\n");
331
 
 
332
301
                        printf("\"indices\" : [");
333
 
                        
 
302
 
334
303
                        // Triangle indices
335
 
                        for(i=0;i<(trianglecount*3);i++){
336
 
                                if(i<((trianglecount*3)-1)) {
337
 
                                                printf("%i,",localtrianglelist[i]);
338
 
                                }else{
339
 
                                                printf("%i",localtrianglelist[i]);
 
304
                        for(i = 0; i < (trianglecount * 3); i++) {
 
305
                                if(i < ((trianglecount * 3) - 1)) {
 
306
                                        printf("%i,", localtrianglelist[i]);
 
307
                                } else {
 
308
                                        printf("%i", localtrianglelist[i]);
340
309
                                }
341
 
                        }               
 
310
                        }
342
311
 
343
312
                        // Quad indices
344
 
                        for(i=0;i<quadcount;i++){                               
345
 
                                if(i==(quadcount-1)) {
346
 
                                                printf("%i,%i,%i,%i,%i,%i,",localquadlist[i],localquadlist[i+1],localquadlist[i+2],localquadlist[i+2],localquadlist[i+3],localquadlist[i]);
347
 
                                }else{
348
 
                                                printf("%i,%i,%i,%i,%i,%i ",localquadlist[i],localquadlist[i+1],localquadlist[i+2],localquadlist[i+2],localquadlist[i+3],localquadlist[i]);
 
313
                        for(i = 0; i < quadcount; i++) {
 
314
                                if(i == (quadcount - 1)) {
 
315
                                        printf("%i,%i,%i,%i,%i,%i,", localquadlist[i], localquadlist[i + 1], localquadlist[i + 2], localquadlist[i + 2], localquadlist[i + 3], localquadlist[i]);
 
316
                                } else {
 
317
                                        printf("%i,%i,%i,%i,%i,%i ", localquadlist[i], localquadlist[i + 1], localquadlist[i + 2], localquadlist[i + 2], localquadlist[i + 3], localquadlist[i]);
349
318
                                }
350
 
                        }               
 
319
                        }
351
320
 
352
321
                        printf("]\n");
353
 
 
354
322
                        printf("}\n");
355
323
 
356
324
                        // Free buffers
363
331
                        free(localquadlist);
364
332
                        free(localquadlistuv);
365
333
                        free(localquadlistnormal);
366
 
 
367
 
                }else{
 
334
                } else {
368
335
                        // Multigon
369
 
                        printf("Error: Object did not contain any triangles or quads!\n");              
370
 
                }
371
 
 
372
 
                fclose ( pFile );
373
 
 
374
 
                if(!foundobj){
375
 
                        printf("Error: Failed to find object %s!\n",openobj);           
376
 
                }else{
377
 
                        printf("Msg: Sucessful Parsing of Object %s in File %s\nMsg: %i vertices %i normals %i texture coordinates %i faces (%i triangles %i quads %i polys)\n",openobj,objfilename,vertcount,normalcount,texturecount,facecount,trianglecount,quadcount,polycount);
378
 
                }
379
 
                
380
 
        }else{
381
 
                printf("Error: Failed to open %s!\n",objfilename);      
 
336
                        printf("Error: Object did not contain any triangles or quads!\n");
 
337
                }
 
338
 
 
339
                fclose(pFile);
 
340
                if(!foundobj) {
 
341
                        printf("Error: Failed to find object %s!\n", openobj);
 
342
                } else {
 
343
                        printf("Msg: Sucessful Parsing of Object %s in File %s\nMsg: %i vertices %i normals %i texture coordinates %i faces (%i triangles %i quads %i polys)\n", openobj, objfilename, vertcount, normalcount, texturecount, facecount, trianglecount, quadcount, polycount);
 
344
                }
 
345
 
 
346
        } else {
 
347
                printf("Error: Failed to open %s!\n", objfilename);
382
348
        }
383
 
 
384
349
        free(buf);
385
350
}
386
351
 
387
 
int main (int argc, char** argv) {
388
 
 
389
 
        if (argc < 3) {
390
 
                        printf("ERROR: not enough arguments. must have LOADFILE and SAVEFILE and if you want to process only a specific object, the name of object to process\n");
391
 
                        return 1;
 
352
int main(int argc, char** argv) {
 
353
        if(argc < 3) {
 
354
                printf("ERROR: not enough arguments. must have LOADFILE and SAVEFILE and if you want to process only a specific object, the name of object to process\n");
 
355
                return 1;
392
356
        }
393
357
 
394
358
        printf("--------------------------------\n");
395
359
        printf("Loading Wavefront Object file: %s\n", argv[1]);
396
360
        printf("--------------------------------\n");
397
361
 
398
 
        if(argc>3){
399
 
                        printf("Msg: Loading Object: %s\n", argv[3]);
400
 
                        loadobj(argv[1],argv[3],argv[2]);
401
 
        }else{
402
 
                        printf("Msg: Loading all Objects\n");   
403
 
                        loadobj(argv[1],"ALL",argv[2]);
 
362
        if(argc > 3) {
 
363
                printf("Msg: Loading Object: %s\n", argv[3]);
 
364
                loadobj(argv[1], argv[3], argv[2]);
 
365
        } else {
 
366
                printf("Msg: Loading all Objects\n");
 
367
                loadobj(argv[1], "ALL", argv[2]);
404
368
        }
405
 
 
406
369
        return 0;
407
 
 
408
 
}
 
 
b'\\ No newline at end of file'
 
370
}