Panda3D

objToEggConverter.h
Go to the documentation of this file.
1 /**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file objToEggConverter.h
10 * @author drose
11 * @date 2010-12-07
12 */
13
14 #ifndef OBJTOEGGCONVERTER_H
15 #define OBJTOEGGCONVERTER_H
16
17 #include "pandatoolbase.h"
18
19 #include "somethingToEggConverter.h"
20 #include "eggVertexPool.h"
21 #include "eggGroup.h"
22 #include "geomVertexData.h"
23 #include "geomVertexWriter.h"
24 #include "geomPrimitive.h"
25 #include "geomNode.h"
26 #include "pandaNode.h"
27 #include "pvector.h"
28 #include "epvector.h"
29
30 /**
31 * Convert an Obj file to egg data.
32 */
33 class ObjToEggConverter : public SomethingToEggConverter {
34 public:
35 ObjToEggConverter();
36 ObjToEggConverter(const ObjToEggConverter &copy);
37 ~ObjToEggConverter();
38
39 virtual SomethingToEggConverter *make_copy();
40
41 virtual std::string get_name() const;
42 virtual std::string get_extension() const;
43 virtual bool supports_compressed() const;
44 virtual bool supports_convert_to_node(const LoaderOptions &options) const;
45
46 virtual bool convert_file(const Filename &filename);
47 virtual PT(PandaNode) convert_to_node(const LoaderOptions &options, const Filename &filename);
48
49 protected:
50 bool process(const Filename &filename);
51 bool process_line(const std::string &line);
52 bool process_ref_plane_res(const std::string &line);
53
54 bool process_v(vector_string &words);
55 bool process_vt(vector_string &words);
56 bool process_xvt(vector_string &words);
57 bool process_xvc(vector_string &words);
58 bool process_vn(vector_string &words);
59 bool process_f(vector_string &words);
60 bool process_g(vector_string &words);
61
62 EggVertex *get_face_vertex(const std::string &face_reference);
63 void generate_egg_points();
64
65 bool process_node(const Filename &filename);
66 bool process_line_node(const std::string &line);
67
68 bool process_f_node(vector_string &words);
69 bool process_g_node(vector_string &words);
70
71 void generate_points();
72 int add_synth_normal(const LVecBase3d &normal);
73
74 // Read from the obj file.
75 int _line_number;
76 typedef epvector<LVecBase4d> Vec4Table;
77 typedef epvector<LVecBase3d> Vec3Table;
78 typedef epvector<LVecBase2d> Vec2Table;
79 typedef pmap<LVecBase3d, int> UniqueVec3Table;
80
81 Vec4Table _v_table;
82 Vec3Table _vn_table, _rgb_table;
83 Vec3Table _vt_table;
84 Vec2Table _xvt_table;
85 Vec3Table _synth_vn_table;
86 UniqueVec3Table _unique_synth_vn_table;
87 LVecBase2d _ref_plane_res;
88 bool _v4_given, _vt3_given;
89 bool _f_given;
90
91 pset<std::string> _ignored_tags;
92
93 // Structures filled when creating an egg file.
94 PT(EggVertexPool) _vpool;
95 PT(EggGroup) _root_group;
96 EggGroup *_current_group;
97
98 // Structures filled when creating a PandaNode directly.
99 PT(PandaNode) _root_node;
100
101 class VertexEntry {
102 public:
103 VertexEntry();
104 VertexEntry(const ObjToEggConverter *converter, const std::string &obj_vertex);
105
106 INLINE bool operator < (const VertexEntry &other) const;
107 INLINE bool operator == (const VertexEntry &other) const;
108 INLINE bool matches_except_normal(const VertexEntry &other) const;
109
110 // The 1-based vertex, texcoord, and normal index numbers appearing in the
111 // obj file for this vertex. 0 if the index number is not given.
112 int _vi, _vti, _vni;
113
114 // The 1-based index number to the synthesized normal, if needed.
115 int _synth_vni;
116 };
117 typedef pmap<VertexEntry, int> UniqueVertexEntries;
118 typedef pvector<VertexEntry> VertexEntries;
119
120 class VertexData {
121 public:
122 VertexData(PandaNode *parent, const std::string &name);
123
124 int add_vertex(const ObjToEggConverter *converter, const VertexEntry &entry);
125 void add_triangle(const ObjToEggConverter *converter, const VertexEntry &v0,
126 const VertexEntry &v1, const VertexEntry &v2,
127 int synth_vni);
128 void close_geom(const ObjToEggConverter *converter);
129
130 PT(PandaNode) _parent;
131 std::string _name;
132 PT(GeomNode) _geom_node;
133
134 PT(GeomPrimitive) _prim;
135 VertexEntries _entries;
136 UniqueVertexEntries _unique_entries;
137
138 bool _v4_given, _vt3_given;
139 bool _vt_given, _rgb_given, _vn_given;
140 };
141
142 VertexData *_current_vertex_data;
143
144 friend class VertexData;
145 };
146
147 #include "objToEggConverter.I"
148
149 #endif
EggGroup
The main glue of the egg hierarchy, this corresponds to the <Group>, <Instance>, and <Joint> type nod...
Definition: eggGroup.h:34
EggVertexPool
A collection of vertices.
Definition: eggVertexPool.h:41
EggVertex
Any one-, two-, three-, or four-component vertex, possibly with attributes such as a normal.
Definition: eggVertex.h:39
Filename
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
GeomNode
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:34
GeomPrimitive
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:56
LoaderOptions
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:23
ObjToEggConverter
Convert an Obj file to egg data.
Definition: objToEggConverter.h:33
ObjToEggConverter::supports_convert_to_node
virtual bool supports_convert_to_node(const LoaderOptions &options) const
Returns true if this converter can directly convert the model type to internal Panda memory structure...
Definition: objToEggConverter.cxx:97
ObjToEggConverter::convert_file
virtual bool convert_file(const Filename &filename)
Handles the reading of the input file and converting it to egg.
Definition: objToEggConverter.cxx:106
ObjToEggConverter::supports_compressed
virtual bool supports_compressed() const
Returns true if this file type can transparently load compressed files (with a .pz extension),...
Definition: objToEggConverter.cxx:85
ObjToEggConverter::get_name
virtual std::string get_name() const
Returns the English name of the file type this converter supports.
Definition: objToEggConverter.cxx:68
ObjToEggConverter::get_extension
virtual std::string get_extension() const
Returns the common extension of the file type this converter supports.
Definition: objToEggConverter.cxx:76
ObjToEggConverter::make_copy
virtual SomethingToEggConverter * make_copy()
Allocates and returns a new copy of the converter.
Definition: objToEggConverter.cxx:59
PandaNode
A basic node of the scene graph or data graph.
Definition: pandaNode.h:65
SomethingToEggConverter
This is a base class for a family of converter classes that manage a conversion from some file type t...
Definition: somethingToEggConverter.h:38
pmap< LVecBase3d, int >
pset< std::string >
pvector< VertexEntry >
eggGroup.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
eggVertexPool.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
epvector.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geomNode.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geomPrimitive.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geomVertexData.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geomVertexWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
objToEggConverter.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandaNode.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandatoolbase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pvector.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
somethingToEggConverter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.