![]() CATEGORIES: BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism |
Приложение А. Исходный код работыДобавить в файл kg_lab.h: … struct s_figure { s_vector3 obj_scale, obj_rot, obj_pos; … s_figure( const s_vector3& pos_, const s_vector3& rot_ = vec0, const s_vector3& scale_ = vec1, DWORD color_ = 0xff808080UL, float level_ = 0.5f, float spower_ = 16.f, bool anim_ = true );
s_figure* child; s_figure* next() const { return child; } void set_child( s_figure* child_ ) { child = child_; }
};
void ReleaseScene(); void InitScene(); void AddFigure( s_figure* pFig ); }; //namespace scene
namespace figures { using namespace directx; struct s_cube : public scene::s_figure { … };
struct s_plane : public scene::s_figure { virtual const s_vertex* verts( DWORD& cnt ) const; virtual const t_index* inds( DWORD& cnt ) const; s_plane( const s_vector3& pos_, const s_vector3& rot_ = vec0, const s_vector3& scale_ = vec1, DWORD color_ = 0xff808080UL, float level_ = 0.5f, float spower_ = 16.f ) : scene::s_figure( pos_, rot_, scale_, color_, level_, spower_, false ) {} };
struct s_cube_smooth : public scene::s_figure { virtual const s_vertex* verts( DWORD& cnt ) const; virtual const t_index* inds( DWORD& cnt ) const;
s_cube_smooth( const s_vector3& pos_, const s_vector3& rot_ = vec0, const s_vector3& scale_ = vec1, DWORD color_ = 0xff808080UL, float level_ = 0.5f, float spower_ = 16.f ) : scene::s_figure( pos_, rot_, scale_, color_, level_, spower_ ) {} };
}; //namespace figures Добавить и изменить в файле scene.cpp: void RenderFigures( IDirect3DDevice9* Dev, LPD3DXEFFECT pEffect ) { const s_figure* f = pSceneRoot; while( f ) { DWORD vcnt = 0, icnt = 0; const s_vertex* v = f->verts( vcnt ); const t_index* i = f->inds( icnt ); DWORD tri_cnt = icnt / 3;
const s_matrix& MatWorld = f->matrix(); const s_vector4& vObjColor = f->color();
pEffect->SetMatrix( hMatWorld, &MatWorld ); pEffect->SetVector( hObjColor, &vObjColor ); pEffect->SetFloat( hPower, f->power() ); pEffect->SetFloat( hSpecLevel, f->spec_level() );
pEffect->CommitChanges();
Dev->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST, 0, vcnt, tri_cnt, i, D3DFMT_INDEX16, v, sizeof( s_vertex ) );
f = f->next();
}// while( f )
}
s_figure::s_figure( const s_vector3& pos_, const s_vector3& rot_, const s_vector3& scale_, DWORD color_, float level_, float spower_, bool anim_ ) : obj_scale( scale_ ), obj_rot( rot_ ), obj_pos( pos_ ), level( level_ ), spower( spower_ ), changes( true ), anim( anim_ ), child( 0 ) { D3DXMatrixIdentity( &world ); set_color( color_ );
}
… void AddFigure( s_figure* pFig ) { pFig->set_child( pSceneRoot ); pSceneRoot = pFig;
}
void ReleaseScene() { s_figure* f = pSceneRoot; while( f ) { s_figure* f1 = f; f = f->next(); delete f1;
}// while( f )
pSceneRoot = 0; }
void InitScene() { vEyePos = vec0; vAtPos = s_vector3( 0.f, 0.f, 1.f ); fFovY = 60.f;
AddFigure( new figures::s_plane( s_vector3( 0.f, -8.f, 20.f ), s_vector3( 0.f, 0.f, 0.f ), s_vector3( 15.f, 1.f, 35.f ), 0xff808080UL, 0.2f, 64.f ) );
AddFigure( new figures::s_cube( s_vector3( 10.f, -4.f, 30.f ), s_vector3( 0.f, 0.f, 0.f ), s_vector3( 3.f, 3.f, 3.f ), 0xff0000ffUL, 1.f, 32.f ) ); AddFigure( new figures::s_cube_smooth( s_vector3( 0.f, 5.f, 35.f ), s_vector3( 0.f, 0.f, 0.f ), s_vector3( 2.f, 2.f, 2.f ), 0xff00ff00UL, 0.5f, 16.f ) ); AddFigure( new figures::s_cube_smooth( s_vector3( -10.f, -2.f, 25.f ), s_vector3( 0.f, 0.f, 0.f ), s_vector3( 3.f, 2.f, 1.f ), 0xffff0000UL, 0.5f, 3.f ) ); }
Добавить в файл figures.cpp: … const t_index* s_cube::inds( DWORD& cnt ) const { …
21, 20, 23, 20, 22, 23,//правая грань };
cnt = sizeof( inds ) / sizeof( inds[0] ); return inds; }
const s_vertex* s_plane::verts( DWORD& cnt ) const { static s_vertex verts[] = { s_vertex( s_vector3( -1.f, 0.f, -1.f ) ), s_vertex( s_vector3( 1.f, 0.f, -1.f ) ), s_vertex( s_vector3( -1.f, 0.f, 1.f ) ), s_vertex( s_vector3( 1.f, 0.f, 1.f ) ),
};
cnt = sizeof( verts ) / sizeof( verts[0] ); return verts; }
const t_index* s_plane::inds( DWORD& cnt ) const { static const t_index inds[] = { 1, 0, 2, 1, 2, 3, };
cnt = sizeof( inds ) / sizeof( inds[0] ); return inds; }
const s_vertex* s_cube_smooth::verts( DWORD& cnt ) const { static s_vertex verts[] = { s_vertex( s_vector3( -1.f, -1.f, -1.f ) ), s_vertex( s_vector3( 1.f, -1.f, -1.f ) ), s_vertex( s_vector3( -1.f, 1.f, -1.f ) ), s_vertex( s_vector3( 1.f, 1.f, -1.f ) ),
s_vertex( s_vector3( -1.f, -1.f, 1.f ) ), s_vertex( s_vector3( 1.f, -1.f, 1.f ) ), s_vertex( s_vector3( -1.f, 1.f, 1.f ) ), s_vertex( s_vector3( 1.f, 1.f, 1.f ) ), };
cnt = sizeof( verts ) / sizeof( verts[0] ); return verts; }
const t_index* s_cube_smooth::inds( DWORD& cnt ) const { static const t_index inds[] = { 1, 0, 2, 1, 2, 3, //дальняя грань 4, 5, 6, 5, 7, 6, //ближняя грань
0, 1, 4, 1, 5, 4, //верхняя грань 6, 7, 2, 7, 3, 2, //нижняя грань
0, 4, 2, 4, 6, 2, //левая грань 5, 1, 3, 5, 3, 7, //правая грань };
cnt = sizeof( inds ) / sizeof( inds[0] ); return inds; }
}; //namespace figures
Date: 2016-03-03; view: 1577
|