Skip to content

Commit

Permalink
gl: fix Geometry attributes check and VAO binding
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Aug 1, 2016
1 parent b2a7715 commit 164caf9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/opengl/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ void Geometry::allocate(int nbVertex, int nbIndex)

bool Geometry::compare(const Geometry *other) const
{
if (this == other)
return true;
// this == other: attributes and stride can be different
if (!other)
return false;
if (stride() != other->stride())
Expand Down Expand Up @@ -175,7 +174,7 @@ void TexturedGeometry::setTextureCount(int value)
allocate(vertexCount());
if (a.size()-1 < value) { // the first is position
for (int i = a.size()-1; i < value; ++i)
a << Attribute(TypeF32, 2, (i+1)* 2*sizeof(float));
a << Attribute(TypeF32, 2, int((i+1)* 2*sizeof(float)));
} else {
a.resize(value + 1);
}
Expand Down
8 changes: 5 additions & 3 deletions src/opengl/GeometryRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GeometryRenderer::GeometryRenderer()
: g(NULL)
, features_(kVBO|kIBO|kVAO)
, ibo(QOpenGLBuffer::IndexBuffer)
, stride(0)
{
static bool disable_ibo = qgetenv("QTAV_NO_IBO").toInt() > 0;
setFeature(kIBO, !disable_ibo);
Expand Down Expand Up @@ -78,7 +79,6 @@ bool GeometryRenderer::testFeatures(int value) const

void GeometryRenderer::updateGeometry(Geometry *geo)
{
const Geometry* old = g; // old geometry will be compared later, so make sure destroy old after updateGeometry
g = geo;
if (!g) {
ibo.destroy();
Expand Down Expand Up @@ -124,8 +124,10 @@ void GeometryRenderer::updateGeometry(Geometry *geo)
#if QT_VAO
if (!vao.isCreated())
return;
if (g->compare(old))
if (stride == g->stride() && attrib == g->attributes())
return;
stride = g->stride();
attrib = g->attributes();
//qDebug("geometry attributes changed, rebind vao...");
// TODO: call once is enough if no feature and no geometry attribute is changed
if (vbo.isCreated()) {
Expand All @@ -136,7 +138,7 @@ void GeometryRenderer::updateGeometry(Geometry *geo)
QGLF(glVertexAttribPointer(an, a.tupleSize(), a.type(), a.normalize(), g->stride(), reinterpret_cast<const void *>(qptrdiff(a.offset())))); //TODO: in setActiveShader
QGLF(glEnableVertexAttribArray(an));
}
vbo.release(); // unbind after vao unbind?
vbo.release(); // unbind after vao unbind? http://www.zwqxin.com/archives/opengl/vao-and-vbo-stuff.html
}
// bind ibo to vao thus no bind is required later
if (ibo.isCreated())// if not bind here, glDrawElements(...,NULL) crashes and must use ibo data ptr, why?
Expand Down
8 changes: 6 additions & 2 deletions src/opengl/GeometryRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GeometryRenderer
static const int kVBO = 0x01;
static const int kIBO = 0x02;
static const int kVAO = 0x04;

// TODO: VAB, VBUM etc.
GeometryRenderer();
// call updateBuffer internally in bindBuffer if feature is changed
void setFeature(int f, bool on);
Expand All @@ -59,7 +59,7 @@ class GeometryRenderer
* \param geo null: release vao/vbo
*/
void updateGeometry(Geometry* geo = NULL);
void render();
virtual void render();
protected:
void bindBuffers();
void unbindBuffers();
Expand All @@ -71,6 +71,10 @@ class GeometryRenderer
QOpenGLVertexArrayObject vao;
#endif //QT_VAO
QOpenGLBuffer ibo;

// geometry characteristic
int stride;
QVector<Attribute> attrib;
};

} //namespace QtAV
Expand Down

0 comments on commit 164caf9

Please sign in to comment.