home *** CD-ROM | disk | FTP | other *** search
- Save Format v1.3
- @begin ClassFile "RenderBox"
- Exported 0;
-
- @begin-code BaseClassList
-
- public WObject
-
- @end-code;
-
- @begin UserFunction "RenderBox( WWindow * box )"
- GencodeSrcLine 131;
- FunctionName "RenderBox::RenderBox( WWindow * box )";
- @end;
-
- @begin UserFunction "Prototype for RenderBox( WWindow * box )"
- Private 1;
- GencodeSrcLine 21;
- FunctionName "RenderBox::Prototype for RenderBox( WWindow * box )";
- @end;
-
- @begin UserFunction "~RenderBox()"
- GencodeSrcLine 141;
- FunctionName "RenderBox::~RenderBox()";
- @end;
-
- @begin UserFunction "Prototype for ~RenderBox()"
- Private 1;
- GencodeSrcLine 23;
- FunctionName "RenderBox::Prototype for ~RenderBox()";
- @end;
-
- @begin UserFunction "HdlBoxPaint( WObject *, WPaintEventData * event )"
- Private 1;
- GencodeSrcLine 146;
- FunctionName "RenderBox::HdlBoxPaint( WObject *, WPaintEventData * event )";
- @end;
-
- @begin UserFunction "Prototype for HdlBoxPaint( WObject *, WPaintEventData * event )"
- Private 1;
- GencodeSrcLine 25;
- FunctionName "RenderBox::Prototype for HdlBoxPaint( WObject *, WPaintEventData * event )";
- @end;
-
- @begin UserFunction "Render( WCanvas & canvas )"
- GencodeSrcLine 171;
- FunctionName "RenderBox::Render( WCanvas & canvas )";
- @end;
-
- @begin UserFunction "Prototype for Render( WCanvas & canvas )"
- Private 1;
- GencodeSrcLine 27;
- FunctionName "RenderBox::Prototype for Render( WCanvas & canvas )";
- @end;
-
- @begin UserFunction "DrawObject( WCanvas & canvas, const grBaseTransform & xfrm, grPoint3 * lines, WUInt n )"
- Private 1;
- GencodeSrcLine 231;
- FunctionName "RenderBox::DrawObject( WCanvas & canvas, const grBaseTransform & xfrm, grPoint3 * lines, WUInt n )";
- @end;
-
- @begin UserFunction "Prototype for DrawObject( WCanvas & canvas, const grBaseTransform & xfrm, grPoint3 * lines, WUInt n )"
- Private 1;
- GencodeSrcLine 29;
- FunctionName "RenderBox::Prototype for DrawObject( WCanvas & canvas, const grBaseTransform & xfrm, grPoint3 * lines, WUInt n )";
- @end;
-
- @begin UserFunction "ClipLine( grPoint3 & A, grPoint3 & B )"
- Private 1;
- GencodeSrcLine 251;
- FunctionName "RenderBox::ClipLine( grPoint3 & A, grPoint3 & B )";
- @end;
-
- @begin UserFunction "Prototype for ClipLine( grPoint3 & A, grPoint3 & B )"
- Private 1;
- GencodeSrcLine 31;
- FunctionName "RenderBox::Prototype for ClipLine( grPoint3 & A, grPoint3 & B )";
- @end;
-
- @begin UserFunction "DrawLine( WCanvas & canvas, const grPoint3 & p1, const grPoint3 & p2 )"
- GencodeSrcLine 291;
- FunctionName "RenderBox::DrawLine( WCanvas & canvas, const grPoint3 & p1, const grPoint3 & p2 )";
- @end;
-
- @begin UserFunction "Prototype for DrawLine( WCanvas & canvas, const grPoint3 & p1, const grPoint3 & p2 )"
- Private 1;
- GencodeSrcLine 33;
- FunctionName "RenderBox::Prototype for DrawLine( WCanvas & canvas, const grPoint3 & p1, const grPoint3 & p2 )";
- @end;
-
- @begin UserFunction "MapNDCToDC( const grPoint3 & p )"
- Private 1;
- GencodeSrcLine 313;
- FunctionName "RenderBox::MapNDCToDC( const grPoint3 & p )";
- @end;
-
- @begin UserFunction "Prototype for MapNDCToDC( const grPoint3 & p )"
- Private 1;
- GencodeSrcLine 35;
- FunctionName "RenderBox::Prototype for MapNDCToDC( const grPoint3 & p )";
- @end;
-
- @begin UserFunction "RotateModel( int axis, double radians )"
- GencodeSrcLine 335;
- FunctionName "RenderBox::RotateModel( int axis, double radians )";
- @end;
-
- @begin UserFunction "Prototype for RotateModel( int axis, double radians )"
- Private 1;
- GencodeSrcLine 37;
- FunctionName "RenderBox::Prototype for RotateModel( int axis, double radians )";
- @end;
-
- @begin UserFunction "GetClipPlanes( double & nearf, double & farf )"
- GencodeSrcLine 342;
- FunctionName "RenderBox::GetClipPlanes( double & nearf, double & farf )";
- @end;
-
- @begin UserFunction "Prototype for GetClipPlanes( double & nearf, double & farf )"
- Private 1;
- GencodeSrcLine 39;
- FunctionName "RenderBox::Prototype for GetClipPlanes( double & nearf, double & farf )";
- @end;
-
- @begin UserFunction "SetClipPlanes( double nearf, double farf )"
- GencodeSrcLine 348;
- FunctionName "RenderBox::SetClipPlanes( double nearf, double farf )";
- @end;
-
- @begin UserFunction "Prototype for SetClipPlanes( double nearf, double farf )"
- Private 1;
- GencodeSrcLine 41;
- FunctionName "RenderBox::Prototype for SetClipPlanes( double nearf, double farf )";
- @end;
-
- @begin UserFunction "GetFOVY()"
- GencodeSrcLine 357;
- FunctionName "RenderBox::GetFOVY()";
- @end;
-
- @begin UserFunction "Prototype for GetFOVY()"
- Private 1;
- GencodeSrcLine 43;
- FunctionName "RenderBox::Prototype for GetFOVY()";
- @end;
-
- @begin UserFunction "SetFOVY( double fovy )"
- GencodeSrcLine 364;
- FunctionName "RenderBox::SetFOVY( double fovy )";
- @end;
-
- @begin UserFunction "Prototype for SetFOVY( double fovy )"
- Private 1;
- GencodeSrcLine 45;
- FunctionName "RenderBox::Prototype for SetFOVY( double fovy )";
- @end;
-
- @begin UserFunction "TranslateView( double, double, double )"
- GencodeSrcLine 372;
- FunctionName "RenderBox::TranslateView( double, double, double )";
- @end;
-
- @begin UserFunction "Prototype for TranslateView( double, double, double )"
- Private 1;
- GencodeSrcLine 47;
- FunctionName "RenderBox::Prototype for TranslateView( double, double, double )";
- @end;
-
- @begin HPPPrefixBlock
- @begin-code HPPPrefix
-
- class RenderBoxImplementation;
- class grVector;
- class grPoint3;
- class grBaseTransform;
-
- @end-code;
- GencodeSrcLine 10;
- @end;
-
- @begin CPPPrefixBlock
- @begin-code CPPPrefix
-
- #include <math.h>
- #include "grVector.hpp"
- #include "grTransform.hpp"
-
- #define PI 3.14159265359
-
- #define NEARF 0.1
- #define FARF 100.0
- #define FOVY (45.0 * PI/180.0)
- #define ASPECT 1.0
-
- //----------------- ClipPlane class --------------------//
-
- class WCMCLASS ClipPlane { // REPRESENT CLIPPING PLANES
- public:
- ClipPlane(){}
-
- ClipPlane( const ClipPlane & o )
- : point( o.point ), normal( o.normal ){}
-
- ClipPlane( const grPoint3 & p, const grVector & n )
- : point( p ), normal( n ) {}
-
- grPoint3 point; // point on plane
- grVector normal; // unit inward normal
- };
- #define NUMCLIPPLANES 6
-
- //------------ RenderBoxImplementation class ---------------//
-
- class WCMCLASS RenderBoxImplementation : public WObject
- {
- public:
- RenderBoxImplementation();
- Reset();
- InitClipPlanes();
- public:
- grTransform _modelTransform;
- grVector _modelScale;
- grTransform _viewTransform;
- grTransform _projTransform; // projection transform
- double _nearF;
- double _farF;
- double _fovy;
-
- ClipPlane _clipPlanes[ NUMCLIPPLANES ];
- };
-
- RenderBoxImplementation::RenderBoxImplementation()
- : _nearF( NEARF )
- , _farF( FARF )
- , _fovy( FOVY )
- //------------------------------------------------
- {
- InitClipPlanes();
- Reset();
- }
-
- RenderBoxImplementation::InitClipPlanes()
- //---------------------------------------
- {
- grPoint3 point;
- grVector normal;
- double ctan;
-
- ctan = 1.0 / tan( _fovy / 2.0 );
-
- // front
- point = grPoint3( 0, 0, -_nearF );
- normal = grVector( 0, 0, -1 );
- _clipPlanes[ 0 ] = ClipPlane( point, normal );
-
- // back
- point = grPoint3( 0, 0, -_farF );
- normal = grVector( 0, 0, 1 );
- _clipPlanes[ 1 ] = ClipPlane( point, normal );
-
- // top
- point = grPoint3( 0, _nearF * ctan, -_nearF );
- normal = grVector( 0, -1.0, -ctan );
- normal.normalize();
- _clipPlanes[ 2 ] = ClipPlane( point, normal );
-
- // bottom
- point = grPoint3( 0, -_nearF * ctan, -_nearF );
- normal = grVector( 0, 1.0, -ctan );
- normal.normalize();
- _clipPlanes[ 3 ] = ClipPlane( point, normal );
-
- // right
- point = grPoint3( _nearF * ctan / ASPECT, 0, -_nearF );
- normal = grVector( -1.0, 0, -ctan / ASPECT );
- normal.normalize();
- _clipPlanes[ 4 ] = ClipPlane( point, normal );
-
- // left
- point = grPoint3( -_nearF * ctan / ASPECT, 0, -_nearF );
- normal = grVector( 1.0, 0, -ctan / ASPECT );
- normal.normalize();
- _clipPlanes[ 5 ] = ClipPlane( point, normal );
-
- grPerspective pers( _fovy, ASPECT, _nearF, _farF );
- _projTransform = pers;
- }
-
- RenderBoxImplementation::Reset()
- //------------------------------
- {
- _modelTransform = grBaseTransform();
- _modelScale = grVector( 1.0, 1.0, 1.0 );
- _viewTransform = grTranslate( grVector( 0.0, 0.0, -4.0 ) );
-
- InitClipPlanes();
- }
-
- void grError( const char * msg )
- //------------------------------
- {
- WMessageBox::Message( NULL, WMBLevelError, WMBButtonOk, "RenderBox Demo", msg, WMBDefault1, WMBAttributeTaskModal );
- }
-
- @end-code;
- GencodeSrcLine 11;
- @end;
-
- @begin ClassContentsBlock
- @begin-code ClassContents
-
- private:
- WWindow * _box;
- RenderBoxImplementation * _imp;
-
- @end-code;
- GencodeSrcLine 18;
- @end;
-
- @begin-code GeneratedClassContents
-
-
- @end-code;
-
- @begin-code Code "RenderBox::RenderBox( WWindow * box )"
-
- RenderBox::RenderBox( WWindow * box )
- : _box( box )
- , _imp( NULL )
- //----------------
- {
- _imp = new RenderBoxImplementation;
-
- _box->SetEventHandler( WPaintEvent, this,
- (WEventHandler)&RenderBox::HdlBoxPaint );
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for RenderBox( WWindow * box )"
-
- public:
- RenderBox( WWindow * box );
-
- @end-code;
-
- @begin-code Code "RenderBox::~RenderBox()"
-
- RenderBox::~RenderBox()
- //---------------------
- {
- delete _imp;
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for ~RenderBox()"
-
- public:
- ~RenderBox();
-
- @end-code;
-
- @begin-code Code "RenderBox::HdlBoxPaint( WObject *, WPaintEventData * event )"
-
- WBool RenderBox::HdlBoxPaint( WObject *, WPaintEventData * event )
- //----------------------------------------------------------------
- // Draw the scene onto the target window in response to a paint
- // event. Use double buffering to reduce screen flicker.
- {
- WRect br;
- WCanvas dup( *event->canvas, TRUE );
- WBitmap bmp;
- WBitmap old_bmp;
-
- br = _box->GetRectangle();
- br.x = 0;
- br.y = 0;
-
- bmp.Create( event->canvas->GetHandle(), br.w, br.h );
- old_bmp = dup.GetBitmap();
- dup.SetBackColor( _box->GetBackColor() );
- dup.SetBitmap( bmp );
- dup.FillRectangle( br );
- Render( dup );
- event->canvas->DrawFromCanvas( br, dup );
- dup.SetBitmap( old_bmp );
-
- return( TRUE );
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for HdlBoxPaint( WObject *, WPaintEventData * event )"
-
- private:
- WBool HdlBoxPaint( WObject *, WPaintEventData * event );
-
- @end-code;
-
- @begin-code Code "RenderBox::Render( WCanvas & canvas )"
-
- static grPoint3 WorldGnomon[] = {
- grPoint3( 0.0, 0.0, 0.0 ), grPoint3( +0.5, 0.0, 0.0 ),
- grPoint3( 0.0, 0.0, 0.0 ), grPoint3( 0.0, +0.5, 0.0 ),
- grPoint3( 0.0, 0.0, 0.0 ), grPoint3( 0.0, 0.0, +0.5 ),
- };
- #define NUMWORLDGLINES (sizeof(WorldGnomon) / (2*sizeof(WorldGnomon[0])) )
-
- static grPoint3 BoxGnomon[] = {
- grPoint3( 0.0, 0.0, 0.0 ), grPoint3( +0.5, 0.0, 0.0 ),
- grPoint3( 0.0, 0.0, 0.0 ), grPoint3( 0.0, +0.5, 0.0 ),
- grPoint3( 0.0, 0.0, 0.0 ), grPoint3( 0.0, 0.0, +0.5 ),
- };
- #define NUMBOXGLINES (sizeof(BoxGnomon) / (2*sizeof(BoxGnomon[0])) )
-
- static grPoint3 Box[] = {
- grPoint3( -1.0, -1.0, -1.0 ), grPoint3( +1.0, -1.0, -1.0 ),
- grPoint3( +1.0, -1.0, -1.0 ), grPoint3( +1.0, -1.0, +1.0 ),
- grPoint3( +1.0, -1.0, +1.0 ), grPoint3( -1.0, -1.0, +1.0 ),
- grPoint3( -1.0, -1.0, +1.0 ), grPoint3( -1.0, -1.0, -1.0 ),
-
- grPoint3( -1.0, -1.0, -1.0 ), grPoint3( -1.0, +1.0, -1.0 ),
- grPoint3( -1.0, +1.0, -1.0 ), grPoint3( -1.0, +1.0, +1.0 ),
- grPoint3( -1.0, +1.0, +1.0 ), grPoint3( -1.0, -1.0, +1.0 ),
-
- grPoint3( -1.0, +1.0, +1.0 ), grPoint3( +1.0, +1.0, +1.0 ),
- grPoint3( +1.0, +1.0, +1.0 ), grPoint3( +1.0, +1.0, -1.0 ),
- grPoint3( +1.0, +1.0, -1.0 ), grPoint3( -1.0, +1.0, -1.0 ),
-
- grPoint3( +1.0, +1.0, +1.0 ), grPoint3( +1.0, -1.0, +1.0 ),
- grPoint3( +1.0, +1.0, -1.0 ), grPoint3( +1.0, -1.0, -1.0 ),
- };
- #define NUMBOXLINES (sizeof(Box) / (2*sizeof(Box[0])) )
-
-
- void RenderBox::Render( WCanvas & canvas )
- //----------------------------------------
- {
- grTransform boxg( _imp->_modelTransform );
- grTransform box;
- grTransform view( _imp->_viewTransform );
- grVector mScale( _imp->_modelScale );
- WPen old_pen;
- WPen red( WColorBrightRed );
-
- box.scale( grPoint3(0,0,0), mScale.x(), mScale.y(), mScale.z() );
- box *= boxg;
- box *= view;
- boxg *= view;
-
- old_pen = canvas.GetPen();
- DrawObject( canvas, view, WorldGnomon, NUMWORLDGLINES );
-
- canvas.SetPen( red );
- DrawObject( canvas, boxg, BoxGnomon, NUMBOXGLINES );
-
- canvas.SetPen( red );
- DrawObject( canvas, box, Box, NUMBOXLINES );
-
- canvas.SetPen( old_pen );
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for Render( WCanvas & canvas )"
-
- public:
- virtual void Render( WCanvas & canvas );
-
- @end-code;
-
- @begin-code Code "RenderBox::DrawObject( WCanvas & canvas, const grBaseTransform & xfrm, grPoint3 * lines, WUInt n )"
-
- void RenderBox::DrawObject(
- WCanvas & canvas,
- const grBaseTransform & xfrm,
- grPoint3 * lines,
- WUInt numLines )
- //-------------------------------
- {
- WUInt i;
- grPoint3 p1, p2;
-
- for( i = 0; i < numLines; i++ ) {
- p1 = lines[ 2 * i ];
- p2 = lines[ 2 * i + 1 ];
-
- xfrm.apply( p1 );
- xfrm.apply( p2 );
-
- DrawLine( canvas, p1, p2 );
- }
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for DrawObject( WCanvas & canvas, const grBaseTransform & xfrm, grPoint3 * lines, WUInt n )"
-
- private:
- void DrawObject( WCanvas & canvas, const grBaseTransform & xfrm, grPoint3 * lines, WUInt n );
-
- @end-code;
-
- @begin-code Code "RenderBox::ClipLine( grPoint3 & A, grPoint3 & B )"
-
- /*
- * Liang Barsky 3-D clipping _Computer_Graphics_ (Hearn, Baker) p. 231,
- * _Computer_Graphics,_Principle_and_Practice (Foley, van Dam, Feiner, Hughes)
- * p. 122, 275.
- *
- * The formulation of ClipTest is from [Hearn,Baker]; the [Foley,et al]
- * uses 'denom' instead of 'p', 'num' instead of 'q', 'tE' instead of 'u1',
- * and 'tL' instead of 'u2'.
- */
-
- WBool RenderBox::ClipLine( grPoint3 & A, grPoint3 & B )
- //-----------------------------------------------------
- {
- double wecA, wecB; // -- window edge coordinates
- double t;
- ClipPlane * plane;
- uint i;
-
- for( i = 0; i < NUMCLIPPLANES; i++ ) {
- plane = &_imp->_clipPlanes[ i ];
- wecA = dot( A - plane->point, plane->normal );
- wecB = dot( B - plane->point, plane->normal );
-
- if( wecA < 0 && wecB < 0 ) {
- return grFALSE; // reject
- }
- if( wecA >= 0 && wecB >= 0 ) {
- // accept trivially
- } else {
- t = wecA / (wecA - wecB);
- if( wecA < 0 ) {
- A = A + t * (B - A);
- } else {
- B = A + t * (B - A);
- }
- }
- }
-
- return grTRUE;
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for ClipLine( grPoint3 & A, grPoint3 & B )"
-
- private:
- WBool ClipLine( grPoint3 & A, grPoint3 & B );
-
- @end-code;
-
- @begin-code Code "RenderBox::DrawLine( WCanvas & canvas, const grPoint3 & p1, const grPoint3 & p2 )"
-
- void RenderBox::DrawLine( // DRAW A LINE BETWEEN 2 3-SPACE POINTS
- WCanvas & canvas,
- const grPoint3 & p1, // -- first point
- const grPoint3 & p2 ) // -- second point
- //-----------------------
- {
- WPoint wp1, wp2;
- grPoint3 point1( p1 );
- grPoint3 point2( p2 );
-
- if( !ClipLine( point1, point2 ) ) {
- return;
- }
-
- _imp->_projTransform.apply( point1 );
- _imp->_projTransform.apply( point2 );
-
- wp1 = MapNDCToDC( point1 );
- wp2 = MapNDCToDC( point2 );
-
- canvas.DrawLine( wp1, wp2 );
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for DrawLine( WCanvas & canvas, const grPoint3 & p1, const grPoint3 & p2 )"
-
- public:
- void DrawLine( WCanvas & canvas, const grPoint3 & p1, const grPoint3 & p2 );
-
- @end-code;
-
- @begin-code Code "RenderBox::MapNDCToDC( const grPoint3 & p )"
-
- WPoint RenderBox::MapNDCToDC(
- const grPoint3 & p )
- //----------------------
- // convert a point from normalized device
- // coordinates (-1.0..1.0) to device coordinates (0..max)
- {
- WRect boxRect;
- WPoint ret;
- WUInt base;
- grPoint3 point( p );
-
- point.fixup();
- point.x( point.x() * 0.5 + 0.5 );
- point.y( point.y() * 0.5 + 0.5 );
-
- boxRect = _box->GetRectangle();
- base = __min( boxRect.w, boxRect.h );
- ret.x = point.x() * base;
- ret.y = base - point.y() * base;
-
- return( ret );
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for MapNDCToDC( const grPoint3 & p )"
-
- private:
- WPoint MapNDCToDC( const grPoint3 & p );
-
- @end-code;
-
- @begin-code Code "RenderBox::RotateModel( int axis, double radians )"
-
- void RenderBox::RotateModel( int axis, double radians )
- //-----------------------------------------------------
- {
- _imp->_modelTransform.rotate( axis, radians );
- _box->Invalidate( TRUE );
- _box->Update();
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for RotateModel( int axis, double radians )"
-
- public:
- void RotateModel( int axis, double radians );
-
- @end-code;
-
- @begin-code Code "RenderBox::GetClipPlanes( double & nearf, double & farf )"
-
- void RenderBox::GetClipPlanes( double & nearf, double & farf )
- //----------------------------------------------------------
- {
- nearf = _imp->_nearF;
- farf = _imp->_farF;
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for GetClipPlanes( double & nearf, double & farf )"
-
- public:
- void GetClipPlanes( double & nearf, double & farf );
-
- @end-code;
-
- @begin-code Code "RenderBox::SetClipPlanes( double nearf, double farf )"
-
- void RenderBox::SetClipPlanes( double nearf, double farf )
- //------------------------------------------------------
- {
- _imp->_nearF = nearf;
- _imp->_farF = farf;
- _imp->InitClipPlanes();
- _box->Invalidate();
- _box->Update();
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for SetClipPlanes( double nearf, double farf )"
-
- public:
- void SetClipPlanes( double nearf, double farf );
-
- @end-code;
-
- @begin-code Code "RenderBox::GetFOVY()"
-
- double RenderBox::GetFOVY()
- //-------------------------
- {
- double fovy( _imp->_fovy );
- fovy *= 180.0 / PI;
- return( fovy );
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for GetFOVY()"
-
- public:
- double GetFOVY();
-
- @end-code;
-
- @begin-code Code "RenderBox::SetFOVY( double fovy )"
-
- void RenderBox::SetFOVY( double fovy )
- //------------------------------------
- {
- _imp->_fovy = fovy * PI / 180.0;
- _imp->InitClipPlanes();
- _box->Invalidate();
- _box->Update();
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for SetFOVY( double fovy )"
-
- public:
- void SetFOVY( double fovy );
-
- @end-code;
-
- @begin-code Code "RenderBox::TranslateView( double, double, double )"
-
- void RenderBox::TranslateView( double x, double y, double z )
- {
- grVector v( x, y, z );
- grTranslate t( v );
- _imp->_viewTransform = t;
- }
-
- @end-code;
-
- @begin-code Code "RenderBox::Prototype for TranslateView( double, double, double )"
-
- public:
- void TranslateView( double, double, double );
-
- @end-code;
- @end;
-