Exemplo n.º 1
0
/*!
    \property QGLMaterial::textureUrl
    \brief URL of the 2D texture associated with \a layer on this material.

    By default \a layer is 0, the primary texture.

    If the URL has not been specified, then this property is a null QUrl.

    Setting this property to a non-empty URL will replace any existing texture
    with a new texture based on the image at the given \a url.  If that
    image is not a valid texture then the new texture will be a null texture.

    If an empty url is set, this has the same effect as \c{setTexture(0)}.

    \sa texture(), setTexture()
*/
QUrl QGLMaterial::textureUrl(int layer) const
{
    Q_D(const QGLMaterial);
    QGLTexture2D *tex = d->textures.value(layer, 0);
    if (tex)
        return tex->url();
    else
        return QUrl();
}
Exemplo n.º 2
0
void ImageViewer::setFile(const QString &fileName)
{
    QGLTexture2D *tex = body->material()->texture();
    if (tex) {
        tex->release();
        tex->deleteLater();
    }
    tex = new QGLTexture2D;
    tex->setImage(QImage(fileName.isEmpty() ? defaultImage : fileName));
    body->material()->setTexture(tex);
}
Exemplo n.º 3
0
/*!
    \internal
*/
void QGLMaterial::bindTextures(QGLPainter *painter)
{
    Q_D(const QGLMaterial);
    QMap<int, QGLTexture2D *>::ConstIterator it;
    for (it = d->textures.begin(); it != d->textures.end(); ++it) {
        QGLTexture2D *tex = it.value();
        painter->glActiveTexture(GL_TEXTURE0 + it.key());
        if (tex)
            tex->bind();
        else
            glBindTexture(GL_TEXTURE_2D, 0);
    }
}
Exemplo n.º 4
0
void QGLMaterial::setTextureUrl(const QUrl &url, int layer)
{
    Q_ASSERT(layer >= 0);
    if (textureUrl(layer) != url)
    {
        QGLTexture2D *tex = 0;
        if (!url.isEmpty())
        {
            tex = new QGLTexture2D(this);
            connect(tex, SIGNAL(textureUpdated()), this, SIGNAL(texturesChanged()));
            tex->setUrl(url);
        }
        setTexture(tex, layer);
    }
}
Exemplo n.º 5
0
/*!
    Constructs a QGLTexture2D object that wraps the supplied literal
    texture identifier \a id, with the dimensions specified by \a size.

    The \a id is assumed to have been created by the application in
    the current GL context, and it will be destroyed by the application
    after the returned QGLTexture2D object is destroyed.

    This function is intended for interfacing to existing code that
    uses raw GL texture identifiers.  The returned QGLTexture2D can
    only be used with the current GL context.

    \sa textureId()
*/
QGLTexture2D *QGLTexture2D::fromTextureId(GLuint id, const QSize& size)
{
    const QGLContext *ctx = QGLContext::currentContext();
    if (!id || !ctx)
        return 0;

    QGLTexture2D *texture = new QGLTexture2D();
    if (!size.isNull())
        texture->setSize(size);
    QGLTexture2DTextureInfo *info = new QGLTexture2DTextureInfo
    (ctx, id, texture->d_ptr->imageGeneration,
     texture->d_ptr->parameterGeneration, true);
    texture->d_ptr->infos = info;
    return texture;
}
Exemplo n.º 6
0
void QGeoTileCache::GLContextAvailable()
{
    QMutexLocker ml(&cleanupMutex_);

    /* Throttle the cleanup to 10 items/frame to avoid blocking the render
     * for too long. Normally only 6-20 tiles are on screen at a time so
     * eviction rates shouldn't be much higher than this. */
    int todo = qMin(cleanupList_.size(), 10);
    for (int i = 0; i < todo; ++i) {
        QGLTexture2D *texture = cleanupList_.front();
        if (texture) {
            texture->release();
            texture->cleanupResources();
            delete texture;
        }
        cleanupList_.pop_front();
    }
}
Exemplo n.º 7
0
void loadProperty(const QString &property, QTextStream &value) {
    if (property == "material") {
        QGLMaterial *mat = new QGLMaterial();
        QString name, file;
        value >> name >> file;

        if (file != "-") {
            QGLTexture2D *tex = new QGLTexture2D();
            tex->setImage(QImage(dataDir + file));
            mat->setTexture(tex);
            mat->setTextureCombineMode(QGLMaterial::Decal);
        }

        int r, g, b;
        value >> r >> g >> b; if (r != -1) mat->setAmbientColor(QColor(r, g, b));
        value >> r >> g >> b; if (r != -1) mat->setDiffuseColor(QColor(r, g, b));
        value >> r >> g >> b; if (r != -1) mat->setSpecularColor(QColor(r, g, b));
        qreal shin; value >> shin; mat->setShininess(shin);

        palette.insert(name, mat);

    } else if (property == "size") {
Exemplo n.º 8
0
static QGLMaterial *qCreateFluid()
{
    QImage image(QSize(128,128), QImage::Format_ARGB32);
    QRgb col = qRgba(rval(), rval(), rval(), 196);
    image.fill(col);
    QPainter painter(&image);
    QLinearGradient linearGrad(QPointF(56, 56), QPointF(72, 72));
    linearGrad.setColorAt(0, col);
    linearGrad.setColorAt(1, QColor(col).lighter());
    linearGrad.setSpread(QGradient::ReflectSpread);
    painter.fillRect(image.rect(), linearGrad);
    painter.end();
    QGLMaterial *mat = new QGLMaterial;
    QColor white(Qt::white);
    white.setAlpha(128);
    mat->setAmbientColor(white);
    mat->setDiffuseColor(white);
    QGLTexture2D *tex = new QGLTexture2D(mat);
    tex->setImage(image);
    mat->setTexture(tex);
    return mat;
}
Exemplo n.º 9
0
/*!
  Initializes the Qt3D materials used in the application.
*/
void GameView::initializeMaterials()
{
    m_MaterialCollection = new QGLMaterialCollection(this);

    QGLTexture2D *lightTexture = new QGLTexture2D;
    lightTexture->setImage(QImage(":/reflectionmap.png"));

    QColor ambientColor = QColor(64, 64, 190);

    QGLMaterial *material = new QGLMaterial;
    material->setObjectName("PauseButtonMaterial");
    QGLTexture2D *texture = new QGLTexture2D;
    texture->setImage(QImage(":/pause_button.png"));
    material->setTexture(texture);
    m_MaterialCollection->addMaterial(material);

    // Black hole material
    material = new QGLMaterial;
    material->setObjectName("BlackHoleMaterial");
    texture = new QGLTexture2D;
    texture->setImage(QImage(":/BlackHole.jpg"));
    material->setTexture(texture);
    texture = new QGLTexture2D;
    texture->setImage(QImage(":/bh_sprial.png"));
    material->setTexture(texture, 1);
    material->setTextureCombineMode(QGLMaterial::Replace);
    m_MaterialCollection->addMaterial(material);


    // Black hole center material
    material = new QGLMaterial;
    material->setObjectName("BlackHoleFrontMaterial");
    texture = new QGLTexture2D;
    texture->setImage(QImage(":/bh_sprial.png"));
    material->setTexture(texture);
    material->setTexture(lightTexture, 1);
    m_MaterialCollection->addMaterial(material);

    // Platform material
    material = new QGLMaterial;
    material->setObjectName("PlatformMaterial");
    texture = new QGLTexture2D;
    texture->setImage(QImage(":/platformtex.png"));
    material->setTexture(texture);
    m_MaterialCollection->addMaterial(material);

    // Blok material
    material = new QGLMaterial;
    material->setObjectName("BlokMaterial");
    texture = new QGLTexture2D;
    texture->setImage(QImage(":/SimpleBlock.png"));
    material->setTexture(texture);
    material->setTexture(lightTexture, 1);
    material->setAmbientColor(ambientColor);
    material->setShininess(0.2f);
    m_MaterialCollection->addMaterial(material);

    // Ball materials
    material = new QGLMaterial;
    material->setObjectName("BallMaterial1");
    material->setAmbientColor(ambientColor);
    material->setDiffuseColor(QColor(255, 255, 64));
    material->setTexture(texture, 0);
    material->setTexture(lightTexture, 1);
    material->setShininess(1.0f);
    material->setSpecularColor(material->diffuseColor());
    m_MaterialCollection->addMaterial(material);

    material = new QGLMaterial;
    material->setObjectName("BallMaterial2");
    material->setAmbientColor(ambientColor);
    material->setDiffuseColor(QColor(255, 64, 64));
    material->setTexture(texture, 0);
    material->setTexture(lightTexture, 1);
    material->setShininess(1.0f);
    material->setSpecularColor(material->diffuseColor());
    m_MaterialCollection->addMaterial(material);

    material = new QGLMaterial;
    material->setObjectName("BallMaterial3");
    material->setDiffuseColor(QColor(64, 64, 255));
    material->setAmbientColor(ambientColor);
    material->setTexture(texture, 0);
    material->setTexture(lightTexture, 1);
    material->setShininess(1.0f);
    material->setSpecularColor(material->diffuseColor());
    m_MaterialCollection->addMaterial(material);

    material = new QGLMaterial;
    material->setObjectName("BallMaterial4");
    material->setDiffuseColor(QColor(64, 255, 64));
    material->setAmbientColor(ambientColor);
    material->setTexture(texture, 0);
    material->setTexture(lightTexture,1);
    material->setShininess(1.0f);
    material->setSpecularColor(material->diffuseColor());
    m_MaterialCollection->addMaterial(material);

    material = new QGLMaterial;
    material->setObjectName("LightFlareMaterial");
    texture = new QGLTexture2D;
    texture->setImage(QImage(":/flare.png"));
    material->setTexture(texture);
    m_MaterialCollection->addMaterial(material);

    material = new QGLMaterial;
    material->setObjectName("FontMaterial");
    texture = new QGLTexture2D;
    texture->setImage(QImage(":/scorefont.png"));
    material->setTexture(texture);
    m_MaterialCollection->addMaterial(material);
}
Exemplo n.º 10
0
void Mg3dScene::drawText(QGLPainter * painter, const QStringList& str)
{
	QString longest;
	const int minWidth = 100;
	for (int i = 0; i < str.size(); ++i)
	{
		if(str[i].length()>longest.length())
			longest = str[i];
	}

	QFont f = font();
	f.setPointSize(11);

	QFontMetrics metrics(f);

	QRect rect = metrics.boundingRect(longest);

	if(rect.width()<minWidth)
		rect.setWidth(minWidth);

	QImage image(
			QSize(rect.width()+10,rect.height()*str.size()+10),
			QImage::Format_ARGB32
			);
	image.fill(0);
	QPainter p2(&image);
	p2.setFont(f);
	p2.setPen(Qt::yellow);
	p2.setRenderHint(QPainter::TextAntialiasing);
	for(int y= 0;y<str.size();++y)
		p2.drawText(
				0 ,
				((y+1) * rect.height()),
				" "+str[y]
		);


	p2.end();

	QGLTexture2D texture;
	texture.setImage(image);
	texture.bind();
	glDisable(GL_DEPTH_TEST);


	d_ptr->textureDisplayShader.setActive(painter,true);

	QVector2D size((float)image.width()/width(),
			(float)image.height()/height());
	QVector2D center(
			remapVal(size.x()/2,0,1,-1,1),
			remapVal(1-size.y()/2,0,1,-1,1));

	d_ptr->textureDisplayShader.setCenter(center);
	d_ptr->textureDisplayShader.setSize(size);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D,texture.textureId());
	drawFboVertices(painter);
	d_ptr->textureDisplayShader.setActive(painter,false);
	texture.release();

	glEnable(GL_DEPTH_TEST);
}
Exemplo n.º 11
0
void tst_QGLRender::sequence()
{
    QSKIP("QWidget: Cannot create a QWidget when no GUI is being used");
    QSharedPointer<QGLMaterialCollection> palette(new QGLMaterialCollection());

    // create a yellow lit material
    QGLMaterial *mat = new QGLMaterial;
    mat->setAmbientColor(Qt::yellow);
    int ix0 = palette->addMaterial(mat);

    // create a blue lit material
    mat = new QGLMaterial;
    mat->setAmbientColor(Qt::blue);
    int ix1 = palette->addMaterial(mat);

    // create a grey textured material
    int tx0;
    {
        QImage uv(1024, 1024, QImage::Format_ARGB32);
        uv.fill(qRgba(196, 196, 196, 196));
        mat = new QGLMaterial;
        mat->setAmbientColor(Qt::gray);
        QGLTexture2D *tex = new QGLTexture2D;
        tex->setImage(uv);
        mat->setTexture(tex);
        tx0 = palette->addMaterial(mat);
    }

    QGLSceneNode *scene = new QGLSceneNode;
    scene->setPalette(palette);
    QGLSceneNode *node = 0;
    QGLSceneNode *prim;
    {
        QGLBuilder builder(palette);
        QVector3D a(-1.0f, -1.0f, 0.0f);
        QVector3D b(1.0f, -1.0f, 0.0f);
        QVector3D c(1.0f, 1.0f, 0.0f);
        QGeometryData p;
        p.appendVertex(a, b, c);
        p.generateTextureCoordinates();
        builder.addTriangles(p);
        prim = builder.currentNode();
        prim->setMaterialIndex(ix0);
        builder.newSection();
        QVector3D d(-1.2f, -1.2f, 0.0f);
        QVector3D e(1.2f, -1.2f, 0.0f);
        QVector3D f(1.2f, 1.2f, 0.0f);
        QVector3D g(-1.2f, 1.2f, 0.0f);
        QGeometryData q;
        q.appendVertex(d, e, f, g);
        q.generateTextureCoordinates();
        builder.addQuads(q);
        prim = builder.currentNode();
        prim->setMaterialIndex(ix1);
        node = builder.finalizedSceneNode();
    }

    scene->addNode(node);
    QGLSceneNode *cl = prim->clone(scene);
    cl->setMaterialIndex(tx0);
    cl->setEffect(QGL::LitDecalTexture2D);

    TestView widget(scene);
    if (!widget.context()->isValid())
        QSKIP("GL Implementation not valid");

    TestPainter *ptr = new TestPainter(&widget);

    widget.paintGL(ptr);

    QList<int> starts = ptr->starts();
    QList<int> counts = ptr->counts();
    QCOMPARE(starts.at(0), 0);
    QCOMPARE(counts.at(0), 3);
    QCOMPARE(starts.at(1), 3);
    QCOMPARE(counts.at(1), 6);
}