ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/txtprt_import.C
Revision: 1.11
Committed: Thu Oct 7 23:39:43 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +147 -90 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     material m;
51 root 1.11 match_number (c1);
52     match_space ();
53     match_number (c2);
54     match_space ();
55     match_number (c3);
56     match_space ();
57     match_number (c4);
58     match_nl ();
59 root 1.6 m.diffuse = colour (c1, c2, c3, c4);
60 root 1.11 match_number (c1);
61     match_space ();
62     match_number (c2);
63     match_space ();
64     match_number (c3);
65     match_space ();
66     match_number (c4);
67     match_nl ();
68 root 1.6 m.specular = colour (c1, c2, c3, c4);
69 root 1.11 match_number (c1);
70     match_space ();
71     match_number (c2);
72     match_space ();
73     match_number (c3);
74     match_space ();
75     match_number (c4);
76     match_nl ();
77 root 1.6 m.emission = colour (c1, c2, c3, c4);
78 root 1.11 match_number (c1);
79     match_nl ();
80 root 1.6 m.shininess = (GLfloat) c1;
81 root 1.11
82 root 1.6 match_section ('V', tmpn);
83 root 1.11 if (tmpn <= 0)
84     throw txtprt_i_exception ("No Vertices?!");
85 root 1.6
86 root 1.11 vector < vertex2d > verts;
87     for (i = 0; i < tmpn; i++)
88     {
89     double x, y, z;
90     match_number (x);
91     match_space ();
92     match_number (y);
93     match_space ();
94     match_number (z);
95     match_nl ();
96     verts.
97     push_back (vertex2d
98     (point (l0 + x, l1 + y, l2 + z), vec3 (0, 0, 1)));
99     // FIXME: propably vertex coloUrs ?
100     }
101 root 1.6 match_section ('N', tmpn);
102 root 1.11 if (tmpn > verts.size ())
103     {
104     throw txtprt_i_exception ("Too many vertex normals!!");
105     }
106     for (i = 0; i < tmpn; i++)
107     {
108     double x, y, z;
109     match_number (x);
110     match_space ();
111     match_number (y);
112     match_space ();
113     match_number (z);
114     match_nl ();
115     verts[i].n = vec3 (x, y, z);
116     }
117    
118     match_section ('A', tmpn);
119    
120     vector < vertex2d > vd;
121     for (i = 0; i < tmpn; i++)
122     {
123     double a, b, c;
124     match_number (a);
125     if (verts.size () < a)
126     throw txtprt_i_exception ("Vertex a out of range!");
127     match_space ();
128     match_number (b);
129     if (verts.size () < b)
130     throw txtprt_i_exception ("Vertex b out of range!");
131     match_space ();
132     match_number (c);
133     if (verts.size () < c)
134     throw txtprt_i_exception ("Vertex c out of range!");
135     match_nl ();
136     double smooth_fl;
137     match_number (smooth_fl);
138     match_nl ();
139     double u, v;
140     match_number (u);
141     match_space ();
142     match_number (v);
143     match_nl ();
144     verts[(int) a].t = texc (1 - u, 1 - v);
145     match_number (u);
146     match_space ();
147     match_number (v);
148     match_nl ();
149     verts[(int) b].t = texc (1 - u, 1 - v);
150     match_number (u);
151     match_space ();
152     match_number (v);
153     match_nl ();
154     verts[(int) c].t = texc (1 - u, 1 - v);
155    
156     if (!smooth_fl)
157     {
158     point ap = verts[(int) a].p;
159     point bp = verts[(int) b].p;
160     point cp = verts[(int) c].p;
161     vec3 nv = normalize (cross (vec3 (cp.x - bp.x, cp.y - bp.y, cp.z - bp.z),
162     vec3 (ap.x - bp.x, ap.y - bp.y, ap.z - bp.z)));
163    
164     verts[(int) a].n = nv;
165     verts[(int) b].n = nv;
166     verts[(int) c].n = nv;
167     }
168    
169     vd.push_back (verts[(int) a]);
170     vd.push_back (verts[(int) b]);
171     vd.push_back (verts[(int) c]);
172     }
173    
174     geometry_triangles *tri = new geometry_triangles;
175 root 1.9 tri->m = m;
176     tri->set (vd);
177 root 1.11 objs->add (tri);
178     }
179    
180     return objs;
181 root 1.1 }
182 root 1.11