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

📄 ul.h

📁 游戏编程精华02-含有几十个游戏编程例子
💻 H
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************NVMH2****
Path:  C:\Dev\devrel\Nv_sdk_4\Include
File:  UL.h

Copyright (C) 1999, 2000 NVIDIA Corporation
This file is provided without support, instruction, or implied warranty of any
kind.  NVIDIA makes no guarantee of its fitness for a particular purpose and is
not liable under any circumstances for any damages or loss whatsoever arising
from the use or inability to use this file or items derived from it.

Comments:
   Utility Library Header
    // forward references
    
      class Mat3; 3x3 matrix
      class Mat4; 4x4 matrix
      class MatR; matrix implemented as 3x3 rotation matrix and a position vector
      class MatQ; matrix implemented as quaternion
      
        class Vec2; // 2 element vector
        class Vec3; // 3
        class Vec4; // 4
        
          class Quat; // quaternion class
          
            class ulOrientation : orientation


******************************************************************************/


#ifndef UL_H
#define UL_H

#ifdef MULTI_THREAD
  #if _MSC_VER > 1000
    #pragma once
  #endif // _MSC_VER > 1000

  #define VC_EXTRALEAN		// Exclude rarely-used stuff from Windows headers

  // =================================================================//
  //  Include MFC components for multithreaded (MT) builds.  Behavior
  //    of new and delete operators is different for MFC, so to use
  //    the utility library with an MFC app, we have to include these
  //    MFC headers.

  #include <afxwin.h>         // MFC core and standard components
  #include <afxext.h>         // MFC extensions
  #include <afxdisp.h>        // MFC Automation classes
  #include <afxcview.h>        // CListView, CTreeView
  #include <afxdtctl.h>		// MFC support for Internet Explorer 4 Common Controls
  #ifndef _AFX_NO_AFXCMN_SUPPORT
     #include <afxcmn.h>			// MFC support for Windows Common Controls
  #endif // _AFX_NO_AFXCMN_SUPPORT

#endif			// ifdef MULTI_THREAD


//////////////////////////////////////////////////////////////
// Link the library with anything that includes this header

#ifdef MULTI_THREAD
	#if (FLOAT_SIZE==64)
		#pragma message( "Utility Library - utility64MT.lib" )
		#pragma comment(lib, "utility64MT.lib")
	#else
		#pragma message( "Utility Library - utilityMT.lib" )
		#pragma comment(lib, "utilityMT.lib")
	#endif
#else
	#if (FLOAT_SIZE==64)
		#pragma message( "Utility Library - utility64.lib" )
		#pragma comment(lib, "utility64.lib")
	#else
		#pragma message( "Utility Library - utility.lib" )
		#pragma comment(lib, "utility.lib")
	#endif
#endif



/////////////////////////////////////////////////////////////////
//  Common includes for all modules:

#include "..\CommonSrc\Utility\ulCommon.h"


/////////////////////////////////////////////////////////////////
//	2D Vector
//  3D Vector
//  4D Vector

#include "..\CommonSrc\Utility\ulVec2.h"
#include "..\CommonSrc\Utility\ulVec3.h"
#include "..\CommonSrc\Utility\ulVec4.h"


/////////////////////////////////////////////////////////////////
//	3 x 3 Matrix
//  4 x 4 Matrix
//	Quaternion

#include "..\CommonSrc\Utility\ulMat3.h"
#include "..\CommonSrc\Utility\ulMat4.h"
#include "..\CommonSrc\Utility\ulQuat.h"

/////////////////////////////////////////////////////////////////
//	3 x 3 Rotation + Translation vector

#include "..\CommonSrc\Utility\ulMatR.h"


/////////////////////////////////////////////////////////////////
//	Quaternion + Translation Vector

#include "..\CommonSrc\Utility\ulMatQ.h"


/****************************************************************
*                                                               *
*                        Plane                                  *
*                                                               *
****************************************************************/



class Plane
{
    
public:
    
    
    union
    {
        struct 
        {
            Vec3 vec3;
            Float D;
        };
        struct 
        {
            Float a,b,c,d;
        };
        struct 
        {
            Vec4 vec4;
        };
    };
    
    Plane(const Vec3 & _vec3, Float _d);
    Plane(const Vec4 & _vec4);
    Plane();
    
    const Vec3 &GetNormal() const {return vec3;}
    const Vec3 &normal() const {return vec3;}
    
    const Float &GetOffset() const {return D;}
    const Float &offset() const {return D;}
    
    Int FindPlanePos ( Float x0 , Float y0 , Float z0 , 
        Float x1 , Float y1 , Float z1 , 
        Float x2 , Float y2 , Float z2 ,
        Int unitize = 0);
    
    Int FindPlane ( Float x0 , Float y0 , Float z0 , 
        Float x1 , Float y1 , Float z1 , 
        Float x2 , Float y2 , Float z2, int unitize = 0 );
    
    Int FindPlane ( Vec3 &vp0 , Vec3 &vp1 , Vec3 &vp2 , int unitize = 0);
    
    Float DistanceToPoint(const Vec3 &point) const;
    
    
    void set(const Vec3 & _vec3, Float _d);
    void set(const Vec3 & v1, const Vec3 & v2);
    
    ostream& print(ostream &os) const;
    
};



class MatrixStack
{
public:
    MatrixStack();
    void PushMatrix();
    void PopMatrix();
    void InitMatrixStack();
    Mat4 & GetMatrix ();
    void LoadMatrix(Mat4 &mat);
    
    
private:
    
    Mat4 matrix_stack[256];
    int matrix_top;
    Mat4 *matrix; // top of stack
};


template <class _Type> 
class Rect2D
{
public:
    _Type x1, y1, x2, y2;
};
template <class _Type> 
class Rect3D
{
public:
    _Type x1, y1, x2, y2, z1, z2;
};





typedef union UPointer
{
    char * c;		// type 1
    byte * b;		// type 2
    
    short * s;		// type 3
    ushort * w;		// type 4
    
    Int * i;		// type 5
    DWORD * l;		// type 6
    
    Float32 * f;	// type 7
    void ** p;		// type 8
    Float64 * d;	// type 9
    void * v;		// type 10
} UPointer;





class ulColor
{
public:
    union
    {
        DWORD color;  
        struct
        {
            unsigned char b, g, r, a;
        };
    };
    ulColor() {color = 0;}
    ulColor(DWORD c) {color = c;}
    ulColor(unsigned char alpha, unsigned char red, unsigned char green, unsigned char blue) 
    {a = alpha; r = red; g = green; b = blue;}
};


/////////////////////////////////////////////////////////////////
// Vector - Matrix operator declarations
// Only partial for now.

Vec3 operator * (const Vec3& v3, const Mat4& m4);

Vec4 operator * (const Vec4& v4, const Mat4& m4);
Vec4 operator * (const Mat4& v4, const Vec4& m4);

/////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////


template <class _Type> 
class ulLeaf
{
public:
    ulLeaf() { m_parent = 0; m_left = 0; m_right = 0;}
    
    int m_id;
    _Type * m_parent;
    _Type * m_left;
    _Type * m_right;
};



template <class _Type> 
class ulTree
{
public:
    ulTree() { m_top = 0;  }
    // 0 == top
    _Type * Top() { return m_top; }
    
    _Type * GetUpperRight(_Type * c)
    {
        _Type * parent;
        if (c == 0)
            return 0;
        for(;;)
        {
            parent = c->m_parent;
            if (parent == 0)
                return c; // at the top
            
            if (parent->m_left == c)
                c = parent;
            else
                return c;
        }
        
    }
    
    void InsertRightEnd(_Type * parent, _Type * child)
    {
        if (m_top == 0)
        {
            m_top = child;
            return;
        }
        if (parent == 0)
            parent = m_top;
        
        while(parent->m_right != 0)
            parent = parent->m_right;
        // find right leaf
        
        parent->m_right = child;
        
        child->m_right = 0;
        child->m_left = 0;
        child->m_parent = parent;
    }
    
    void InsertLeftEnd(_Type * parent, _Type * child)
    {
        if (m_top == 0)
        {
            m_top = child;
            return;
        }
        if (parent == 0)
            parent = m_top;
        
        while(parent->m_left != 0)
            parent = parent->m_left;
        // find right leaf
        
        parent->m_left = child;
        child->m_right = 0;
        child->m_left = 0;
        child->m_parent = parent;
    }
    
    void InsertLeft(_Type * parent, _Type * child)
    {
        if (m_top == 0)
        {
            m_top = child;
            return;
        }
        if (parent == 0)
            parent = m_top;
        
        _Type * left = parent->m_left;
        
        parent->m_left = child;
        if (left)
            left->m_parent = child;
        child->m_left = left;
        child->m_right = 0;
        child->m_parent = parent;
    }
    
    
    void InsertRight(_Type * parent, _Type * child)
    {
        if (m_top == 0)
        {
            m_top = child;
            return;
        }
        if (parent == 0)
            parent = m_top;
        
        _Type * right = parent->m_right;
        
        parent->m_right = child;
        if (right)
            right->m_parent = child;
        child->m_right = right;
        child->m_left = 0;
        child->m_parent = parent;
    }
    
    void EraseTree();
    
    void EraseKeepLeft(_Type * n)
    {
        if (n == 0)
            return;
        _Type * parent = n->m_parent;
        
        if (parent == 0)
        {
            // we are top
            m_top = n->m_left;
        }
        else
        {
            if (parent->m_left == n)
                parent->m_left = n->m_left;
            else
                parent->m_right = n->m_left;
        }
        
        delete n;
        
    }
    void EraseKeepRight(_Type * node);
    
    // deletes this and all childen,
    void EraseWithChildren(_Type * n)
    {
        if (n == 0)
            return;
        if (n->m_parent == 0)
        {
            m_top = 0;
        }
        
        // make parent a leaf
        if (n->m_parent->m_left == n)
            n->m_parent->m_left = 0;
        else if (n->m_parent->m_right == n)
            n->m_parent->m_right = 0;
        
        DeleteTree(n);
        
    }
    
    typedef void  ( *ulTreeFunc)(_Type *);
    
    void TraversePost(_Type * n, ulTreeFunc func)
    {
        if (n == 0)
            return;
        
        TraversePost(n->m_left, func);
        TraversePost(n->m_right, func);
        
        func(n);
        
    }
    
    
    void TraversePost(ulTreeFunc func)
    {
        TraversePost(m_top, func);
    }
    
    void TraversePre(_Type * n, ulTreeFunc func)
    {
        if (n == 0)
            return;
        
        func(n);
        
        TraversePre(n->m_left, func);
        TraversePre(n->m_right, func);
        
    }
    
    void TraversePre(ulTreeFunc func)
    {
        TraversePre(m_top, func);
    }
    
    
    void DeleteTree(_Type * n)
    {
        if (n == 0)
            return;
        
        DeleteTree(n->m_left);
        DeleteTree(n->m_right);
        
        delete n;
        
    }
    
    void DeleteTree()
    {
        DeleteTree(m_top);  
    }
    
    
    
    void FindById(_Type * n, int id)
    {
        if (n == 0)
            return;
        
        if (m_found)
            return;
        
        if (n->m_id == id)
        {
            m_found = 1;
            m_foundnode = n;
            return;
        }
        FindById(n->m_left, id);
        FindById(n->m_right, id);
        
    }
    
    _Type * FindById(int id)
    {
        m_found = 0;
        m_foundnode = 0;
        FindById(m_top, id);
        return m_foundnode;
    }
    
    _Type * m_top;
    bool m_found;
    _Type * m_foundnode;
    int level;
    
    
};




#define NextItem(nptr) (nptr) = (nptr)->GetNext()
#define PrevItem(nptr) (nptr) = (nptr)->GetPrev()
//#define NextItem(nptr) ++nptr


/****************************************************************
*                                                               *
*                             ComRef                            *
*                                                               *
****************************************************************/


class ulName
{
public:
    ulName() {m_name = 0;}
    
    void ReleaseName() 
    {
        SAFE_ARRAY_DELETE(m_name);
    }
    
    virtual ~ulName()
    { 
        SAFE_ARRAY_DELETE(m_name);
        
        
    }
    const char * GetName() const { return m_name;}
    char * GetName() { return m_name;}
    void ClearName() { m_name = 0;} 
    void SetName(const char * name) 
    { 
        SAFE_ARRAY_DELETE(m_name);
        
        if (name == 0)
            return;
        
        m_name = new char [strlen(name)+1];
        strcpy(m_name, name);
    }
    
    bool IsName(const char * name)
    {
        if (name == 0)
            return false;
        if (strcmpi(m_name, name) == 0)
            return true;
        
        return false;
        
    }
    
private:
    char * m_name;
};








class Dirt
{
public:
    Dirt() { m_dirty = true; }
    
    virtual bool RefreshRenderer() = 0; // flush properties to renderer
    
    virtual HRESULT AddToRenderer() = 0;   // build an object for the renderer
    virtual HRESULT RemoveFromRenderer() = 0; // kill renderer specific
    
    virtual HRESULT Apply() = 0; // Make this the current
    virtual HRESULT DeleteLists() = 0; // remove all lists and pointers
    
    void SetChanged(const bool & b) { m_dirty = b;}
    bool IsChanged() { return m_dirty;}
    
private:
    bool m_dirty;
    

⌨️ 快捷键说明

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