3 This file is part of libmspgl
4 Copyright © 2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/getopt.h>
9 #include <msp/gl/mesh.h>
10 #include <msp/io/print.h>
11 #include <msp/strings/formatter.h>
18 "unsigned char", "GL_UNSIGNED_BYTE",
19 "unsigned short", "GL_UNSIGNED_SHORT",
20 "unsigned", "GL_UNSIGNED_INT"
23 int main(int argc, char **argv)
26 bool render_func = false;
28 getopt.add_option('p', "prefix", prefix, GetOpt::REQUIRED_ARG);
29 getopt.add_option('r', "render-func", render_func, GetOpt::NO_ARG);
32 const vector<string> &args = getopt.get_args();
35 IO::print(IO::cerr, "Usage: %s [options] <meshfile>\n", argv[0]);
40 DataFile::load(mesh, args[0]);
42 IO::print("/* Exported from %s */\n", args[0]);
44 const GL::VertexArray &array = mesh.get_vertices();
45 const GL::VertexFormat &fmt = array.get_format();
46 IO::print("float %svertex_data[] =\n{", prefix);
47 unsigned stride = fmt.stride();
48 for(unsigned i=0; i<array.size(); ++i)
50 const float *ptr = array[i];
52 for(unsigned j=0; j<stride; ++j)
53 IO::print("%g, ", ptr[j]);
55 IO::print("\n};\n\n");
57 const char **type = 0;
58 if(array.size()<0x100)
60 else if(array.size()<0x10000)
65 IO::print("%s %sindices[] =\n{", type[0], prefix);
66 for(list<GL::Batch>::const_iterator i=mesh.get_batches().begin(); i!=mesh.get_batches().end(); ++i)
68 const vector<unsigned> &indices = i->get_indices();
69 for(unsigned j=0; j<indices.size(); ++j)
73 IO::print("%d, ", indices[j]);
76 IO::print("\n};\n\n");
79 IO::print("void %srender()\n{\n", prefix);
81 IO::print("/*\nTo render the object, #include this file and add the following code:\n");
83 for(const unsigned char *i=fmt.begin(); i!=fmt.end(); ++i)
85 unsigned kind = (*i)>>2;
86 unsigned size = ((*i)&3)+1;
90 IO::print("\tglVertexPointer(%d, GL_FLOAT, %d*sizeof(float), %svertex_data+%d);\n",
91 size, stride, prefix, offset);
92 IO::print("\tglEnableClientState(GL_VERTEX_ARRAY);\n");
95 IO::print("\tglNormalPointer(GL_FLOAT, %d*sizeof(float), %svertex_data+%d);\n",
96 stride, prefix, offset);
97 IO::print("\tglEnableClientState(GL_NORMAL_ARRAY);\n");
100 IO::print("\tglTexCoordPointer(%d, GL_FLOAT, %d*sizeof(float), %svertex_data+%d);\n",
101 size, stride, prefix, offset);
102 IO::print("\tglEnableClientState(GL_TEXTURE_COORD_ARRAY);\n");
105 IO::print("\tglColorPointer(%d, GL_FLOAT, %d*sizeof(float), %svertex_data+%d);\n",
106 size, stride, prefix, offset);
107 IO::print("\tglEnableClientState(GL_COLOR_ARRAY);\n");
110 IO::print("\tglVertexAttribPointer(%d, %d, GL_FLOAT, GL_FALSE, %d*sizeof(float), %svertex_data+%d);\n",
111 kind-4, size, stride, prefix, offset);
112 IO::print("\tglEnableVertexAttribArray(%d);\n", kind-4);
118 for(list<GL::Batch>::const_iterator i=mesh.get_batches().begin(); i!=mesh.get_batches().end(); ++i)
121 switch(i->get_type())
123 case GL_TRIANGLE_STRIP: mode = "GL_TRIANGLE_STRIP"; break;
124 case GL_TRIANGLES: mode = "GL_TRIANGLES"; break;
125 default: mode = format("%d", i->get_type()); break;
127 IO::print("\tglDrawElements(%s, %d, %s, %sindices+%d);\n", mode, i->get_indices().size(), type[1], prefix, offset);
128 offset += i->get_indices().size();
133 IO::print("/* To render this object, #include this file and call %srender() */\n", prefix);