Skip to content

Commit

Permalink
gl: simplify Geometry and renderer api
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Jul 14, 2016
1 parent 64298f4 commit 5626f23
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 41 deletions.
30 changes: 15 additions & 15 deletions src/opengl/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
******************************************************************************/

#include "Geometry.h"
#include "opengl/OpenGLHelper.h"
#include <QtDebug>

namespace QtAV {

Expand Down Expand Up @@ -60,26 +60,26 @@ Geometry::Geometry(int vertexCount, int indexCount, DataType indexType)
int Geometry::indexDataSize() const
{
switch (indexType()) {
case GL_UNSIGNED_SHORT: return indexCount()*2;
case GL_UNSIGNED_INT: return indexCount()*4;
case TypeU16: return indexCount()*2;
case TypeU32: return indexCount()*4;
default: return indexCount();
}
}

void Geometry::setIndexValue(int index, int value)
{
switch (indexType()) {
case GL_UNSIGNED_BYTE: {
case TypeU8: {
quint8* d = (quint8*)m_idata.data();
*(d+index) = value;
}
break;
case GL_UNSIGNED_SHORT: {
case TypeU16: {
quint16* d = (quint16*)m_idata.data();
*(d+index) = value;
}
break;
case GL_UNSIGNED_INT: {
case TypeU32: {
quint32* d = (quint32*)m_idata.data();
*(d+index) = value;
}
Expand All @@ -92,21 +92,21 @@ void Geometry::setIndexValue(int index, int value)
void Geometry::setIndexValue(int index, int v1, int v2, int v3)
{
switch (indexType()) {
case GL_UNSIGNED_BYTE: {
case TypeU8: {
quint8* d = (quint8*)m_idata.data();
*(d+index++) = v1;
*(d+index++) = v2;
*(d+index++) = v2;
}
break;
case GL_UNSIGNED_SHORT: {
case TypeU16: {
quint16* d = (quint16*)m_idata.data();
*(d+index++) = v1;
*(d+index++) = v2;
*(d+index++) = v3;
}
break;
case GL_UNSIGNED_INT: {
case TypeU32: {
quint32* d = (quint32*)m_idata.data();
*(d+index++) = v1;
*(d+index++) = v2;
Expand All @@ -128,13 +128,13 @@ void Geometry::allocate(int nbVertex, int nbIndex)
return;
}
switch (indexType()) {
case GL_UNSIGNED_BYTE:
case TypeU8:
m_idata.resize(nbIndex*sizeof(quint8));
break;
case GL_UNSIGNED_SHORT:
case TypeU16:
m_idata.resize(nbIndex*sizeof(quint16));
break;
case GL_UNSIGNED_INT:
case TypeU32:
m_idata.resize(nbIndex*sizeof(quint32));
break;
default:
Expand Down Expand Up @@ -199,7 +199,7 @@ void TexturedGeometry::setRect(const QRectF &r, const QRectF &tr, int texIndex)
{
setPoint(0, r.topLeft(), tr.topLeft(), texIndex);
setPoint(1, r.bottomLeft(), tr.bottomLeft(), texIndex);
switch (primitiveType()) {
switch (primitive()) {
case TriangleStrip:
setPoint(2, r.topRight(), tr.topRight(), texIndex);
setPoint(3, r.bottomRight(), tr.bottomRight(), texIndex);
Expand All @@ -219,7 +219,7 @@ void TexturedGeometry::setGeometryRect(const QRectF &r)
{
setGeometryPoint(0, r.topLeft());
setGeometryPoint(1, r.bottomLeft());
switch (primitiveType()) {
switch (primitive()) {
case TriangleStrip:
setGeometryPoint(2, r.topRight());
setGeometryPoint(3, r.bottomRight());
Expand All @@ -239,7 +239,7 @@ void TexturedGeometry::setTextureRect(const QRectF &tr, int texIndex)
{
setTexturePoint(0, tr.topLeft(), texIndex);
setTexturePoint(1, tr.bottomLeft(), texIndex);
switch (primitiveType()) {
switch (primitive()) {
case TriangleStrip:
setTexturePoint(2, tr.topRight(), texIndex);
setTexturePoint(3, tr.bottomRight(), texIndex);
Expand Down
8 changes: 4 additions & 4 deletions src/opengl/Geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ Q_AV_EXPORT QDebug operator<<(QDebug debug, const Attribute &a);
class Geometry {
public:
/// Strip or Triangles is preferred by ANGLE. The values are equal to opengl
enum PrimitiveType {
enum Primitive {
Triangles = 0x0004,
TriangleStrip = 0x0005, //default
TriangleFan = 0x0006, // Not recommended
};
Geometry(int vertexCount = 0, int indexCount = 0, DataType indexType = TypeU16);
virtual ~Geometry() {}
PrimitiveType primitiveType() const {return m_primitive;}
void setPrimitiveType(PrimitiveType value) { m_primitive = value;}
Primitive primitive() const {return m_primitive;}
void setPrimitive(Primitive value) { m_primitive = value;}
int vertexCount() const {return m_vcount;}
void setVertexCount(int value) {m_vcount = value;} // TODO: remove, or allocate data here
// TODO: setStride and no virtual
Expand All @@ -98,7 +98,7 @@ class Geometry {
*/
void allocate(int nbVertex, int nbIndex = 0);
protected:
PrimitiveType m_primitive;
Primitive m_primitive;
DataType m_itype;
int m_vcount;
int m_icount;
Expand Down
26 changes: 17 additions & 9 deletions src/opengl/GeometryRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace QtAV {

GeometryRenderer::GeometryRenderer()
: program(NULL)
, g(NULL)
, try_vbo(true)
, try_vao(true)
, try_ibo(true)
Expand All @@ -40,8 +41,9 @@ void GeometryRenderer::setShaderProgram(QOpenGLShaderProgram *sp)
program = sp;
}

bool GeometryRenderer::updateBuffers(Geometry *g)
bool GeometryRenderer::updateBuffers(Geometry *geo)
{
g = geo;
if (!g) {
vbo.destroy();
#if QT_VAO
Expand Down Expand Up @@ -106,8 +108,10 @@ bool GeometryRenderer::updateBuffers(Geometry *g)
return true;
}

void GeometryRenderer::bindBuffers(Geometry *g)
void GeometryRenderer::bindBuffers()
{
if (!g)
return;
if (try_ibo && ibo.isCreated())
ibo.bind();
#if QT_VAO
Expand Down Expand Up @@ -137,8 +141,10 @@ void GeometryRenderer::bindBuffers(Geometry *g)
}
}

void GeometryRenderer::unbindBuffers(Geometry *g)
void GeometryRenderer::unbindBuffers()
{
if (!g)
return;
if (try_ibo && ibo.isCreated())
ibo.release();
#if QT_VAO
Expand All @@ -147,21 +153,23 @@ void GeometryRenderer::unbindBuffers(Geometry *g)
return;
}
#endif //QT_VAO
for (int an = 0; an < g->attributes().size(); ++an) {
program->disableAttributeArray(an); //TODO: in setActiveShader
}
// release vbo. qpainter is affected if vbo is bound
if (try_vbo && vbo.isCreated()) {
vbo.release();
}
for (int an = 0; an < g->attributes().size(); ++an) {
program->disableAttributeArray(an); //TODO: in setActiveShader
}
}

void GeometryRenderer::render(Geometry *g)
void GeometryRenderer::render()
{
if (!g)
return;
if (g->indexCount() > 0) {
DYGL(glDrawElements(g->primitiveType(), g->indexCount(), g->indexType(), g->indexData()));
DYGL(glDrawElements(g->primitive(), g->indexCount(), g->indexType(), g->indexData()));
} else {
DYGL(glDrawArrays(g->primitiveType(), 0, g->vertexCount()));
DYGL(glDrawArrays(g->primitive(), 0, g->vertexCount()));
}
}
} //namespace QtAV
11 changes: 6 additions & 5 deletions src/opengl/GeometryRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ class GeometryRenderer
{
public:
GeometryRenderer();
void setShaderProgram(QOpenGLShaderProgram *sp);
void setShaderProgram(QOpenGLShaderProgram *sp); // TODO: remove this if use gl api directly
/// assume attributes are bound in the order 0, 1, 2,....
/// null geometry: release vao/vbo
bool updateBuffers(Geometry* g = NULL);
void bindBuffers(Geometry* g);
void render(Geometry* g);
void unbindBuffers(Geometry *g);
bool updateBuffers(Geometry* geo = NULL);
void bindBuffers();
void render();
void unbindBuffers();
private:
QOpenGLShaderProgram *program;
Geometry *g;
// TODO: setFeatures(VAO|VBO)
bool try_vbo; // check environment var and opengl support
bool try_vao;
Expand Down
8 changes: 4 additions & 4 deletions src/opengl/OpenGLVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class OpenGLVideoPrivate : public DPtrPrivate<OpenGLVideo>
// update geometry(vertex array) set attributes or bind VAO/VBO.
void bindAttributes(VideoShader* shader, const QRectF& t, const QRectF& r);
void unbindAttributes(VideoShader*) {
gr.unbindBuffers(&geometry);
gr.unbindBuffers();
}
public:
QOpenGLContext *ctx;
Expand Down Expand Up @@ -120,7 +120,7 @@ void OpenGLVideoPrivate::bindAttributes(VideoShader* shader, const QRectF &t, co
}
}
if (!update_geo) {
gr.bindBuffers(&geometry);
gr.bindBuffers();
return;
}
//qDebug("updating geometry...");
Expand All @@ -135,7 +135,7 @@ void OpenGLVideoPrivate::bindAttributes(VideoShader* shader, const QRectF &t, co
}
update_geo = false;
gr.updateBuffers(&geometry);
gr.bindBuffers(&geometry);
gr.bindBuffers();
}

OpenGLVideo::OpenGLVideo() {}
Expand Down Expand Up @@ -305,7 +305,7 @@ void OpenGLVideo::render(const QRectF &target, const QRectF& roi, const QMatrix4
DYGL(glEnable(GL_BLEND));
DYGL(glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA));
}
d.gr.render(&d.geometry);
d.gr.render();
if (blending)
DYGL(glDisable(GL_BLEND));
// d.shader->program()->release(); //glUseProgram(0)
Expand Down
2 changes: 1 addition & 1 deletion src/opengl/SubImagesGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ SubImagesGeometry::SubImagesGeometry()
, m_w(0)
, m_h(0)
{
setPrimitiveType(Geometry::Triangles);
setPrimitive(Geometry::Triangles);
m_attributes << Attribute(TypeF32, 2)
<< Attribute(TypeF32, 2, 2*sizeof(float))
#if U8COLOR
Expand Down
6 changes: 3 additions & 3 deletions src/opengl/SubImagesRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ void SubImagesRenderer::render(const SubImageSet &ass, const QRect &target, cons
m_program.setUniformValue("u_Texture", 0);
m_program.setUniformValue("u_Matrix", transform*m_mat);
m_renderer->setShaderProgram(&m_program);
m_renderer->bindBuffers(m_geometry);
m_renderer->bindBuffers();
DYGL(glEnable(GL_BLEND));
if (m_geometry->images().format() == SubImageSet::ASS)
gl().BlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
else
gl().BlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

m_renderer->render(m_geometry);
m_renderer->render();

DYGL(glDisable(GL_BLEND));
m_renderer->unbindBuffers(m_geometry);
m_renderer->unbindBuffers();
}

void SubImagesRenderer::setProjectionMatrixToRect(const QRectF &v)
Expand Down

0 comments on commit 5626f23

Please sign in to comment.