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

📄 wmlgeometry.cpp

📁 3D Game Engine Design Source Code非常棒
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Magic Software, Inc.
// http://www.magic-software.com
// http://www.wild-magic.com
// Copyright (c) 2003.  All Rights Reserved
//
// The Wild Magic Library (WML) source code is supplied under the terms of
// the license agreement http://www.magic-software.com/License/WildMagic.pdf
// and may not be copied or disclosed except in accordance with the terms of
// that agreement.

#include "WmlGeometry.h"
#include "WmlRenderer.h"
using namespace Wml;

WmlImplementRTTI(Geometry,Spatial);
WmlImplementStream(Geometry);

//----------------------------------------------------------------------------
Geometry::Geometry (int iVertexQuantity, Vector3f* akVertex,
    Vector3f* akNormal, ColorRGB* akColor, Vector2f* akTexture,
    Vector2f* akTexture1, Vector2f* akTexture2, Vector2f* akTexture3,
    Vector2f* akTextureBump, VertexShader* pkVertexShader,
    PixelShader* pkPixelShader)
{
    m_iVertexQuantity = iVertexQuantity;
    m_akVertex = akVertex;
    m_akNormal = akNormal;
    m_akColor = akColor;
    m_akTexture = akTexture;
    m_akTexture1 = akTexture1;
    m_akTexture2 = akTexture2;
    m_akTexture3 = akTexture3;
    m_akTextureBump = akTextureBump;

    m_pkVertexShaderConsts = NULL;
    m_pkPixelShaderConsts = NULL;
    SetVertexShader(pkVertexShader);
    SetPixelShader(pkPixelShader);

    if ( akTextureBump && !akColor )
        m_akColor = new ColorRGB[iVertexQuantity];

    UpdateModelBound();
}
//----------------------------------------------------------------------------
Geometry::Geometry ()
{
    m_iVertexQuantity = 0;
    m_akVertex = NULL;
    m_akNormal = NULL;
    m_akColor = NULL;
    m_akTexture = NULL;
    m_akTexture1 = NULL;
    m_akTexture2 = NULL;
    m_akTexture3 = NULL;
    m_akTextureBump = NULL;
    m_pkVertexShaderConsts = NULL;
    m_pkPixelShaderConsts = NULL;
    SetVertexShader( NULL );
    SetPixelShader( NULL );
}
//----------------------------------------------------------------------------
Geometry::~Geometry ()
{
    delete[] m_akVertex;
    delete[] m_akNormal;
    delete[] m_akColor;
    delete[] m_akTexture;
    delete[] m_akTexture1;
    delete[] m_akTexture2;
    delete[] m_akTexture3;
    delete[] m_akTextureBump;

    for (int i = 0; i < RenderState::RS_MAX_STATE; i++)
        m_aspkState[i] = NULL;

    SetVertexShader( NULL );
    SetPixelShader( NULL );
}
//----------------------------------------------------------------------------
void Geometry::Reconstruct (int iVertexQuantity)
{
    m_iVertexQuantity = iVertexQuantity;

    delete[] m_akVertex;
    delete[] m_akNormal;
    delete[] m_akColor;
    delete[] m_akTexture;
    delete[] m_akTexture1;
    delete[] m_akTexture2;
    delete[] m_akTexture3;
    delete[] m_akTextureBump;

    if ( m_iVertexQuantity > 0 )
    {
        m_akVertex = new Vector3f[m_iVertexQuantity];

        if ( m_akNormal )
            m_akNormal = new Vector3f[m_iVertexQuantity];

        if ( m_akColor )
            m_akColor = new ColorRGB[m_iVertexQuantity];

        if ( m_akTexture )
            m_akTexture = new Vector2f[m_iVertexQuantity];

        if ( m_akTexture1 )
            m_akTexture1 = new Vector2f[m_iVertexQuantity];

        if ( m_akTexture2 )
            m_akTexture2 = new Vector2f[m_iVertexQuantity];

        if ( m_akTexture3 )
            m_akTexture3 = new Vector2f[m_iVertexQuantity];

        if ( m_akTextureBump )
            m_akTextureBump = new Vector2f[m_iVertexQuantity];
    }
    else
    {
        m_akVertex = NULL;
        m_akNormal = NULL;
        m_akColor = NULL;
        m_akTexture = NULL;
        m_akTexture1 = NULL;
        m_akTexture2 = NULL;
        m_akTexture3 = NULL;
        m_akTextureBump = NULL;
    }
}
//----------------------------------------------------------------------------
void Geometry::Reconstruct (int iVertexQuantity, Vector3f* akVertex,
    Vector3f* akNormal, ColorRGB* akColor, Vector2f* akTexture,
    Vector2f* akTexture1, Vector2f* akTexture2, Vector2f* akTexture3,
    Vector2f* akTextureBump)
{
    m_iVertexQuantity = iVertexQuantity;

    if ( m_akVertex != akVertex )
    {
        delete[] m_akVertex;
        m_akVertex = akVertex;
        if ( m_akVertex )
            UpdateModelBound();
    }

    if ( m_akNormal != akNormal )
    {
        delete[] m_akNormal;
        m_akNormal = akNormal;
    }

    if ( m_akColor != akColor )
    {
        delete[] m_akColor;
        m_akColor = akColor;
    }

    if ( m_akTexture != akTexture )
    {
        delete[] m_akTexture;
        m_akTexture = akTexture;
    }

    if ( m_akTexture1 != akTexture1 )
    {
        delete[] m_akTexture1;
        m_akTexture1 = akTexture1;
    }

    if ( m_akTexture2 != akTexture2 )
    {
        delete[] m_akTexture2;
        m_akTexture2 = akTexture2;
    }

    if ( m_akTexture3 != akTexture3 )
    {
        delete[] m_akTexture3;
        m_akTexture3 = akTexture3;
    }

    if ( m_akTextureBump != akTextureBump )
    {
        delete[] m_akTextureBump;
        m_akTextureBump = akTextureBump;
    }
}
//----------------------------------------------------------------------------
void Geometry::UpdateModelBound ()
{
    m_kBound.ComputeFromData(m_iVertexQuantity,m_akVertex);
}
//----------------------------------------------------------------------------
void Geometry::UpdateWorldBound ()
{
    m_kWorldBound = m_kBound.TransformBy(m_kWorldRotate,m_kWorldTranslate,
        m_fWorldScale);
}
//----------------------------------------------------------------------------
void Geometry::UpdateModelNormals ()
{
    // stub for derived classes
}
//----------------------------------------------------------------------------
void Geometry::UpdateRenderState (RenderState::Stack* pkStack)
{
    pkStack->CopyTo(m_aspkState);
}
//----------------------------------------------------------------------------
void Geometry::Draw (Renderer& rkRenderer)
{
    // The initial render state array has NULL pointers.  These are filled
    // in by a call to UpdateRS() at the root node N of a subtree that
    // contains the geometry object.  All states pointers are non-NULL as a
    // result of this call.  Any topological change in the subtree of N
    // requires you to call UpdateRS().
    //
    // If you reach this assert, you have not called UpdateRS() as you should
    // have.  And just in case you are doing this in Release mode, the
    // function returns to avoid a crash in the renderer.
    assert( m_aspkState[0] );
    if ( !m_aspkState[0] )
        return;

    // Due to DX9 and shader considerations, shaders need to be set before
    // other state (which may be affected or need to be nullified by the
    // presence of shaders).
    rkRenderer.SetShaderState(this);
    rkRenderer.SetState(m_aspkState);
}
//----------------------------------------------------------------------------
Geometry::PickRecord::PickRecord (Geometry* pkObject, float fRayT)
    :
    Spatial::PickRecord(pkObject,fRayT)
{
    // stub for derived classes
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// streaming
//----------------------------------------------------------------------------
Object* Geometry::Factory (Stream&)
{
    // Geometry is abstract, Factory never called
    return NULL;
}
//----------------------------------------------------------------------------
void Geometry::Load (Stream& rkStream, Stream::Link* pkLink)
{
    Spatial::Load(rkStream,pkLink);

    // native data
    StreamRead(rkStream,m_iVertexQuantity);
    m_akVertex = new Vector3f[m_iVertexQuantity];
    StreamRead(rkStream,m_akVertex,m_iVertexQuantity);

    StreamRead(rkStream,m_akNormal);
    if ( m_akNormal )
    {
        m_akNormal = new Vector3f[m_iVertexQuantity];
        StreamRead(rkStream,m_akNormal,m_iVertexQuantity);
    }

⌨️ 快捷键说明

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