17 #include "eggVertexPool.h"
18 #include "eggVertex.h"
19 #include "eggPolygon.h"
20 #include "eggPrimitive.h"
21 #include "eggGroupNode.h"
22 #include "eggPolysetMaker.h"
24 #include "string_utils.h"
36 set_program_brief(
"convert .egg geometry into compilable C tables");
37 set_program_description
38 (
"This program reads Egg files and outputs code that will almost "
39 "compile as a C or C++ program. You get to define the data structures "
40 "for the program after the fact; the program only generates tables "
41 "of vertices and polygons.");
49 "Generate a table of vertex positions.",
50 &EggToC::dispatch_none, &_vertices);
54 "Generate a table of UV's per each vertex.",
55 &EggToC::dispatch_none, &_uvs);
59 "Generate a table of normals per each vertex.",
60 &EggToC::dispatch_none, &_vertex_normals);
64 "Generate a table of colors per each vertex.",
65 &EggToC::dispatch_none, &_vertex_colors);
69 "Generate a table of polygons that index into the above tables.",
70 &EggToC::dispatch_none, &_polygons);
74 "Generate a table of normals per each polygon.",
75 &EggToC::dispatch_none, &_polygon_normals);
79 "Generate a table of colors per each polygon.",
80 &EggToC::dispatch_none, &_polygon_colors);
84 "Output only triangles by subdividing higher-order polygons.",
85 &EggToC::dispatch_none, &_triangulate_polygons);
95 nout <<
"Removing invalid primitives.\n";
96 int num_removed = _data->remove_invalid_primitives(
true);
97 nout <<
" (" << num_removed <<
" removed.)\n";
99 if (_triangulate_polygons) {
100 nout <<
"Triangulating polygons.\n";
101 int num_produced = _data->triangulate_polygons(~0);
102 nout <<
" (" << num_produced <<
" triangles produced.)\n";
105 _data->apply_texmats();
106 _data->flatten_transforms();
107 _data->remove_unused_vertices(
true);
116 <<
" * Generated by:\n"
121 _next_vpool_index = 0;
133 if (node->
is_of_type(EggVertexPool::get_class_type())) {
136 }
else if (node->
is_of_type(EggBin::get_class_type())) {
137 write_bin(DCAST(
EggBin, node));
139 }
else if (node->
is_of_type(EggGroupNode::get_class_type())) {
142 EggGroupNode::const_iterator ci;
143 for (ci = group->begin(); ci != group->end(); ++ci) {
160 out <<
"/* Vertex pool index " << _next_vpool_index
161 <<
": " << vpool->get_name() <<
" */\n";
162 _vertex_pools[vpool] = _next_vpool_index;
166 out <<
"/* Vertex definition for " << vpool->get_name() <<
" */\n"
167 <<
"vertex vertices_" << vpool->get_name() <<
"[" << highest_index
169 for (i = 0; i < highest_index; i++) {
172 out <<
" vertex(), /* " << i <<
" */\n";
177 out <<
" vertex(" << p[0] <<
"), /* " << i <<
" */\n";
181 out <<
" vertex(" << p[0] <<
", " << p[1]
182 <<
"), /* " << i <<
" */\n";
186 out <<
" vertex(" << p[0] <<
", " << p[1] <<
", " << p[2]
187 <<
"), /* " << i <<
" */\n";
191 out <<
" vertex(" << p[0] <<
", " << p[1] <<
", " << p[2]
192 <<
", " << p[3] <<
"), /* " << i <<
" */\n";
196 out <<
" vertex(), /* error */\n";
204 out <<
"/* UV's for " << vpool->get_name() <<
" */\n"
205 <<
"uv uvs_" << vpool->get_name() <<
"[" << highest_index
207 for (i = 0; i < highest_index; i++) {
210 out <<
" uv(), /* " << i <<
" */\n";
213 out <<
" uv(" << uv[0] <<
", " << uv[1]
214 <<
"), /* " << i <<
" */\n";
220 if (_vertex_normals) {
221 out <<
"/* Vertex normals for " << vpool->get_name() <<
" */\n"
222 <<
"normal normals_" << vpool->get_name() <<
"[" << highest_index
224 for (i = 0; i < highest_index; i++) {
226 if (vert == (
EggVertex *)NULL || !vert->has_normal()) {
227 out <<
" normal(), /* " << i <<
" */\n";
230 out <<
" normal(" << n[0] <<
", " << n[1] <<
", " << n[2]
231 <<
"), /* " << i <<
" */\n";
237 if (_vertex_colors) {
238 out <<
"/* Vertex colors for " << vpool->get_name() <<
" */\n"
239 <<
"color colors_" << vpool->get_name() <<
"[" << highest_index
241 for (i = 0; i < highest_index; i++) {
243 if (vert == (
EggVertex *)NULL || !vert->has_color()) {
244 out <<
" color(), /* " << i <<
" */\n";
247 out <<
" color(" << c[0] <<
", " << c[1] <<
", " << c[2]
248 <<
", " << c[3] <<
"), /* " << i <<
" */\n";
264 string bin_name = bin->get_name();
265 if (bin_name.empty()) {
266 bin_name = format_string(_next_bin_index);
270 out <<
"/* Polygon group " << bin_name <<
" */\n";
272 size_t num_children = bin->size();
275 out <<
"/* Polygon definitions for " << bin_name <<
" */\n";
276 string prim_type =
"polygon";
277 if (_triangulate_polygons) {
278 prim_type =
"triangle";
281 out << prim_type <<
" polys_" << bin_name <<
"[" << num_children
284 if (_triangulate_polygons) {
285 out <<
" /* vpool index, vertex0, vertex1, vertex2 */\n";
287 out <<
" /* vpool index, num vertices, vertex0, vertex1, vertex2, ... */\n";
290 EggGroupNode::const_iterator ci;
291 size_t prim_index = 0;
292 for (ci = bin->begin(); ci != bin->end(); ++ci) {
294 if (!child->
is_of_type(EggPrimitive::get_class_type())) {
295 out <<
" " << prim_type <<
"(), /* error */\n";
299 int vpool_index = -1;
300 VertexPools::const_iterator pi = _vertex_pools.find(vpool);
301 if (pi != _vertex_pools.end()) {
302 vpool_index = (*pi).second;
305 out <<
" " << prim_type <<
"(" << vpool_index;
306 if (!_triangulate_polygons) {
307 out <<
", " << prim->size();
309 EggPrimitive::const_iterator vi;
310 for (vi = prim->begin(); vi != prim->end(); ++vi) {
314 out <<
"), /* " << prim_index <<
" */\n";
321 if (_polygon_normals) {
323 out <<
"/* Polygon normals for " << bin_name <<
" */\n";
324 out <<
"normal polys_" << bin_name <<
"[" << num_children
327 EggGroupNode::const_iterator ci;
328 size_t prim_index = 0;
329 for (ci = bin->begin(); ci != bin->end(); ++ci) {
331 if (!child->
is_of_type(EggPrimitive::get_class_type())) {
332 out <<
" normal(), /* error */\n";
335 if (!prim->has_normal()) {
336 out <<
" normal(), /* " << prim_index <<
" */\n";
339 out <<
" normal(" << n[0] <<
", " << n[1] <<
", " << n[2]
340 <<
"), /* " << prim_index <<
" */\n";
348 if (_polygon_colors) {
350 out <<
"/* Polygon colors for " << bin_name <<
" */\n";
351 out <<
"color polys_" << bin_name <<
"[" << num_children
354 EggGroupNode::const_iterator ci;
355 size_t prim_index = 0;
356 for (ci = bin->begin(); ci != bin->end(); ++ci) {
358 if (!child->
is_of_type(EggPrimitive::get_class_type())) {
359 out <<
" color(), /* error */\n";
362 if (!prim->has_color()) {
363 out <<
" color(), /* " << prim_index <<
" */\n";
366 out <<
" color(" << c[0] <<
", " << c[1] <<
", " << c[2]
367 <<
", " << c[3] <<
"), /* " << prim_index <<
" */\n";
377 int main(
int argc,
char *argv[]) {
A base class for any of a number of kinds of geometry primitives: polygons, point lights...
void set_properties(int properties)
Sets the set of properties that determines which polygons are allowed to be grouped together into a s...
LTexCoordd get_uv() const
Returns the unnamed UV coordinate pair on the vertex.
EggVertex * get_vertex(int index) const
Returns the vertex in the pool with the indicated index number, or NULL if no vertices have that inde...
LColor get_color() const
Returns the color set on this particular attribute.
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
A base class for nodes in the hierarchy that are not leaf nodes.
bool has_uv() const
Returns true if the vertex has an unnamed UV coordinate pair, false otherwise.
This is a four-component point in space.
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
This is a two-component point in space.
LPoint4d get_pos4() const
This is always valid, regardless of the value of get_num_dimensions.
Any one-, two-, three-, or four-component vertex, possibly with attributes such as a normal...
int get_highest_index() const
Returns the highest index number used by any vertex in the pool (except forward references).
This is the base class for all three-component vectors and points.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
ostream & get_output()
Returns an output stream that corresponds to the user's intended egg file output–either stdout...
string get_exec_command() const
Returns the command that invoked this program, as a shell-friendly string, suitable for pasting into ...
This is the general base class for a file-converter program that reads some model file format and gen...
A base class for things that may be directly added into the egg hierarchy.
int make_bins(EggGroupNode *root_group)
The main entry point to EggBinMaker.
A specialization on EggBinMaker for making polysets that share the same basic rendering characteristi...
int get_num_dimensions() const
Returns the number of dimensions the vertex uses.
A collection of vertices.
int get_index() const
Returns the index number of the vertex within its pool.
A type of group node that holds related subnodes.
EggVertexPool * get_pool() const
Returns the vertex pool associated with the vertices of the primitive, or NULL if the primitive has n...