Трехмерная графикаРефераты >> Программирование и компьютеры >> Трехмерная графика
{
Matrix res;
int j;
for ( int i = 0; i < 4; i++ )
for ( j = 0; j < 4; j++ ) {
double sum = 0;
for ( int k = 0; k < 4; k++ )
sum += A.x [i][k] * B.x [k][j];
res.x [i][j] = sum;
}
return res;
}
Matrix operator * ( const Matrix& A, double v )
{
Matrix res;
int j;
for ( int i = 0; i < 4; i++ )
for ( j = 0; j < 4; j++ )
res.x [i][j] = A.x [i][j] * v;
return res;
}
Vector operator * ( const Matrix& M, const Vector& v )
{
Vector 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.y = v.x * M.x [0][1] + v.y * M.x [1][1] + v.z * M.x [2][1] + M.x [3][1];
res.z = v.x * M.x [0][2] + v.y * M.x [1][2] + v.z * M.x [2][2] + M.x [3][2];
double denom = v.x * M.x [0][3] + v.y * M.x [1][3] +
v.z * M.x [2][3] + M.x[3][3];
if ( denom != 1.0 )
res /= denom;
return res;
}
Matrix Translate ( const Vector& Loc )
{
Matrix res ( 1 );
res.x [3][0] = Loc.x;
res.x [3][1] = Loc.y;
res.x [3][2] = Loc.z;
return res;
};
Matrix Scale ( const Vector& v )
{
Matrix res ( 1 );
res.x [0][0] = v.x;
res.x [1][1] = v.y;
res.x [2][2] = v.z;
return res;
};
Matrix RotateX ( double Angle )
{
Matrix res ( 1 );
double Cosine = cos ( Angle );
double Sine = sin ( Angle );
res.x [1][1] = Cosine;
res.x [2][1] = - Sine;
res.x [1][2] = Sine;
res.x [2][2] = Cosine;
return res;
};
Matrix RotateY ( double Angle )
{
Matrix res ( 1 );
double Cosine = cos ( Angle );
double Sine = sin ( Angle );
res.x [0][0] = Cosine;
res.x [2][0] = - Sine;
res.x [0][2] = Sine;
res.x [2][2] = Cosine;
return res;
};
Matrix RotateZ ( double Angle )
{
Matrix res ( 1 );
double Cosine = cos ( Angle );
double Sine = sin ( Angle );
res.x [0][0] = Cosine;
res.x [1][0] = - Sine;
res.x [0][1] = Sine;
res.x [1][1] = Cosine;
return res;
};
Matrix Rotate ( const Vector& axis, double angle )
{
Matrix res ( 1 );
double Cosine = cos ( angle );
double Sine = sin ( angle );
res.x [0][0] = axis.x * axis.x + ( 1 - axis.x * axis.x ) * Cosine;
res.x [0][1] = axis.x * axis.y * ( 1 - Cosine ) + axis.z * Sine;
res.x [0][2] = axis.x * axis.z * ( 1 - Cosine ) - axis.y * Sine;
res.x [0][3] = 0;
res.x [1][0] = axis.x * axis.y * ( 1 - Cosine ) - axis.z * Sine;
res.x [1][1] = axis.y * axis.y + ( 1 - axis.y * axis.y ) * Cosine;
res.x [1][2] = axis.y * axis.z * ( 1 - Cosine ) + axis.x * Sine;
res.x [1][3] = 0;
res.x [2][0] = axis.x * axis.z * ( 1 - Cosine ) + axis.y * Sine;
res.x [2][1] = axis.y * axis.z * ( 1 - Cosine ) - axis.x * Sine;
res.x [2][2] = axis.z * axis.z + ( 1 - axis.z * axis.z ) * Cosine;
res.x [2][3] = 0;
res.x [3][0] = 0;
res.x [3][1] = 0;
res.x [3][2] = 0;
res.x [3][3] = 1;
return res;
};
Matrix MirrorX ()
{
Matrix res ( 1 );
res.x [0][0] = -1;
return res;
};
Matrix MirrorY ()
{
Matrix res ( 1 );
res.x [1][1] = -1;
return res;
};
Matrix MirrorZ ()
{
Matrix res ( 1 );
res.x [2][2] = -1;
return res;
}
В следующей библиотеке была реализована работа с трехмерными объектами: гранью, графическим объектом и пространством. Реализованы следующие возможности:
- поворот объектов вокруг координатных осей;
- зеркальное отображение объектов по отношению к координатным осям;
- центральное и параллельное проектирование;
- масштабирование объектов;
- удаление невидимых поверхностей;
- перемещение объектов в пространстве.
//Файл 3dworks.h
#ifndef 3DWORKS
#define 3DWORKS
#include <graphics.h>
#include <stdlib.h>
#include "vector.h"
#include "matrix.h"
#define OneSd 0
#define TwoSds 1
#define MaxPoints 10
#define MaxFacets 10
#define MaxObjects 10
class Polygon
{
public:
int PointNumber;
Vector * Point;
Vector Normal;
Vector Center;
int Color;
int TwoSides;
Polygon () {};
Polygon ( Vector *, int, int, int );
void Draw ( const Vector& );
void Move ( const Vector& );
void Rotate ( double, double, double );
void PolyScale ( const Vector& );
void PolyMirrorX ();
void PolyMirrorY ();
void PolyMirrorZ ();
};
class GrObject
{
public:
int FacetNumber;
Polygon * Facet;
Vector Coords;
GrObject () {};
GrObject ( Polygon *, int, const Vector& );
void Move ( const Vector& );
void Rotate ( double, double, double );
void ObjScale ( const Vector& );
void ObjMirrorX ();
void ObjMirrorY ();
void ObjMirrorZ ();
};
struct BSPNode
{
Polygon * Poly;
double d;
BSPNode * Left;
BSPNode * Right;
};
class Space
{
public:
int ObjectNumber;
GrObject * Object [MaxObjects];
Space () { ObjectNumber = 0; };
Space ( GrObject *, int );
void Add ( GrObject * );
void Draw ( const Vector& );
};
int IsVisible ( const Polygon&, const Vector& );
void DrawBSPTree ( BSPNode *, const Vector& );
#endif
//----------------------------------------------------------------------------
//Файл 3dworks.cpp
#include "3dworks.h"
// Polygon's methods
Polygon :: Polygon ( Vector * PointArr, int PointNum, int Col, int TS )
{
if ( PointNum <= MaxPoints )
{
PointNumber = PointNum;
Point = PointArr;
Color = Col;
TwoSides = TS;
Normal = Normalize (
( Point [1] - Point [0] ) ^ ( Point [PointNumber-1] - Point [0] ));
Center = 0;
for ( int i = 0; i < PointNumber; i++ )
Center += Point[i];
Center /= PointNumber;
}
}
void Polygon :: Move ( const Vector& v )
{
Matrix m = Translate ( v );
for ( int i = 0; i < PointNumber; i++ )
Point[i] = m * Point[i];
Center = m * Center;
}
void Polygon :: Rotate ( double Alfa, double Beta, double Gamma )
{
Matrix m = RotateX ( Alfa ) * RotateY ( Beta ) * RotateZ ( Gamma );
for ( int i = 0; i < PointNumber; i++ )
Point[i] = m * Point[i];
Normal = m * Normal;
Center = m * Center;
}
void Polygon :: PolyScale ( const Vector& v )
{
Matrix m = Scale ( v );
for ( int i = 0; i < PointNumber; i++ )
Point[i] = m * Point[i];
Center = m * Center;
}
void Polygon :: PolyMirrorX ()
{
Matrix m = MirrorX();
for ( int i = 0; i < PointNumber; i++ )
Point[i] = m * Point[i];
Center = m * Center;
Normal = m * Normal;
}
void Polygon :: PolyMirrorY ()