ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/txtprt_import.C
Revision: 1.15
Committed: Sun Oct 31 19:16:28 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +13 -5 lines
Log Message:
*** empty log message ***

File Contents

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