Пример #1
0
inline void AssimpScene::retrieveColor(
    const aiMaterial * mtl
,   const char * pKey
,   const unsigned int type
,   const unsigned int index
,   QVector4D & color
,   const float r
,   const float g
,   const float b
,   const float a) 
{
    aiColor4D acolor;
    if (AI_SUCCESS == aiGetMaterialColor(mtl, pKey, type, index, &acolor))
    {
        color.setX(acolor.r);
        color.setY(acolor.g);
        color.setZ(acolor.b);
        color.setW(acolor.a);
    }
    else
    {
        color.setX(r);
        color.setY(g);
        color.setZ(b);
        color.setW(a);
    }
}
Пример #2
0
void PropertyGroup::getValue(QVector4D & v)
{
	v.setX(properties.at(0)->getValue());
	v.setY(properties.at(1)->getValue());
	v.setZ(properties.at(2)->getValue());
	v.setW(properties.at(3)->getValue());
}
Пример #3
0
void GLWidget::_fromScreenToWorld(QVector3D *answer, const QVector4D &screenCoordinates, bool forcedHeight, double height)
{
    QVector4D worldCoordinates;
    if(projection == PERSPECTIVE)
    {
        TwoDimArray a(finalMatrixInverse.data());

        QVector4D screenCoordPersp;
        screenCoordinatesPerspective(&screenCoordPersp, a, height, screenCoordinates);
        if(!forcedHeight && screenCoordPersp.z() < 0)
        {
            height = int(camera[projection].position().z()) + 2 * (camera[projection].position().z() > 0) - 1;
            screenCoordinatesPerspective(&screenCoordPersp, a, height, screenCoordinates);
        }
        worldCoordinates = finalMatrixInverse * screenCoordPersp;
        worldCoordinates.setZ(height);
    }
    else worldCoordinates = finalMatrixInverse * screenCoordinates;
    *answer = QVector3D(worldCoordinates);
}
Пример #4
0
bool PropertyMatrixModel::setData(const QModelIndex &index, const QVariant &data, int role)
{
  if (!index.isValid())
    return false;

  if (role != Qt::EditRole)
    return false;

  bool ok = false;
  float floatData = data.toFloat(&ok);

  if (!ok)
    return false;

  switch (m_matrix.type()) {
    case QVariant::Vector2D: {
      QVector2D value = m_matrix.value<QVector2D>();
      switch (index.row()) {
        case 0: value.setX(floatData); break;
        case 1: value.setY(floatData); break;
      }

      m_matrix = value;
      break;
    }

    case QVariant::Vector3D: {
      QVector3D value = m_matrix.value<QVector3D>();
      switch (index.row()) {
        case 0: value.setX(floatData); break;
        case 1: value.setY(floatData); break;
        case 2: value.setZ(floatData); break;
      }

      m_matrix = value;
      break;
    }

    case QVariant::Vector4D: {
      QVector4D value = m_matrix.value<QVector4D>();
      switch (index.row()) {
        case 0: value.setX(floatData); break;
        case 1: value.setY(floatData); break;
        case 2: value.setZ(floatData); break;
        case 3: value.setW(floatData); break;
      }

      m_matrix = value;
      break;
    }

#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
    case QVariant::Quaternion: {
      float pitch, yaw, roll;

      const QQuaternion value = m_matrix.value<QQuaternion>();
      value.getEulerAngles(&pitch, &yaw, &roll);
      switch (index.row()) {
        case 0: pitch = floatData; break;
        case 1: yaw = floatData; break;
        case 2: roll = floatData; break;
      }

      m_matrix = QQuaternion::fromEulerAngles(pitch, yaw, roll);
      break;
    }
#endif

    case QVariant::Matrix: {
      QMatrix value = m_matrix.value<QMatrix>();

      switch (index.row() << 4 | index.column()) {
        case 0x00: value.setMatrix(floatData, value.m12(), value.m21(), value.m22(), value.dx(), value.dy()); break;
        case 0x01: value.setMatrix(value.m11(), floatData, value.m21(), value.m22(), value.dx(), value.dy()); break;
        case 0x10: value.setMatrix(value.m11(), value.m12(), floatData, value.m22(), value.dx(), value.dy()); break;
        case 0x11: value.setMatrix(value.m11(), value.m12(), value.m21(), floatData, value.dx(), value.dy()); break;
        case 0x20: value.setMatrix(value.m11(), value.m12(), value.m21(), value.m22(), floatData, value.dy()); break;
        case 0x21: value.setMatrix(value.m11(), value.m12(), value.m21(), value.m22(), value.dx(), floatData); break;
      }

      m_matrix = value;
      break;
    }

    case QVariant::Transform: {
      QTransform value = m_matrix.value<QTransform>();

      switch (index.row() << 4 | index.column()) {
        case 0x00: value.setMatrix(floatData, value.m12(), value.m13(), value.m21(), value.m22(), value.m23(), value.m31(), value.m32(), value.m33()); break;
        case 0x01: value.setMatrix(value.m11(), floatData, value.m13(), value.m21(), value.m22(), value.m23(), value.m31(), value.m32(), value.m33()); break;
        case 0x02: value.setMatrix(value.m11(), value.m12(), floatData, value.m21(), value.m22(), value.m23(), value.m31(), value.m32(), value.m33()); break;
        case 0x10: value.setMatrix(value.m11(), value.m12(), value.m13(), floatData, value.m22(), value.m23(), value.m31(), value.m32(), value.m33()); break;
        case 0x11: value.setMatrix(value.m11(), value.m12(), value.m13(), value.m21(), floatData, value.m23(), value.m31(), value.m32(), value.m33()); break;
        case 0x12: value.setMatrix(value.m11(), value.m12(), value.m13(), value.m21(), value.m22(), floatData, value.m31(), value.m32(), value.m33()); break;
        case 0x20: value.setMatrix(value.m11(), value.m12(), value.m13(), value.m21(), value.m22(), value.m23(), floatData, value.m32(), value.m33()); break;
        case 0x21: value.setMatrix(value.m11(), value.m12(), value.m13(), value.m21(), value.m22(), value.m23(), value.m31(), floatData, value.m33()); break;
        case 0x22: value.setMatrix(value.m11(), value.m12(), value.m13(), value.m21(), value.m22(), value.m23(), value.m31(), value.m32(), floatData); break;
      }

      m_matrix = value;
      break;
    }

    case QVariant::Matrix4x4: {
      QMatrix4x4 value = m_matrix.value<QMatrix4x4>();

      value(index.row(), index.column()) = floatData;

      m_matrix = value;
      break;
    }
  }

  emit dataChanged(index, index);

  return true;
}
void CombinedNavRenderer::leftMouseDown( int x, int y )
{
    float xf = static_cast<float>( x );
    float yf = static_cast<float>( m_height - y );

    QVector4D test;
    test.setX( ( 2 * xf ) / m_width - 1 );
    test.setY( ( 2 * yf ) / m_height - 1 );
    test.setZ( 1 );
    test.setW( 1 );

    int xout;
    int yout;

    QVector4D out = m_mvpMatrix.inverted() * test;
    QModelIndex mi;
    if ( m_ratio > 1.5 )
    {
        xout = out.x() / m_dx;
        if ( ( (float)out.x()  / m_dx ) < m_nx )
        {
            xout = qMax( 0, qMin( xout, static_cast<int>( m_nx - 1.0 ) ) );
            yout = out.y() / m_dy;
            yout = qMax( 0, qMin( yout, static_cast<int>( m_ny - 1.0 ) ) );
            mi = model()->index( (int)Fn::Global::SAGITTAL_CORONAL, 0 );
        }
        else if ( ( (float)out.x() / m_dx ) < m_nx * 2  )
        {
            xout = xout - m_nx;
            xout = qMax( 0, qMin( xout, static_cast<int>( m_nx - 1.0 ) ) );
            yout = out.y() / m_dz;
            yout = qMax( 0, qMin( yout, static_cast<int>( m_nz - 1.0 ) ) );
            mi = model()->index( (int)Fn::Global::SAGITTAL_AXIAL, 0 );
        }
        else
        {
            xout = xout - ( 2 * m_nx );
            xout = qMax( 0, qMin( m_ny - xout - 1, static_cast<int>( m_ny - 1.0 ) ) );
            yout = out.y() / m_dz;
            yout = qMax( 0, qMin( yout, static_cast<int>( m_nz - 1.0 ) ) );
            mi = model()->index( (int)Fn::Global::CORONAL_AXIAL, 0 );
        }
    }
    else if ( m_ratio < 0.66 )
    {
        yout = out.y() / m_dz;
        if ( ( (float)out.y() / m_dz ) < m_nz )
        {
            xout = out.x() / m_dy;
            xout = qMax( 0, qMin( m_ny - xout - 1, static_cast<int>( m_ny - 1.0 ) ) );
            yout = qMax( 0, qMin( yout, static_cast<int>( m_nz - 1.0 ) ) );
            mi = model()->index( (int)Fn::Global::CORONAL_AXIAL, 0 );
        }
        else if ( ( (float)out.y() / m_dz ) < m_nz * 2  )
        {
            xout = out.x() / m_dx;
            xout = qMax( 0, qMin( xout, static_cast<int>( m_nx - 1.0 ) ) );
            yout = yout - m_nz;
            yout = qMax( 0, qMin( yout, static_cast<int>( m_nz - 1.0 ) ) );
            mi = model()->index( (int)Fn::Global::SAGITTAL_AXIAL, 0 );
        }
        else
        {
            xout = out.x() / m_dx;
            xout = qMax( 0, qMin( xout, static_cast<int>( m_nx - 1.0 ) ) );
            yout = yout - ( 2 * m_nz );
            yout = qMax( 0, qMin( yout, static_cast<int>( m_ny - 1.0 ) ) );
            mi = model()->index( (int)Fn::Global::SAGITTAL_CORONAL, 0 );
        }
    }
    else
    {
        xout = out.x() / m_dx;
        yout = out.y() / m_dy;
        if ( ( (float)out.x()  / m_dx ) < m_nx )
        {
            if ( ( (float)out.y() / m_dy ) < m_nz )
            {
                xout = qMax( 0, qMin( xout, static_cast<int>( m_nx - 1.0 ) ) );
                yout = qMax( 0, qMin( yout, static_cast<int>( m_ny - 1.0 ) ) );
                mi = model()->index( (int)Fn::Global::SAGITTAL_CORONAL, 0 );
            }
            else
            {
                xout = qMax( 0, qMin( xout, static_cast<int>( m_nx - 1.0 ) ) );
                yout = yout - m_ny;
                yout = qMax( 0, qMin( yout, static_cast<int>( m_nz - 1.0 ) ) );
                mi = model()->index( (int)Fn::Global::SAGITTAL_AXIAL, 0 );
            }
        }
        else
        {
            xout = xout - m_nx;
            xout = qMax( 0, qMin( m_ny - xout- 1, static_cast<int>( m_ny - 1.0 ) ) );
            yout = yout - m_ny;
            yout = qMax( 0, qMin( yout, static_cast<int>( m_nz - 1.0 ) ) );
            mi = model()->index( (int)Fn::Global::CORONAL_AXIAL, 0 );
        }
    }
    QPoint p( xout, yout );

    if ( mi.isValid() )
    {
        model()->setData( mi, p );
    }
}
static MeshPtr importObject(QDataStream &stream)
{
    using namespace Ogre;

    QVector4D bbMin, bbMax;
    stream >> bbMin >> bbMax;

    float distance, distanceSquared; // Here's a bug for you: writes "double"'s instead of floats
    stream >> distanceSquared >> distance;

    MeshPtr ogreMesh = MeshManager::getSingleton().createManual("conversion",
                                                               ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

    int vertexCount, indexCount;
    stream >> vertexCount >> indexCount;

    VertexData *vertexData = new VertexData();
    ogreMesh->sharedVertexData = vertexData;

    LogManager::getSingleton().logMessage("Reading geometry...");
    VertexDeclaration* decl = vertexData->vertexDeclaration;
    VertexBufferBinding* bind = vertexData->vertexBufferBinding;
    unsigned short bufferId = 0;

    // Information for calculating bounds
    Vector3 min = Vector3::ZERO, max = Vector3::UNIT_SCALE, pos = Vector3::ZERO;
    Real maxSquaredRadius = -1;
    bool firstVertex = true;

    /*
      Create a vertex definition for our buffer
      */
    size_t offset = 0;

    const VertexElement &positionElement = decl->addElement(bufferId, offset, VET_FLOAT3, VES_POSITION);
    offset += VertexElement::getTypeSize(VET_FLOAT3);

    const VertexElement &normalElement = decl->addElement(bufferId, offset, VET_FLOAT3, VES_NORMAL);
    offset += VertexElement::getTypeSize(VET_FLOAT3);

    // calculate how many vertexes there actually are
    vertexData->vertexCount = vertexCount;

    // Now create the vertex buffer
    HardwareVertexBufferSharedPtr vbuf = HardwareBufferManager::getSingleton().
            createVertexBuffer(offset, vertexData->vertexCount,
                               HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);

    // Bind it
    bind->setBinding(bufferId, vbuf);

    // Lock it
    unsigned char *pVert = static_cast<unsigned char*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
    unsigned char *pVertStart = pVert;

    QVector<float> positions;
    positions.reserve(vertexCount * 3);

    // Iterate over all children (vertexbuffer entries)
    for (int i = 0; i < vertexCount; ++i) {
        float *pFloat;

        QVector4D vertex;
        stream >> vertex;
		vertex.setZ(vertex.z() * -1);

        /* Copy over the position */
        positionElement.baseVertexPointerToElement(pVert, &pFloat);
        *(pFloat++) = (float)vertex.x();
        *(pFloat++) = (float)vertex.y();
        *(pFloat++) = (float)vertex.z();

        positions.append(vertex.x());
        positions.append(vertex.y());
        positions.append(vertex.z());

        /* While we're at it, calculate the bounding sphere */
        pos.x = vertex.x();
        pos.y = vertex.y();
        pos.z = vertex.z();

        if (firstVertex) {
            min = max = pos;
            maxSquaredRadius = pos.squaredLength();
            firstVertex = false;
        } else {
            min.makeFloor(pos);
            max.makeCeil(pos);
            maxSquaredRadius = qMax(pos.squaredLength(), maxSquaredRadius);
        }

        pVert += vbuf->getVertexSize();
    }

    // Set bounds
    const AxisAlignedBox& currBox = ogreMesh->getBounds();
    Real currRadius = ogreMesh->getBoundingSphereRadius();
    if (currBox.isNull())
    {
        //do not pad the bounding box
        ogreMesh->_setBounds(AxisAlignedBox(min, max), false);
        ogreMesh->_setBoundingSphereRadius(Math::Sqrt(maxSquaredRadius));
    }
    else
    {
        AxisAlignedBox newBox(min, max);
        newBox.merge(currBox);
        //do not pad the bounding box
        ogreMesh->_setBounds(newBox, false);
        ogreMesh->_setBoundingSphereRadius(qMax(Math::Sqrt(maxSquaredRadius), currRadius));
    }

    /*
       Create faces
     */
    // All children should be submeshes
    SubMesh* sm = ogreMesh->createSubMesh();
    sm->setMaterialName("clippingMaterial");
    sm->operationType = RenderOperation::OT_TRIANGLE_LIST;
    sm->useSharedVertices = true;

    // tri list
    sm->indexData->indexCount = indexCount;

    // Allocate space
    HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton().
            createIndexBuffer(
                HardwareIndexBuffer::IT_16BIT,
                sm->indexData->indexCount,
                HardwareBuffer::HBU_DYNAMIC,
                false);
    sm->indexData->indexBuffer = ibuf;

    unsigned short *pShort = static_cast<unsigned short*>(ibuf->lock(HardwareBuffer::HBL_DISCARD));

    QVector<EdgeData::Triangle> triangles(indexCount / 3);

    for (int i = 0; i < indexCount / 3; ++i) {
        quint16 i1, i2, i3;

        stream >> i1 >> i2 >> i3;
        *pShort++ = i1;
        *pShort++ = i2;
        *pShort++ = i3;

        triangles[i].vertIndex[0] = i1;
        triangles[i].vertIndex[1] = i2;
        triangles[i].vertIndex[2] = i3;

    }

    /* Recalculate the vertex normals */
    Vector4 *faceNormals = (Vector4*)_aligned_malloc(sizeof(Vector4) * triangles.size(), 16);

    OptimisedUtil *util = OptimisedUtil::getImplementation();
    util->calculateFaceNormals(positions.constData(),
                               triangles.data(),
                               faceNormals,
                               indexCount / 3);

	 // Iterate over all children (vertexbuffer entries)
	pVert = pVertStart;
    for (int i = 0; i < vertexCount; ++i) {
        float *pFloat;

		Vector3 normal = Vector3::ZERO;
		
		int count = 0;

		/* Search for all faces that use this vertex */
		for (int j = 0; j < triangles.size(); ++j) {
			if (triangles[j].vertIndex[0] == i 
				|| triangles[j].vertIndex[1] == i 
				|| triangles[j].vertIndex[2] == i) {
				normal.x += faceNormals[j].x / faceNormals[j].w;
				normal.y += faceNormals[j].y / faceNormals[j].w;
				normal.z += faceNormals[j].z / faceNormals[j].w;
				count++;
			}
		}

		normal.normalise();

        /* Copy over the position */
		normalElement.baseVertexPointerToElement(pVert, &pFloat);
        *(pFloat++) = normal.x;
        *(pFloat++) = normal.y;
        *(pFloat++) = normal.z;
		
        pVert += vbuf->getVertexSize();
    }

    _aligned_free(faceNormals);

    vbuf->unlock();
    ibuf->unlock();

    return ogreMesh;
}