Browse Source

begin obj lexer/parser

develop
Robin Thoni 7 years ago
parent
commit
e6ca633299

+ 5
- 0
.gitignore View File

@@ -12,3 +12,8 @@ Makefile
12 12
 *.so.*
13 13
 TheGame/TheGame
14 14
 tests/tests
15
+
16
+lexer-*.cpp
17
+lexer-*.h
18
+parser-*.cpp
19
+parser-*.h

+ 1
- 1
TheGame/openglrenderdevice.cpp View File

@@ -44,7 +44,7 @@ void OpenGLRenderDevice::initialize(int fov, int width, int height)
44 44
     _width = width;
45 45
     _height = height;
46 46
     glClearColor(_clearColor.redF(), _clearColor.greenF(),
47
-                 _clearColor.blue(), _clearColor.alpha());
47
+                 _clearColor.blueF(), _clearColor.alphaF());
48 48
 
49 49
     glEnable(GL_DEPTH_TEST);
50 50
     glEnable(GL_CULL_FACE);

+ 6
- 1
TheGame/renderwidget.cpp View File

@@ -5,6 +5,7 @@
5 5
 #include <QDebug>
6 6
 #include "entities/ugeentitycube.h"
7 7
 #include "entities/ugeentityaxes.h"
8
+#include "utils/wavefrontobj.h"
8 9
 
9 10
 RenderWidget::RenderWidget(QWidget *parent) :
10 11
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
@@ -20,6 +21,10 @@ RenderWidget::RenderWidget(QWidget *parent) :
20 21
     _engine->addEntity(new UGEEntityAxes(_engine));
21 22
     setMouseTracking(true);
22 23
     setFocusPolicy(Qt::StrongFocus);
24
+
25
+
26
+    WaveFrontObj obj;
27
+    qDebug() << obj.openFile("/home/robin/Downloads/enterprise/obj/USSEnterprise.obj");
23 28
 }
24 29
 
25 30
 void RenderWidget::initializeGL()
@@ -78,7 +83,7 @@ void RenderWidget::mouseMoveEvent(QMouseEvent *event)
78 83
 
79 84
     Vector3D dd = _device->get2DFrom3D(Vector3D(0.5, 0.5, 0.5));
80 85
     dd.setY(height() - dd.getY());
81
-    qDebug() << event->pos() << dd;
86
+//    qDebug() << event->pos() << dd;
82 87
     pos = _device->get3DFrom2D(event->x(), height() - event->y());
83 88
 
84 89
     update();

+ 57
- 2
UGameEngine/UGameEngine.pro View File

@@ -17,7 +17,8 @@ SOURCES += engine/ugameengine.cpp \
17 17
     utils/vector3d.cpp \
18 18
     utils/colorvector3d.cpp \
19 19
     entities/ugeentitycube.cpp \
20
-    entities/ugeentityaxes.cpp
20
+    entities/ugeentityaxes.cpp \
21
+    utils/wavefrontobj.cpp
21 22
 
22 23
 HEADERS += engine/ugameengine.h\
23 24
     engine/abstractrenderdevice.h \
@@ -25,7 +26,61 @@ HEADERS += engine/ugameengine.h\
25 26
     utils/vector3d.h \
26 27
     utils/colorvector3d.h \
27 28
     entities/ugeentitycube.h \
28
-    entities/ugeentityaxes.h
29
+    entities/ugeentityaxes.h \
30
+    utils/wavefrontobj.h
31
+
32
+
33
+# FLEX && BISON
34
+
35
+LIBS += -lfl -ly
36
+
37
+FLEXSOURCES = utils/lexer-wavefront-obj.l
38
+BISONSOURCES = utils/parser-wavefront-obj.y
39
+
40
+OTHER_FILES +=  \
41
+    $$FLEXSOURCES \
42
+    $$BISONSOURCES
43
+
44
+flexsource.input = FLEXSOURCES
45
+flexsource.output = ${QMAKE_FILE_BASE}.cpp
46
+flexsource.commands = flex --header-file=${QMAKE_FILE_BASE}.h -o ${QMAKE_FILE_BASE}.cpp ${QMAKE_FILE_IN}
47
+flexsource.variable_out = SOURCES
48
+flexsource.name = Flex Sources ${QMAKE_FILE_IN}
49
+flexsource.CONFIG += target_predeps
50
+
51
+QMAKE_EXTRA_COMPILERS += flexsource
52
+
53
+flexheader.input = FLEXSOURCES
54
+flexheader.output = ${QMAKE_FILE_BASE}.h
55
+flexheader.commands = @true
56
+flexheader.variable_out = HEADERS
57
+flexheader.name = Flex Headers ${QMAKE_FILE_IN}
58
+flexheader.CONFIG += target_predeps no_link
59
+
60
+QMAKE_EXTRA_COMPILERS += flexheader
61
+
62
+bisonsource.input = BISONSOURCES
63
+bisonsource.output = ${QMAKE_FILE_BASE}.cpp
64
+bisonsource.commands = bison -d --defines=${QMAKE_FILE_BASE}.h -o ${QMAKE_FILE_BASE}.cpp ${QMAKE_FILE_IN}
65
+bisonsource.variable_out = SOURCES
66
+bisonsource.name = Bison Sources ${QMAKE_FILE_IN}
67
+bisonsource.CONFIG += target_predeps
68
+
69
+QMAKE_EXTRA_COMPILERS += bisonsource
70
+
71
+bisonheader.input = BISONSOURCES
72
+bisonheader.output = ${QMAKE_FILE_BASE}.h
73
+bisonheader.commands = @true
74
+bisonheader.variable_out = HEADERS
75
+bisonheader.name = Bison Headers ${QMAKE_FILE_IN}
76
+bisonheader.CONFIG += target_predeps no_link
77
+
78
+QMAKE_EXTRA_COMPILERS += bisonheader
79
+
80
+
81
+
82
+
83
+
29 84
 
30 85
 unix {
31 86
     target.path = /usr/lib

+ 66
- 0
UGameEngine/utils/lexer-wavefront-obj.l View File

@@ -0,0 +1,66 @@
1
+%{
2
+// In this section we can add all needed headers, from Qt or another libraries.
3
+//#include <QtScript>
4
+#include <QVariant>
5
+#include <QDebug>
6
+
7
+// Also, we must add the parser's header where are defined the tokens.
8
+#include "parser-wavefront-obj.h"
9
+
10
+#define TOKEN(type) \
11
+  TOK_##type
12
+
13
+%}
14
+
15
+%option noyywrap
16
+%option prefix="wavefront_obj"
17
+
18
+%%
19
+
20
+ /* Whitespace */
21
+[ \t]+       {                           }
22
+
23
+ /* Newline */
24
+\n|\n\r      { ++wavefront_objlineno;    }
25
+
26
+ /* Comment */
27
+"#"[^\n\r]*  {                        }
28
+
29
+ /* Material library */
30
+"mtllib"     { return TOKEN(MTLLIB);  }
31
+
32
+ /* Use material */
33
+"usemtl"     { return TOKEN(USEMTL);  }
34
+
35
+ /* Named object */
36
+"o"     { return TOKEN(O);  }
37
+
38
+ /* Vertex */
39
+"v"     { return TOKEN(V);  }
40
+
41
+ /* Texture coordinate */
42
+"vt"     { return TOKEN(VT);  }
43
+
44
+ /* Vertex normal */
45
+"vn"     { return TOKEN(VN);  }
46
+
47
+ /* Parameter space vertices */
48
+"vp"     { return TOKEN(VP);  }
49
+
50
+ /* Face */
51
+"f"     { return TOKEN(F);  }
52
+
53
+ /* Smooth */
54
+"s"     { return TOKEN(S);  }
55
+
56
+ /* Slash */
57
+"/"     { return TOKEN(SLASH);  }
58
+
59
+ /* Number */
60
+-?[0-9]+(.[0-9]+)?     { return TOKEN(NUMBER); }
61
+
62
+ /* String */
63
+[^\n\r\t ]+  { return TOKEN(STRING);  }
64
+
65
+
66
+%%

+ 105
- 0
UGameEngine/utils/parser-wavefront-obj.y View File

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

+ 33
- 0
UGameEngine/utils/wavefrontobj.cpp View File

@@ -0,0 +1,33 @@
1
+#include "wavefrontobj.h"
2
+#include <QFile>
3
+#include <QDebug>
4
+#include "lexer-wavefront-obj.h"
5
+#include "parser-wavefront-obj.h"
6
+
7
+WaveFrontObj::WaveFrontObj(QObject *parent) :
8
+    QObject(parent)
9
+{
10
+}
11
+
12
+bool WaveFrontObj::openFile(const QString &filename)
13
+{
14
+    QFile f(filename);
15
+    if (f.open(QIODevice::ReadOnly)) {
16
+        bool res = load(f);
17
+        f.close();
18
+        return res;
19
+    }
20
+    _error = "Failed to open file: " + f.errorString();
21
+    return false;
22
+}
23
+
24
+bool WaveFrontObj::load(QIODevice &device)
25
+{
26
+    QString file = device.readAll();
27
+
28
+    YY_BUFFER_STATE bufferState = wavefront_obj_scan_string(file.toUtf8().constData());
29
+    int res = wavefront_objparse();
30
+    wavefront_obj_delete_buffer(bufferState);
31
+
32
+    return res == 0;
33
+}

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

@@ -0,0 +1,28 @@
1
+#ifndef WAVEFRONTOBJ_H
2
+#define WAVEFRONTOBJ_H
3
+
4
+#include <QObject>
5
+#include <QIODevice>
6
+
7
+class WaveFrontObj : public QObject
8
+{
9
+    Q_OBJECT
10
+public:
11
+    explicit WaveFrontObj(QObject *parent = 0);
12
+
13
+signals:
14
+
15
+public slots:
16
+    bool openFile(const QString& filename);
17
+    bool load(QIODevice& device);
18
+
19
+private:
20
+    QString _error;
21
+
22
+};
23
+
24
+struct WaveFrontObjData
25
+{
26
+};
27
+
28
+#endif // WAVEFRONTOBJ_H

Loading…
Cancel
Save