src/vertexcompiler.cpp

Fri, 27 Apr 2018 16:27:14 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 27 Apr 2018 16:27:14 +0300
changeset 1385
2f18c0da749d
permissions
-rw-r--r--

added vertex rendering

1385
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "vertexcompiler.h"
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include "vertexselection.h"
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include "model.h"
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 static const Vertex sphere[] = {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {1, 0, 0}, {0.7071, 0.7071, 0}, {0.7071, 0, 0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 {0, 0.7071, 0.7071}, {0, 0, 1}, {0.7071, 0, 0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 {0.7071, 0.7071, 0}, {0, 0.7071, 0.7071}, {0.7071, 0, 0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {0, 1, 0}, {0, 0.7071, 0.7071}, {0.7071, 0.7071, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 {0.7071, 0, -0.7071}, {0.7071, 0.7071, 0}, {1, 0, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 {0.7071, 0, -0.7071}, {0, 0, -1}, {0, 0.7071, -0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 {0.7071, 0, -0.7071}, {0, 0.7071, -0.7071}, {0.7071, 0.7071, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 {0.7071, 0.7071, 0}, {0, 0.7071, -0.7071}, {0, 1, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 {-0.7071, 0, 0.7071}, {-0.7071, 0.7071, 0}, {-1, 0, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {-0.7071, 0, 0.7071}, {0, 0, 1}, {0, 0.7071, 0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 {-0.7071, 0, 0.7071}, {0, 0.7071, 0.7071}, {-0.7071, 0.7071, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 {-0.7071, 0.7071, 0}, {0, 0.7071, 0.7071}, {0, 1, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 {-1, 0, 0}, {-0.7071, 0.7071, 0}, {-0.7071, 0, -0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 {0, 0.7071, -0.7071}, {0, 0, -1}, {-0.7071, 0, -0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 {-0.7071, 0.7071, 0}, {0, 0.7071, -0.7071}, {-0.7071, 0, -0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 {0, 1, 0}, {0, 0.7071, -0.7071}, {-0.7071, 0.7071, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 {-0.7071, 0, -0.7071}, {0, 0, -1}, {0, -0.7071, -0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 {0, -1, 0}, {0, -0.7071, 0.7071}, {-0.7071, -0.7071, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 {0.7071, -0.7071, 0}, {0, -0.7071, -0.7071}, {0.7071, 0, -0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 {-1, 0, 0}, {-0.7071, -0.7071, 0}, {-0.7071, 0, 0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 {-0.7071, -0.7071, 0}, {0, -0.7071, -0.7071}, {0, -1, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 {0, -1, 0}, {0, -0.7071, -0.7071}, {0.7071, -0.7071, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 {0.7071, 0, 0.7071}, {0, -0.7071, 0.7071}, {0.7071, -0.7071, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 {-0.7071, 0, -0.7071}, {-0.7071, -0.7071, 0}, {-1, 0, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 {1, 0, 0}, {0.7071, -0.7071, 0}, {0.7071, 0, -0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 {0.7071, 0, 0.7071}, {0, 0, 1}, {0, -0.7071, 0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 {-0.7071, 0, -0.7071}, {0, -0.7071, -0.7071}, {-0.7071, -0.7071, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 {0.7071, -0.7071, 0}, {0, -0.7071, 0.7071}, {0, -1, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 {0.7071, 0, 0.7071}, {0.7071, -0.7071, 0}, {1, 0, 0},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 {-0.7071, -0.7071, 0}, {0, -0.7071, 0.7071}, {-0.7071, 0, 0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 {0, -0.7071, -0.7071}, {0, 0, -1}, {0.7071, 0, -0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 {0, -0.7071, 0.7071}, {0, 0, 1}, {-0.7071, 0, 0.7071},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 };
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 static const float sphereSize = 0.5;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 static const QColor sphereColor {192, 192, 255};
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 VertexCompiler::VertexCompiler(Model* model, VertexSelection* selection, QObject* parent) :
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 QObject {parent},
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 model {model}
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 connect(model, &Model::objectAdded, this, &VertexCompiler::handleNewObject);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 connect(model, &Model::aboutToRemoveObject, this, &VertexCompiler::handleRemovedObject);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 if (selection != nullptr)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 this->connect(
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 selection,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 &VertexSelection::vertexSelected,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 this,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 &VertexCompiler::updateVertex
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 );
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 this->connect(
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 selection,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 &VertexSelection::vertexDeselected,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 this,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 &VertexCompiler::updateVertex
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 );
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 VertexCompiler::~VertexCompiler()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 if (this->initialized)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 GLuint buffers[NumVbos];
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 for (int i = 0; i < countof(this->vbos); i += 1)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 buffers[i] = this->vbos[i].buffer;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 this->glDeleteBuffers(countof(buffers), &buffers[0]);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 void VertexCompiler::handleNewObject(const QModelIndex &index)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 LDObject* object = this->model->lookup(index);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 this->addVerticesFromObject(object);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 connect(object, &LDObject::aboutToBeModified, this, &VertexCompiler::handlePreModifiedObject);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 connect(object, &LDObject::modified, this, &VertexCompiler::handleModifiedObject);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 void VertexCompiler::handleRemovedObject(const QModelIndex &index)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 LDObject* object = this->model->lookup(index);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 this->removeVerticesFromObject(object);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 void VertexCompiler::handlePreModifiedObject()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 removeVerticesFromObject(qobject_cast<LDObject*>(this->sender()));
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117 void VertexCompiler::handleModifiedObject()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119 addVerticesFromObject(qobject_cast<LDObject*>(this->sender()));
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 void VertexCompiler::initialize()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 if (not this->initialized)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126 this->initializeOpenGLFunctions();
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 this->initialized = true;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 GLuint buffers[NumVbos];
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 this->glGenBuffers(countof(buffers), &buffers[0]);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131 for (int i = 0; i < countof(this->vbos); i += 1)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132 this->vbos[i].buffer = buffers[i];
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 void VertexCompiler::addVerticesFromObject(LDObject* object)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 for (int i = 0; i < object->numPolygonVertices(); i += 1)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 this->addVertex(object->vertex(i));
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
140
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
141 this->needCompile = true;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
142 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
143
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 void VertexCompiler::removeVerticesFromObject(LDObject* object)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 for (int i = 0; i < object->numPolygonVertices(); i += 1)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147 this->removeVertex(object->vertex(i));
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 this->needCompile = true;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
150 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
152 void VertexCompiler::addVertex(const Vertex& vertex)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
153 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154 auto iterator = this->vertices.find(vertex);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156 if (iterator == this->vertices.end())
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 this->vertices.insert(vertex, 1);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 else
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 iterator.value() += 1;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
160 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
161
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
162 void VertexCompiler::removeVertex(const Vertex& vertex)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
163 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
164 auto iterator = this->vertices.find(vertex);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
165
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
166 if (iterator != this->vertices.end())
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
167 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
168 if (iterator.value() > 1)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
169 iterator.value() -= 1;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
170 else
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
171 this->vertices.erase(iterator);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
172 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
173 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
174
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
175 void VertexCompiler::updateVertex(const Vertex&)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
176 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
177 this->needCompile = true;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
178 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
179
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
180 /*
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
181 * Returns the GL buffer for the given vbo.
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
182 */
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
183 GLuint VertexCompiler::vbo(Vbo vboIndex)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
184 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
185 if (this->needCompile)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
186 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
187 this->compile();
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
188 this->upload();
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
189 this->needCompile = false;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
190 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
191
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
192 return this->vbos[vboIndex].buffer;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
193 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
194
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
195 /*
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
196 * Returns the size of the given vbo.
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
197 */
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
198 size_t VertexCompiler::vboSize(Vbo vboIndex) const
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
199 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
200 return this->vbos[vboIndex].data.size();
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
201 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
202
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
203 /*
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
204 * Clears the vertex scene.
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
205 */
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
206 void VertexCompiler::clear()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
207 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
208 for (auto& vbo : this->vbos)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
209 vbo.data.clear();
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
210 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
211
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
212 /*
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
213 * Builds the vertex scene
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
214 */
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
215 void VertexCompiler::compile()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
216 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
217 this->clear();
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
218 QMapIterator<Vertex, unsigned int> iterator {this->vertices};
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
219
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
220 while (iterator.hasNext())
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
221 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
222 iterator.next();
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
223 this->compileVertex(iterator.key());
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
224 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
225 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
226
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
227 /*
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
228 * Adds a little sphere into the scene to represent the given vertex.
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
229 */
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
230 void VertexCompiler::compileVertex(const Vertex& vertex)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
231 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
232 for (int i = 0; i < countof(::sphere); i += 1)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
233 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
234 // Y and Z inverted like the rest of the scene
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
235 Vertex spherePoint = {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
236 ::sphere[i][X] * ::sphereSize + vertex.x,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
237 ::sphere[i][Y] * ::sphereSize - vertex.y,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
238 ::sphere[i][Z] * ::sphereSize - vertex.z
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
239 };
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
240 this->vbos[Surfaces].data << spherePoint.x << spherePoint.y << spherePoint.z;
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
241 // The points of the sphere are position vectors, which in this case are also unit normals.
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
242 this->vbos[Normals].data << ::sphere[i][X] << ::sphere[i][Y] << ::sphere[i][Z];
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
243 this->vbos[Colors].data
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
244 << ::sphereColor.redF()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
245 << ::sphereColor.greenF()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
246 << ::sphereColor.blueF()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
247 << ::sphereColor.alphaF();
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
248 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
249 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
250
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
251 /*
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
252 * Uploads VBO data to the graphics card.
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
253 */
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
254 void VertexCompiler::upload()
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
255 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
256 for (const auto& vbo : this->vbos)
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
257 {
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
258 glBindBuffer(GL_ARRAY_BUFFER, vbo.buffer);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
259 glBufferData(
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
260 GL_ARRAY_BUFFER,
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
261 countof(vbo.data) * sizeof(GLfloat),
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
262 vbo.data.constData(),
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
263 GL_STATIC_DRAW
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
264 );
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
265 }
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
266 glBindBuffer(GL_ARRAY_BUFFER, 0);
2f18c0da749d added vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
267 }

mercurial