1 // txt2cmod.cpp
2 //
3 // Copyright (C) 2007, Chris Laurel <claurel@shatters.net>
4 //
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.
9 //
10 // Convert a text file of vertices and facets from the Itokawa shape model
11 // into an ASCII cmod for Celestia.
12 //
13 // These files have the form:
14 //
15 // <vertex count>
16 //
17 // <vertex id> <x> <y> <z>
18 // ...
19 //
20 // <face count>
21 //
22 // <face id> <vertex0> <vertex1> <vertex2>
23 // ...
24 //
25 // face and vertex ids are 1-based
26 // vertex positions are floating point values
27 //
28 // The resulting cmod file should be processed by cmodfix to generate
29 // normals and convert to the binary cmod format. I use this command line:
30 //
31 // cmodfix --normals --smooth 90 --weld --binary <input file> <output file>
32 
33 #include <iostream>
34 
35 using namespace std;
36 
37 
main(int argc,char * argv[])38 int main(int argc, char* argv[])
39 {
40     // Write the cmod header
41     cout << "#celmodel__ascii\n\n";
42     cout << "material\n";
43     cout << "diffuse 1 1 1\n";
44     cout << "end_material\n";
45     cout << "\n";
46 
47     cout << "mesh\n";
48     cout << "vertexdesc\n";
49     cout << "position f3\n";
50     cout << "end_vertexdesc\n";
51     cout << "\n";
52 
53     // Get the vertex count
54     unsigned int vertexCount;
55     cin >> vertexCount;
56     cout << "vertices " << vertexCount << "\n";
57 
58     // Read the vertices
59     for (unsigned int vertex = 0; vertex < vertexCount; vertex++)
60     {
61         unsigned int v;
62         float x, y, z;
63         cin >> v >> x >> y >> z;
64         if (!cin.good())
65         {
66             cerr << "Error reading txt model at vertex " << vertex+1 << "\n";
67             exit(1);
68         }
69 
70         cout << x << " " << y << " " << z << "\n";
71     }
72 
73     cout << "\n";
74 
75     // Get the face count
76     unsigned int faceCount;
77     cin >> faceCount;
78     cout << "trilist 0 " << faceCount * 3 << "\n";
79 
80     // Read the faces
81     for (unsigned int face = 0; face < faceCount; face++)
82     {
83         unsigned int f;
84         unsigned int v0, v1, v2;
85         cin >> f >> v0 >> v1 >> v2;
86         if (!cin.good())
87         {
88             cerr << "Error reading txt model at face " << face + 1 << "\n";
89             exit(1);
90         }
91 
92         // vertex indices in txt file are one based.
93         cout << v0 - 1 << " " << v1 - 1 << " " << v2 - 1 << "\n";
94     }
95 
96     cout << "\n";
97     cout << "end_mesh\n";
98 
99     exit(0);
100 
101     return 0;
102 }
103