ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/txtprt_import.C
Revision: 1.13
Committed: Sat Oct 16 23:23:21 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +6 -6 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <vector>
5 #include "txtprt_import.h"
6 #include "entity.h"
7 #include "util.h"
8
9 using namespace std;
10
11 // FIXME: In case of failed loading a memory leak can happen
12
13 geometry *
14 txtprt_parser::read (string filename)
15 {
16 double tmpn;
17 string tmps;
18 int i, o;
19
20 geometry_container *objs = new geometry_container;
21
22 ifs.open (filename.c_str (), ifstream::in);
23
24 while (ifs.good ())
25 {
26 char c[1024];
27 ifs.getline (c, 1024);
28 line_data += (string) c + "\n";
29 }
30
31 ifs.close ();
32
33 double nobj;
34
35 match_section ('O', nobj);
36
37 for (o = 0; o < nobj; o++)
38 {
39 double c1, c2, c3, c4, l0, l1, l2;
40 match_section ('M', tmps);
41 match_section ('L', tmps);
42 match_number (l0);
43 match_space ();
44 match_number (l1);
45 match_space ();
46 match_number (l2);
47 match_nl ();
48 match_section ('T', tmps);
49
50 simple_material *m = new simple_material;
51
52 match_number (c1);
53 match_space ();
54 match_number (c2);
55 match_space ();
56 match_number (c3);
57 match_space ();
58 match_number (c4);
59 match_nl ();
60 m->diffuse = colour (c1, c2, c3, c4);
61 match_number (c1);
62 match_space ();
63 match_number (c2);
64 match_space ();
65 match_number (c3);
66 match_space ();
67 match_number (c4);
68 match_nl ();
69 m->specular = colour (c1, c2, c3, c4);
70 match_number (c1);
71 match_space ();
72 match_number (c2);
73 match_space ();
74 match_number (c3);
75 match_space ();
76 match_number (c4);
77 match_nl ();
78 m->emission = colour (c1, c2, c3, c4);
79 match_number (c1);
80 match_nl ();
81 m->shininess = (GLfloat) c1;
82
83 match_section ('V', tmpn);
84 if (tmpn <= 0)
85 throw txtprt_i_exception ("No Vertices?!");
86
87 vector < vertex_t2f_n3f_v3f > verts;
88 for (i = 0; i < tmpn; i++)
89 {
90 double x, y, z;
91 match_number (x);
92 match_space ();
93 match_number (y);
94 match_space ();
95 match_number (z);
96 match_nl ();
97 verts.
98 push_back (vertex_t2f_n3f_v3f
99 (point (l0 + x, l1 + y, l2 + z), vec3 (0, 0, 1)));
100 // FIXME: propably vertex coloUrs ?
101 }
102 match_section ('N', tmpn);
103 if (tmpn > verts.size ())
104 {
105 throw txtprt_i_exception ("Too many vertex normals!!");
106 }
107 for (i = 0; i < tmpn; i++)
108 {
109 double x, y, z;
110 match_number (x);
111 match_space ();
112 match_number (y);
113 match_space ();
114 match_number (z);
115 match_nl ();
116 verts[i].n = vec3 (x, y, z);
117 }
118
119 match_section ('A', tmpn);
120
121 vector < vertex_t2f_n3f_v3f > vd;
122 for (i = 0; i < tmpn; i++)
123 {
124 double a, b, c;
125 match_number (a);
126 if (verts.size () < a)
127 throw txtprt_i_exception ("Vertex a out of range!");
128 match_space ();
129 match_number (b);
130 if (verts.size () < b)
131 throw txtprt_i_exception ("Vertex b out of range!");
132 match_space ();
133 match_number (c);
134 if (verts.size () < c)
135 throw txtprt_i_exception ("Vertex c out of range!");
136 match_nl ();
137 double smooth_fl;
138 match_number (smooth_fl);
139 match_nl ();
140 double u, v;
141 match_number (u);
142 match_space ();
143 match_number (v);
144 match_nl ();
145 verts[(int) a].t = tex2 (1 - u, 1 - v);
146 match_number (u);
147 match_space ();
148 match_number (v);
149 match_nl ();
150 verts[(int) b].t = tex2 (1 - u, 1 - v);
151 match_number (u);
152 match_space ();
153 match_number (v);
154 match_nl ();
155 verts[(int) c].t = tex2 (1 - u, 1 - v);
156
157 if (!smooth_fl)
158 {
159 point ap = verts[(int) a].p;
160 point bp = verts[(int) b].p;
161 point cp = verts[(int) c].p;
162 vec3 nv = normalize (cross (vec3 (cp.x - bp.x, cp.y - bp.y, cp.z - bp.z),
163 vec3 (ap.x - bp.x, ap.y - bp.y, ap.z - bp.z)));
164
165 verts[(int) a].n = nv;
166 verts[(int) b].n = nv;
167 verts[(int) c].n = nv;
168 }
169
170 vd.push_back (verts[(int) a]);
171 vd.push_back (verts[(int) b]);
172 vd.push_back (verts[(int) c]);
173 }
174
175 geometry_triangles *tri = new geometry_triangles;
176 tri->m = m;
177 tri->set (vd);
178 objs->add (tri);
179 }
180
181 return objs;
182 }
183