C:/ScoutCGC/ScoutCGLib.h

説明を見る。
00001 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 //---------------------依存するライブラリ----------------------//
00023 #include <stdio.h>
00024 #include <conio.h>
00025 #include <windows.h>
00026 #include <cmath>
00027 #include <vector>
00028 #include <typeinfo>
00029 
00030 /*-------------------------------------------------------------*
00031                         libpng-PNG関係のライブラリ
00032                         http://www.libpng.org/pub/png/libpng.html
00033 
00034                         zlib-PNGが使う圧縮ライブラリ
00035                         http://www.zlib.net/
00036  *-------------------------------------------------------------*/
00037 #include "lib/PNG_LIB/png.h"
00038 #pragma comment( lib, "../lib/PNG_LIB/libpng.lib" )
00039 
00040 #include "lib/PNG_LIB/zlib.h"
00041 #pragma comment( lib, "../lib/PNG_LIB/zdll.lib" )
00042 
00043 /*-------------------------------------------------------------*
00044                         IJG-JPEGLIB-JPEG関係のライブラリ
00045                         http://www.ijg.org/ 
00046  *-------------------------------------------------------------*/
00047 #include "lib/JPEG_LIB/jpeglib.h"
00048 #pragma comment( lib, "../lib/JPEG_LIB/jpeg.lib" )
00049 
00050 
00051 #include <gl/gl.h>
00052 #include <gl/glu.h>
00053 #include <gl/glut.h>
00054 #pragma comment( lib, "opengl32.lib" )
00055 #pragma comment( lib, "glu32.lib" )
00056 #pragma comment( lib, "glut32.lib" )
00057 
00058 
00059 
00060 //---------------------------本体----------------------------//
00061 
00062 namespace SCGL
00063 {
00064 /*-------------------------------------------------------------/
00065 
00066                         3DCG用お役立ち構造体
00067                         CGでよく使うデータ構造を定義
00068                                                                                    \author Scout
00069                                                                                    \date   2006-06-15
00070 /--------------------------------------------------------------*/
00071 
00072 #ifndef __SCGL_FUNC__
00073 #define __SCGL_FUNC__
00074 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
00082 
00083 extern const double ToRadian(const double &degree);
00084 extern const double ToDegree(const double &radian);
00085 
00086 
00087 #endif //__SCGL_FUNC__
00088 
00089 
00090 
00091 #ifndef __3DSTRUCTS__
00092 #define __3DSTRUCTS__
00093 
00094 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
00103 
00104 class VECTOR3
00105 {
00106 public:
00107         union
00108         {
00109                 struct{double x;double y;double z;};
00110                 double v[3];                                            
00111         };
00112 
00114         VECTOR3();
00115         VECTOR3(const VECTOR3& vec);
00116         VECTOR3(const double &vx,
00117                     const double &vy,
00118                         const double &vz);
00119 
00120         void init();
00121 
00122         virtual ~VECTOR3();
00123 
00125         const double norm() const;
00126 
00128         const bool unit();
00129 
00131         const VECTOR3 operator+(const VECTOR3 &a) const;
00132         const VECTOR3 operator-(const VECTOR3 &a) const;
00133 
00134         const VECTOR3 operator*(const double &a) const;
00135         const VECTOR3 operator/(const double &a) const;
00136 
00137         
00138         VECTOR3& operator+=(const VECTOR3 &a);
00139         VECTOR3& operator-=(const VECTOR3 &a);
00140 
00141 
00142         VECTOR3& operator+=(const double &a);
00143         VECTOR3& operator-=(const double &a);
00144 
00145         VECTOR3& operator*=(const double &a);
00146         VECTOR3& operator/=(const double &a);
00147 
00149         const double  operator&(const VECTOR3 &a) const;
00150 
00152         const VECTOR3 operator*(const VECTOR3 &a) const;
00153         VECTOR3& operator*=(const VECTOR3 &a);
00154 
00156         bool operator==(const VECTOR3 &a) const;
00157 
00159         const VECTOR3 operator-() const;
00160 
00161 };
00162 
00164 union MATRIX44
00165 {
00166         struct
00167         {
00168                 double        _11, _12, _13, _14;
00169                 double        _21, _22, _23, _24;
00170                 double        _31, _32, _33, _34;
00171                 double        _41, _42, _43, _44;
00172         };
00173         double m[4][4];
00174 };
00175 
00177 class Color
00178 {
00179 public:
00180         union
00181         {
00182                 struct 
00183                 {
00184                         BYTE Red;
00185                         BYTE Green;
00186                         BYTE Blue;
00187                         BYTE Alpha;
00188                 };
00189                 BYTE Colors[4];
00190         };
00191 
00192         Color():Red(0),Green(0),Blue(0),Alpha(255){}
00193         virtual ~Color(){}
00194 };
00195 
00197 class RGBA
00198 {
00199 public:
00200         union
00201         {
00202                 struct 
00203                 {
00204                         float Red;
00205                         float Green;
00206                         float Blue;
00207                         float Alpha;
00208                 };
00209                 float  Colors[4];
00210         };
00211 
00212         RGBA():Red(0.0),Green(0.0),Blue(0.0),Alpha(0.0){}
00213         virtual ~RGBA(){}
00214 };
00215 
00217 struct MATERIAL_COLOR{
00218         RGBA Ambient;
00219     RGBA Diffuse;
00220     RGBA Specular;
00221 };
00222 
00224 union TEXTURE_COORDINATE
00225 {
00226         struct{ double u,v; };
00227         double val[2];
00228         TEXTURE_COORDINATE():u(0.0),v(0.0){}
00229 };
00230 
00232 struct VERTEX
00233 {
00234         VECTOR3 Position;       
00235         VECTOR3 Normal;         
00236         TEXTURE_COORDINATE Texture_Coordinate;
00237         Color              VertexColor; 
00238         MATERIAL_COLOR Material;
00239         std::vector<unsigned int> JointPlane;
00240 };
00241 
00243 class TRIANGLE
00244 {
00245 public:
00246         union{
00247                 struct{unsigned int Vec1,Vec2,Vec3;};
00248                 unsigned int Vec[3];
00249         };
00250 
00252         VECTOR3 Normal;
00254         Color              PlaneColor;
00256         MATERIAL_COLOR Material;
00258         VECTOR3 GravPoint;
00260         double  S;
00261         TRIANGLE() :Vec1(0),Vec2(0),Vec3(0),S(0.0){}
00262         virtual ~TRIANGLE(){}
00263 };
00264 
00265 
00266 #endif  //__3DSTRUCTS_H__
00267 
00268 
00269 
00270 
00271 
00272 
00273 #ifndef __CARRAY_H__
00274 #define __CARRAY_H__
00275 
00276 #define INIT_ARRAY_SIZE 32      //初期配列サイズ
00277 #define ID unsigned int         //IDの型
00278 #define FAIL -1                         //エラーコード
00279 
00280 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
00290 template <typename Type>
00291 class CArray  
00292 {
00293 public:
00295         CArray()
00296         {
00297                 m_DataList = new Type[INIT_ARRAY_SIZE];
00298                 m_Used = 0;
00299                 m_Free = INIT_ARRAY_SIZE;
00300                 m_Locked = false;
00301         }
00302 
00304         virtual ~CArray(){delete[] m_DataList;}
00305 
00307         CArray<Type>& operator =(CArray<Type>& sorce)
00308         {
00309                 clear();
00310                 for(ID id=0;id<sorce.size();id++)push(sorce[id]);
00311 
00312                 return *this;
00313         }
00314 
00315 
00319         const ID size() const {return m_Used;}
00320 
00325 
00326         const ID push(const Type& Data) 
00327         {
00328                 if(m_Locked)return FAIL;
00329 
00330                 if(m_Free == 1)
00331                         if(!ExpandArray((m_Used+m_Free)*2))
00332                                 return FAIL;
00333 
00334                 m_DataList[m_Used] = (Type)Data;
00335                 m_Free--;m_Used++;
00336 
00337                 return m_Used-1;
00338         }
00339         
00345 
00346         const ID push(Type* Data,int size)
00347         {
00348                 if(m_Locked)return FAIL;
00349 
00350                 if(m_Free < size)
00351                 {
00352                         while(m_Free < size)
00353                         {
00354                                 if(!ExpandArray((m_Used+m_Free)*2))
00355                                         return FAIL;
00356                         }
00357                 }
00358 
00359                 for(int t=0;t<size;t++)
00360                         m_DataList[m_Used+t] = Data[t];
00361 
00362                 m_Free-=size;m_Used+=size;
00363 
00364                 return m_Used-1-size;
00365         }
00366 
00367 
00371 
00372         Type& pop()     
00373         {
00374                 if(m_Locked)return m_DataList[m_Used-1];
00375                 if(m_Used == 0)return (*(Type*)NULL);
00376 
00377                 m_Free++;m_Used--;
00378                 return m_DataList[m_Used];
00379         }
00380 
00382         void clear()
00383         {       
00384                 if(m_Locked)return;
00385 
00386                 delete[] m_DataList;
00387                 m_DataList = new Type[INIT_ARRAY_SIZE];
00388                 m_Used = 0;
00389                 m_Free = INIT_ARRAY_SIZE;
00390         }
00391         
00393         void lock()     
00394         {
00395                 m_Locked = true;
00396 
00397                 Type *pNewDataList = new Type[m_Used];
00398                 for(int t=0;t<m_Used;t++)
00399                         pNewDataList[t] = m_DataList[t];
00400 
00401                 delete[] m_DataList;
00402 
00403                 m_DataList = pNewDataList;
00404                 m_Free = 0;
00405         }
00406 
00408         void unlock(){m_Locked=false;}
00409 
00411         template <class T>
00412         Type& operator[](const T& val) const
00413         {
00414                 ID id = static_cast<ID>(val);
00415                 return m_DataList[id];
00416         }
00417 
00421 
00422         const Type* GetFirstAdder() const {return m_DataList;}
00423 
00424 private:
00429         bool ExpandArray(const ID &size)        
00430         {
00431                 if(m_Used + m_Free > size)return false;
00432 
00433                 Type *pNewDataList = new Type[size];
00434                 if(pNewDataList == NULL)return false;
00435 
00436                 for(int t=0;t<m_Used;t++)
00437                         pNewDataList[t] = m_DataList[t];
00438 
00439                 delete[] m_DataList;
00440 
00441                 m_DataList = pNewDataList;
00442                 m_Free = size-m_Used;
00443 
00444                 return true;
00445         }
00446 
00447         Type* m_DataList;               
00448         ID m_Used;                              
00449         ID m_Free;                              
00450 
00451         bool m_Locked;                  
00452 };
00453 
00454 //定数を破棄
00455 #undef INIT_ARRAY_SIZE
00456 #undef ID
00457 #undef FAIL
00458 
00459 #endif // __CARRAY_H__
00460 
00461 #ifndef __CNDARRAY_H__
00462 #define __CNDARRAY_H__
00463 
00464 template <class Type>
00465 class CNDArray
00466 {
00467         public:
00468         CNDArray()
00469         {
00470                 m_Data=NULL;
00471                 m_CurrentOffset=0;
00472                 m_RefCount=0;
00473                 m_DataSize=0;
00474         }
00475 
00476         virtual ~CNDArray(){delete[] m_Data;}
00477 
00478         void SetDim(unsigned int Dim,...)
00479         {
00480                 delete[] m_Data;
00481                 m_Offset.clear();
00482                 m_Size.clear();
00483 
00484                 
00485                 va_list SizeList;
00486                 va_start(SizeList,Dim);
00487                 
00488                 for(int d=0;d<Dim;d++)
00489                         m_Size.push_back(va_arg(SizeList,unsigned int));
00490                 
00491                 va_end(SizeList);
00492 
00493                 unsigned int TotalSize=1;
00494                 m_Offset.resize(Dim);
00495 
00496                 for(d=Dim-1;d>=0;d--)
00497                 {
00498                         m_Offset[d] = TotalSize;
00499                         TotalSize *= m_Size[d];
00500                 }
00501                 
00502                 m_Data = new Type[TotalSize];
00503                 if(m_Data == NULL)
00504                 {
00505                         printf("Fail:allocates memory.(size:%dByte)\n",sizeof(Type)*TotalSize);
00506                         exit(-1);
00507                 }
00508 
00509                 m_RefCount=0;
00510                 m_DataSize=TotalSize;
00511         }
00512 
00513         operator Type&()
00514         {
00515                 Type &ret       = m_Data[m_CurrentOffset];
00516                 m_CurrentOffset = 0;
00517                 m_RefCount      = 0;
00518                 return ret;
00519         }
00520 
00521         operator Type*()
00522         {
00523                 Type* ret       = &m_Data[m_CurrentOffset];
00524                 m_CurrentOffset = 0;
00525                 m_RefCount      = 0;
00526                 return ret;
00527         }
00528 
00529 
00530         template <class ArgType>
00531         CNDArray<Type>& operator[](const ArgType index)
00532         {
00533                 unsigned int ID = static_cast<unsigned int>(index);
00534 
00535                 m_CurrentOffset+=m_Offset[m_RefCount]*ID;
00536                 m_RefCount++;
00537 
00538                 if(m_RefCount > m_Offset.size() || m_CurrentOffset > m_DataSize)
00539                 {
00540                         m_CurrentOffset = 0;
00541                         m_RefCount      = 0;
00542                         return *((CNDArray<Type>*)NULL);
00543                 }
00544                 else
00545                         return *this;
00546         }
00547 
00548         Type& operator=(const Type &val)
00549         {
00550                 Type &ret   = m_Data[m_CurrentOffset];
00551                 ret = val;
00552 
00553                 m_CurrentOffset=0;
00554                 m_RefCount=0;
00555 
00556                 return ret;
00557         }
00558 
00559         unsigned int GetSize(const unsigned int Dim)
00560         {
00561                 if(Dim >= m_Size.size())
00562                         return 0;
00563                 else
00564                         return m_Size[Dim];
00565         }
00566 
00567         void InitData(const Type &InitVal)
00568         {
00569                 for(int id=0;id<m_DataSize;id++)
00570                         m_Data[id]=InitVal;
00571         }
00572 private:
00573         std::vector<unsigned int> m_Offset;
00574         std::vector<unsigned int> m_Size;
00575 
00576         Type *m_Data;
00577         unsigned int m_CurrentOffset;
00578         unsigned int m_RefCount;
00579         unsigned int m_DataSize;
00580 };
00581 
00582 
00583 #endif //__CNDARRAY_H__
00584 
00585 
00586 
00587 #ifndef __QUATERNION_H__
00588 #define __QUATERNION_H__
00589 
00590 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
00598 
00599 class CQuaternion
00600 {
00601 public:
00602         double w,x,y,z;
00603 
00605 
00606         CQuaternion();
00607 
00611 
00612         CQuaternion(double val);
00613 
00620 
00621         CQuaternion(double qw,double qx,double qy,double qz);
00622 
00627 
00628         CQuaternion(double ww,const VECTOR3 &vec);
00629 
00634 
00635         CQuaternion(VECTOR3 dir, VECTOR3 up);
00636 
00638         virtual ~CQuaternion();
00639 
00641         void init();
00642 
00647 
00648         void SetRotate(const VECTOR3 &axis,const double &theta);
00649 
00654 
00655         CQuaternion& operator=(const CQuaternion &q );
00656 
00660 
00661         const CQuaternion operator -() const;
00662 
00667 
00668         CQuaternion& operator+=(const CQuaternion &q);
00669 
00670 
00675 
00676         CQuaternion& operator+=(const double &val );
00677         
00678 
00683 
00684         CQuaternion& operator-=(const CQuaternion &q);
00685 
00686 
00691 
00692         CQuaternion& operator-=(const double &val );
00693 
00694 
00699 
00700         CQuaternion& operator*=(const CQuaternion &q);
00701 
00702 
00707 
00708         CQuaternion& operator*=(const double &val);
00709 
00710 
00715 
00716         const CQuaternion operator*(const CQuaternion &q) const ;
00717 
00718 
00723 
00724         CQuaternion& operator /=(const CQuaternion &q);
00725 
00730 
00731         CQuaternion &operator /=(const double &val);
00732 
00737 
00738         const CQuaternion  operator / (const CQuaternion &q) const ;
00739 
00744 
00745         const CQuaternion  operator / (const double &val) const ;
00746 
00750 
00751         const double norm() const;
00752 
00756 
00757         const double norm2() const;
00758 
00759 
00763 
00764         const CQuaternion conjugate() const;
00765 
00769 
00770         const CQuaternion inv() const;
00771 
00772 
00776 
00777         const CQuaternion unit() const;
00778 
00779 
00783 
00784         double operator&(CQuaternion &q);
00785 
00786 
00791 
00792         VECTOR3 Rotate(const VECTOR3 &vec) const;
00793 
00794         const VECTOR3 operator*(const VECTOR3 &vec) const;
00795 
00796         void Rotate(VECTOR3 v1, VECTOR3 v2);
00797 
00798         CQuaternion Rotate(VECTOR3 v1,VECTOR3 v2,VECTOR3 axis);
00799         
00800 
00804         MATRIX44 GetMatrix();
00805 
00806         bool operator ==( const CQuaternion &q ) const;
00807         bool operator !=( const CQuaternion &q ) const;
00808         bool operator <=( const CQuaternion &q ) const;
00809         bool operator > ( const CQuaternion &q ) const;
00810         bool operator >=( const CQuaternion &q ) const;
00811         bool operator < ( const CQuaternion &q ) const;
00812 
00813 };
00814 
00815 #endif //__QUATERNION_H__
00816 
00817 
00818 
00819 /*これからつくる
00820 
00821 00644 
00822 00645 
00823 00646 
00824 00655 template < class T1, class T2 >
00825 00656 const quaternion< double > interpolate( const quaternion< T1 > &q1, const quaternion< T2 > &q2, double t )
00826 00657 {
00827 00658     typedef quaternion< double > quaternion_type;
00828 00659 
00829 00660     quaternion_type Q1( q1.unit( ) );
00830 00661     quaternion_type Q2( q2.unit( ) );
00831 00662 
00832 00663     double dot = Q1.inner( Q2 );
00833 00664 
00834 00665     if( std::abs( dot ) < 1.0e-6 )
00835 00666     {
00836 00667         return( Q1 );
00837 00668     }
00838 00669     else if( dot < 0.0 )
00839 00670     {
00840 00671         double theta = std::acos( dot );
00841 00672 
00842 00673         // 球面線形補間を行う
00843 00674         return( quaternion_type( Q1 * std::sin( theta * ( 1.0 - t ) ) - Q2 * std::sin( theta * t ) ).unit( ) );
00844 00675     }
00845 00676     else
00846 00677     {
00847 00678         double theta = std::acos( dot );
00848 00679 
00849 00680         // 球面線形補間を行う
00850 00681         return( quaternion_type( Q1 * std::sin( theta * ( 1.0 - t ) ) + Q2 * std::sin( theta * t ) ).unit( ) );
00851 00682     }
00852 00683 }
00853 00684 
00854 00685 
00855 
00856 00709 
00857 00710 // 
00858 00711 // 
00859 00712 //
00860 00713 //
00861 00714 
00862 00715 
00863 00743 template < class T >
00864 00744 const quaternion< T > track_ball( const vector2< T > &p1, const vector2< T > &p2, const vector3< T > &axisX, const vector3< T > axisY, const vector3< T > axisZ, const typename vector3< T >::value_type &trackball_size )
00865 00745 {
00866 00746     typedef typename quaternion< T >::value_type value_type;
00867 00747 
00868 00748     if( p1 == p2 )
00869 00749     {
00870 00750         return( quaternion< T >( 1, 0, 0, 0 ) );
00871 00751     }
00872 00752 
00873 00753     vector3< T > sp1( p1.x, p1.y, 0 ), sp2( p2.x, p2.y, 0 );
00874 00754     value_type l, _2 = std::sqrt( value_type( 2.0 ) );
00875 00755 
00876 00756     // 点1の座標を仮想トラックボール上に投影
00877 00757     l = p1.length( );
00878 00758     if( l < trackball_size / _2 )
00879 00759     {
00880 00760         sp1.z = - std::sqrt( trackball_size * trackball_size - l * l );
00881 00761     }
00882 00762     else
00883 00763     {
00884 00764         sp1.z = - trackball_size * trackball_size / 2.0 / l;
00885 00765     }
00886 00766 
00887 00767     // 点2の座標を仮想トラックボール上に投影
00888 00768     l = p2.length( );
00889 00769     if( l < trackball_size / _2 )
00890 00770     {
00891 00771         sp2.z = - std::sqrt( trackball_size * trackball_size - l * l );
00892 00772     }
00893 00773     else
00894 00774     {
00895 00775         sp2.z = - trackball_size * trackball_size / 2.0 / l;
00896 00776     }
00897 00777 
00898 00778     //  sp1 = sp1.unit();
00899 00779     //  sp2 = sp2.unit();
00900 00780 
00901 00781     // 右手系と左手系でここの外積の向きを反転させる
00902 00782     //  Vector3<double> axis = (sp2 * sp1).unit();
00903 00783     vector3< T > axis = ( sp1 * sp2 ).unit( );
00904 00784     axis = ( axis.x * axisX + axis.y * axisY + axis.z * axisZ ).unit( );
00905 00785 
00906 00786     l = ( sp2 - sp1 ).length( ) / ( 2 * trackball_size );
00907 00787     //  l = (l < -1.0)? -1.0: l;
00908 00788     l = l > 1 ? 1: l;
00909 00789 
00910 00790     double phi = std::asin( l );
00911 00791     //  fprintf(stdout, "axis(%.1f, %.1f, %.1f)   theta = %.1f\n", axis.x, axis.y, axis.z, phi * 180 / PAI);
00912 00792     //  printf("%.1f\n", phi * 180 / PAI);
00913 00793     return( quaternion< T >( std::cos( phi ), std::sin( phi ) * axis ) );
00914 00794 }
00915 00795 
00916 00822 template < class T >
00917 00823 inline const quaternion< T > track_ball( const vector2< T > &p1, const vector2< T > &p2, const vector3< T > &axisX, const vector3< T > axisY, const vector3< T > axisZ )
00918 00824 {
00919 00825     return( track_ball( p1, p2, axisX, axisY, axisZ, 0.8 ) );
00920 00826 }
00921 00827 
00922 00828 
00923 00844 template < class T >
00924 00845 const quaternion< T > track_ball( const typename vector3< T >::value_type &x1, const typename vector3< T >::value_type &y1, const typename vector3< T >::value_type &x2, const typename vector3< T >::value_type &y2,
00925 00846                                                 const vector3< T > &axisX, const vector3< T > &axisY, const vector3< T > &axisZ, const typename vector3< T >::value_type &trackball_size )
00926 00847 {
00927 00848     return( track_ball( vector2< T >( x1, y1 ), vector2< T >( x2, y2 ), axisX, axisY, axisZ, trackball_size ) );
00928 00849 }
00929 00850 
00930 00851 
00931 00866 template < class T >
00932 00867 const quaternion< T > track_ball( const typename vector3< T >::value_type &x1, const typename vector3< T >::value_type &y1, const typename vector3< T >::value_type &x2,
00933 00868                                             const typename vector3< T >::value_type &y2, const vector3< T > &axisX, const vector3< T > &axisY, const vector3< T > &axisZ )
00934 00869 {
00935 00870     return( track_ball( vector2< T >( x1, y1 ), vector2< T >( x2, y2 ), axisX, axisY, axisZ, 0.8 ) );
00936 00871 }
00937 00872 
00938 00873 
00939 00874 // mist名前空間の終わり
00940 00875 _MIST_END
00941 00876 
00942 00877 #endif // __INCLUDE_MIST_QUATERNION__
00943 00878 
00944 
00945 */
00946 
00947 
00948 #ifndef __C3DTRNAS_H__
00949 #define __C3DTRNAS_H__
00950 
00951 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
00960 class C3DTrans  
00961 {
00962 public:
00964         C3DTrans():m_AxisX(1.0,0.0,0.0),
00965                            m_AxisY(0.0,1.0,0.0),
00966                            m_AxisZ(0.0,0.0,1.0) {}
00967         virtual ~C3DTrans(){}
00968         
00969         CQuaternion m_Quaternion;
00970         VECTOR3 m_Trans;                 
00971 
00973         void Init(){m_Quaternion.init();
00974                             m_Trans.init();}
00975 
00976 
00980 
00981         void Translate(const VECTOR3 &TransVec)
00982         {
00983                 m_Trans += m_Quaternion.Rotate(TransVec);
00984         }
00985 
00991 
00992         void Translate(double x,double y,double z)
00993         {
00994                 m_Trans += m_Quaternion.Rotate(VECTOR3(x,y,z));
00995         }
00996         
01001 
01002         void Rotate(const VECTOR3 &Axis,double theta)
01003         {
01004                 CQuaternion q;
01005                 q.SetRotate(Axis,theta);
01006                 m_Quaternion*=q;
01007         }
01008 
01012 
01013         void RotateX(double theta)
01014         {
01015                 CQuaternion q;
01016                 q.SetRotate(m_AxisX,theta);
01017                 m_Quaternion*=q;
01018         }
01019 
01023 
01024         void RotateY(double theta)
01025         {
01026                 CQuaternion q;
01027                 q.SetRotate(m_AxisY,theta);
01028                 m_Quaternion*=q;
01029         }
01030 
01034 
01035         void RotateZ(double theta)
01036         {
01037                 CQuaternion q;
01038                 q.SetRotate(m_AxisZ,theta);
01039                 m_Quaternion*=q;
01040         }
01041 
01046 
01047         const VECTOR3 TransVec(const VECTOR3 &vec) const 
01048         {
01049                 return m_Quaternion.Rotate(vec) + m_Trans;
01050         }
01051         
01053         void PushState()
01054         {
01055                 m_QuatStack.push_back(m_Quaternion);
01056                 m_TransStack.push_back(m_Trans);
01057         }
01058 
01060         void PopState()
01061         {
01062                 if(m_QuatStack.size()>0)
01063                 {
01064                         m_Quaternion = m_QuatStack[m_QuatStack.size()-1];
01065                         m_QuatStack.pop_back();
01066                         m_Trans = m_TransStack[m_TransStack.size()-1];
01067                         m_TransStack.pop_back();
01068                 }
01069         }
01070         
01072         void TransByOGL()
01073         {
01074                 glTranslated(m_Trans.x,m_Trans.y,m_Trans.z);
01075                 const double angle = ToDegree(acos(m_Quaternion.w)); 
01076 
01077                 if(angle != 0)
01078                 {
01079                         VECTOR3 RotVec(m_Quaternion.x,m_Quaternion.y,m_Quaternion.z);
01080                         RotVec.unit();
01081                         glRotated(angle,RotVec.x,RotVec.y,RotVec.z);
01082                 }
01083         }
01084 private:
01085         const VECTOR3 m_AxisX;
01086         const VECTOR3 m_AxisY;
01087         const VECTOR3 m_AxisZ;
01088 
01090         std::vector<CQuaternion> m_QuatStack;
01091         std::vector<VECTOR3>     m_TransStack;
01092 };
01093 
01094 #endif //__C3DTRNAS_H__
01095 
01096 
01097 #ifndef __CTEXTURE_H__
01098 #define __CTEXTURE_H__
01099 
01101 struct RGBA_BYTE
01102 {
01103         BYTE Blue;
01104         BYTE Green;
01105         BYTE Red;
01106         BYTE Alpha;
01107 };
01108 
01110 struct TextureData
01111 {
01112         RGBA_BYTE *pColor;
01113         int Width;
01114         int Height;
01115 };
01116 
01117 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
01127 class CTexture  
01128 {
01129 public:
01131         CTexture();
01133         virtual ~CTexture();
01134 
01136         void Init();
01137 
01138 
01143 
01144         bool LoadTexture(char* pFileName);
01145 
01147         bool LoadPngFile(FILE *fp);
01149         bool LoadJpegFile(FILE *fp);
01150 
01154 
01155         void LoadTexture(TextureData Tex);
01156 
01160 
01161         TextureData GetTextureData() const;
01162 
01163 
01165         void BindTextureOGL();
01166 
01167 
01168 private:
01170         static void PNGReader(png_structp png_ptr,png_bytep data,png_size_t length);
01171 
01173         int m_TextureNumber;
01175         bool bIsBinded;
01176         
01178         TextureData m_Texture_Data;
01179 };
01180 
01181 
01182 #endif // __CTEXTURE_H__
01183 
01184 
01185 
01186 
01187 #ifndef __3DONJECT_H__
01188 #define __3DONJECT_H__
01189 
01190 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
01202 
01203 typedef unsigned int ID_VERTEX;
01204 typedef unsigned int ID_TRIANGLE;
01205 
01206 typedef std::vector<unsigned int> ID_LIST_VERTEX;
01207 typedef std::vector<unsigned int> ID_LIST_TRIANGLE;
01208 
01209 class CBasic3DObject
01210 {
01211 public:
01213         CBasic3DObject();
01214         virtual ~CBasic3DObject();
01215 
01217         virtual void Init();
01218         
01225 
01226         virtual const ID_TRIANGLE  AddTriangle(const ID_VERTEX &Vec1,
01227                                                                                    const ID_VERTEX &Vec2,
01228                                                                                    const ID_VERTEX &Vec3);      
01233 
01234         virtual const ID_VERTEX  AddVertex(const VERTEX &Vertex);
01235 
01240 
01241         virtual const ID_VERTEX  AddVertex(const VECTOR3 &Vector);
01242 
01249 
01250         virtual const ID_VERTEX  AddVertex(double x,double y,double z);
01251 
01255 
01256         virtual const ID_VERTEX  AddVertex();
01257 
01262 
01263         virtual const ID_LIST_VERTEX AddVertexList(const std::vector<VERTEX> &VertexList);
01264         virtual const ID_LIST_VERTEX AddVertexList(const std::vector<VECTOR3> &VectorList);
01265         virtual const ID_LIST_VERTEX AddVertexList(const std::vector<double> &PointList);
01266         virtual const ID_LIST_VERTEX AddVertexList(const int Num);
01267 
01268 
01273 
01274         bool SetTexture(char* pFileName);
01275 
01279 
01280         void SetTexture(TextureData Tex);
01281 
01285 
01286         TextureData GetTexture() const;
01287 
01289         virtual CBasic3DObject& operator=(const CBasic3DObject &sorce);
01290 
01292         virtual CBasic3DObject& operator*=(const C3DTrans &trans);
01293 
01294 
01299 
01300         virtual CBasic3DObject& operator+=(CBasic3DObject &sorce);
01301 
01306 
01307         VERTEX& Vertex(ID_VERTEX VertexID) const ;
01308 
01313 
01314         TRIANGLE& Triangle(ID_TRIANGLE TriangleID) const ;
01315 
01317         const int GetVertexNum() const ;
01319         const int GetTriangleNum() const ;
01320         
01322         void Normalize();
01323 
01324         C3DTrans Position;                                      
01325         MATERIAL_COLOR          Material;               
01326 
01327         CTexture                        m_Texture;              
01328 protected:
01329         CArray<VERTEX>          m_Vertex;               
01330         CArray<TRIANGLE>        m_Triangle;             
01331 };
01332 
01333 
01334 class C3DObject : public CBasic3DObject
01335 {
01336 public:
01338         C3DObject();
01339         virtual ~C3DObject();
01340         virtual void Init();
01341 
01343         virtual const ID_VERTEX AddVertex(const VERTEX &Vertex);
01344         virtual const ID_VERTEX AddVertex(const VECTOR3 &Vector)                {return CBasic3DObject::AddVertex(Vector);}
01345         virtual const ID_VERTEX AddVertex(double x,double y,double z)   {return CBasic3DObject::AddVertex(x,y,z);}
01346         virtual const ID_VERTEX AddVertex()                                                             {return CBasic3DObject::AddVertex();}
01347 
01349         virtual void Draw() = 0;
01350         
01352         void BeginVertexCache();
01354         void EndVertexCache();
01355 
01357         void  TransVC(const C3DTrans &Trans);
01358 
01360         const ID_LIST_VERTEX CopyVC();
01362         const ID_LIST_VERTEX CopyWithTransVC(const C3DTrans &Trans);
01363 
01364 
01365 
01372 
01373         const ID_LIST_VERTEX MakeCircle(double Radius,          
01374                                                                         const int VertexNum,
01375                                                                         VECTOR3 &Normal = VECTOR3(0.0,0.0,1.0),
01376                                                                         VECTOR3 &Center = VECTOR3());
01378         const ID_LIST_TRIANGLE MakeWall(const ID_LIST_VERTEX &VertexList1,
01379                                                                         const ID_LIST_VERTEX &VertexList2,
01380                                                                         const bool IsLoop = false);//頂点リストをループ状に繋げるかどうか
01381 
01383         const ID_LIST_VERTEX MakeWallFromVC(const ID_LIST_VERTEX &VertexList,
01384                                                                                 const bool IsLoop = false);
01385 
01387         void MakeWallByVC(const C3DTrans &Trans,
01388                                           const bool IsLoop = false);
01389 
01398 
01399         void MakeSolidRotation(const ID_LIST_VERTEX &VertexList,
01400                                                    int Div,
01401                                                    const bool IsLoop = false,
01402                                                    const VECTOR3 &BasePos = VECTOR3(0.0,0.0,0.0),
01403                                                    const double Theta = 360.0,
01404                                                    const VECTOR3 &Axis = VECTOR3(0.0,1.0,0.0));
01405 
01406         void MakeSolidRotationVC(const int Div,
01407                                                          const bool IsLoop = false,
01408                                                          const VECTOR3 &BasePos = VECTOR3(0.0,0.0,0.0),
01409                                                          const double Theta = 360.0,
01410                                                          const VECTOR3 &Axis = VECTOR3(0.0,1.0,0.0));
01411 
01412 
01417 
01418         virtual const ID_VERTEX  FindVertex(const VECTOR3 &Vector) const ;
01419 
01426 
01427         virtual const ID_VERTEX  FineVertex(double x,double y,double z) const;
01428 
01429 
01430 
01434 
01435         void SetSphere(int detail=2);
01436 
01440 
01441         bool LoadOBJFile(char* FileName);
01442         
01443 protected:
01444 
01445         std::vector<ID_VERTEX> m_VertexCache; 
01446         bool    VertexIsCached; 
01447 
01454 
01455         void C3DObject::subdivide(ID_VERTEX V1_ID, ID_VERTEX V2_ID, ID_VERTEX V3_ID,int depth);
01456 };
01457 
01458 
01459 #endif // __3DONJECT_H__
01460 
01461 #ifndef __CGLOBJECT_H__
01462 #define __CGLOBJECT_H__
01463 
01464 
01465 // glext.h からの VBO Extension の定義
01466 #define GL_ARRAY_BUFFER_ARB                     0x8892
01467 #define GL_ELEMENT_ARRAY_BUFFER_ARB     0x8893
01468 #define GL_STATIC_DRAW_ARB      0x88E4
01469 #define GL_DYNAMIC_DRAW_ARB 0x88E8
01470 typedef void (APIENTRY * PFNGLBINDBUFFERARBPROC)    (GLenum target, GLuint buffer);
01471 typedef void (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
01472 typedef void (APIENTRY * PFNGLGENBUFFERSARBPROC)    (GLsizei n, GLuint *buffers);
01473 typedef void (APIENTRY * PFNGLBUFFERDATAARBPROC)    (GLenum target, int size, const GLvoid *data, GLenum usage);
01474 
01476 enum GLObjectOptions{   GLOps_Normal            =       1,              
01477                                                 GLOps_Texture           =       2,              
01478                                                 GLOps_ObjectMaterial=   4,              
01479                                                 GLOps_VertexMaterial=   8,              
01480                                                 GLOps_PlaneMaterial     =       16,             
01481                                                 GLOps_VertexColor       =       32,             
01482                                                 GLOps_PlaneColor        =       64,             
01483                                                 GLOps_ALPHA                     =       128,    
01484                                                 GLOps_VBO                       =       256};   
01485 
01486 
01487 #define IfEnable(flag) if(m_DrawOption&(flag))
01488 
01489 
01490 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
01498 
01499 class CGLObject :public C3DObject  
01500 {
01501 public:
01502         CGLObject();
01503         virtual ~CGLObject();
01504 
01506         void Draw();
01507         
01509 
01510         const unsigned int  AddTriangle(const unsigned int &Vec1,
01511                                                                         const unsigned int &Vec2,
01512                                                                         const unsigned int &Vec3);
01514         init();
01515 
01517         void SetDrawOption(GLObjectOptions OptionFlag);
01519         void Lock();
01521         void Unlock();
01522 
01523 private:
01524 
01526         void CreateVBO();
01528         void InitVBO();
01530         void DrawVBO();
01531 
01533         bool IsExtensionSupported( char* szTargetExtension );
01534         
01535         unsigned int m_VBO_VERTEX;      
01536         unsigned int m_VBO_NORMAL;      
01537         unsigned int m_VBO_TEXCODE; 
01538         unsigned int m_VBO_COLOR;       
01539         unsigned int m_IBO_ID;  
01540         
01541         CArray<float> m_VertexList;     
01542         CArray<float> m_NormalList;     
01543         CArray<float> m_TexCodeList;    
01544         CArray<BYTE>  m_ColorList;      
01545         CArray<unsigned int> m_IndexList;       
01546 
01547         
01549         void DrawStandard();
01550 
01552         GLObjectOptions m_DrawOption;
01553         bool IsStatic;                  
01554 };
01555 
01556 #endif // __CGLOBJECT_H__
01557 
01558 
01559 
01560 
01561 
01562 
01563 
01564 
01565 
01566 #ifndef __CGLUTVIEW_H__
01567 #define __CGLUTVIEW_H__
01568 
01570 struct MouseState
01571 {
01572         int state;
01573         int button;
01574         int option;
01575         int x;
01576         int y;
01577 };
01578 
01580 struct KeyState
01581 {
01582         unsigned char key;
01583         int SpKey;
01584         int x;
01585         int y;
01586 };
01587 
01589 struct GLCameraState
01590 {
01591         double Aspect;
01592         double NearPlane;
01593         double FarPlane;
01594         double ViewAngle;
01595 };
01596 
01597 
01598 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
01608 template<class Type>
01609 class CGLUTView  
01610 {
01611 public:
01613         CGLUTView(){m_pDoc =NULL;}
01614         virtual ~CGLUTView(){}
01615         
01616         //仮想関数・各種コールバックを受け取る関数を空宣言
01617         virtual void OnDraw(){glutSwapBuffers();}               
01618         virtual void OnMouseMove(MouseState mouse){}    
01619         virtual void OnMouseEvent(MouseState mouse){}   
01620         virtual void OnKeyEvent(KeyState State){}               
01621         virtual void OnTimer(int ID){}                                  
01622         virtual void OnMenu (unsigned int ID){}                 
01623         virtual void OnReshape(int width, int height){} 
01624 
01626         void SetDocument(Type *pDoc){m_pDoc = pDoc;}
01627 protected:
01628         Type *m_pDoc;   
01629 };
01630 
01631 #endif // __CGLUTVIEW_H__
01632 
01633 
01634 
01635 
01636 
01637 
01638 #ifndef __GLUTMASTER_H__
01639 #define __GLUTMASTER_H__
01640 
01642 struct TimerState
01643 {
01644         int WindowID;   
01645         int TimerInt;   
01646 };
01647 
01648 /*-------------------------------------------------------------*//*-------------------------------------------------------------*/
01658 template<class Type>
01659 class CGLUTMaster  
01660 {
01661 public:
01663         CGLUTMaster()
01664         {
01665                 LPSTR argvt;
01666                 int argc=1;
01667                 argvt=GetCommandLine();
01668 
01669                 glutInit(&argc,&argvt);
01670                 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
01671         }
01672         virtual ~CGLUTMaster(){}
01673 
01675         int CreateGLUTWindow(CGLUTView<Type>* pViewClass,char *WindowTitle=NULL,int WindowWidth=200,int WindowHeight=200,int WndPosX=0,int WndPosY=0)
01676         {
01677                 int WindowID;
01678                 //窓サイズ
01679                 glutInitWindowPosition(WndPosX,WndPosY); 
01680                 glutInitWindowSize(WindowWidth, WindowHeight);
01681                 //glutで窓作成
01682                 if(WindowTitle==NULL)WindowID = glutCreateWindow("GLUT");
01683                 else WindowID = glutCreateWindow(WindowTitle);
01684 
01685                 //ViewClassをリストに登録
01686                 m_pViewClassList.resize(WindowID+1,NULL);
01687                 m_pViewClassList[WindowID] = pViewClass;
01688 
01689                 pViewClass->SetDocument(&m_Document);
01690                 //各種コールバック関数をGLUTに教える。
01691                 //全てこのクラスが呼ばれる
01692                 glutDisplayFunc(Display);
01693                 glutMouseFunc(Mouse);
01694                 glutMotionFunc(Motion);
01695                 glutReshapeFunc(Reshape);
01696                 glutKeyboardFunc(Keyboard);
01697                 glutSpecialFunc(SpecialKey);
01698 
01699                 return WindowID;
01700         }
01701 
01702         void MainLoop(){glutMainLoop();}
01703 
01705         static void Display()
01706         {
01707                 //呼び出された窓IDからViewClassを検索
01708                 int Call_ID=glutGetWindow();
01709                 //Viewのメソッドを呼び出し
01710                 m_pViewClassList[Call_ID]->OnDraw();
01711         }
01712 
01714         static void Reshape(int width, int height)
01715         {
01716                 int Call_ID=glutGetWindow();
01717                 m_pViewClassList[Call_ID]->OnReshape(width,height);
01718         }
01719 
01721         static void Keyboard(unsigned char key, int x, int y)
01722         {
01723                 m_KeyState.key   = key;
01724                 m_KeyState.SpKey = -1;
01725                 m_KeyState.x=x;
01726                 m_KeyState.y=y;
01727 
01728                 int Call_ID=glutGetWindow();
01729                 m_pViewClassList[Call_ID]->OnKeyEvent(m_KeyState);
01730         }
01731 
01733         static void SpecialKey(int key, int x, int y)
01734         {
01735                 m_KeyState.key   = -1;
01736                 m_KeyState.SpKey = key;
01737                 m_KeyState.x=x;
01738                 m_KeyState.y=y;
01739 
01740                 int Call_ID=glutGetWindow();
01741                 m_pViewClassList[Call_ID]->OnKeyEvent(m_KeyState);
01742         }
01743 
01745         static void Mouse(int button, int state, int x, int y)
01746         {
01747                 m_Mouse.button  = button;
01748                 m_Mouse.state   = state;
01749                 m_Mouse.x = x;
01750                 m_Mouse.y = y;
01751 
01752                 int Call_ID=glutGetWindow();
01753                 m_pViewClassList[Call_ID]->OnMouseEvent(m_Mouse);
01754         }
01755 
01757         static void Motion(int x, int y)
01758         {
01759                 m_Mouse.x = x;
01760                 m_Mouse.y = y;
01761 
01762                 int Call_ID=glutGetWindow();
01763                 m_pViewClassList[Call_ID]->OnMouseEvent(m_Mouse);
01764         }
01765 
01767         static void Idle(){/*(´ー`●)*/}
01768 
01770         static void Timer(int ID)
01771         {
01772                 glutSetWindow(m_TimerList[ID].WindowID);
01773 
01774                 m_pViewClassList[m_TimerList[ID].WindowID]->OnTimer(ID);
01775                 m_TimerList[ID].WindowID = -1;
01776         }
01777 
01779         static void AutoRepeatTimer(int ID)
01780         {
01781                 glutSetWindow(m_TimerList[ID].WindowID);
01782                 m_pViewClassList[m_TimerList[ID].WindowID]->OnTimer(ID);
01783                 glutTimerFunc(m_TimerList[ID].TimerInt,AutoRepeatTimer,ID);
01784         }
01785 
01787         void SetTimer(int TimerInt,int WindowID,bool IsRepeat=false)
01788         {
01789                 TimerState NewTimer;
01790 
01791                 NewTimer.TimerInt = TimerInt;
01792                 NewTimer.WindowID = WindowID;
01793 
01794                 int TID;
01795 
01796                 for(TID=0;TID<m_TimerList.size();TID++)
01797                 {
01798                         if(m_TimerList[TID].WindowID==-1)
01799                         {
01800                                 m_TimerList[TID]=NewTimer;
01801                                 break;
01802                         }
01803                 }
01804                 if(TID == m_TimerList.size())
01805                         m_TimerList.push_back(NewTimer);
01806 
01807                 if(IsRepeat)
01808                         glutTimerFunc(NewTimer.TimerInt,AutoRepeatTimer,TID);
01809                 else
01810                         glutTimerFunc(NewTimer.TimerInt,Timer,TID);
01811         }
01812         
01813         Type* GetDoc(){return &m_Document;}
01814 
01815 private:
01816         static std::vector<CGLUTView<Type>*> m_pViewClassList;  
01817         static Type m_Document;                                                                 
01818         static MouseState m_Mouse;                                                              
01819         static KeyState   m_KeyState;                                                   
01820         static std::vector<TimerState> m_TimerList;                             
01821 };
01822 
01823 //各データの実体を定義
01824 template<class Type>
01825 Type CGLUTMaster<Type>::m_Document;
01826 
01827 template<class Type>
01828 MouseState CGLUTMaster<Type>::m_Mouse;
01829 
01830 template<class Type>
01831 std::vector<CGLUTView<Type>*> CGLUTMaster<Type>::m_pViewClassList;
01832 
01833 template<class Type>
01834 KeyState   CGLUTMaster<Type>::m_KeyState;
01835 
01836 template<class Type>
01837 std::vector<TimerState> CGLUTMaster<Type>::m_TimerList;
01838 
01839 #endif // __GLUTMASTER_H__
01840 
01841 
01842 
01843 
01844 
01845 
01846 
01847 
01848 
01849 
01850 
01851 
01852 };
01853 /*------------------------------------------------The-End-of-SCGL*/

ScoutCGLibに対してSat Jun 17 15:19:36 2006に生成されました。  doxygen 1.4.6-NO