ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/txtprt_import.C
Revision: 1.5
Committed: Sun Oct 3 04:25:17 2004 UTC (22 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +3 -3 lines
Log Message:
foo

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