Browse Source

basic obj pasring

develop
Robin Thoni 7 years ago
parent
commit
4b673bdd87

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

5
 #include <QDebug>
5
 #include <QDebug>
6
 
6
 
7
 // Also, we must add the parser's header where are defined the tokens.
7
 // Also, we must add the parser's header where are defined the tokens.
8
+#include "utils/vector3d.h"
8
 #include "parser-wavefront-obj.h"
9
 #include "parser-wavefront-obj.h"
9
 
10
 
10
 #define TOKEN(type) \
11
 #define TOKEN(type) \
57
 "/"     { return TOKEN(SLASH);  }
58
 "/"     { return TOKEN(SLASH);  }
58
 
59
 
59
  /* Number */
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
  /* String */
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
 /*#include <QtGui>*/
2
 /*#include <QtGui>*/
3
 #include <QVariant>
3
 #include <QVariant>
4
 #include <QDebug>
4
 #include <QDebug>
5
+#include "utils/vector3d.h"
5
 #include "utils/wavefrontobj.h"
6
 #include "utils/wavefrontobj.h"
6
 #include "lexer-wavefront-obj.h"
7
 #include "lexer-wavefront-obj.h"
7
 
8
 
13
 
14
 
14
 // Bison uses the yyerror function for informing us when a parsing error has
15
 // Bison uses the yyerror function for informing us when a parsing error has
15
 // occurred.
16
 // occurred.
16
-void wavefront_objerror(const char *s);
17
+void wavefront_objerror(WaveFrontObjData* data, const char *s);
17
 %}
18
 %}
18
 
19
 
19
 %define api.prefix {wavefront_obj}
20
 %define api.prefix {wavefront_obj}
20
 %define api.token.prefix {TOK_}
21
 %define api.token.prefix {TOK_}
21
 
22
 
23
+%parse-param {struct WaveFrontObjData* wavefrontData}
24
+
22
 // Here we define our custom variable types.
25
 // Here we define our custom variable types.
23
 // Custom types must be of fixed size.
26
 // Custom types must be of fixed size.
24
 %union {
27
 %union {
25
-    float number;
28
+    double number;
29
+    int integer;
26
     char* string;
30
     char* string;
27
-    void* wavefront;
31
+    Vector3D* vector3d;
32
+    QList<int>* integers;
28
 }
33
 }
29
 
34
 
30
 // Define the terminal expression types.
35
 // Define the terminal expression types.
31
-%token <float_number> NUMBER
36
+%token <number> NUMBER
32
 %token <string> STRING
37
 %token <string> STRING
33
 %token SLASH
38
 %token SLASH
34
 %token MTLLIB
39
 %token MTLLIB
41
 %token F
46
 %token F
42
 %token S
47
 %token S
43
 
48
 
49
+%destructor { free($$); } <string>
50
+%destructor { delete $$; } <vector3d>
51
+%destructor { delete $$; } <integers>
52
+
44
 // Define the non-terminal expression types.
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
 stmt_list: {}
65
 stmt_list: {}
61
 stmt: mtllib {}
68
 stmt: mtllib {}
62
             | usemtl {}
69
             | usemtl {}
63
             | named_object {}
70
             | named_object {}
64
-            | vertex {}
71
+            | vertex { wavefrontData->_vertexes.append(*$1); delete $1; }
65
             | texture_coordinate {}
72
             | texture_coordinate {}
66
             | vertex_normal {}
73
             | vertex_normal {}
67
             | vertex_space {}
74
             | vertex_space {}
68
-            | face {}
75
+            | face { wavefrontData->_faces.append(*$1); delete $1; }
69
             | smooth {};
76
             | smooth {};
70
 
77
 
71
 mtllib: MTLLIB STRING {};
78
 mtllib: MTLLIB STRING {};
74
 
81
 
75
 named_object: O STRING {};
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
 texture_coordinate: VT NUMBER NUMBER {}
87
 texture_coordinate: VT NUMBER NUMBER {}
81
             | VT NUMBER NUMBER NUMBER {};
88
             | VT NUMBER NUMBER NUMBER {};
84
 
91
 
85
 vertex_space: VP NUMBER NUMBER NUMBER {};
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
 smooth: S NUMBER {}
104
 smooth: S NUMBER {}
98
             | S STRING {};
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
 
23
 
24
 bool WaveFrontObj::load(QIODevice &device)
24
 bool WaveFrontObj::load(QIODevice &device)
25
 {
25
 {
26
+    _error.clear();
26
     QString file = device.readAll();
27
     QString file = device.readAll();
27
 
28
 
28
     YY_BUFFER_STATE bufferState = wavefront_obj_scan_string(file.toUtf8().constData());
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
     wavefront_obj_delete_buffer(bufferState);
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
     return res == 0;
41
     return res == 0;
33
 }
42
 }

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

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

Loading…
Cancel
Save