Parcourir la source

loading and drawing obj

develop
Robin Thoni il y a 8 ans
Parent
révision
c1daa42014

+ 6
- 2
TheGame/renderwidget.cpp Voir le fichier

@@ -6,6 +6,7 @@
6 6
 #include "entities/ugeentitycube.h"
7 7
 #include "entities/ugeentityaxes.h"
8 8
 #include "utils/wavefrontobj.h"
9
+#include "entities/ugeentitywavefrontobj.h"
9 10
 
10 11
 RenderWidget::RenderWidget(QWidget *parent) :
11 12
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
@@ -22,9 +23,12 @@ RenderWidget::RenderWidget(QWidget *parent) :
22 23
     setMouseTracking(true);
23 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 34
 void RenderWidget::initializeGL()

+ 4
- 2
UGameEngine/UGameEngine.pro Voir le fichier

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

+ 21
- 0
UGameEngine/entities/ugeentitywavefrontobj.cpp Voir le fichier

@@ -0,0 +1,21 @@
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 Voir le fichier

@@ -0,0 +1,24 @@
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 Voir le fichier

@@ -14,13 +14,13 @@ extern int wavefront_objlex(void);
14 14
 
15 15
 // Bison uses the yyerror function for informing us when a parsing error has
16 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 20
 %define api.prefix {wavefront_obj}
21 21
 %define api.token.prefix {TOK_}
22 22
 
23
-%parse-param {struct WaveFrontObjData* wavefrontData}
23
+%parse-param {struct WaveFrontObj* wavefrontData}
24 24
 
25 25
 // Here we define our custom variable types.
26 26
 // Custom types must be of fixed size.
@@ -68,11 +68,11 @@ stmt_list: {}
68 68
 stmt: mtllib {}
69 69
             | usemtl {}
70 70
             | named_object {}
71
-            | vertex { wavefrontData->_vertexes.append(*$1); delete $1; }
71
+            | vertex { wavefrontData->addVertex(*$1); delete $1; }
72 72
             | texture_coordinate {}
73 73
             | vertex_normal {}
74 74
             | vertex_space {}
75
-            | face { wavefrontData->_faces.append(*$1); delete $1; }
75
+            | face { wavefrontData->addFace(*$1); delete $1; }
76 76
             | smooth {};
77 77
 
78 78
 mtllib: MTLLIB STRING {};
@@ -106,7 +106,7 @@ smooth: S NUMBER {}
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 Voir le fichier

@@ -9,6 +9,16 @@ WaveFrontObj::WaveFrontObj(QObject *parent) :
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 22
 bool WaveFrontObj::openFile(const QString &filename)
13 23
 {
14 24
     QFile f(filename);
@@ -27,16 +37,30 @@ bool WaveFrontObj::load(QIODevice &device)
27 37
     QString file = device.readAll();
28 38
 
29 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 42
     wavefront_obj_delete_buffer(bufferState);
33 43
     if (res) {
34
-        _error = "Parse error: " + data._error;
35 44
         qDebug() << _error;
36 45
     }
37 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 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 Voir le fichier

@@ -11,22 +11,23 @@ class WaveFrontObj : public QObject
11 11
 public:
12 12
     explicit WaveFrontObj(QObject *parent = 0);
13 13
 
14
+    QList<QList<int> > getFaces() const;
15
+    QList<Vector3D> getVertexes() const;
16
+
14 17
 signals:
15 18
 
16 19
 public slots:
17 20
     bool openFile(const QString& filename);
18 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 26
 private:
21 27
     QString _error;
22
-
23
-};
24
-
25
-struct WaveFrontObjData
26
-{
27 28
     QList<Vector3D> _vertexes;
28 29
     QList<QList<int> > _faces;
29
-    QString _error;
30
+
30 31
 };
31 32
 
32 33
 #endif // WAVEFRONTOBJ_H

Chargement…
Annuler
Enregistrer