Skip to content

Commit

Permalink
gl: sphere is a textured geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Oct 26, 2016
1 parent 5c480c5 commit e3dcb67
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 88 deletions.
122 changes: 53 additions & 69 deletions src/opengl/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

#include "Geometry.h"
#include <QtDebug>
#include <QtMath>
#include <QtCore/qmath.h>

namespace QtAV {

Attribute::Attribute(DataType type, int tupleSize, int offset, bool normalize)
Expand Down Expand Up @@ -196,6 +197,7 @@ bool Geometry::compare(const Geometry *other) const
TexturedGeometry::TexturedGeometry()
: Geometry()
, nb_tex(0)
, geo_rect(-1, 1, 2, -2) // (-1, -1, 2, 2) flip y
{
setVertexCount(4);
a = QVector<Attribute>()
Expand All @@ -207,18 +209,10 @@ TexturedGeometry::TexturedGeometry()

void TexturedGeometry::setTextureCount(int value)
{
if (value < 1)
value = 1;
if (value == nb_tex)
return;
texRect.resize(value);
nb_tex = 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, int((i+1)* 2*sizeof(float)));
} else {
a.resize(value + 1);
}
}

int TexturedGeometry::textureCount() const
Expand Down Expand Up @@ -268,64 +262,80 @@ void TexturedGeometry::setRect(const QRectF &r, const QRectF &tr, int texIndex)

void TexturedGeometry::setGeometryRect(const QRectF &r)
{
setGeometryPoint(0, r.topLeft());
setGeometryPoint(1, r.bottomLeft());
switch (primitive()) {
case TriangleStrip:
setGeometryPoint(2, r.topRight());
setGeometryPoint(3, r.bottomRight());
break;
case TriangleFan:
setGeometryPoint(3, r.topRight());
setGeometryPoint(2, r.bottomRight());
break;
case Triangles:
break;
default:
break;
}
geo_rect = r;
}

void TexturedGeometry::setTextureRect(const QRectF &tr, int texIndex)
{
setTexturePoint(0, tr.topLeft(), texIndex);
setTexturePoint(1, tr.bottomLeft(), texIndex);
if (texRect.size() <= texIndex)
texRect.resize(texIndex+1);
texRect[texIndex] = tr;
}

const QVector<Attribute>& TexturedGeometry::attributes() const
{
return a;
}

void TexturedGeometry::create()
{
allocate(vertexCount());
if (a.size()-1 < textureCount()) { // the first is position
for (int i = a.size()-1; i < textureCount(); ++i)
a << Attribute(TypeF32, 2, int((i+1)* 2*sizeof(float)));
} else {
a.resize(textureCount() + 1);
}

setGeometryPoint(0, geo_rect.topLeft());
setGeometryPoint(1, geo_rect.bottomLeft());
switch (primitive()) {
case TriangleStrip:
setTexturePoint(2, tr.topRight(), texIndex);
setTexturePoint(3, tr.bottomRight(), texIndex);
setGeometryPoint(2, geo_rect.topRight());
setGeometryPoint(3, geo_rect.bottomRight());
break;
case TriangleFan:
setTexturePoint(3, tr.topRight(), texIndex);
setTexturePoint(2, tr.bottomRight(), texIndex);
setGeometryPoint(3, geo_rect.topRight());
setGeometryPoint(2, geo_rect.bottomRight());
break;
case Triangles:
break;
default:
break;
}
}

const QVector<Attribute>& TexturedGeometry::attributes() const
{
return a;
for (int i = 0; i < texRect.size(); ++i) {
const QRectF tr = texRect[i];
setTexturePoint(0, tr.topLeft(), i);
setTexturePoint(1, tr.bottomLeft(), i);
switch (primitive()) {
case TriangleStrip:
setTexturePoint(2, tr.topRight(), i);
setTexturePoint(3, tr.bottomRight(), i);
break;
case TriangleFan:
setTexturePoint(3, tr.topRight(), i);
setTexturePoint(2, tr.bottomRight(), i);
break;
case Triangles:
break;
default:
break;
}
}
}


Sphere::Sphere()
: Geometry()
, nb_tex(0)
, ru(128)
, rv(128)
: TexturedGeometry()
, r(1)
{
setPrimitive(Triangles);
setVertexCount((ru+1)*(rv+1));
setResolution(128, 128);
a = QVector<Attribute>()
<< Attribute(TypeF32, 3, 0)
<< Attribute(TypeF32, 2, 3*sizeof(float))
;
setTextureCount(1);
}

void Sphere::setResolution(int w, int h)
Expand All @@ -345,33 +355,6 @@ float Sphere::radius() const
return r;
}

void Sphere::setTextureCount(int value)
{
if (value < 1)
value = 1;
if (value == nb_tex)
return;
texRect.resize(value);
nb_tex = value;
}

int Sphere::textureCount() const
{
return nb_tex;
}

void Sphere::setTextureRect(const QRectF &tr, int texIndex)
{
if (texRect.size() <= texIndex)
texRect.resize(texIndex+1);
texRect[texIndex] = tr;
}

const QVector<Attribute>& Sphere::attributes() const
{
return a;
}

void Sphere::create()
{
allocate(vertexCount(), ru*rv*3*2); // quads * 2 triangles,
Expand All @@ -382,6 +365,7 @@ void Sphere::create()
a.resize(nb_tex + 1);
}

// TODO: use geo_rect?
float *vd = (float*)m_vdata.constData();
const float dTheta = M_PI*2.0/float(ru);
const float dPhi = M_PI/float(rv);
Expand Down
29 changes: 11 additions & 18 deletions src/opengl/Geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,44 +132,37 @@ class TexturedGeometry : public Geometry {
*/
void setTextureCount(int value);
int textureCount() const;
void setPoint(int index, const QPointF& p, const QPointF& tp, int texIndex = 0);
void setGeometryPoint(int index, const QPointF& p);
void setTexturePoint(int index, const QPointF& tp, int texIndex = 0);

void setRect(const QRectF& r, const QRectF& tr, int texIndex = 0);
void setGeometryRect(const QRectF& r);
void setTextureRect(const QRectF& tr, int texIndex = 0);
int stride() const Q_DECL_OVERRIDE { return 3*sizeof(float)+2*sizeof(float)*textureCount(); }
int stride() const Q_DECL_OVERRIDE { return 2*sizeof(float)*(textureCount()+1); }
const QVector<Attribute>& attributes() const Q_DECL_OVERRIDE;
virtual void create();
private:
void setPoint(int index, const QPointF& p, const QPointF& tp, int texIndex = 0);
void setGeometryPoint(int index, const QPointF& p);
void setTexturePoint(int index, const QPointF& tp, int texIndex = 0);
protected:
int nb_tex;
QRectF geo_rect;
QVector<QRectF> texRect;
QVector<Attribute> a;
};

class Sphere : public Geometry {
class Sphere : public TexturedGeometry {
public:
Sphere();
void setResolution(int w, int h); // >= 2x2
void setRadius(float value);
float radius() const;
/*!
* \brief setTextureCount
* sometimes we needs more than 1 texture coordinates, for example we have to set rectangle texture
* coordinates for each plane.
*/
void setTextureCount(int value);
int textureCount() const;
void setTextureRect(const QRectF& tr, int texIndex = 0);
void create();
void create() Q_DECL_OVERRIDE;
int stride() const Q_DECL_OVERRIDE { return 3*sizeof(float)+2*sizeof(float)*textureCount(); }
const QVector<Attribute>& attributes() const Q_DECL_OVERRIDE;
protected:
using Geometry::setPrimitive;
private:
int nb_tex;
int ru, rv;
float r;
QVector<QRectF> texRect;
QVector<Attribute> a;
};
} //namespace QtAV
#endif //QTAV_GEOMETRY_H
5 changes: 4 additions & 1 deletion src/opengl/OpenGLVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ void OpenGLVideoPrivate::updateGeometry(VideoShader* shader, const QRectF &t, co
// setTextureCount may change the vertex data. Call it before setRect()
qDebug() << "target rect: " << target_rect ;
geometry.setTextureCount(shader->textureTarget() == GL_TEXTURE_RECTANGLE ? tc : 1);
geometry.setRect(target_rect, material->mapToTexture(0, roi));
geometry.setGeometryRect(target_rect);
geometry.setTextureRect(material->mapToTexture(0, roi));
if (shader->textureTarget() == GL_TEXTURE_RECTANGLE) {
for (int i = 1; i < tc; ++i) {
// tc can > planes, but that will compute chroma plane
geometry.setTextureRect(material->mapToTexture(i, roi), i);
}
}
geometry.create();
update_geo = false;
gr.updateGeometry(&geometry);
}
Expand Down Expand Up @@ -240,6 +242,7 @@ void OpenGLVideo::setViewport(const QRectF &r)
d.rect = r;
if (d.norm_viewport) {
d.matrix.setToIdentity();
//d.matrix.perspective(45, 1, 0.1, 100);
} else {
d.matrix.setToIdentity();
d.matrix.ortho(r);
Expand Down

0 comments on commit e3dcb67

Please sign in to comment.