Трехмерная графика. Теория

Ea?aay
oi?ea i?ino?ainoaa (e?iia ia?aeuiie oi?ee I) ii?ao auou caaaia ?aoaa?eie
iaiia?aiaiii ia ?aaiuo ioe? ?enae ((x,y,z,1) eee, aieaa iaua, (hx,hy,hz,h), aaa
). Yoa ?aoaa?ea
ii?aaaeaia iaiicia?ii n oi?iinou? ai iauaai iii?eoaey. I?aaei?aiiue iiaoia aaao
aicii?iinou ainiieuciaaouny iao?e?iie caienu? e a aieaa nei?iuo , o?aoia?iuo
caaa?ao. Eae ecaanoii, e?aia aooeiiia i?aia?aciaaiea a o?aoia?iii i?ino?ainoaa
ii?ao auou i?aanoaaeaii a aeaa noia?iiceoee a?auaiee ?anoy?aiee, io?a?aiee e
ia?aiinia. Iiyoiio ainoaoi?ii iia?iaii iienaou iao?eou oieuei yoeo iineaaieo
i?aia?aciaaiee. caanu
a>0 - eiyooeoeaio ?anoy?aiey (n?aoey) aaieu ine aanoenn,b>0-eiyooeoeaio
?anoy?aiey (n?aoey) aaieu ine i?aeiao,y>0-eiyooeoeaio ?anoy?aiey (n?aoey)
aaieu ine aiieeeao. Caiaoei, ?oi, eae e a aaoia?iii neo?aa , ana auienaiiua
iao?eou ia au?i?aaiu. I?oia?aoe?aneay i?iaeoey - ea?oeiiay ieineinou
niaiaaaao n iaiie ec eii?aeiaoiuo ieineinoae eee ia?aeeaeuia ae. Iao?eoa
i?iaeoe?iaaiey aaieu ine O ia ieineinou YOZ eiaao aea A neo?aa , anee ieineinou
i?iaeoe?iaaiey ia?aeeaeuia eii?aeiaoiie ieineinoe, iaiaoiaeii oiii?eou iao?eoo
[Px] ia iao?eoo naaeaa . Eiaai Aiaeiae?ii caienuaa?ony
iao?eou i?iaeoe?iaaiey aaieu 2-o eii?aeiaoiuo inae: Aeniiiiao?e?aneay
i?iaeoey - i?iaeoe?o?uea i?yiua ia?iaiaeeoey?iu ea?oeiiie ieineinoe
. o?eiao?ey-ii?iaeuiue aaeoi?
ea?oeiiie ieineinoe ia?acoao n i?oaie eii?aeiaoiuo inae iiia?ii ?acee?iua
oaeu(?en.15); - eciiao?ey-ana o?e oaea ia?ao ii?iaeu?
ea?oeiiie ieineinoe e eii?aeiaoiuie inyie ?aaiu (?en. 17). Ea?aue ec o?ao
aeaia oeacaiiuo i?iaeoee iieo?aaony eiiaeiaoeae iiai?ioia, ca eioi?ie neaaoao
ia?aeeaeuiia i?iaeoe?iaaiea. Ia?niaeoeaiua (oaio?aeuiua) i?iaeoee no?iyony
aieaa nei?ii . I?aaiiei?ei ?oi oaio? i?iaeoe?iaaiey ea?eo ia ine Z - C (0,0,c) a
ieineinou i?iaeoe?iaaiey niaiaaaao n eii?aeiaoiie ieineinou? XOY (?en. 19) .
Aicuiai a i?ino?ainoaa i?iecaieuio? oi?eo M(x,y,z), i?iaaaai ?a?ac iaa e oi?eo N
i?yio? e caieoai aa ia?aiao?e?aneea o?aaiaiey . Eiaai: Iaeaai eii?aeiaou oi?ee ia?ana?aiey yoie i?yiie n ieineinou? XOY. Ec
oiai , ?oi z`=0, iieo?aai Mao?eoa i?iaeoe?iaaiey,
eiia?ii, au?i?aaia ; iao?eoa ?a niioaaonoao?uaai ia?niaeoeaiiai
i?aia?aciaaiey(aac i?iaeoe?iaaiey) eiaao neaao?uee aea ?cue N++ i?aainoaaeyao
i?aiu oaiaiua n?aanoaa, iicaiey?uea caiaoii oi?inoeou ?aaioo n aaeoi?aie e
i?aia?aciaaieyie a i?ino?ainoaa. #ifndef __VECTOR__#define __VECTOR__#include
<math.h>class Vector{public: double x, y, z; Vector () {}; Vector ( double
v ) { x = y = z = v; }; Vector ( double vx, double vy, double vz ) { x = vx; y = vy; z =
vz; }; Vector& operator = ( double f ) { x = y = z = f;
return *this; }; Vector& operator -= ( const Vector& ); Vector& operator /= ( double ); friend Vector operator - ( const
Vector&, const Vector& ); friend Vector operator * ( double, const
Vector& );
friend Vector operator / ( const Vector&, double ); friend double operator
& ( const Vector& u, const Vector& v ) friend Vector operator ^ ( const Vector&, const
Vector& ); double& operator [] ( int n ) { return *( &x + n ); }; int operator > ( double v ) { return x > v && y
> v && z > v; }; Ray ( Vector& o, Vector& d ) { Org
= o, Dir = d; }; return Vector (
-x, -y, -z ); return Vector ( u.x + v.x, u.y + v.y, u.z + v.z
); return Vector ( u.x - v.x, u.y - v.y, u.z - v.z ); return
Vector ( u.x * v.x, u.y * v.y, u.z * v.z ); return Vector ( u.x * f, u.y * f, u.z * f
);
return Vector ( f * v.x, f * v.y, f * v.z ); return Vector ( u.x / v.x, u.y
/ v.y, u.z / v.z ); return Vector ( u.x / f, u.y / f, u.z / f ); z += v.z; y -= v.y; x *= v.x; x
*= v; y /= v; Vector RndVector (); ---------------------------------------------------
------------------------- Vector
operator ^ ( const Vector& u, const Vector& v ) Vector RndVector () return Normalize ( v
); if ( v.x > 1.0 ) v.x = 1.0; else N yoie
oaeu? nicaaaony eeann Vector, niaa??auee a naaa eiiiiiaiou aaeoi?a, e aey yoiai
eeanna ia?aii?aaaey?ony iniiaiua ciaee iia?aoee. * -
oiii?aiea aaeoi?a ia ?enei; & - neaey?iia
i?iecaaaaiea aaeoi?ia; I?e yoii noaiaa?oiua i?ei?eoaou iia?aoee
nio?aiy?ony. Normalize – ii?ie?iaaiea aaeoi?a; Clip – iona?aiea aaeoi?a. Aiaeiae?iui ia?acii aaiaeony eeann Matrix, neo?auee aey
i?aanoaaeaiey iao?eo i?aia?aciaaiee a o?aoia?iii i?ino?ainoaa. Aey yoiai eeanna
oae?a i?iecaiaeony ia?aii?aaaeaiea iniiaiuo ciaeia iia?aoee. public:
Matrix& operator -= ( const Matrix& ); Matrix&
operator /= ( double ); friend Matrix
operator - ( const Matrix&, const Matrix& ); friend Matrix operator * ( const Matrix&,
const Matrix& ); Matrix Translate ( const Vector& ); Matrix RotateZ ( double ); #endif #include <math.h>#include
"matrix.h"Matrix :: Matrix ( double v ){ int j; for ( int i = 0; i < 4; i++ )
for ( j = 0; j < 4; j++ ) x [i][j] = ( i == j ) ? v : 0.0; x [3][3] = 1;}void
Matrix :: Invert () for ( int j = 0; j < 4; j++
) { if ( x[j][i] != 0.0 ) { Out.x [j][k] -= mulby * Out.x [i][k];
for ( int i = 0; i < 4; i++ ) for ( j = 0; j < 4; j++ ) int j; } for ( j = 0; j < 4; j++ ) Matrix res = *this; for ( int k =
0; k < 4; k++ ) Matrix operator + ( const Matrix& A, const
Matrix& B ) res.x [i][j] = A.x [i][j] + B.x [i][j]; int j; return
res; for ( int i = 0; i < 4; i++ ) sum += A.x [i][k] * B.x [k][j]; Matrix
res; return res; res.x = v.x *
M.x [0][0] + v.y * M.x [1][0] + v.z * M.x [2][0] + M.x [3][0]; res.z = v.x *
M.x [0][2] + v.y * M.x [1][2] + v.z * M.x [2][2] + M.x [3][2]; res /= denom; res.x
[3][1] = Loc.y; res.x [0][0] = v.x; Matrix res ( 1 ); res.x [2][1] = -
Sine; Matrix res ( 1 ); res.x [2][0] = - Sine; Matrix
res ( 1 ); res.x [1][0] = - Sine; Matrix Rotate ( const
Vector& axis, double angle ) res.x [0][0] = axis.x * axis.x + (
1 - axis.x * axis.x ) * Cosine; res.x [0][2] = axis.x * axis.z * ( 1 - Cosine ) - axis.y *
Sine; res.x [1][1] = axis.y * axis.y + ( 1 - axis.y * axis.y ) *
Cosine; res.x [2][0] = axis.x * axis.z * ( 1 - Cosine ) + axis.y *
Sine;
res.x [2][2] = axis.z * axis.z + ( 1 - axis.z * axis.z ) * Cosine; return res; Matrix MirrorY () res.x [2][2] = -1; A
neaao?uae aeaeeioaea auea ?aaeeciaaia ?aaioa n o?aoia?iuie iauaeoaie: a?aiu?,
a?aoe?aneei iauaeoii e i?ino?ainoaii. ?aaeeciaaiu neaao?uea
aicii?iinoe: oaio?aeuiia e
ia?aeeaeuiia i?iaeoe?iaaiea; //Oaee3dworks.h #include
"vector.h" #define MaxObjects
10 int Color; void Move ( const Vector& ); void
PolyMirrorY (); Vector
Coords; void Rotate ( double, double, double ); { Space ( GrObject *, int ); int IsVisible (
const Polygon&, const Vector& ); //----------------------------------------------
------------------------------ #include "3dworks.h"//
Polygon's methodsPolygon :: Polygon ( Vector * PointArr, int PointNum, int Col,
int TS ){ if ( PointNum <= MaxPoints ) { PointNumber = PointNum; Point =
PointArr; Color = Col; TwoSides = TS; for (
int i = 0; i < PointNumber; i++ )
Matrix m = Translate ( v ); void Polygon :: Rotate
( double Alfa, double Beta, double Gamma ) for ( int i = 0; i < PointNumber;
i++ ) void Polygon :: PolyScale ( const Vector& v ) Center = m * Center; Point[i]
= m * Point[i]; for ( int i = 0; i
< PointNumber; i++ ) { Center = m * Center; int VisPoint[MaxPoints * 2], k =
0; VisPoint[k++] = ( int ) Point[i].x * Coeff +
320; fillpoly ( PointNumber, VisPoint
); GrObject :: GrObject ( Polygon *
FacetArr, int FacetNum, const Vector& Crds ) } Facet[i].Move ( v ); { Coords = RotateX ( Alfa ) * RotateY (
Beta ) * RotateZ ( Gamma ) * Coords;
Facet[i].PolyScale ( v ); for ( int i = 0; i
< FacetNumber; i++ ) Matrix m = MirrorY(); void GrObject :: ObjMirrorZ () Coords = m * Coords; if ( ObjectNum <= MaxObjects ) } if ( ObjectNumber < MaxObjects ) Object [ObjectNumber++] =
Obj; int IsVisible ( const Polygon&
Poly, const Vector& PrCenter ) void DrawBSPTree ( BSPNode *
Tree, const Vector& PrCntr ) if ( Tree -> Right != NULL ) DrawBSPTree (
Tree -> Right, PrCntr ); }
Tree -> Poly -> Draw ( PrCntr ); Aaeaa i?aanoaaeaia
aaiiino?aoeiiiay i?ia?aiia, eioi?ay auiieiyao ana auoaia?a?eneaiiua iia?aoee n
oao?aya?ii. #include <stdio.h> void
DrawObject ( GrObject* Obj, const Vector& v ) if ( IsVisible ( Obj->Facet[i], v )) Obj-
>Facet[i].Draw ( v ); B ( 0, 0, 50
), Poly1[1] = D; Poly2[1] = D; Poly3[2] = B; Poly4[2] = A; Polygon *
P3 = new Polygon ( Poly3, 3, 13, OneSd );
delete P1; delete P2; double fi = 0.1, psi = 0.1, step =
0.1; if ( ( res = graphresult () ) != grOk ) { DrawObject ( Obj, PrCenter );
switch ( ch = getch() ) { case 'x': Obj-
>ObjMirrorX (); break; switch ( ch =
getch () ) { case 77 : psi -= step; break;
setvisualpage ( Page++ % 2 ); closegraph
();