⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testclodmesh.cpp

📁 《3D游戏引擎设计》的源码
💻 CPP
字号:
// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// FREE SOURCE CODE
// http://www.magic-software.com/License.html/free.pdf

#include "TestCLodMesh.h"

//---------------------------------------------------------------------------
MgcApplication* MgcApplication::Create ()
{
    return new TestCLodMesh;
}
//---------------------------------------------------------------------------
TestCLodMesh::TestCLodMesh ()
    :
    MgcApplication("TestCLodMesh",640,480,0,1)
{
    // set up bindings for camera turret
    m_fTrnSpeed = 0.1;
    m_fRotSpeed = 0.1;
    m_kTurret.BindKeysToTranslateRight('[',']',m_fTrnSpeed);
    m_kTurret.BindKeysToTranslateUp('{','}',m_fTrnSpeed);
    m_kTurret.BindKeysToTranslateDirection(VK_DOWN,VK_UP,m_fTrnSpeed);
    m_kTurret.BindKeysToRotateRight(VK_PRIOR,VK_NEXT,0.1*m_fRotSpeed);
    m_kTurret.BindKeysToRotateUp(VK_RIGHT,VK_LEFT,0.1*m_fRotSpeed);

    // initialize 'last time'
    MeasureTime();
}
//---------------------------------------------------------------------------
MgcCLodMesh* TestCLodMesh::CreateSphere ()
{
    // create a sphere by one subdivision of an icosahedron

    const MgcReal gold = 0.5f*(MgcMath::Sqrt(5.0f)+1.0f);

    unsigned int uiNumVerts = 12;
    MgcVector3* pVertex = new MgcVector3[12];
    pVertex[0] = MgcVector3( gold,  1.0f,  0.0f );
    pVertex[1] = MgcVector3(-gold,  1.0f,  0.0f );
    pVertex[2] = MgcVector3( gold, -1.0f,  0.0f );
    pVertex[3] = MgcVector3(-gold, -1.0f,  0.0f );
    pVertex[4] = MgcVector3( 1.0f,  0.0f,  gold );
    pVertex[5] = MgcVector3( 1.0f,  0.0f, -gold );
    pVertex[6] = MgcVector3(-1.0f,  0.0f,  gold );
    pVertex[7] = MgcVector3(-1.0f,  0.0f, -gold );
    pVertex[8] = MgcVector3( 0.0f,  gold,  1.0f );
    pVertex[9] = MgcVector3( 0.0f, -gold,  1.0f );
    pVertex[10] = MgcVector3( 0.0f,  gold, -1.0f );
    pVertex[11] = MgcVector3( 0.0f, -gold, -1.0f );

    unsigned int uiNumTris = 20;
    unsigned int* pConnect = new unsigned int[3*20];
    pConnect[ 0] =  0;  pConnect[ 1] =  8;  pConnect[ 2] =  4;
    pConnect[ 3] =  0;  pConnect[ 4] =  5;  pConnect[ 5] = 10;
    pConnect[ 6] =  2;  pConnect[ 7] =  4;  pConnect[ 8] =  9;
    pConnect[ 9] =  2;  pConnect[10] = 11;  pConnect[11] =  5;
    pConnect[12] =  1;  pConnect[13] =  6;  pConnect[14] =  8;
    pConnect[15] =  1;  pConnect[16] = 10;  pConnect[17] =  7;
    pConnect[18] =  3;  pConnect[19] =  9;  pConnect[20] =  6;
    pConnect[21] =  3;  pConnect[22] =  7;  pConnect[23] = 11;
    pConnect[24] =  0;  pConnect[25] = 10;  pConnect[26] =  8;
    pConnect[27] =  1;  pConnect[28] =  8;  pConnect[29] = 10;
    pConnect[30] =  2;  pConnect[31] =  9;  pConnect[32] = 11;
    pConnect[33] =  3;  pConnect[34] = 11;  pConnect[35] =  9;
    pConnect[36] =  4;  pConnect[37] =  2;  pConnect[38] =  0;
    pConnect[39] =  5;  pConnect[40] =  0;  pConnect[41] =  2;
    pConnect[42] =  6;  pConnect[43] =  1;  pConnect[44] =  3;
    pConnect[45] =  7;  pConnect[46] =  3;  pConnect[47] =  1;
    pConnect[48] =  8;  pConnect[49] =  6;  pConnect[50] =  4;
    pConnect[51] =  9;  pConnect[52] =  4;  pConnect[53] =  6;
    pConnect[54] = 10;  pConnect[55] =  5;  pConnect[56] =  7;
    pConnect[57] = 11;  pConnect[58] =  7;  pConnect[59] =  5;


    MgcColor* pColor = new MgcColor[12];
    unsigned int i;
    for (i = 0; i < uiNumVerts; i++)
    {
        pColor[i] = MgcColor(MgcMath::UnitRandom(),0.0,
            MgcMath::UnitRandom());
        pVertex[i].Unitize();
    }

    return new MgcCLodMesh(uiNumVerts,pVertex,0,pColor,0,uiNumTris,pConnect);
}
//---------------------------------------------------------------------------
MgcCLodMesh* TestCLodMesh::CreateCLodMesh ()
{
    unsigned int uiN = 3;
    unsigned int uiVertexQuantity = (uiN+1)*(uiN+1);
    unsigned int uiTriangleQuantity = 2*uiN*uiN;
    MgcVector3* akVertex = new MgcVector3[uiVertexQuantity];
    MgcColor* akColor = new MgcColor[uiVertexQuantity];
    unsigned int* auiConnect = new unsigned int[3*uiTriangleQuantity];

    unsigned int uiI = 0;
    unsigned int uiX, uiY;
    for (uiY = 0; uiY <= uiN; uiY++)
    {
        MgcReal fY = 2.0*(uiY/MgcReal(uiN)) - 1.0;
        for (uiX = 0; uiX <= uiN; uiX++)
        {
            MgcReal fX = 2.0*(uiX/MgcReal(uiN)) - 1.0;

            akVertex[uiI].x = fX;
            akVertex[uiI].y = fY;
            akVertex[uiI].z = 1.0 - fX*fX - fY*fY;
            akColor[uiI] = MgcColor(MgcMath::UnitRandom(),0.0,
                MgcMath::UnitRandom());

            uiI++;
        }
    }

    unsigned int* puiConnect = auiConnect;
    unsigned int uiYStart = 0;
    for (uiY = 0; uiY < uiN; uiY++)
    {
        unsigned int uiI0 = uiYStart;
        uiYStart += uiN+1;
        unsigned int uiI1 = uiI0 + 1;
        unsigned int uiI2 = uiYStart;
        unsigned int uiI3 = uiI2 + 1;
        for (uiX = 0; uiX < uiN; uiX++)
        {
            *puiConnect++ = uiI0;
            *puiConnect++ = uiI1;
            *puiConnect++ = uiI2;
            *puiConnect++ = uiI1;
            *puiConnect++ = uiI3;
            *puiConnect++ = uiI2;
            uiI0++;
            uiI1++;
            uiI2++;
            uiI3++;
        }
    }

    MgcCLodMesh* pkMesh = new MgcCLodMesh(uiVertexQuantity,akVertex,0,
        akColor,0,uiTriangleQuantity,auiConnect);

    return pkMesh;
}
//---------------------------------------------------------------------------
bool TestCLodMesh::Initialize ()
{
    m_spkCamera = new MgcOglCamera(640,480);
    m_spkCamera->SetFrustum(1.0,100.0,-0.55,0.55,0.4125,-0.4125);
    MgcVector3 kCLoc(0.0,0.0,4.0);
    MgcVector3 kCLeft = -MgcVector3::UNIT_X;
    MgcVector3 kCUp = MgcVector3::UNIT_Y;
    MgcVector3 kCDir = -MgcVector3::UNIT_Z;
    m_spkCamera->SetFrame(kCLoc,kCLeft,kCUp,kCDir);
    m_spkRenderer = new MgcOglRenderer(GetWindowHandle(),640,480);
    m_spkRenderer->SetBackgroundColor(MgcColor(0.5f,0.0f,1.0f));
    m_spkRenderer->SetCamera(m_spkCamera);

    m_spkScene = new MgcNode;
    //m_spkCLod = CreateCLodMesh();
    m_spkCLod = CreateSphere();
    m_spkScene->AttachChild(m_spkCLod);

    m_spkWireframeState = new MgcWireframeState;
    m_spkScene->SetRenderState(m_spkWireframeState);

    m_spkZBufferState = new MgcZBufferState;
    m_spkZBufferState->Enabled() = true;
    m_spkZBufferState->Writeable() = true;
    m_spkZBufferState->Compare() = MgcZBufferState::CF_LEQUAL;
    m_spkScene->SetRenderState(m_spkZBufferState);

    m_spkCamera->Update();
    m_spkScene->UpdateGS(0.0);
    m_spkScene->UpdateRS();
    return true;
}
//---------------------------------------------------------------------------
void TestCLodMesh::Terminate ()
{
    m_spkWireframeState = 0;
    m_spkZBufferState = 0;
    m_spkCamera = 0;
    m_spkRenderer = 0;
    m_spkScene = 0;
}
//---------------------------------------------------------------------------
void TestCLodMesh::OnIdle ()
{
    MeasureTime();

    if ( GetFocus() == GetWindowHandle() )
    {
        if ( m_kTurret.ReadKeyboard() )
        {
            m_spkRenderer->GetCamera()->SetFrame(m_kTurret.GetTranslate(),
                m_kTurret.GetRotate());
        }
    }

    m_spkCLod->UpdateGS(0.0);
    m_spkRenderer->ClearBuffers();
    m_spkRenderer->Draw(m_spkScene);
    m_spkRenderer->DisplayBackBuffer();

    DrawFrameRate();
    UpdateClicks();
}
//---------------------------------------------------------------------------
bool TestCLodMesh::WmChar (char cCharCode, long lKeyData)
{
    switch ( cCharCode )
    {
    case '0':  // reset frame rate measurements
        ResetTime();
        return true;
    case '+':
        m_spkCLod->SetTargetRecord(m_spkCLod->GetTargetRecord()+1);
        return true;
    case '-':
        m_spkCLod->SetTargetRecord(m_spkCLod->GetTargetRecord()-1);
        return true;
    case 'w':
        m_spkWireframeState->Enabled() = !m_spkWireframeState->Enabled();
        return true;
    case 'q':
    case 'Q':
    case VK_ESCAPE:
        PostMessage(GetWindowHandle(),WM_DESTROY,0,0);
        return true;
    }

    return false;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -