git-svn-id: svn://db.shs.com.ru/libs@664 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2019-12-09 20:36:54 +00:00
parent 1a210defb1
commit 86f4e0ad78
4 changed files with 48 additions and 23 deletions

View File

@@ -57,17 +57,22 @@ Mesh * Mesh::clone() {
void Mesh::init(QOpenGLExtraFunctions * f) { void Mesh::init(QOpenGLExtraFunctions * f) {
if (!isInit()) {
buffer_geom.init(f); buffer_geom.init(f);
buffer_ind .init(f); buffer_ind .init(f);
vao.bindBuffers(f, buffer_geom, buffer_ind);
changed = true; changed = true;
} }
}
void Mesh::destroy(QOpenGLExtraFunctions * f) { void Mesh::destroy(QOpenGLExtraFunctions * f) {
buffer_geom.destroy(f); buffer_geom.destroy(f);
buffer_ind .destroy(f); buffer_ind .destroy(f);
vao.destroy(f); QList<VertexObject*> vaol = vao_map.values();
foreach (VertexObject* vao, vaol)
vao->destroy(f);
qDeleteAll(vao_map);
vao_map.clear();
} }
@@ -119,6 +124,15 @@ void Mesh::calculateTangents() {
} }
VertexObject * Mesh::vaoByType(int type) {
VertexObject *& vao(vao_map[type]);
if (!vao) {
vao = new VertexObject();
}
return vao;
}
bool Mesh::rebuffer(QOpenGLExtraFunctions * f) { bool Mesh::rebuffer(QOpenGLExtraFunctions * f) {
changed = false; changed = false;
if (vertices_.isEmpty()) return true; if (vertices_.isEmpty()) return true;
@@ -159,15 +173,18 @@ bool Mesh::rebuffer(QOpenGLExtraFunctions * f) {
} }
void Mesh::draw(QOpenGLExtraFunctions * f, int count) { void Mesh::draw(QOpenGLExtraFunctions * f, int count, int type) {
if (isEmpty()) return; if (isEmpty()) return;
if (!isInit()) init(f); if (!isInit()) init(f);
if (changed) rebuffer(f); if (changed) rebuffer(f);
//qDebug() << "draw" << geom_type << vert_count << count; //qDebug() << "draw" << geom_type << vert_count << count;
VertexObject * vao = vaoByType(type);
vao->bindBuffers(f, buffer_geom, buffer_ind);
if (geom_type == GL_TRIANGLES) if (geom_type == GL_TRIANGLES)
vao.draw(f, geom_type, triangles_.size() * 3, count); vao->draw(f, geom_type, triangles_.size() * 3, count);
else else
vao.draw(f, geom_type, lines_.size() * 2, count); vao->draw(f, geom_type, lines_.size() * 2, count);
} }
@@ -184,19 +201,21 @@ void Mesh::clear() {
} }
void Mesh::loadObject(QOpenGLExtraFunctions * f, const Object & object) { void Mesh::loadObject(QOpenGLExtraFunctions * f, const Object & object, int type) {
vao.loadObject(f, object); VertexObject * vao = vaoByType(type);
vao->loadObject(f, object);
} }
void Mesh::loadObjects(QOpenGLExtraFunctions * f, const QVector<Object> & objects) { void Mesh::loadObjects(QOpenGLExtraFunctions * f, const QVector<Object> & objects, int type) {
vao.loadObjects(f, objects); VertexObject * vao = vaoByType(type);
vao->loadObjects(f, objects);
} }
void Mesh::loadSelections(QOpenGLExtraFunctions * f, const QVector<uchar> & sels) { void Mesh::loadSelections(QOpenGLExtraFunctions * f, const QVector<uchar> & sels, int type) {
//qDebug() << "loadSelections" << sels; VertexObject * vao = vaoByType(type);
vao.loadSelections(f, sels); vao->loadSelections(f, sels);
} }

View File

@@ -41,16 +41,16 @@ public:
void init (QOpenGLExtraFunctions * f); void init (QOpenGLExtraFunctions * f);
void destroy (QOpenGLExtraFunctions * f); void destroy (QOpenGLExtraFunctions * f);
bool rebuffer(QOpenGLExtraFunctions * f); bool rebuffer(QOpenGLExtraFunctions * f);
void draw (QOpenGLExtraFunctions * f, int count); void draw (QOpenGLExtraFunctions * f, int count, int type = 0);
void clear(); void clear();
void loadObject (QOpenGLExtraFunctions * f, const QGLEngineShaders::Object & object); void loadObject (QOpenGLExtraFunctions * f, const QGLEngineShaders::Object & object, int type = 0);
void loadObjects (QOpenGLExtraFunctions * f, const QVector<QGLEngineShaders::Object> & objects); void loadObjects (QOpenGLExtraFunctions * f, const QVector<QGLEngineShaders::Object> & objects, int type = 0);
void loadSelections(QOpenGLExtraFunctions * f, const QVector<uchar> & sels); void loadSelections(QOpenGLExtraFunctions * f, const QVector<uchar> & sels, int type = 0);
int verticesCount() const {return vertices_.size();} int verticesCount() const {return vertices_.size();}
int trianglesCount() const {return triangles_.size();} int trianglesCount() const {return triangles_.size();}
int linesCount() const {return lines_.size();} int linesCount() const {return lines_.size();}
bool isInit() const {return vao.isInit();} bool isInit() const {return buffer_geom.isInit();}
bool isEmpty() const {return vertices_.isEmpty();} bool isEmpty() const {return vertices_.isEmpty();}
uint hash() const; uint hash() const;
@@ -78,6 +78,7 @@ public:
private: private:
void calculateNormals(); void calculateNormals();
void calculateTangents(); void calculateTangents();
VertexObject * vaoByType(int type);
QVector<QVector3D> vertices_, normals_, tangents_, bitangents_; QVector<QVector3D> vertices_, normals_, tangents_, bitangents_;
QVector<QVector2D> texcoords_; QVector<QVector2D> texcoords_;
@@ -87,7 +88,7 @@ private:
QVector<QGLEngineShaders::Vertex> data_; QVector<QGLEngineShaders::Vertex> data_;
GLenum geom_type; GLenum geom_type;
Buffer buffer_geom, buffer_ind; Buffer buffer_geom, buffer_ind;
VertexObject vao; QMap<int, VertexObject * > vao_map;
mutable uint hash_; mutable uint hash_;
mutable bool hash_changed; mutable bool hash_changed;
int vert_count; int vert_count;

View File

@@ -27,6 +27,7 @@ VertexObject::VertexObject():
buffer_obj (GL_ARRAY_BUFFER, GL_STREAM_DRAW), buffer_obj (GL_ARRAY_BUFFER, GL_STREAM_DRAW),
buffer_sel (GL_ARRAY_BUFFER, GL_STREAM_DRAW) { buffer_sel (GL_ARRAY_BUFFER, GL_STREAM_DRAW) {
vao_ = 0; vao_ = 0;
buffers_binded = false;
} }
@@ -64,7 +65,10 @@ void VertexObject::release(QOpenGLExtraFunctions * f) {
} }
void VertexObject::bindBuffers(QOpenGLExtraFunctions * f, Buffer & geom, Buffer & elem) { void VertexObject::bindBuffers(QOpenGLExtraFunctions * f, Buffer & geom, Buffer & elem, bool force) {
if (!force && buffers_binded) return;
buffers_binded = true;
init(f); init(f);
bind(f); bind(f);

View File

@@ -36,7 +36,7 @@ public:
void bind (QOpenGLExtraFunctions * f); void bind (QOpenGLExtraFunctions * f);
void release (QOpenGLExtraFunctions * f); void release (QOpenGLExtraFunctions * f);
void bindBuffers (QOpenGLExtraFunctions * f, Buffer & geom, Buffer & elem); void bindBuffers (QOpenGLExtraFunctions * f, Buffer & geom, Buffer & elem, bool force = false);
void loadObject (QOpenGLExtraFunctions * f, const QGLEngineShaders::Object & object); void loadObject (QOpenGLExtraFunctions * f, const QGLEngineShaders::Object & object);
void loadObjects (QOpenGLExtraFunctions * f, const QVector<QGLEngineShaders::Object> & objects); void loadObjects (QOpenGLExtraFunctions * f, const QVector<QGLEngineShaders::Object> & objects);
void loadSelections(QOpenGLExtraFunctions * f, const QVector<uchar> & sels); void loadSelections(QOpenGLExtraFunctions * f, const QVector<uchar> & sels);
@@ -51,6 +51,7 @@ private:
GLuint vao_; GLuint vao_;
Buffer buffer_obj, buffer_sel; Buffer buffer_obj, buffer_sel;
bool buffers_binded;
}; };