ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/txtprt_import.C
Revision: 1.10
Committed: Wed Oct 6 09:41:48 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.9: +6 -12 lines
Log Message:
Texture mapping

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 entity* txtprt_parser::read (string filename) {
14 double tmpn;
15 string tmps;
16 int i, o;
17
18 entity *e = new entity;
19 entity_container *ec = new entity_container;
20 e->set (ec);
21
22 ifs.open (filename.c_str(), ifstream::in);
23 while (ifs.good ()) {
24 char c[1024];
25 ifs.getline (c, 1024);
26 line_data += (string) c + "\n";
27 }
28 ifs.close();
29
30 double objs;
31
32 match_section ('O', objs);
33
34 for (o = 0; o < objs; o++) {
35 double c1, c2, c3, c4, l0, l1, l2;
36 match_section ('M', tmps);
37 match_section ('L', tmps);
38 match_number (l0); match_space (); match_number (l1); match_space (); match_number (l2); match_nl ();
39 match_section ('T', tmps);
40
41 material m;
42 match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl ();
43 m.diffuse = colour (c1, c2, c3, c4);
44 match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl ();
45 m.specular = colour (c1, c2, c3, c4);
46 match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl ();
47 m.emission = colour (c1, c2, c3, c4);
48 match_number (c1); match_nl ();
49 m.shininess = (GLfloat) c1;
50
51 match_section ('V', tmpn);
52 if (tmpn <= 0)
53 throw txtprt_i_exception ("No Vertices?!");
54
55 vector<vertex2d> verts;
56 for (i = 0; i < tmpn; i++) {
57 double x, y, z;
58 match_number (x);
59 match_space ();
60 match_number (y);
61 match_space ();
62 match_number (z);
63 match_nl ();
64 verts.push_back (vertex2d (point (l0 + x, l1 + y, l2 + z), vec3 (0, 0, 1)));
65 // FIXME: propably vertex coloUrs ?
66 }
67 match_section ('N', tmpn);
68 if (tmpn > verts.size()) { throw txtprt_i_exception ("Too many vertex normals!!"); }
69 for (i = 0; i < tmpn; i++) {
70 double x, y, z;
71 match_number (x);
72 match_space ();
73 match_number (y);
74 match_space ();
75 match_number (z);
76 match_nl ();
77 verts[i].n = vec3 (x, y, z);
78 }
79 match_section ('A', tmpn);
80
81 vector<vertex2d> vd;
82 for (i = 0; i < tmpn; i++) {
83 double a, b, c;
84 match_number (a);
85 if (verts.size () < a)
86 throw txtprt_i_exception ("Vertex a out of range!");
87 match_space ();
88 match_number (b);
89 if (verts.size () < b)
90 throw txtprt_i_exception ("Vertex b out of range!");
91 match_space ();
92 match_number (c);
93 if (verts.size () < c)
94 throw txtprt_i_exception ("Vertex c out of range!");
95 match_nl ();
96 double smooth_fl;
97 match_number (smooth_fl); match_nl ();
98 double u, v;
99 match_number (u); match_space (); match_number (v); match_nl (); verts[(int)a].t = texc (1 - u,1 - v);
100 match_number (u); match_space (); match_number (v); match_nl (); verts[(int)b].t = texc (1 - u,1 - v);
101 match_number (u); match_space (); match_number (v); match_nl (); verts[(int)c].t = texc (1 - u,1 - v);
102
103 if (!smooth_fl) {
104 point ap = verts[(int)a].p;
105 point bp = verts[(int)b].p;
106 point cp = verts[(int)c].p;
107 vec3 nv = normalize (cross (vec3 (cp.x - bp.x, cp.y - bp.y, cp.z - bp.z),
108 vec3 (ap.x - bp.x, ap.y - bp.y, ap.z - bp.z)));
109
110 verts[(int)a].n = nv;
111 verts[(int)b].n = nv;
112 verts[(int)c].n = nv;
113 }
114 vd.push_back (verts[(int)a]);
115 vd.push_back (verts[(int)b]);
116 vd.push_back (verts[(int)c]);
117 }
118
119 entity_triangles *tri = new entity_triangles;
120 tri->m = m;
121 tri->set (vd);
122 ec->add (tri);
123 }
124 return e;
125 }