Browse Source

basic obj pasring

develop
Robin Thoni 8 years ago
parent
commit
4b673bdd87

+ 3
- 2
UGameEngine/utils/lexer-wavefront-obj.l View File

@@ -5,6 +5,7 @@
5 5
 #include <QDebug>
6 6
 
7 7
 // Also, we must add the parser's header where are defined the tokens.
8
+#include "utils/vector3d.h"
8 9
 #include "parser-wavefront-obj.h"
9 10
 
10 11
 #define TOKEN(type) \
@@ -57,10 +58,10 @@
57 58
 "/"     { return TOKEN(SLASH);  }
58 59
 
59 60
  /* Number */
60
--?[0-9]+(.[0-9]+)?     { return TOKEN(NUMBER); }
61
+-?[0-9]+(.[0-9]+)?     { wavefront_objlval.number=atof(yytext); return TOKEN(NUMBER); }
61 62
 
62 63
  /* String */
63
-[^\n\r\t ]+  { return TOKEN(STRING);  }
64
+[^\n\r\t ]+  { wavefront_objlval.string = strcpy((char*)malloc(strlen(yytext)), yytext); return TOKEN(STRING);  }
64 65
 
65 66
 
66 67
 %%

+ 32
- 25
UGameEngine/utils/parser-wavefront-obj.y View File

@@ -2,6 +2,7 @@
2 2
 /*#include <QtGui>*/
3 3
 #include <QVariant>
4 4
 #include <QDebug>
5
+#include "utils/vector3d.h"
5 6
 #include "utils/wavefrontobj.h"
6 7
 #include "lexer-wavefront-obj.h"
7 8
 
@@ -13,22 +14,26 @@ extern int wavefront_objlex(void);
13 14
 
14 15
 // Bison uses the yyerror function for informing us when a parsing error has
15 16
 // occurred.
16
-void wavefront_objerror(const char *s);
17
+void wavefront_objerror(WaveFrontObjData* data, const char *s);
17 18
 %}
18 19
 
19 20
 %define api.prefix {wavefront_obj}
20 21
 %define api.token.prefix {TOK_}
21 22
 
23
+%parse-param {struct WaveFrontObjData* wavefrontData}
24
+
22 25
 // Here we define our custom variable types.
23 26
 // Custom types must be of fixed size.
24 27
 %union {
25
-    float number;
28
+    double number;
29
+    int integer;
26 30
     char* string;
27
-    void* wavefront;
31
+    Vector3D* vector3d;
32
+    QList<int>* integers;
28 33
 }
29 34
 
30 35
 // Define the terminal expression types.
31
-%token <float_number> NUMBER
36
+%token <number> NUMBER
32 37
 %token <string> STRING
33 38
 %token SLASH
34 39
 %token MTLLIB
@@ -41,18 +46,20 @@ void wavefront_objerror(const char *s);
41 46
 %token F
42 47
 %token S
43 48
 
49
+%destructor { free($$); } <string>
50
+%destructor { delete $$; } <vector3d>
51
+%destructor { delete $$; } <integers>
52
+
44 53
 // Define the non-terminal expression types.
45
-%type <wavefront> wavefront_obj
54
+%type <vector3d> vertex
55
+%type <integer>   face_vertex
56
+%type <integers>  face_vertex_list
57
+%type <integers>  face
46 58
 
47
-%%
48 59
 
49
-start: wavefront_obj {
50
-/*    qDebug() << $1;*/
51
-};
60
+%%
52 61
 
53
-wavefront_obj: stmt_list {
54
-                  }
55
-                ;
62
+wavefront_obj: stmt_list {};
56 63
 
57 64
 
58 65
 stmt_list: {}
@@ -61,11 +68,11 @@ stmt_list: {}
61 68
 stmt: mtllib {}
62 69
             | usemtl {}
63 70
             | named_object {}
64
-            | vertex {}
71
+            | vertex { wavefrontData->_vertexes.append(*$1); delete $1; }
65 72
             | texture_coordinate {}
66 73
             | vertex_normal {}
67 74
             | vertex_space {}
68
-            | face {}
75
+            | face { wavefrontData->_faces.append(*$1); delete $1; }
69 76
             | smooth {};
70 77
 
71 78
 mtllib: MTLLIB STRING {};
@@ -74,8 +81,8 @@ usemtl: USEMTL STRING {};
74 81
 
75 82
 named_object: O STRING {};
76 83
 
77
-vertex: V NUMBER NUMBER NUMBER {}
78
-            | V NUMBER NUMBER NUMBER NUMBER {};
84
+vertex: V NUMBER NUMBER NUMBER { $$ = new Vector3D($2, $3, $4); }
85
+            | V NUMBER NUMBER NUMBER NUMBER { $$ = new Vector3D($2, $3, $4); };
79 86
 
80 87
 texture_coordinate: VT NUMBER NUMBER {}
81 88
             | VT NUMBER NUMBER NUMBER {};
@@ -84,22 +91,22 @@ vertex_normal: VN NUMBER NUMBER NUMBER {};
84 91
 
85 92
 vertex_space: VP NUMBER NUMBER NUMBER {};
86 93
 
87
-face: F face_vertex_list {};
94
+face: F face_vertex_list { $$ = $2; };
88 95
 
89
-face_vertex_list: {}
90
-            | face_vertex_list face_vertex {};
96
+face_vertex_list: { $$ = new QList<int>(); }
97
+            | face_vertex_list face_vertex { $1->append($2); };
91 98
 
92
-face_vertex: NUMBER {}
93
-            | NUMBER SLASH NUMBER {}
94
-            | NUMBER SLASH NUMBER SLASH NUMBER {}
95
-            | NUMBER SLASH SLASH NUMBER {};
99
+face_vertex: NUMBER { $$ = $1; }
100
+            | NUMBER SLASH NUMBER { $$ = $1; }
101
+            | NUMBER SLASH NUMBER SLASH NUMBER { $$ = $1; }
102
+            | NUMBER SLASH SLASH NUMBER { $$ = $1; };
96 103
 
97 104
 smooth: S NUMBER {}
98 105
             | S STRING {};
99 106
 
100 107
 %%
101 108
 
102
-void wavefront_objerror(const char *s)
109
+void wavefront_objerror(WaveFrontObjData* data, const char *s)
103 110
 {
104
-    qDebug() << "parse error:" << s << "at line" << wavefront_objlineno << "at" << wavefront_objtext;
111
+    data->_error = QString(s) + " at line " + QString::number(wavefront_objlineno) + " at " + QString(wavefront_objtext);
105 112
 }

+ 10
- 1
UGameEngine/utils/wavefrontobj.cpp View File

@@ -23,11 +23,20 @@ bool WaveFrontObj::openFile(const QString &filename)
23 23
 
24 24
 bool WaveFrontObj::load(QIODevice &device)
25 25
 {
26
+    _error.clear();
26 27
     QString file = device.readAll();
27 28
 
28 29
     YY_BUFFER_STATE bufferState = wavefront_obj_scan_string(file.toUtf8().constData());
29
-    int res = wavefront_objparse();
30
+    WaveFrontObjData data;
31
+    int res = wavefront_objparse(&data);
30 32
     wavefront_obj_delete_buffer(bufferState);
33
+    if (res) {
34
+        _error = "Parse error: " + data._error;
35
+        qDebug() << _error;
36
+    }
37
+    else {
38
+        qDebug() << "faces:" << data._faces.size() << "vertexes" << data._vertexes.size();
39
+    }
31 40
 
32 41
     return res == 0;
33 42
 }

+ 4
- 0
UGameEngine/utils/wavefrontobj.h View File

@@ -3,6 +3,7 @@
3 3
 
4 4
 #include <QObject>
5 5
 #include <QIODevice>
6
+#include "utils/vector3d.h"
6 7
 
7 8
 class WaveFrontObj : public QObject
8 9
 {
@@ -23,6 +24,9 @@ private:
23 24
 
24 25
 struct WaveFrontObjData
25 26
 {
27
+    QList<Vector3D> _vertexes;
28
+    QList<QList<int> > _faces;
29
+    QString _error;
26 30
 };
27 31
 
28 32
 #endif // WAVEFRONTOBJ_H

Loading…
Cancel
Save