void Bitmap3DRender::createScene() { Q_D(Bitmap3DRender); Q_ASSERT(d->bitmap != NULL); QGLBuilder builder(&d->materials); int const width = d->bitmap->width () / 2; int const height = d->bitmap->height() / 2; int const depth = d->bitmap->depth () / 2; for (int z = -depth; z < depth; z++) { for (int y = -height; y < depth; y++) { for (int x = -width; x < width; x++) { QColor color = d->bitmap->pixel(x + width, y + height, z + depth); if (color != Qt::transparent) { builder << QGL::Faceted << QGLSphere(0.1f, 1); builder.currentNode()->setPosition(QVector3D( x / d->scale, y / d->scale, z / d->scale)); builder.currentNode()->setMaterial(material(color)); } } } } d->scene.reset(builder.finalizedSceneNode()); d->scene->setEffect(QGL::LitDecalTexture2D); }
sphere3d::sphere3d(float x_, float y_, float z_, float rad, int r, int g, int b, int a) : x(x_), y(y_), z(z_), radius(rad), node(NULL), col(r, g, b, a) { QGLBuilder builder; builder << QGLSphere(radius * 2); node = builder.finalizedSceneNode(); //EDEBUG("win3d added: " << describe()); }
QGLSceneNode * Boid::boidObject() { static QGLSceneNode *v = 0; if (!v) { QGLBuilder build; build << QGLSphere(2.0f); v = build.finalizedSceneNode(); v->setObjectName("Boid"); QGraphicsRotation3D * rot = new QGraphicsRotation3D(v); rot->setAngle(90.0f); rot->setAxis(QVector3D(1, 0, 0)); v->addTransform(rot); v->setY(-v->boundingBox().minimum().y()); } return v; }
/* Creates an individual randomly positioned ball */ QGLSceneNode *ModelBall::createNode(QGLMaterial *material) { const float defaultSize = 1.0; const float positionX = -1.5; const float positionY = 40.0; const float randomPositionScale = 1000.0; QGLBuilder builder; QGLSceneNode *node; builder.newNode(); builder << QGLSphere(defaultSize); node = builder.finalizedSceneNode(); node->setMaterial(material); node->setEffect(QGL::LitMaterial); QGraphicsTranslation3D *trans = new QGraphicsTranslation3D(); node->addTransform(trans); QGraphicsTranslation3D *transStart = new QGraphicsTranslation3D(); QVector3D *randomStart = new QVector3D(positionX + randomPosition()/randomPositionScale, positionY + randomPosition()/randomPositionScale, randomPosition()/randomPositionScale); transStart->setTranslate(*randomStart); node->addTransform(transStart); return node; }
/*! \internal */ void SphereMesh::createGeometry() { // We cache a maximum of 10 levels of detail for lod animations. // Create a new geometry node for this level of detail if necessary. QGLSceneNode *geometry = d->lodGeometry.value(d->lod, 0); if (!geometry) { QGLBuilder builder; builder.newSection(QGL::Faceted); builder << QGLSphere(2.0f, d->lod); geometry = builder.finalizedSceneNode(); geometry->setParent(this); d->lodGeometry.insert(d->lod, geometry); } Q_ASSERT_X(geometry != 0, Q_FUNC_INFO, "Could not create/find geometry!"); if (d->currentSphere != geometry) { if (d->currentSphere) d->topNode->removeNode(d->currentSphere); d->topNode->addNode(geometry); d->currentSphere = geometry; } // Set the radius as a scale on the modelview transformation. // This way, we don't have to regenerate the geometry every // frame if the radius is being animated. if (d->radius != 1.0f) { if (!d->scale) { d->scale = new QGraphicsScale3D(d->topNode); d->topNode->addTransform(d->scale); } if (d->scale->scale().x() != d->radius) { d->scale->setScale(QVector3D(d->radius, d->radius, d->radius)); } } else { // If there is already a scale set it to be the identity scale. // This case is optimised for in QGraphicsScale. Removing it from // the transform list is too expensive, especially if the size is // being animated, and the next frame will recreate it. if (d->scale) d->scale->setScale(QVector3D(1, 1, 1)); } // Also rotate the geometry into the correct axis orientation. const QVector3D Y_AXIS = QVector3D(0, 1, 0); // for Qt::XAxis we rotate around Y const QVector3D X_AXIS = QVector3D(1, 0, 0); // for Qt::YAxis we rotate around X if (d->axis != Qt::ZAxis && !d->rot) { d->rot = new QGraphicsRotation3D(d->topNode); d->topNode->addTransform(d->rot); } if (d->axis == Qt::XAxis && d->rot->axis().y() != Y_AXIS.y()) { d->rot->setAxis(Y_AXIS); d->rot->setAngle(90.0f); } else if (d->axis == Qt::YAxis && d->rot->axis().x() != X_AXIS.x()) { d->rot->setAxis(X_AXIS); d->rot->setAngle(-90.0f); } else if (d->axis == Qt::ZAxis && d->rot && d->rot->angle() != 0.0f) { d->rot->setAngle(0.0f); d->rot->setAxis(QVector3D(0, 0, 0)); } if (!d->sceneSet) { setScene(new SphereScene(d->topNode)); d->sceneSet = true; } }