📄 torussector.cpp
字号:
////////////////////////////////////////////////////////////////////////
//
// TorusSector.cpp
//
// Copyright (c) 2003 Nokia Mobile Phones Ltd. All rights reserved.
//
////////////////////////////////////////////////////////////////////////
#include "TorusSector.h"
#include "ScanConverter.h"
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
TTorusSector::TTorusSector(TInt aMajorRadius, TInt aMinorRadius, TMathLookup &aMath ) :
iRotAxis(0),
iRotTwist(0),
iPos()
{
// Generate verts:
{
TInt vert = 0;
TInt ringAng = -32;
TInt arcAng = 1024 / numArcs;
TInt arcAngOffset = 0;
TMatrix3x3 arcMat;
TVector3 arcVec(aMajorRadius, 0, 0);
TAffineTransform arcAff(arcVec);
TVector3 rawVert(0, aMinorRadius, 0);
TVector3 transVert;
for ( TInt ring = 0 ; ring <= numRings ; ring++ )
{
TMatrix3x3 ringMat;
ringMat.MakeRy( ring * ringAng, aMath );
TAffineTransform ringAff( ringMat );
for ( TInt arc = 0 ; arc < numArcs ; arc++ )
{
TInt thisArcAng = ( arcAng * arc ) + arcAngOffset;
arcMat.MakeRz(thisArcAng, aMath);
arcAff.SetMatrix(arcMat);
arcAff.SetVector(arcVec);
arcAff.Transform(rawVert, transVert);
ringAff.Transform(transVert, iVertex[ vert ]);
vert++;
}
arcAngOffset += arcAng >> 1;
}
}
// Generate faces:
{
TInt face = 0;
TInt vert = 0;
TInt vertIdx[ 3 ];
//
// Note: these values assume 16 bpp and a pixel format of 444 RGB
//
TUint16 faceColorOddA = 0x0fff;
TUint16 faceColorOddB = 0x007f;
TUint16 faceColorEvenA = 0x0fff;
TUint16 faceColorEvenB = 0x00f0;
for ( TInt ring = 0 ; ring < numRings ; ring++ )
{
TInt prev = vert + numArcs - 1;
TUint16 faceColorA;
TUint16 faceColorB;
if ( ( ring & 0x0001 ) == 0 )
{
faceColorA = faceColorEvenA;
faceColorB = faceColorEvenB;
}
else
{
faceColorA = faceColorOddA;
faceColorB = faceColorOddB;
}
for ( TInt arc = 0 ; arc < numArcs ; arc++ )
{
vertIdx[ 0 ] = prev;
vertIdx[ 1 ] = prev + numArcs;
vertIdx[ 2 ] = vert;
//
iFace[ face ] = TFace(faceColorA, 3, vertIdx, iVertex);
//
face++;
vertIdx[ 0 ] = prev + numArcs;
vertIdx[ 1 ] = vert + numArcs;
vertIdx[ 2 ] = vert;
//
iFace[ face ] = TFace(faceColorB, 3, vertIdx, iVertex);
//
face++;
prev = vert;
vert++;
}
}
}
}
////////////////////////////////////////////////////////////////////////
void TTorusSector::SetPosition(const TVector3 &aPos)
{
iPos = aPos;
}
////////////////////////////////////////////////////////////////////////
void TTorusSector::Simulate()
{
//
// Torus sector does not actually rotate continuously past camera:
// instead it rotates for 64 frames and then snaps back to its start
// position. This snap is not visible, however, since it coincides with
// the repeat in the torus mesh.
//
iRotAxis = ( iRotAxis + 1 ) & 0x3f;
iRotTwist++;
}
////////////////////////////////////////////////////////////////////////
void TTorusSector::Render( TScanConverter &aScanConverter, TMathLookup &aMath )
{
TMatrix3x3 matRAxis;
matRAxis.MakeRy(iRotAxis, aMath );
TAffineTransform affAxisAndPos(matRAxis);
affAxisAndPos.SetVector(iPos );
TMatrix3x3 matRTwist;
matRTwist.MakeRz(iRotTwist >> 2, aMath);
TAffineTransform affTwist(matRTwist);
TAffineTransform affModelToWorld;
affModelToWorld.MakeCompound(affAxisAndPos, affTwist);
TScreenVertex transVertex[ numVerts ];
// Transform verts into camera space:
TVector3 aCameraInModelSpace;
aScanConverter.TransformVerts(numVerts, &iVertex[ 0 ],
affModelToWorld, &transVertex[ 0 ], aCameraInModelSpace);
TInt face = numFaces - 1;
//
// Find illumination based on (arc) distance from camera
// and use this to shade base color:
//
TInt illumVariation = TShadedColor::illuminationUnity;
TInt illumStepPerRing = illumVariation / numRings;
TInt distanceToFarEnd = ( numRings << 5 ) - iRotAxis;
TInt illumFalloff = ( illumStepPerRing * distanceToFarEnd ) >> 5;
// Empirical tuning term to brighten up foreground:
TInt illumBias = ( illumStepPerRing * 4 ) >> 1;
TInt illum = TShadedColor::illuminationUnity + illumBias - illumFalloff;
for ( TInt ring = 0 ; ring < numRings ; ring++ )
{
for ( TInt arcFace = 0 ; arcFace < numArcFaces ; arcFace++ )
{
TInt illumClamped;
if ( illum < 0 )
{
illumClamped = 0;
}
else if ( illum > TShadedColor::illuminationUnity )
{
illumClamped = TShadedColor::illuminationUnity;
}
else
{
illumClamped = illum;
}
iFace[ face ].SetIllumination(illumClamped);
iFace[ face ].Render(aScanConverter, aMath, &transVertex[0], aCameraInModelSpace);
face--;
}
illum += illumStepPerRing;
}
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -