CMesh::CMesh(void)
{
    DrawSmooth = true;
    _CurBBMin = Vec3D<>(0,0,0);
    _CurBBMax = Vec3D<>(0,0,0);
    MeshChanged();

}
//---------------------------------------------------------------------------
void CMesh::Translate(Vec3D<> d)
//---------------------------------------------------------------------------
{// translate geometry
    for (int i=0; i<(int)Vertices.size(); i++) {
        Vertices[i].v += d;
    }
    UpdateBoundingBox();
    MeshChanged();
}
//---------------------------------------------------------------------------
void CMesh::RotZ(vfloat a)
//---------------------------------------------------------------------------
{
    for (int i=0; i<(int)Vertices.size(); i++) {
        Vertices[i].v.RotZ(a);
        Vertices[i].n.RotZ(a);
    }
    for (int i=0; i<(int)Facets.size(); i++) {
        Facets[i].n.RotZ(a);
    }
    UpdateBoundingBox();
    MeshChanged();

}
//---------------------------------------------------------------------------
void CMesh::AddFacet(const Vec3D<>& v1, const Vec3D<>& v2, const Vec3D<>& v3, const CColor& Col1, const CColor& Col2, const CColor& Col3, bool QuickAdd) //adds a facet... with color info
//---------------------------------------------------------------------------
{
    vfloat WeldThresh = 1e-10f; //This needs to be around the precision of a float.

    Vec3D<> Points[3]; //make a local array for easy referencing
    Points[0] = v1;
    Points[1] = v2;
    Points[2] = v3;
    CColor Colors[3];
    Colors[0] = Col1;
    Colors[1] = Col2;
    Colors[2] = Col3;


    int FoundIndex[3]; //each index for a triangle...

    for (int j=0; j<3; j++){ //each point in this facet
        FoundIndex[j] = -1;

        if (!QuickAdd){
            for (int k=Vertices.size()-1; k>=0; k--){ //DO THIS BACKWARDS!!!! (more likely to have just added one next to us...)
                if (abs(Points[j].x - Vertices[k].v.x) < WeldThresh  &&  abs(Points[j].y - Vertices[k].v.y) < WeldThresh  &&  abs(Points[j].z - Vertices[k].v.z) < WeldThresh){ //if points are identical...
                    FoundIndex[j] = k;
                    break; //kicks out of for loop, because we've found!
                }
            }
        }

        if (FoundIndex[j] == -1){ //if we didn't find one...
            CVertex ThisPoint;
            ThisPoint.v.x = Points[j].x;
            ThisPoint.v.y = Points[j].y;
            ThisPoint.v.z = Points[j].z;
            ThisPoint.VColor = Colors[j];

            Vertices.push_back(ThisPoint);
            FoundIndex[j] = (int)Vertices.size() - 1; //-1 because zero-index based.
        }

    }

    //	CFacet ThisFacet;
    //	for (int m=0; m<3; m++) ThisFacet.vi[m] = FoundIndex[m];

    Facets.push_back(CFacet(FoundIndex[0], FoundIndex[1], FoundIndex[2])); //TODO... select whether to create new object or add to existing...
    MeshChanged();

}
//---------------------------------------------------------------------------
void CMesh::Rotate(Vec3D<> ax, vfloat a)
//---------------------------------------------------------------------------
{

    for (int i=0; i<(int)Vertices.size(); i++) {
        Vertices[i].v = Vertices[i].v.Rot(ax, a);
        Vertices[i].n = Vertices[i].n.Rot(ax, a);
        Vertices[i].DrawOffset = Vertices[i].DrawOffset.Rot(ax, a);


    }
    for (int i=0; i<(int)Facets.size(); i++) {
        Facets[i].n = Facets[i].n.Rot(ax, a);
    }

    UpdateBoundingBox();
    MeshChanged();

}
//---------------------------------------------------------------------------
void CMesh::Scale(Vec3D<> s)
//---------------------------------------------------------------------------
{// scale geometry

    //check for zero scale factor
    if(s.x==0 || s.y==0 || s.z==0) return;
    for (int i=0; i<(int)Vertices.size(); i++) {
        Vertices[i].v.x *= s.x;
        Vertices[i].v.y *= s.y;
        Vertices[i].v.z *= s.z;
        //		Vertices[i].n.x *= s.x; //do we really want to scale these?
        //		Vertices[i].n.y *= s.y;
        //		Vertices[i].n.z *= s.z;
        ///		Facets[i].n.Normalize();
    }
    UpdateBoundingBox();
    MeshChanged();

}
//overload =
CMesh& CMesh::operator=(const CMesh& s) {

    Facets.resize(s.Facets.size());
    for (int i = 0; i<(int)Facets.size(); i++)
        Facets[i] = s.Facets[i];

    Vertices.resize(s.Vertices.size());
    for (int i = 0; i<(int)Vertices.size(); i++)
        Vertices[i] = s.Vertices[i];

    Lines.resize(s.Lines.size());
    for (int i = 0; i<(int)Lines.size(); i++)
        Lines[i] = s.Lines[i];

    DrawSmooth = s.DrawSmooth;
    _CurBBMin = s._CurBBMin;
    _CurBBMax = s._CurBBMax;

    MeshChanged();

    return *this;
}
bool CMesh::LoadSTL(std::string filename)
{
    FILE *fp;
    bool binary=false;
#ifdef WIN32
    fopen_s(&fp, filename.c_str(), "r"); //secure version. preferred on windows platforms...
#else
    fp = fopen(filename.c_str(), "r");
#endif

    if(fp == NULL) return false;

    /* Find size of file */
    fseek(fp, 0, SEEK_END);
    int file_size = ftell(fp);
    int facenum;
    /* Check for binary or ASCII file */
    fseek(fp, STL_LABEL_SIZE, SEEK_SET);
    fread(&facenum, sizeof(int), 1, fp);
    int expected_file_size=STL_LABEL_SIZE + 4 + (sizeof(short)+12*sizeof(float) )*facenum ;
    if(file_size ==  expected_file_size) binary = true;
    unsigned char tmpbuf[128];
    fread(tmpbuf,sizeof(tmpbuf),1,fp);
    for(unsigned int i = 0; i < sizeof(tmpbuf); i++){
        if(tmpbuf[i] > 127){
            binary=true;
            break;
        }
    }
    // Now we know if the stl file is ascii or binary.
    fclose(fp);
    bool RetVal;
    if(binary) RetVal =  LoadBinarySTL(filename);
    else RetVal = LoadAsciiSTL(filename);

    UpdateBoundingBox(); //get the bounding box here and now...
    MeshChanged();
    return RetVal;
}
void CMesh::WeldClose(float Distance)
{

    int* NumVertHere = new int[Vertices.size()]; //keeps track of how many vertices have been averaged to get here...
    int* ConsolidateMap = new int[Vertices.size()]; //maps the whole vertex list to the welded vertex list (IE has holes)
    int* OldNewMap = new int [Vertices.size()]; //maps the old, larger vertex list to the new, smaller one.
    for (int i=0; i<(int)Vertices.size(); i++){
        NumVertHere[i] = 1;
        ConsolidateMap[i] = i;
        OldNewMap[i] = -1;
    }

    for (int i=0; i<(int)Facets.size(); i++){ //look through facets so we don't have to do exhaustive On2 search of all vertex combos
        for (int j=0; j<3; j++){ //look at all three combinations of vertices...
            int Vi1 = Facets[i].vi[j];
            int np = -1; while (np != Vi1){ np = Vi1; Vi1 = ConsolidateMap[Vi1]; } //iterates NewMap to get the final value...

            int Vi2 = Facets[i].vi[(j+1)%3];
            np = -1; while (np != Vi2){ np = Vi2; Vi2 = ConsolidateMap[Vi2]; } //iterates NewMap to get the final value...

            if (Vi1 != Vi2 && (Vertices[Vi1].v-Vertices[Vi2].v).Length() < Distance){ //if they are close enough but not already the same...
                Vertices[Vi1].v = (Vertices[Vi1].v*NumVertHere[Vi1] + Vertices[Vi2].v*NumVertHere[Vi2]) / (NumVertHere[Vi1]+NumVertHere[Vi2]); //Vertex 1 is the weighted average
                NumVertHere[Vi1] = NumVertHere[Vi1] + NumVertHere[Vi2]; //count how many vertices make up this point now...

                ConsolidateMap[Vi2] = Vi1; //effectively deletes Vi2... (points to Vi1)
            }
        }
    }

    std::vector<CFacet> NewFacets;
    std::vector<CVertex> NewVertices;

    for (int i=0; i<(int)Vertices.size(); i++){
        if (ConsolidateMap[i] == i) { //if this vertex ended up being part of the welded part
            NewVertices.push_back(Vertices[i]); //add to the new vertex list
            OldNewMap[i] = NewVertices.size()-1;
        }
    }

    //update the vertex indices
    for (int i=0; i<(int)Facets.size(); i++){ //look through facets so we don't have to do exhaustive On2 search of all vertex combos
        for (int j=0; j<3; j++){ //look at all three combinations of vertices...
            int n = Facets[i].vi[j];
            int np = -1; while (np != n){ np = n; n = ConsolidateMap[n]; } //iterates NewMap to get the final value...

            Facets[i].vi[j] = OldNewMap[n];
        }
        if (!(Facets[i].vi[0] == Facets[i].vi[1] || Facets[i].vi[0] == Facets[i].vi[2] || Facets[i].vi[2] == Facets[i].vi[1])) //if there aren't any the same...
            NewFacets.push_back(Facets[i]);
    }

    Facets = NewFacets;
    Vertices = NewVertices;

    delete [] NumVertHere;
    NumVertHere = NULL;
    delete [] ConsolidateMap;
    ConsolidateMap = NULL;
    delete [] OldNewMap;
    OldNewMap = NULL;

    CalcVertNormals(); //re-calculate normals!
    MeshChanged();

}
示例#10
0
void EC_MediaPlayer::PrepareComponent()
{
    // Don't do anything if rendering is not enabled
    if (!ViewEnabled() || GetFramework()->IsHeadless())
        return;

    // Some security checks
    if (componentPrepared_)
    {
        LogWarning("EC_MediaPlayer: Preparations seem to be done already, you might not want to do this multiple times.");
    }
    if (!mediaPlayer_)
    {
        LogError("EC_MediaPlayer: Cannot start preparing, webview object is null. This should never happen!");
        return;
    }

    // Get parent and connect to the component removed signal.
    Entity *parent = ParentEntity();
    if (parent)
        connect(parent, SIGNAL(ComponentRemoved(IComponent*, AttributeChange::Type)), SLOT(ComponentRemoved(IComponent*, AttributeChange::Type)), Qt::UniqueConnection);
    else
    {
        LogError("EC_MediaPlayer: Could not get parent entity pointer!");
        return;
    }

    // Get EC_Mesh component
    EC_Mesh *mesh = GetMeshComponent();
    if (!mesh)
    {
        // Wait for EC_Mesh to be added.
        connect(parent, SIGNAL(ComponentAdded(IComponent*, AttributeChange::Type)), SLOT(ComponentAdded(IComponent*, AttributeChange::Type)), Qt::UniqueConnection);
        return;
    }
    else
    {
        // Inspect if this mesh is ready for rendering. EC_Mesh being present != being loaded into Ogre and ready for rendering.
        if (!mesh->GetEntity())
        {
            connect(mesh, SIGNAL(MeshChanged()), SLOT(TargetMeshReady()), Qt::UniqueConnection);
            return;
        }
        else
            connect(mesh, SIGNAL(MaterialChanged(uint, const QString&)), SLOT(TargetMeshMaterialChanged(uint, const QString&)), Qt::UniqueConnection);
    }

    if (sceneCanvasName_.isEmpty())
        sceneCanvasName_ = "VlcMediaPlayerCanvas-" + QUuid::createUuid().toString().replace("{", "").replace("}", "");

    // Get or create local EC_WidgetCanvas component
    ComponentPtr iComponent = parent->GetOrCreateComponent(EC_WidgetCanvas::TypeNameStatic(), sceneCanvasName_, AttributeChange::LocalOnly, false);
    EC_WidgetCanvas *sceneCanvas = dynamic_cast<EC_WidgetCanvas*>(iComponent.get());
    if (!sceneCanvas)
    {
        LogError("EC_MediaPlayer: Could not get or create EC_WidgetCanvas component!");
        return;
    }
    sceneCanvas->SetTemporary(true);
    sceneCanvas->SetSelfIllumination(getilluminating());

    // All the needed components are present, mark prepared as true.
    componentPrepared_ = true;

    // We are now prepared, check enabled state and restore possible materials now
    if (!getenabled())
        sceneCanvas->RestoreOriginalMeshMaterials();

    // Show downloading info icon or if not downloading, 
    // ask for a image update from the player
    if (pendingMediaDownload_)
        OnFrameUpdate(downloadingLogo_);
    else
        mediaPlayer_->ForceUpdateImage();
}