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;
27 bool separate_arrays = false;
29 getopt.add_option('p', "prefix", prefix, GetOpt::REQUIRED_ARG);
30 getopt.add_option('r', "render-func", render_func, GetOpt::NO_ARG);
31 getopt.add_option('s', "separate-arrays", separate_arrays, GetOpt::NO_ARG);
34 const vector<string> &args = getopt.get_args();
37 IO::print(IO::cerr, "Usage: %s [options] <meshfile>\n", argv[0]);
42 mesh.use_buffers(false);
43 DataFile::load(mesh, args[0]);
45 IO::print("/* Exported from %s */\n", args[0]);
47 const GL::VertexArray &array = mesh.get_vertices();
48 const GL::VertexFormat &fmt = array.get_format();
49 unsigned stride = fmt.stride();
53 for(const unsigned char *i=fmt.begin(); i!=fmt.end(); ++i)
55 unsigned kind = (*i)>>2;
56 unsigned size = ((*i)&3)+1;
74 name = format("texcoord%d", kind-3);
76 name = format("attrib%d", kind-11);
80 IO::print("float %s%s_data[] =\n{", prefix, name);
81 for(unsigned j=0; j<array.size(); ++j)
83 const float *ptr = array[j]+offset;
85 for(unsigned k=0; k<size; ++k)
86 IO::print("%g, ", ptr[k]);
88 IO::print("\n};\n\n");
95 IO::print("float %svertex_data[] =\n{", prefix);
96 for(unsigned i=0; i<array.size(); ++i)
98 const float *ptr = array[i];
100 for(unsigned j=0; j<stride; ++j)
101 IO::print("%g, ", ptr[j]);
103 IO::print("\n};\n\n");
106 const char **type = 0;
107 if(array.size()<0x100)
109 else if(array.size()<0x10000)
114 IO::print("%s %sindices[] =\n{", type[0], prefix);
115 for(list<GL::Batch>::const_iterator i=mesh.get_batches().begin(); i!=mesh.get_batches().end(); ++i)
117 unsigned count = i->size();
118 for(unsigned j=0; j<count; ++j)
122 IO::print("%d, ", i->get_index(j));
125 IO::print("\n};\n\n");
128 IO::print("void %srender()\n{\n", prefix);
130 IO::print("/*\nTo render the object, #include this file and add the following code:\n");
132 for(const unsigned char *i=fmt.begin(); i!=fmt.end(); ++i)
134 unsigned kind = (*i)>>2;
135 unsigned size = ((*i)&3)+1;
136 string source = format("%svertex_data+%d", prefix, offset);
141 source = prefix+"vertex_data";
142 IO::print("\tglVertexPointer(%d, GL_FLOAT, %d*sizeof(float), %s);\n",
143 size, stride, source);
144 IO::print("\tglEnableClientState(GL_VERTEX_ARRAY);\n");
148 source = prefix+"normal_data";
149 IO::print("\tglNormalPointer(GL_FLOAT, %d*sizeof(float), %s);\n",
151 IO::print("\tglEnableClientState(GL_NORMAL_ARRAY);\n");
155 source = prefix+"color_data";
156 IO::print("\tglColorPointer(%d, GL_FLOAT, %d*sizeof(float), %s);\n",
157 size, stride, source);
158 IO::print("\tglEnableClientState(GL_COLOR_ARRAY);\n");
162 source = prefix+"texcoord_data";
163 IO::print("\tglTexCoordPointer(%d, GL_FLOAT, %d*sizeof(float), %s);\n",
164 size, stride, source);
165 IO::print("\tglEnableClientState(GL_TEXTURE_COORD_ARRAY);\n");
171 source = format("%stexcoord%d_data", prefix, kind-3);
172 IO::print("\tglClientActiveTexture(GL_TEXTURE%d);\n", kind-3);
173 IO::print("\tglTexCoordPointer(%d, GL_FLOAT, %d*sizeof(float), %s);\n",
174 size, stride, source);
175 IO::print("\tglEnableClientState(GL_TEXTURE_COORD_ARRAY);\n");
180 source = format("%sattrib%d_data", prefix, kind-11);
181 IO::print("\tglVertexAttribPointer(%d, %d, GL_FLOAT, GL_FALSE, %d*sizeof(float), %s);\n",
182 kind-11, size, stride, source);
183 IO::print("\tglEnableVertexAttribArray(%d);\n", kind-11);
190 for(list<GL::Batch>::const_iterator i=mesh.get_batches().begin(); i!=mesh.get_batches().end(); ++i)
193 switch(i->get_type())
195 case GL_TRIANGLE_STRIP: mode = "GL_TRIANGLE_STRIP"; break;
196 case GL_TRIANGLES: mode = "GL_TRIANGLES"; break;
197 default: mode = format("%d", i->get_type()); break;
199 IO::print("\tglDrawElements(%s, %d, %s, %sindices+%d);\n", mode, i->size(), type[1], prefix, offset);
205 IO::print("/* To render this object, #include this file and call %srender() */\n", prefix);