mirror of
				https://notabug.org/scuti/lib3ddevil1
				synced 2025-05-31 14:11:42 +05:30 
			
		
		
		
	Merge branch 'a/writemesh' of scuti/lib3ddevil1 into master
This commit is contained in:
		| @@ -44,10 +44,23 @@ void write(const char *filename, | ||||
|         written = fwrite(t, sizeof(unsigned char), size, out); | ||||
|         fclose(out); | ||||
|         if (written == 0) { | ||||
|             perror("texture write error"); | ||||
|             perror("write error"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
| void append(const char *filename, const char *t, unsigned size) { | ||||
|     if (filename == NULL) { | ||||
|         return; | ||||
|     } | ||||
|     unsigned int written = 0; | ||||
|     FILE *out = fopen(filename, "ab"); | ||||
|     if (out != NULL) { | ||||
|         written = fwrite(t, sizeof(unsigned char), size, out); | ||||
|         fclose(out); | ||||
|         if (written == 0) { | ||||
|             perror("write error"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,6 +1,61 @@ | ||||
| #include "common.h" | ||||
| #include "devil1geo.h" | ||||
|  | ||||
| void writemesh(const struct MeshHeader *mh,  | ||||
|                const struct Mesh *m,  | ||||
|                const char *filename,  | ||||
|                unsigned int i) { | ||||
|     char *fn = (char*)malloc(strlen(filename) + 3 + 4); | ||||
|     sprintf(fn, "%s_%d.msh", filename, i); | ||||
|     struct Header h = { | ||||
|         1, | ||||
|         0, | ||||
|         0, | ||||
|         0, | ||||
|         0xCCCCCCCC, | ||||
|         0 | ||||
|     }; | ||||
|     // mesh header offset batches need to be changed for compability with parsing. | ||||
|     struct MeshHeader newmh = *(mh); | ||||
|     newmh.offsetBatches = sizeof(struct Header) + sizeof(struct MeshHeader); | ||||
|     write(fn, (char*)(&h),         sizeof(struct Header)); | ||||
|     append(fn, (char*)(&newmh),    sizeof(struct MeshHeader)); | ||||
|  | ||||
|  | ||||
|  | ||||
|     // batch data the same treatment. | ||||
|     struct BatchData * newBatches = malloc(newmh.numBatch * sizeof(struct BatchData)); | ||||
|     uint64_t previousBatch = 0; | ||||
|     for (int i = 0; i < newmh.numBatch; i++) { | ||||
|       newBatches[i] = (m -> b) -> bd[i]; | ||||
|       unsigned int nVertices = m -> b -> bd[i].numVertex; | ||||
|       if (previousBatch == 0) { | ||||
|         newBatches[i].offsetPositions = newmh.offsetBatches + ( newmh.numBatch * sizeof(struct BatchData)); | ||||
|       } | ||||
|       else { | ||||
|         newBatches[i].offsetPositions = previousBatch; | ||||
|       } | ||||
|       newBatches[i].offsetNormals     = newBatches[i].offsetPositions   + (sizeof(struct Coordinate)  * nVertices); | ||||
|       newBatches[i].offsetUVs         = newBatches[i].offsetNormals     + (sizeof(struct Coordinate)  * nVertices); | ||||
|       newBatches[i].offsetBoneIndexes = newBatches[i].offsetUVs         + (sizeof(struct UVs)         * nVertices); | ||||
|       newBatches[i].offsetBoneWeights = newBatches[i].offsetBoneIndexes + (sizeof(struct BoneIndexes) * nVertices); | ||||
|       append(fn, (char*)(&newBatches[i]), sizeof(struct BatchData)); | ||||
|       previousBatch                   = newBatches[i].offsetBoneWeights + (sizeof(struct BoneWeights) * nVertices);; | ||||
|     } | ||||
|  | ||||
|     for (int i = 0; i < newmh.numBatch; i++) { | ||||
|       unsigned int nVertices = m -> b -> bd[i].numVertex; | ||||
|       append(fn, (char*)(m -> b[i].vd.positions), sizeof(struct Coordinate)  * nVertices); | ||||
|       append(fn, (char*)(m -> b[i].vd.normals),   sizeof(struct Coordinate)  * nVertices); | ||||
|       append(fn, (char*)(m -> b[i].vd.u),         sizeof(struct UVs)         * nVertices); | ||||
|       append(fn, (char*)(m -> b[i].vd.bi),        sizeof(struct BoneIndexes) * nVertices); | ||||
|       append(fn, (char*)(m -> b[i].vd.bw),        sizeof(struct BoneWeights) * nVertices); | ||||
|     } | ||||
|  | ||||
|     free(newBatches); | ||||
|     free(fn); | ||||
| } | ||||
|  | ||||
| void extractmeshes(const char *filedata, | ||||
|                    unsigned int filesize, | ||||
|                    const char *filename) { | ||||
| @@ -17,7 +72,7 @@ void extractmeshes(const char *filedata, | ||||
|         m.b = (struct Batch*)malloc(sizeof(struct Batch) * (mh -> numBatch)); | ||||
|         if (m.b != NULL) { | ||||
|             DEVIL1GEO.getmesh(&m, i, filedata); | ||||
|             // do something with mesh e.g write to file. | ||||
|             writemesh(mh, &m, filename, i); | ||||
|             free(m.b); | ||||
|         } | ||||
|     } // end for | ||||
| @@ -28,8 +83,6 @@ int main(int argc, char ** argv) { | ||||
|     char *f = argv[1]; | ||||
|     unsigned int bufsize = 0; | ||||
|     char *buffer         = loadfile(f, &bufsize); | ||||
| //    unpackpld(buffer, bufsize, f); | ||||
| //    exporttextures(buffer, bufsize, f); | ||||
|     extractmeshes(buffer, bufsize, f); | ||||
|     free(buffer); | ||||
|     return 0; | ||||
|   | ||||
| @@ -113,7 +113,7 @@ static bool getmesh(struct Mesh *m, | ||||
|         for (j = 0; j < mh -> numBatch; j++) { | ||||
|             unsigned int offset = mh->offsetBatches + j * sizeof(struct BatchData); | ||||
|             getmeshbatch(&b, offset, filedata); | ||||
|             printmeshbatch(&b); | ||||
| //            printmeshbatch(&b); | ||||
|             m -> b[j] = b; | ||||
|         } | ||||
|         done = true; | ||||
| @@ -121,3 +121,4 @@ static bool getmesh(struct Mesh *m, | ||||
|     return done; | ||||
| } | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user