OpenCores
URL https://opencores.org/ocsvn/orsoc_graphics_accelerator/orsoc_graphics_accelerator/trunk

Subversion Repositories orsoc_graphics_accelerator

[/] [orsoc_graphics_accelerator/] [trunk/] [sw/] [utils/] [meshmaker.cpp] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 maiden
/*
2
Per Lenander 2012
3
 
4
Meshconv tool:
5
-------------
6
Converts obj files into a .h file
7
*/
8
 
9
#include <sstream>
10
#include <iostream>
11
#include <fstream>
12
#include <cstdlib>
13
#include <cstring>
14
#include <vector>
15
#include <cstdlib>
16
#include <ctime>
17
 
18
#define GFX_PIXEL_16(R,G,B) (((R >> 3) << 11) | ((G >> 2) << 5) | (B>>3))
19
 
20
using namespace std;
21
 
22
class Point
23
{
24
public:
25
        Point(float X = 0, float Y = 0, float Z = 0)
26
                : x(X), y(Y), z(Z)
27
        {}
28
 
29
        float x, y, z;
30
};
31
 
32
class Face
33
{
34
public:
35
    Face(int P1 = 0, int P2 = 0, int P3 = 0,
36
         unsigned int Color1 = 0, unsigned int Color2 = 0, unsigned int Color3 = 0)
37
        : v1(P1), v2(P2), v3(P3), color1(Color1), color2(Color2), color3(Color3)
38
        {
39
                vt1 = vt2 = vt3 = 0;
40
                vn1 = vn2 = vn3 = 0;
41
        }
42
 
43
        void ParseLine(stringstream& ss)
44
        {
45
                // Parse vertex, texture and normal coordinates for p1
46
                ss >> v1;
47
                if(ss.peek() == '/')
48
                {
49
                        ss.ignore();
50
 
51
                        if(ss.peek() != '/')
52
                                ss >> vt1;
53
 
54
                        if(ss.peek() == '/')
55
                        {
56
                                ss.ignore();
57
                                ss >> vn1;
58
                        }
59
                }
60
 
61
                // Parse vertex, texture and normal coordinates for p2
62
                ss >> v2;
63
                if(ss.peek() == '/')
64
                {
65
                        ss.ignore();
66
 
67
                        if(ss.peek() != '/')
68
                                ss >> vt2;
69
 
70
                        if(ss.peek() == '/')
71
                        {
72
                                ss.ignore();
73
                                ss >> vn2;
74
                        }
75
                }
76
 
77
                // Parse vertex, texture and normal coordinates for p3
78
                ss >> v3;
79
                if(ss.peek() == '/')
80
                {
81
                        ss.ignore();
82
 
83
                        if(ss.peek() != '/')
84
                                ss >> vt3;
85
 
86
                        if(ss.peek() == '/')
87
                        {
88
                                ss.ignore();
89
                                ss >> vn3;
90
                        }
91
                }
92
 
93
                unsigned char r, g, b;
94
                // Temporary
95
                r = rand() % 256;
96
                g = rand() % 256;
97
                b = rand() % 256;
98
 
99
        color1 = GFX_PIXEL_16(r,g,b); // TODO: color
100
 
101
        r = rand() % 256;
102
        g = rand() % 256;
103
        b = rand() % 256;
104
 
105
        color2 = GFX_PIXEL_16(r,g,b); // TODO: color
106
 
107
        r = rand() % 256;
108
        g = rand() % 256;
109
        b = rand() % 256;
110
 
111
        color3 = GFX_PIXEL_16(r,g,b); // TODO: color
112
 
113
                // .obj uses 1 indexed, reduce
114
                v1--;
115
                v2--;
116
                v3--;
117
 
118
                vt1--;
119
                vt2--;
120
                vt3--;
121
 
122
                vn1--;
123
                vn2--;
124
                vn3--;
125
        }
126
 
127
    int v1, v2, v3;    // points
128
    int vt1, vt2, vt3; // texture coords (uvs)
129
    int vn1, vn2, vn3; // normals
130
    unsigned int color1, color2, color3;
131
};
132
 
133
int main ( int argc, char** argv )
134
{
135
        if( argc < 2 )
136
        {
137
        cout << "Usage: " << argv[0] << " filename.obj [bpp=16]" << endl;
138
                return 1;
139
        }
140
 
141
        srand(time(NULL));
142
 
143
        string          filename(argv[1]);
144
 
145
        ifstream input;
146
        input.open(filename.c_str(), ifstream::in);
147
 
148
        if( !input )
149
        {
150
                cout << "Couldn't load " << filename << "!" << endl;
151
                return 1;
152
        }
153
 
154
    int outputBpp = 16;
155
        if(argc >= 3)
156
        {
157
                stringstream ss(argv[2]);
158
                ss >> outputBpp;
159
 
160
                if(outputBpp != 8 && outputBpp != 16 && outputBpp != 32)
161
                {
162
                        cout << "Mode: '" << argv[2] << "' not supported, choose between 8, 16 and 32" << endl;
163
                        return 1;
164
                }
165
        }
166
 
167
        // Load mesh into internal structure
168
        vector<Face> faces;
169
        vector<Point> verts;
170
        vector<Point> uvs;
171
        vector<Point> normals;
172
 
173
        // Parse obj file
174
        while(input)
175
        {
176
                char buf[1024];
177
                input.getline(buf, 1024);
178
                string s(buf);
179
 
180
                if(s.empty())
181
                        continue;
182
 
183
                stringstream ss(s);
184
                string type;
185
                ss >> type;
186
 
187
                if(type[0] == '#')
188
                        continue;
189
                else if(type == "v") // Vertex coordinates: x y z [w] (discarded)
190
                {
191
                        Point p;
192
                        ss >> p.x >> p.y >> p.z;
193
                        verts.push_back(p);
194
                }
195
                else if(type == "vt") // Texture coordinates: u [v] [w]
196
                {
197
                        Point t;
198
                        ss >> t.x >> t.y >> t.z;
199
                        uvs.push_back(t);
200
                }
201
                else if(type == "vn") // Normals: x y z, does not have to be unit
202
                {
203
                        Point n;
204
                        ss >> n.x >> n.y >> n.z;
205
                        normals.push_back(n);
206
                }
207
                else if(type == "f") // Face
208
                {
209
                        Face f;
210
                        f.ParseLine(ss);
211
                        faces.push_back(f);
212
                }
213
        }
214
 
215
        // Done loading
216
        input.close();
217
 
218
        /* Open an output file */
219
        filename        += ".h";
220
        ofstream        output(filename.c_str(),ofstream::out);
221
        if( !output )
222
        {
223
                cout << "Couldn't open output file!" << endl;
224
                return 1;
225
        }
226
 
227
        // ------------------------- //
228
        // Write mesh to output file //
229
        // ------------------------- //
230
        filename = filename.substr(0, filename.find('.'));
231
 
232
        output << "/* Mesh definition */" << endl << endl;
233
        output << "#ifndef " << filename << "_H" << endl;
234
        output << "#define " << filename << "_H" << endl;
235
 
236
    output << "#include \"orgfx_3d.h\"" << endl << endl;
237
 
238
    // Init function
239
    output << "// Call this function to get an initialized mesh:" << endl;
240
    output << "orgfx_mesh init_" << filename << "_mesh();" << endl << endl;
241
 
242
    // define the number of faces, verts and uvs
243
    output << "unsigned int " << filename << "_nverts = " << verts.size() << ";" << endl << endl;
244
    output << "unsigned int " << filename << "_nuvs   = " << uvs.size() << ";" << endl << endl;
245
    output << "unsigned int " << filename << "_nfaces = " << faces.size() << ";" << endl << endl;
246
 
247
    // define all verts
248
    output << "orgfx_point3 " << filename << "_verts[] = {" << endl;
249
    for(unsigned int i = 0; i < verts.size(); i++)
250
        output << "{" << verts[i].x << ", " << verts[i].y << ", " << verts[i].z << "}," << endl;
251
    output << "};" << endl << endl;
252
 
253
    // define all uvs
254
    output << "orgfx_point2 " << filename << "_uvs[] = {" << endl;
255
    for(unsigned int i = 0; i < uvs.size(); i++)
256
        output << "{" << uvs[i].x << ", " << uvs[i].y << "}," << endl;
257
    output << "};" << endl << endl;
258
 
259
    // define all faces
260
    output << "orgfx_face " << filename << "_faces[] = {" << endl;
261
    for(unsigned int i = 0; i < faces.size(); i++)
262
    {
263
        const Face& f = faces[i];
264
        output << "{" << f.v1 << "u, " << f.v2 << "u, " << f.v3 << "u, "
265
               << f.vt1 << "u, " << f.vt2 << "u, " << f.vt3 << "u, "
266
               << f.color1 << "u, " << f.color2 << "u, " << f.color3 << "u}," << endl;
267
    }
268
    output << "};" << endl << endl;
269
 
270
    output << "orgfx_mesh init_" << filename << "_mesh()" << endl;
271
    output << "{" << endl;
272
    output << "  return orgfx3d_make_mesh(" << filename << "_faces,"  << endl
273
           << "                           " << filename << "_nfaces," << endl
274
           << "                           " << filename << "_verts,"  << endl
275
           << "                           " << filename << "_nverts," << endl
276
           << "                           " << filename << "_uvs,"    << endl
277
           << "                           " << filename << "_nuvs);"  << endl;
278
    output << "}" << endl << endl;
279
 
280
        output << "#endif // " << filename << "_H" << endl;
281
 
282
        // done!
283
        output.close();
284
 
285
        return 0;
286
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.