Ш If File Torus.срр
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include 'Vector3D.h" #include "Matrix.h"
#defineN1 40 #define N2 20
Vector prjDir (0,0,1 ); Vector vertex [N1*N2]; Matrix trans = RotateX ( M_PI / 4 );
struct Point // screen point
{
int x, y;
};
struct Facet {
int index [3]; Vector3D n; float depth; float coeff; } * torus, * tmp;
int facetComp ( const void * v1, const void * v2 )
.... (
Facet M1 = (Facet *)v1; Facet * f2 - (Facet *) v2;
Компьютерная графика. Полигональные модели
if (f1 -> depth < f2 -> depth ) return -1;
else
if (f1 -> depth > f2 -> depth ) return 1;
else
return 0;
}
void initTorus ()
float M = 5; float r2 = 1;
//1. Create vertices
for(inti = 0,k = 0;i<N1;i++)
{
float phi = i*2*M_PI/N1;
for (int j = 0; j < N2; j++, k++ ) {
float psi=j*2*M_PI/N2;
vertex [k].x = (r1 + r2 * cos (psi)) * cos (phi); vertex [kj.y = (r1 + r2 * cos (psi)) * sin (phi); vertex [kj.z = r2 * sin ( psi );
vertex [k] = trans * vertex [k]; vertex [kj.z += 10;
}
}
// 2. Create facets
for(i = k = 0;i<N1;i++)
for(intj = 0;j<N2;j++, k+=2)
{
torus [k].index [0] = i*N2 + j;
torus [kj.index [1] = ((i+1)%N1)*N2 + j;
torus [k].index [2] = ((i+1)%N1)*N2 + (j+1)%N2;
torus [k+1].index [0] = i*N2 + j;
torus [k+1].index [1] = ((j+1)%N1)*N2 + (j+1)%N2;
torus [k+1].index [2] = i*N2 + (j + 1)%N2;
}
void drawTorus () {
// compute normals & distances
for ( int i = 0, count = 0; i < N1*N2*2; i++ )
{
torus [i].n = ( vertex [torus [i].index [1]] - vertex [torus [i].index [0] ] ) A ( vertex [torus [i].index [2]] - vertex [torus [i].index [1] ] );
torus [ij.coeff = ( torus [i].n & prjDir )/!torus [i].n; torus [i].depth = vertex [torus [i].index [0]] & prjDir;
10. Yßaneme HeBMflUMbix nwHwti n no-BepxHOCTe
for(intj = 1;j<3;j++) {
float d = vertex [torus [i].index 0]] & prjDir;
if ( d < torus [i].depth ) torus [i].depth = d;