git-svn-id: svn://db.shs.com.ru/libs@627 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -26,10 +26,11 @@ using namespace QGLEngineShaders;
|
||||
|
||||
static int _count = 0;
|
||||
|
||||
Mesh::Mesh(): buffer_geom(GL_ARRAY_BUFFER, GL_STATIC_DRAW),
|
||||
buffer_ind (GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW),
|
||||
buffer_obj (GL_ARRAY_BUFFER, GL_STREAM_DRAW),
|
||||
buffer_sel (GL_ARRAY_BUFFER, GL_STREAM_DRAW) {
|
||||
Mesh::Mesh(GLenum geom_type_): geom_type(geom_type_),
|
||||
buffer_geom(GL_ARRAY_BUFFER, GL_STATIC_DRAW),
|
||||
buffer_ind (GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW),
|
||||
buffer_obj (GL_ARRAY_BUFFER, GL_STREAM_DRAW),
|
||||
buffer_sel (GL_ARRAY_BUFFER, GL_STREAM_DRAW) {
|
||||
vao = 0;
|
||||
hash_ = 0;
|
||||
changed = hash_changed = objects_changed = selected_changed = true;
|
||||
@@ -49,6 +50,8 @@ Mesh * Mesh::clone() {
|
||||
c->normals_ = normals_ ;
|
||||
c->texcoords_ = texcoords_;
|
||||
c->triangles_ = triangles_;
|
||||
c->lines_ = lines_;
|
||||
c->geom_type = geom_type;
|
||||
c->hash_ = hash_;
|
||||
c->hash_changed = hash_changed;
|
||||
//qDebug() << "clone VBO";
|
||||
@@ -163,7 +166,8 @@ bool Mesh::rebuffer(QOpenGLExtraFunctions * f) {
|
||||
v.tex = texcoords_ [i];
|
||||
}
|
||||
int gsize = data_.size() * sizeof(Vertex);
|
||||
int isize = triangles_.size() * sizeof(Vector3i);
|
||||
int tsize = triangles_.size() * sizeof(Vector3i);
|
||||
int lsize = lines_.size() * sizeof(Vector2i);
|
||||
f->glBindVertexArray(vao);
|
||||
|
||||
buffer_geom.bind(f);
|
||||
@@ -172,8 +176,13 @@ bool Mesh::rebuffer(QOpenGLExtraFunctions * f) {
|
||||
prepareDrawGeom(f);
|
||||
|
||||
buffer_ind.bind(f);
|
||||
buffer_ind.resize(f, isize);
|
||||
buffer_ind.load(f, triangles_.constData(), isize);
|
||||
if (geom_type == GL_TRIANGLES) {
|
||||
buffer_ind.resize(f, tsize);
|
||||
buffer_ind.load(f, triangles_.constData(), tsize);
|
||||
} else {
|
||||
buffer_ind.resize(f, lsize);
|
||||
buffer_ind.load(f, lines_.constData(), lsize);
|
||||
}
|
||||
|
||||
buffer_obj.bind(f);
|
||||
prepareDrawObj(f);
|
||||
@@ -247,10 +256,13 @@ void Mesh::draw(QOpenGLExtraFunctions * f, int count) {
|
||||
if (isEmpty()) return;
|
||||
if (!isInit()) init(f);
|
||||
if (changed) rebuffer(f);
|
||||
//qDebug() << "draw" << vert_count << count;
|
||||
//qDebug() << "draw" << geom_type << vert_count << count;
|
||||
|
||||
f->glBindVertexArray(vao);
|
||||
f->glDrawElementsInstanced(GL_TRIANGLES, triangles_.size() * 3, GL_UNSIGNED_INT, 0, count);
|
||||
if (geom_type == GL_TRIANGLES)
|
||||
f->glDrawElementsInstanced(geom_type, triangles_.size() * 3, GL_UNSIGNED_INT, 0, count);
|
||||
else
|
||||
f->glDrawElementsInstanced(geom_type, lines_.size() * 2, GL_UNSIGNED_INT, 0, count);
|
||||
f->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
@@ -262,6 +274,7 @@ void Mesh::clear() {
|
||||
bitangents_.clear();
|
||||
texcoords_.clear();
|
||||
triangles_.clear();
|
||||
lines_.clear();
|
||||
data_.clear();
|
||||
changed = hash_changed = true;
|
||||
}
|
||||
@@ -290,6 +303,7 @@ uint Mesh::hash() const {
|
||||
hash_ ^= qHashBits(normals_ .constData(), normals_ .size() * sizeof(QVector3D));
|
||||
hash_ ^= qHashBits(texcoords_.constData(), texcoords_.size() * sizeof(QVector2D));
|
||||
hash_ ^= qHashBits(triangles_.constData(), triangles_.size() * sizeof( Vector3i));
|
||||
hash_ ^= qHashBits(lines_ .constData(), lines_ .size() * sizeof( Vector2i));
|
||||
}
|
||||
return hash_;
|
||||
}
|
||||
@@ -327,6 +341,10 @@ void Mesh::append(const Mesh * m) {
|
||||
for (int i = 0; i < tri.size(); ++i)
|
||||
tri[i] += vcnt;
|
||||
triangles_.append(tri);
|
||||
QVector<Vector2i> lin = m->lines_;
|
||||
for (int i = 0; i < lin.size(); ++i)
|
||||
lin[i] += vcnt;
|
||||
lines_.append(lin);
|
||||
}
|
||||
|
||||
|
||||
@@ -336,7 +354,7 @@ bool Mesh::saveToFile(const QString & filename) {
|
||||
QByteArray ba;
|
||||
if (f.open(QFile::WriteOnly)) {
|
||||
QDataStream out(&ba, QFile::WriteOnly);
|
||||
out << vertices_ << normals_ << texcoords_ << triangles_;
|
||||
out << vertices_ << normals_ << texcoords_ << triangles_ << lines_;
|
||||
ba = qCompress(ba);
|
||||
f.resize(0);
|
||||
f.write(ba);
|
||||
@@ -356,7 +374,7 @@ bool Mesh::loadFromFile(const QString & filename) {
|
||||
if (ba.isEmpty()) return false;
|
||||
ba = qUncompress(ba);
|
||||
QDataStream in(ba);
|
||||
in >> vertices_ >> normals_ >> texcoords_ >> triangles_;
|
||||
in >> vertices_ >> normals_ >> texcoords_ >> triangles_ >> lines_;
|
||||
changed = hash_changed = true;
|
||||
f.close();
|
||||
return !isEmpty();
|
||||
@@ -397,7 +415,8 @@ Box3D Mesh::boundingBox() const {
|
||||
QDataStream & operator <<(QDataStream & s, const Mesh * m) {
|
||||
ChunkStream cs;
|
||||
//qDebug() << "place VBO" << m.vertices_.size() << m.normals_.size() << m.texcoords_.size() << m.colors_.size() << "...";
|
||||
cs.add(1, m->vertices_).add(2, m->normals_).add(3, m->texcoords_).add(6, m->triangles_);
|
||||
cs.add(1, m->vertices_).add(2, m->normals_).add(3, m->texcoords_)
|
||||
.add(6, m->triangles_).add(7, m->lines_).add(10, int(m->geom_type));
|
||||
//qDebug() << "place VBO done" << cs.data().size() << "...";
|
||||
s << /*qCompress*/(cs.data()); return s;
|
||||
}
|
||||
@@ -411,10 +430,12 @@ QDataStream & operator >>(QDataStream & s, Mesh *& m) {
|
||||
ChunkStream cs(s);
|
||||
while (!cs.atEnd()) {
|
||||
switch (cs.read()) {
|
||||
case 1: cs.get(m->vertices_ ); break;
|
||||
case 2: cs.get(m->normals_ ); break;
|
||||
case 3: cs.get(m->texcoords_); break;
|
||||
case 6: cs.get(m->triangles_); break;
|
||||
case 1 : cs.get(m->vertices_ ); break;
|
||||
case 2 : cs.get(m->normals_ ); break;
|
||||
case 3 : cs.get(m->texcoords_); break;
|
||||
case 6 : cs.get(m->triangles_); break;
|
||||
case 7 : cs.get(m->lines_ ); break;
|
||||
case 10: m->geom_type = cs.getData<int>(); break;
|
||||
}
|
||||
}
|
||||
m->changed = true;
|
||||
|
||||
Reference in New Issue
Block a user