Трехмерная графикаРефераты >> Программирование и компьютеры >> Трехмерная графика
{
Matrix m = MirrorY();
for ( int i = 0; i < PointNumber; i++ )
Point[i] = m * Point[i];
Center = m * Center;
Normal = m * Normal;
}
void Polygon :: PolyMirrorZ ()
{
Matrix m = MirrorZ();
for ( int i = 0; i < PointNumber; i++ )
Point[i] = m * Point[i];
Center = m * Center;
Normal = m * Normal;
}
void Polygon :: Draw ( const Vector& PrCenter )
{
int VisPoint[MaxPoints * 2], k = 0;
for ( int i = 0; i < PointNumber; i++ ) {
double Coeff = 1 / ( 1 - Point[i].z / PrCenter.z );
VisPoint[k++] = ( int ) Point[i].x * Coeff + 320;
VisPoint[k++] = ( int ) -Point[i].y * Coeff + 175;
}
setcolor ( Color );
setfillstyle ( 1, Color );
fillpoly ( PointNumber, VisPoint );
}
// GrObject's methods
GrObject :: GrObject ( Polygon * FacetArr, int FacetNum, const Vector& Crds )
{
if ( FacetNum <= MaxFacets )
{
FacetNumber = FacetNum;
Facet = FacetArr;
Coords = Crds;
}
}
void GrObject :: Move ( const Vector& v )
{
for ( int i = 0; i < FacetNumber; i++ )
Facet[i].Move ( v );
Coords = Translate ( v ) * Coords;
}
void GrObject :: Rotate ( double Alfa, double Beta, double Gamma )
{
for ( int i = 0; i < FacetNumber; i++ )
Facet[i].Rotate ( Alfa, Beta, Gamma );
Coords = RotateX ( Alfa ) * RotateY ( Beta ) * RotateZ ( Gamma ) * Coords;
}
void GrObject :: ObjScale ( const Vector& v )
{
for ( int i = 0; i < FacetNumber; i++ )
Facet[i].PolyScale ( v );
Coords = Scale ( v ) * Coords;
}
void GrObject :: ObjMirrorX ()
{
Matrix m = MirrorX();
for ( int i = 0; i < FacetNumber; i++ )
Facet[i].PolyMirrorX ();
Coords = m * Coords;
}
void GrObject :: ObjMirrorY ()
{
Matrix m = MirrorY();
for ( int i = 0; i < FacetNumber; i++ )
Facet[i].PolyMirrorY ();
Coords = m * Coords;
}
void GrObject :: ObjMirrorZ ()
{
Matrix m = MirrorZ();
for ( int i = 0; i < FacetNumber; i++ )
Facet[i].PolyMirrorZ ();
Coords = m * Coords;
}
// Space's methods
Space :: Space ( GrObject * Obj, int ObjectNum )
{
if ( ObjectNum <= MaxObjects )
{
ObjectNumber = ObjectNum;
for ( int i = 0; i < ObjectNumber; i++ )
Object[i] = &Obj[i];
};
}
void Space :: Add ( GrObject * Obj )
{
if ( ObjectNumber < MaxObjects ) Object [ObjectNumber++] = Obj;
}
void Space :: Draw ( const Vector& PrCenter )
{
}
// Other functions
int IsVisible ( const Polygon& Poly, const Vector& PrCenter )
{
return ( Poly.Normal & ( PrCenter - Poly.Point[0] )) < 0 || Poly.TwoSides;
}
void DrawBSPTree ( BSPNode * Tree, const Vector& PrCntr )
{
if (( Tree -> Poly -> Normal & PrCntr ) > Tree -> d ) {
if ( Tree -> Right != NULL ) DrawBSPTree ( Tree -> Right, PrCntr );
Tree -> Poly -> Draw ( PrCntr );
if ( Tree -> Left != NULL ) DrawBSPTree ( Tree -> Left, PrCntr );
}
else {
if ( Tree -> Left != NULL ) DrawBSPTree ( Tree -> Left, PrCntr );
Tree -> Poly -> Draw ( PrCntr );
if ( Tree -> Right != NULL ) DrawBSPTree ( Tree -> Right, PrCntr );
}
}
Далее представлена демонстрационная программа, которая выполняет все вышеперечисленные операции с тетраэдром.
//Файл 3dgame.cpp
#include <dos.h>
#include <graphics.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include "3dworks.h"
void DrawObject ( GrObject* Obj, const Vector& v )
{
for ( int i = 0; i < Obj->FacetNumber; i++ )
if ( IsVisible ( Obj->Facet[i], v )) Obj->Facet[i].Draw ( v );
}
main ()
{
Vector Poly1[3], Poly2[3], Poly3[3], Poly4[3];
Polygon O[4];
Vector A ( -50, 0, 0 ),
B ( 0, 0, 50 ),
C ( 50, 0, 0 ),
D ( 0, 100, 0 ),
PrCenter ( 0, 0, 1000 );
Poly1[0] = A; Poly2[0] = B;
Poly1[1] = D; Poly2[1] = D;
Poly1[2] = B; Poly2[2] = C;
Poly3[0] = C; Poly4[0] = C;
Poly3[1] = A; Poly4[1] = D;
Poly3[2] = B; Poly4[2] = A;
Polygon * P1 = new Polygon ( Poly1, 3, 11, OneSd );
Polygon * P2 = new Polygon ( Poly2, 3, 12, OneSd );
Polygon * P3 = new Polygon ( Poly3, 3, 13, OneSd );
Polygon * P4 = new Polygon ( Poly4, 3, 14, OneSd );
O[0] = *P1; O[1] = *P2;
O[2] = *P3; O[3] = *P4;
delete P1; delete P2;
delete P3; delete P4;
GrObject * Obj = new GrObject ( O, 4, Vector ( 0 ) );
double fi = 0.1, psi = 0.1, step = 0.1;
int ch = 0, Page = 3;
int driver = DETECT, mode, res;
initgraph ( &driver, &mode, "" );
if ( ( res = graphresult () ) != grOk ) {
printf ( "\nGraphics error: %s\n", grapherrormsg ( res ) );
exit ( 1 );
}
setgraphmode ( 1 );
DrawObject ( Obj, PrCenter );
do {
setactivepage ( Page % 2 );
clearviewport ();
if ( kbhit ())
{
switch ( ch = getch() ) {
case '+': Obj->ObjScale ((1.1,1.1,1.1)); break;
case '-': Obj->ObjScale ((0.9,0.9,0.9)); break;
case 'x': Obj->ObjMirrorX (); break;
case 'y': Obj->ObjMirrorY (); break;
case 'z': Obj->ObjMirrorZ (); break;
};
if ( ch == 0 )
{
switch ( ch = getch () ) {
case 72 : fi -= step; break;
case 80 : fi += step; break;
case 75 : psi += step; break;
case 77 : psi -= step; break;
};
};
};
Obj->Rotate ( fi, psi, 0 );
DrawObject ( Obj, PrCenter );
setvisualpage ( Page++ % 2 );
if ( fi == 0 && psi == 0 ) while ( !kbhit ());
} while ( ch != 27 );
delete Obj;
closegraph ();
}