コード例 #1
0
 void SphereUVModifier::Modify(TriangleBuffer& Buffer)
 {
     for( VertexIterator VertIt = Buffer.GetVertices().begin() ; VertIt != Buffer.GetVertices().end() ; ++VertIt )
     {
         Vector3 NormalizedPos = VertIt->Position.GetNormal();
         Vector2 VecXZ(NormalizedPos.X,NormalizedPos.Z);
         VertIt->UV.X = Vector2::Unit_X().AngleTo(VecXZ) / MathTools::GetTwoPi();
         VertIt->UV.Y = ( MathTools::ATan(NormalizedPos.Y / VecXZ.Length()) + MathTools::GetHalfPi() ) / MathTools::GetPi();
     }
 }
コード例 #2
0
            void BoxUVModifier::Modify(TriangleBuffer& Buffer)
            {
                static const Vector3 Directions[6] = { Vector3::Unit_X(), Vector3::Unit_Y(), Vector3::Unit_Z(), Vector3::Neg_Unit_X(), Vector3::Neg_Unit_Y(), Vector3::Neg_Unit_Z()  };

                for( VertexIterator VertIt = Buffer.GetVertices().begin() ; VertIt != Buffer.GetVertices().end() ; ++VertIt )
                {
                    Vector3 VertPos = VertIt->Position - this->BoxPosition;
                    if( !VertPos.IsZeroLength() ) {
                        //VertPos.Normalize();
                        VertPos.X /= ( this->BoxHalfExtents.X * 2.0 );
                        VertPos.Y /= ( this->BoxHalfExtents.Y * 2.0 );
                        VertPos.Z /= ( this->BoxHalfExtents.Z * 2.0 );
                        Vector3 VertNorm = VertIt->Normal;
                        Real MaxAxis = 0;
                        Integer PrincipalAxis = 0;
                        for( UInt8 AxisIndex = 0 ; AxisIndex < 6 ; ++AxisIndex )
                        {
                            if( Directions[AxisIndex].DotProduct(VertNorm) > MaxAxis ) {
                                MaxAxis = Directions[AxisIndex].DotProduct(VertNorm);
                                PrincipalAxis = AxisIndex;
                            }
                        }

                        Vector3 vX, vY;
                        if( PrincipalAxis % 3 == 1 ) {
                            vY = Vector3::Unit_X();
                        }else{
                            vY = Vector3::Unit_Y();
                        }
                        vX = Directions[PrincipalAxis].CrossProduct(vY);

                        Vector2 UVCoord( 0.5 - vX.DotProduct(VertPos), 0.5 - vY.DotProduct(VertPos) );
                        if( this->Mapping == BoxUVModifier::MT_Full ) {
                            VertIt->UV = UVCoord;
                        }else if( this->Mapping == BoxUVModifier::MT_Cross ) {
                            // Too lazy to think of a more elegant solution
                            switch( PrincipalAxis )
                            {
                                case 1:  VertIt->UV = Vector2( ( UVCoord.X + 2 ) / 4, ( UVCoord.Y + 1 ) / 3 );  break;  // 3,2
                                case 2:  VertIt->UV = Vector2( ( UVCoord.X + 1 ) / 4, UVCoord.Y / 3 );          break;  // 2,1
                                case 3:  VertIt->UV = Vector2( ( UVCoord.X + 3 ) / 4, ( UVCoord.Y + 1 ) / 3 );  break;  // 4,2
                                case 4:  VertIt->UV = Vector2( UVCoord.X / 4, ( UVCoord.Y + 1 ) / 3 );          break;  // 1,2
                                case 5:  VertIt->UV = Vector2( ( UVCoord.X + 1 ) / 4, ( UVCoord.Y + 2 ) / 3 );  break;  // 2,3
                                case 6:  VertIt->UV = Vector2( ( UVCoord.X + 1 ) / 4, ( UVCoord.Y + 1 ) / 3 );  break;  // 2,2
                            }
                        }else if( this->Mapping == BoxUVModifier::MT_Packed ) {
                            VertIt->UV = Vector2( ( UVCoord.X + PrincipalAxis % 3 ) / 3, ( UVCoord.Y + PrincipalAxis / 3 ) / 2 );
                        }
                    }
                }
            }
コード例 #3
0
            void VertexNormalsModifier::Modify(TriangleBuffer& Buffer)
            {
                const IndexContainer& Indices = Buffer.GetIndices();
                VertexContainer& Vertices = Buffer.GetVertices();

                if( this->Compute == VertexNormalsModifier::CM_Triangle ) {
                    for( Whole Index = 0 ; Index < Indices.size() ; Index += 3 )
                    {
                        Vector3 v1 = Vertices[Indices[Index]].Position;
                        Vector3 v2 = Vertices[Indices[Index+1]].Position;
                        Vector3 v3 = Vertices[Indices[Index+2]].Position;
                        Vector3 VertNormal = (v2-v1).CrossProduct(v3-v1).GetNormal();
                        Vertices[Indices[Index]].Normal = VertNormal;
                        Vertices[Indices[Index+1]].Normal = VertNormal;
                        Vertices[Indices[Index+2]].Normal = VertNormal;
                    }
                }else if( this->Compute == VertexNormalsModifier::CM_Vertex ) {
                    std::vector< std::vector<Vector3> > TempNormals;
                    TempNormals.resize(Vertices.size());
                    for( Whole Index = 0 ; Index < Indices.size() ; Index += 3 )
                    {
                        Vector3 v1 = Vertices[Indices[Index]].Position;
                        Vector3 v2 = Vertices[Indices[Index+1]].Position;
                        Vector3 v3 = Vertices[Indices[Index+2]].Position;
                        Vector3 VertNormal = (v2-v1).CrossProduct(v3-v1);
                        TempNormals[Indices[Index]].push_back(VertNormal);
                        TempNormals[Indices[Index+1]].push_back(VertNormal);
                        TempNormals[Indices[Index+2]].push_back(VertNormal);
                    }
                    for( Whole CurrVertex = 0 ; CurrVertex < Vertices.size() ; ++CurrVertex )
                    {
                        Vector3 VertNormal(0.0,0.0,0.0);
                        for( Whole CurrNormal = 0 ; CurrNormal < TempNormals[CurrVertex].size() ; ++CurrNormal )
                        {
                            VertNormal += TempNormals[CurrVertex][CurrNormal];
                        }
                        Vertices[CurrVertex].Normal = VertNormal.GetNormal();
                    }
                }
            }