Browse Source

loading and drawing obj

develop
Robin Thoni 7 years ago
parent
commit
c1daa42014

+ 6
- 2
TheGame/renderwidget.cpp View File

6
 #include "entities/ugeentitycube.h"
6
 #include "entities/ugeentitycube.h"
7
 #include "entities/ugeentityaxes.h"
7
 #include "entities/ugeentityaxes.h"
8
 #include "utils/wavefrontobj.h"
8
 #include "utils/wavefrontobj.h"
9
+#include "entities/ugeentitywavefrontobj.h"
9
 
10
 
10
 RenderWidget::RenderWidget(QWidget *parent) :
11
 RenderWidget::RenderWidget(QWidget *parent) :
11
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
12
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
22
     setMouseTracking(true);
23
     setMouseTracking(true);
23
     setFocusPolicy(Qt::StrongFocus);
24
     setFocusPolicy(Qt::StrongFocus);
24
 
25
 
26
+    WaveFrontObj* wavefrontObj = new WaveFrontObj(this);
27
+    qDebug() << wavefrontObj->openFile("/home/robin/Downloads/enterprise/obj/USSEnterprise.obj");
28
+
29
+    UGEEntityWaveFrontObj* obj = new UGEEntityWaveFrontObj(wavefrontObj, this);
30
+    _engine->addEntity(obj);
25
 
31
 
26
-    WaveFrontObj obj;
27
-    qDebug() << obj.openFile("/home/robin/Downloads/enterprise/obj/USSEnterprise.obj");
28
 }
32
 }
29
 
33
 
30
 void RenderWidget::initializeGL()
34
 void RenderWidget::initializeGL()

+ 4
- 2
UGameEngine/UGameEngine.pro View File

18
     utils/colorvector3d.cpp \
18
     utils/colorvector3d.cpp \
19
     entities/ugeentitycube.cpp \
19
     entities/ugeentitycube.cpp \
20
     entities/ugeentityaxes.cpp \
20
     entities/ugeentityaxes.cpp \
21
-    utils/wavefrontobj.cpp
21
+    utils/wavefrontobj.cpp \
22
+    entities/ugeentitywavefrontobj.cpp
22
 
23
 
23
 HEADERS += engine/ugameengine.h\
24
 HEADERS += engine/ugameengine.h\
24
     engine/abstractrenderdevice.h \
25
     engine/abstractrenderdevice.h \
27
     utils/colorvector3d.h \
28
     utils/colorvector3d.h \
28
     entities/ugeentitycube.h \
29
     entities/ugeentitycube.h \
29
     entities/ugeentityaxes.h \
30
     entities/ugeentityaxes.h \
30
-    utils/wavefrontobj.h
31
+    utils/wavefrontobj.h \
32
+    entities/ugeentitywavefrontobj.h
31
 
33
 
32
 
34
 
33
 # FLEX && BISON
35
 # FLEX && BISON

+ 21
- 0
UGameEngine/entities/ugeentitywavefrontobj.cpp View File

1
+#include "ugeentitywavefrontobj.h"
2
+
3
+UGEEntityWaveFrontObj::UGEEntityWaveFrontObj(WaveFrontObj *obj, QObject *parent)
4
+    : UGEEntity(parent)
5
+    , _obj(obj)
6
+{
7
+}
8
+
9
+void UGEEntityWaveFrontObj::draw(AbstractRenderDevice *device)
10
+{
11
+    QList<QList<int> > faces = _obj->getFaces();
12
+    QList<Vector3D> vertexes = _obj->getVertexes();
13
+    for (int i = 0; i < faces.size(); ++i) {
14
+        QList<int> face = faces[i];
15
+        QList<ColorVector3D> poly;
16
+        for (int j = 0; j < face.size(); ++j) {
17
+            poly.append(ColorVector3D(Qt::gray, vertexes[face[j] - 1]));
18
+        }
19
+        drawPolygon(device, poly);
20
+    }
21
+}

+ 24
- 0
UGameEngine/entities/ugeentitywavefrontobj.h View File

1
+#ifndef UGEENTITYWAVEFRONTOBJ_H
2
+#define UGEENTITYWAVEFRONTOBJ_H
3
+
4
+#include "ugeentity.h"
5
+#include "utils/wavefrontobj.h"
6
+
7
+class UGEEntityWaveFrontObj : public UGEEntity
8
+{
9
+    Q_OBJECT
10
+public:
11
+    explicit UGEEntityWaveFrontObj(WaveFrontObj* obj, QObject *parent = 0);
12
+
13
+    virtual void draw(AbstractRenderDevice* device);
14
+
15
+signals:
16
+
17
+public slots:
18
+
19
+private:
20
+    WaveFrontObj* _obj;
21
+
22
+};
23
+
24
+#endif // UGEENTITYWAVEFRONTOBJ_H

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

14
 
14
 
15
 // 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
16
 // occurred.
16
 // occurred.
17
-void wavefront_objerror(WaveFrontObjData* data, const char *s);
17
+void wavefront_objerror(WaveFrontObj* data, const char *s);
18
 %}
18
 %}
19
 
19
 
20
 %define api.prefix {wavefront_obj}
20
 %define api.prefix {wavefront_obj}
21
 %define api.token.prefix {TOK_}
21
 %define api.token.prefix {TOK_}
22
 
22
 
23
-%parse-param {struct WaveFrontObjData* wavefrontData}
23
+%parse-param {struct WaveFrontObj* wavefrontData}
24
 
24
 
25
 // Here we define our custom variable types.
25
 // Here we define our custom variable types.
26
 // Custom types must be of fixed size.
26
 // Custom types must be of fixed size.
68
 stmt: mtllib {}
68
 stmt: mtllib {}
69
             | usemtl {}
69
             | usemtl {}
70
             | named_object {}
70
             | named_object {}
71
-            | vertex { wavefrontData->_vertexes.append(*$1); delete $1; }
71
+            | vertex { wavefrontData->addVertex(*$1); delete $1; }
72
             | texture_coordinate {}
72
             | texture_coordinate {}
73
             | vertex_normal {}
73
             | vertex_normal {}
74
             | vertex_space {}
74
             | vertex_space {}
75
-            | face { wavefrontData->_faces.append(*$1); delete $1; }
75
+            | face { wavefrontData->addFace(*$1); delete $1; }
76
             | smooth {};
76
             | smooth {};
77
 
77
 
78
 mtllib: MTLLIB STRING {};
78
 mtllib: MTLLIB STRING {};
106
 
106
 
107
 %%
107
 %%
108
 
108
 
109
-void wavefront_objerror(WaveFrontObjData* data, const char *s)
109
+void wavefront_objerror(WaveFrontObj* data, const char *s)
110
 {
110
 {
111
-    data->_error = QString(s) + " at line " + QString::number(wavefront_objlineno) + " at " + QString(wavefront_objtext);
111
+    data->setError("Parse error: " + QString(s) + " at line " + QString::number(wavefront_objlineno) + " at " + QString(wavefront_objtext));
112
 }
112
 }

+ 28
- 4
UGameEngine/utils/wavefrontobj.cpp View File

9
 {
9
 {
10
 }
10
 }
11
 
11
 
12
+QList<QList<int> > WaveFrontObj::getFaces() const
13
+{
14
+    return _faces;
15
+}
16
+
17
+QList<Vector3D> WaveFrontObj::getVertexes() const
18
+{
19
+    return _vertexes;
20
+}
21
+
12
 bool WaveFrontObj::openFile(const QString &filename)
22
 bool WaveFrontObj::openFile(const QString &filename)
13
 {
23
 {
14
     QFile f(filename);
24
     QFile f(filename);
27
     QString file = device.readAll();
37
     QString file = device.readAll();
28
 
38
 
29
     YY_BUFFER_STATE bufferState = wavefront_obj_scan_string(file.toUtf8().constData());
39
     YY_BUFFER_STATE bufferState = wavefront_obj_scan_string(file.toUtf8().constData());
30
-    WaveFrontObjData data;
31
-    int res = wavefront_objparse(&data);
40
+
41
+    int res = wavefront_objparse(this);
32
     wavefront_obj_delete_buffer(bufferState);
42
     wavefront_obj_delete_buffer(bufferState);
33
     if (res) {
43
     if (res) {
34
-        _error = "Parse error: " + data._error;
35
         qDebug() << _error;
44
         qDebug() << _error;
36
     }
45
     }
37
     else {
46
     else {
38
-        qDebug() << "faces:" << data._faces.size() << "vertexes" << data._vertexes.size();
47
+        qDebug() << "faces:" << _faces.size() << "vertexes" << _vertexes.size();
39
     }
48
     }
40
 
49
 
41
     return res == 0;
50
     return res == 0;
42
 }
51
 }
52
+
53
+void WaveFrontObj::addFace(QList<int> face)
54
+{
55
+    _faces.append(face);
56
+}
57
+
58
+void WaveFrontObj::addVertex(const Vector3D &vertex)
59
+{
60
+    _vertexes.append(vertex);
61
+}
62
+
63
+void WaveFrontObj::setError(const QString &error)
64
+{
65
+    _error = error;
66
+}

+ 7
- 6
UGameEngine/utils/wavefrontobj.h View File

11
 public:
11
 public:
12
     explicit WaveFrontObj(QObject *parent = 0);
12
     explicit WaveFrontObj(QObject *parent = 0);
13
 
13
 
14
+    QList<QList<int> > getFaces() const;
15
+    QList<Vector3D> getVertexes() const;
16
+
14
 signals:
17
 signals:
15
 
18
 
16
 public slots:
19
 public slots:
17
     bool openFile(const QString& filename);
20
     bool openFile(const QString& filename);
18
     bool load(QIODevice& device);
21
     bool load(QIODevice& device);
22
+    void addFace(QList<int> face);
23
+    void addVertex(const Vector3D& vertex);
24
+    void setError(const QString& error);
19
 
25
 
20
 private:
26
 private:
21
     QString _error;
27
     QString _error;
22
-
23
-};
24
-
25
-struct WaveFrontObjData
26
-{
27
     QList<Vector3D> _vertexes;
28
     QList<Vector3D> _vertexes;
28
     QList<QList<int> > _faces;
29
     QList<QList<int> > _faces;
29
-    QString _error;
30
+
30
 };
31
 };
31
 
32
 
32
 #endif // WAVEFRONTOBJ_H
33
 #endif // WAVEFRONTOBJ_H

Loading…
Cancel
Save