home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
POLYEDIT.LZH
/
MODEL
/
VIEW4.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-09
|
12KB
|
391 lines
/*
* 表示
*
* Copyright T.Koabayashi 1994.6.26
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#define VIEW_LOCAL
#include "matrix.h"
#include "poly.h"
#include "view.h"
static void VertexConv( Vertex*, int[5][3], Vertex* );
static void DrawBox( ViewWindow*, int, int, int, int );
static void DrawRectangle( int, Vertex*, Vertex*, Vertex*, Vertex* );
static void DrawCube( ViewWindow*, int *, int *, int *);
static void DrawCube4( ViewWindow*, int *, int *, int *, int, int);
static void DrawCube5( ViewWindow*, int *, int *, int *, int, int);
static void DrawCube9( ViewWindow*, int *, int *, int *);
Vertex viewbox_p1, viewbox_p2;
Matrix viewbox_mat;
int viewbox_drawflag = FALSE;
int viewbox2d_x1, viewbox2d_y1, viewbox2d_x2, viewbox2d_y2;
int viewbox2d_drawflag = FALSE;
/* 直方体描画 */
void ViewBox( p1, p2, mat )
Vertex *p1, *p2 ; /* 頂点 */
Matrix mat ;
{
int i ;
Vertex ver[8] ;
int scr[8][3] ;
int pos1[3], pos2[3] ;
int imat[5][3] ;
int vecx[4], vecy[4], vecz[4];
if (viewbox_drawflag
&& memcmp(&viewbox_p1, p1, sizeof(Vertex)) == 0
&& memcmp(&viewbox_p2, p2, sizeof(Vertex)) == 0
&& memcmp(&viewbox_mat, mat, sizeof(Matrix)) == 0) {
viewbox_drawflag = FALSE;
} else {
memcpy(&viewbox_p1, p1, sizeof(Vertex));
memcpy(&viewbox_p2, p2, sizeof(Vertex));
memcpy(viewbox_mat, mat, sizeof(Matrix));
viewbox_drawflag = TRUE;
}
MatToInt( imat, mat );
/* 頂点の設定設定 */
for( i = 0 ; i < 8 ; i++ )
{
memset( &ver[i], 0, sizeof( Vertex ) );
ver[i].x = i & 1 ? p1->x : p2->x ;
ver[i].y = i & 2 ? p1->y : p2->y ;
ver[i].z = i & 4 ? p1->z : p2->z ;
VertexConv( &ver[i], imat, &ver[i] );
ToScreen( scr[i], &ver[i] );
/*printf("scr[%d]=(%d,%d,%d)\n", i, scr[i][0], scr[i][1], scr[i][2]);*/
}
if ( ViewMode & VIEW_PERS ) {
if (p1->x == p2->x) {
DrawLineForPers(&ver[0], &ver[2], CURSOR_COLOR);
DrawLineForPers(&ver[2], &ver[6], CURSOR_COLOR);
DrawLineForPers(&ver[6], &ver[4], CURSOR_COLOR);
DrawLineForPers(&ver[4], &ver[0], CURSOR_COLOR);
} else if (p1->y == p2->y) {
DrawLineForPers(&ver[0], &ver[1], CURSOR_COLOR);
DrawLineForPers(&ver[1], &ver[5], CURSOR_COLOR);
DrawLineForPers(&ver[5], &ver[4], CURSOR_COLOR);
DrawLineForPers(&ver[4], &ver[0], CURSOR_COLOR);
} else if (p1->z == p2->z) {
DrawLineForPers(&ver[0], &ver[1], CURSOR_COLOR);
DrawLineForPers(&ver[1], &ver[3], CURSOR_COLOR);
DrawLineForPers(&ver[3], &ver[2], CURSOR_COLOR);
DrawLineForPers(&ver[2], &ver[0], CURSOR_COLOR);
} else {
DrawLineForPers(&ver[0], &ver[1], CURSOR_COLOR);
DrawLineForPers(&ver[1], &ver[3], CURSOR_COLOR);
DrawLineForPers(&ver[3], &ver[2], CURSOR_COLOR);
DrawLineForPers(&ver[2], &ver[0], CURSOR_COLOR);
DrawLineForPers(&ver[4], &ver[5], CURSOR_COLOR);
DrawLineForPers(&ver[5], &ver[7], CURSOR_COLOR);
DrawLineForPers(&ver[7], &ver[6], CURSOR_COLOR);
DrawLineForPers(&ver[6], &ver[4], CURSOR_COLOR);
DrawLineForPers(&ver[0], &ver[4], CURSOR_COLOR);
DrawLineForPers(&ver[1], &ver[5], CURSOR_COLOR);
DrawLineForPers(&ver[2], &ver[6], CURSOR_COLOR);
DrawLineForPers(&ver[3], &ver[7], CURSOR_COLOR);
}
}
vecx[0] = (scr[0][0] + scr[7][0])/2;
vecy[0] = (scr[0][1] + scr[7][1])/2;
vecz[0] = (scr[0][2] + scr[7][2])/2;
vecx[1] = (scr[1][0] - scr[0][0])/2;
vecy[1] = (scr[1][1] - scr[0][1])/2;
vecz[1] = (scr[1][2] - scr[0][2])/2;
vecx[2] = (scr[2][0] - scr[0][0])/2;
vecy[2] = (scr[2][1] - scr[0][1])/2;
vecz[2] = (scr[2][2] - scr[0][2])/2;
vecx[3] = (scr[4][0] - scr[0][0])/2;
vecy[3] = (scr[4][1] - scr[0][1])/2;
vecz[3] = (scr[4][2] - scr[0][2])/2;
if (ViewMode & VIEW_XY) {
DrawCube(&WinXY, vecx, vecy, vecz);
}
if (ViewMode & VIEW_YZ) {
DrawCube(&WinYZ, vecy, vecz, vecx);
}
if (ViewMode & VIEW_ZX) {
DrawCube(&WinZX, vecx, vecz, vecy);
}
}
static void DrawCube(ViewWindow *win, int *vecx, int *vecy, int *vecz)
{
#if 0
if (vecz[1] == 0) {
if (vecz[2] == 0) {
DrawCube4(win, vecx, vecy, vecz, 1, 2);
} else if (vecz[3] == 0) {
DrawCube4(win, vecx, vecy, vecz, 1, 3);
} else {
DrawCube5(win, vecx, vecy, vecz, 2, 3);
}
} else if (vecz[2] == 0) {
if (vecz[3] == 0) {
DrawCube4(win, vecx, vecy, vecz, 2, 3);
} else {
DrawCube5(win, vecx, vecy, vecz, 1, 3);
}
} else if (vecz[3] == 0) {
DrawCube5(win, vecx, vecy, vecz, 1, 2);
} else {
DrawCube9(win, vecx, vecy, vecz);
}
#endif
if (vecx[1] == 0 && vecy[1] == 0) {
if (vecx[2] == 0 && vecy[2] == 0) {
ClipLine( win,
win->offx + vecx[0] + vecx[3], win->offy - vecy[0] - vecy[3],
win->offx + vecx[0] - vecx[3], win->offy - vecy[0] + vecy[3],
CURSOR_COLOR );
} else if (vecx[3] == 0 && vecy[3] == 0) {
ClipLine( win,
win->offx + vecx[0] + vecx[2], win->offy - vecy[0] - vecy[2],
win->offx + vecx[0] - vecx[2], win->offy - vecy[0] + vecy[2],
CURSOR_COLOR );
} else {
DrawCube4(win, vecx, vecy, vecz, 2, 3);
}
} else if (vecx[2] == 0 && vecy[2] == 0) {
if (vecx[3] == 0 && vecy[3] == 0) {
ClipLine( win,
win->offx + vecx[0] + vecx[1], win->offy - vecy[0] - vecy[1],
win->offx + vecx[0] - vecx[1], win->offy - vecy[0] + vecy[1],
CURSOR_COLOR );
} else {
DrawCube4(win, vecx, vecy, vecz, 1, 3);
}
} else if (vecx[3] == 0 && vecy[3] == 0) {
DrawCube4(win, vecx, vecy, vecz, 1, 2);
} else if (vecz[1] == 0) {
DrawCube5(win, vecx, vecy, vecz, 2, 3);
} else if (vecz[2] == 0) {
DrawCube5(win, vecx, vecy, vecz, 1, 3);
} else if (vecz[3] == 0) {
DrawCube5(win, vecx, vecy, vecz, 1, 2);
} else {
DrawCube9(win, vecx, vecy, vecz);
}
}
static void DrawCube4(ViewWindow *win, int *vecx, int *vecy, int *vecz, int p1, int p2)
{
int x1, x2, x3, x4;
int y1, y2, y3, y4;
x1 = win->offx + (vecx[0] + vecx[p1] + vecx[p2]);
y1 = win->offy - (vecy[0] + vecy[p1] + vecy[p2]);
x2 = win->offx + (vecx[0] + vecx[p1] - vecx[p2]);
y2 = win->offy - (vecy[0] + vecy[p1] - vecy[p2]);
x3 = win->offx + (vecx[0] - vecx[p1] - vecx[p2]);
y3 = win->offy - (vecy[0] - vecy[p1] - vecy[p2]);
x4 = win->offx + (vecx[0] - vecx[p1] + vecx[p2]);
y4 = win->offy - (vecy[0] - vecy[p1] + vecy[p2]);
ClipLine( win, x1, y1, x2, y2, CURSOR_COLOR );
ClipLine( win, x2, y2, x3, y3, CURSOR_COLOR );
ClipLine( win, x3, y3, x4, y4, CURSOR_COLOR );
ClipLine( win, x4, y4, x1, y1, CURSOR_COLOR );
}
static void DrawCube5(ViewWindow *win, int *vecx, int *vecy, int *vecz, int p1, int p2)
{
int xp1, yp1, xp2, yp2, xp3, yp3;
int x[8], y[8], i;
if (vecz[p1] > 0) {
xp1 = vecx[p1];
yp1 = vecy[p1];
} else {
xp1 = -vecx[p1];
yp1 = -vecy[p1];
}
if (vecz[p2] > 0) {
xp2 = vecx[p2];
yp2 = vecy[p2];
} else {
xp2 = -vecx[p2];
yp2 = -vecy[p2];
}
xp3 = vecx[6-p1-p2];
yp3 = vecy[6-p1-p2];
for (i = 0; i < 8; ++i) {
x[i] = win->offx + vecx[0]
+ (i & 1 ? xp1 : -xp1)
+ (i & 2 ? xp2 : -xp2)
+ (i & 4 ? xp3 : -xp3);
y[i] = win->offy - vecy[0]
- (i & 1 ? yp1 : -yp1)
- (i & 2 ? yp2 : -yp2)
- (i & 4 ? yp3 : -yp3);
}
ClipLine( win, x[1], y[1], x[2], y[2], CURSOR_COLOR );
ClipLine( win, x[5], y[5], x[6], y[6], CURSOR_COLOR );
ClipLine( win, x[0], y[0], x[4], y[4], CURSOR_COLOR );
ClipLine( win, x[1], y[1], x[5], y[5], CURSOR_COLOR );
ClipLine( win, x[3], y[3], x[7], y[7], CURSOR_COLOR );
ClipLine( win, x[2], y[2], x[6], y[6], CURSOR_COLOR );
}
static void DrawCube9(ViewWindow *win, int *vecx, int *vecy, int *vecz)
{
int x[8], y[8], i;
for (i = 0; i < 8; ++i) {
x[i] = win->offx + vecx[0]
+ (i & 1 ? vecx[1] : -vecx[1])
+ (i & 2 ? vecx[2] : -vecx[2])
+ (i & 4 ? vecx[3] : -vecx[3]);
y[i] = win->offy - vecy[0]
- (i & 1 ? vecy[1] : -vecy[1])
- (i & 2 ? vecy[2] : -vecy[2])
- (i & 4 ? vecy[3] : -vecy[3]);
}
ClipLine( win, x[0], y[0], x[1], y[1], CURSOR_COLOR );
ClipLine( win, x[1], y[1], x[3], y[3], CURSOR_COLOR );
ClipLine( win, x[3], y[3], x[2], y[2], CURSOR_COLOR );
ClipLine( win, x[2], y[2], x[0], y[0], CURSOR_COLOR );
ClipLine( win, x[4], y[4], x[5], y[5], CURSOR_COLOR );
ClipLine( win, x[5], y[5], x[7], y[7], CURSOR_COLOR );
ClipLine( win, x[7], y[7], x[6], y[6], CURSOR_COLOR );
ClipLine( win, x[6], y[6], x[4], y[4], CURSOR_COLOR );
ClipLine( win, x[0], y[0], x[4], y[4], CURSOR_COLOR );
ClipLine( win, x[1], y[1], x[5], y[5], CURSOR_COLOR );
ClipLine( win, x[3], y[3], x[7], y[7], CURSOR_COLOR );
ClipLine( win, x[2], y[2], x[6], y[6], CURSOR_COLOR );
}
static void VertexConv( ret, mat, ver )
Vertex *ret, *ver ;
int mat[5][3] ;
{
int x, y, z ;
#if 0
x = (int)ver->x * mat[0][0]
+ (int)ver->y * mat[1][0]
+ (int)ver->z * mat[2][0]
+ mat[3][0] ;
y = (int)ver->x * mat[0][1]
+ (int)ver->y * mat[1][1]
+ (int)ver->z * mat[2][1]
+ mat[3][1] ;
z = (int)ver->x * mat[0][2]
+ (int)ver->y * mat[1][2]
+ (int)ver->z * mat[2][2]
+ mat[3][2] ;
ret->x = (short)( x >> 16 );
ret->y = (short)( y >> 16 );
ret->z = (short)( z >> 16 );
#else
x = (((int)ver->x * mat[0][0]
+ (int)ver->y * mat[1][0]
+ (int)ver->z * mat[2][0]) >> 16)
+ mat[3][0] ;
y = (((int)ver->x * mat[0][1]
+ (int)ver->y * mat[1][1]
+ (int)ver->z * mat[2][1]) >> 16)
+ mat[3][1] ;
z = (((int)ver->x * mat[0][2]
+ (int)ver->y * mat[1][2]
+ (int)ver->z * mat[2][2]) >> 16)
+ mat[3][2] ;
ret->x = (short)x;
ret->y = (short)y;
ret->z = (short)z;
#endif
}
static void DrawBox( win, x1, y1, x2, y2 )
ViewWindow *win ;
int x1, y1, x2, y2 ;
{
x1 += win->offx ;
x2 += win->offx ;
y1 += win->offy ;
y2 += win->offy ;
ClipLine( win, x1, y1, x2, y1, CURSOR_COLOR );
ClipLine( win, x1, y1, x1, y2, CURSOR_COLOR );
ClipLine( win, x1, y2, x2, y2, CURSOR_COLOR );
ClipLine( win, x2, y1, x2, y2, CURSOR_COLOR );
}
#if 0
static void DrawRectangle( flag, p0, p1, p2, p3 )
int flag ;
Vertex *p0, *p1, *p2, *p3 ;
{
int s0[3], s1[3], s2[3], s3[3] ;
ToScreen( s0, p0 );
ToScreen( s1, p1 );
ToScreen( s2, p2 );
ToScreen( s3, p3 );
if ( ViewMode & VIEW_PERS )
DrawLineForPers( p0, p1, CURSOR_COLOR );
if ( ! flag )
DrawLineForWin( s0, s1, CURSOR_COLOR );
if ( ViewMode & VIEW_PERS )
DrawLineForPers( p0, p2, CURSOR_COLOR );
if ( ! flag )
DrawLineForWin( s0, s2, CURSOR_COLOR );
if ( memcmp( p0, p1, sizeof( Vertex ) ) != 0 )
{
if ( ViewMode & VIEW_PERS )
DrawLineForPers( p1, p3, CURSOR_COLOR );
if ( ! flag )
DrawLineForWin( s1, s3, CURSOR_COLOR );
}
if ( memcmp( p0, p2, sizeof( Vertex ) ) != 0 )
{
if ( ViewMode & VIEW_PERS )
DrawLineForPers( p2, p3, CURSOR_COLOR );
if ( ! flag )
DrawLineForWin( s2, s3, CURSOR_COLOR );
}
}
#endif
/* 透視図に長方形を描く */
void ViewBox2D( x1, y1, x2, y2 )
int x1, y1, x2, y2 ;
{
if (viewbox2d_drawflag
&& x1 == viewbox2d_x1 && y1 == viewbox2d_y1
&& x2 == viewbox2d_x2 && y2 == viewbox2d_y2) {
viewbox2d_drawflag = FALSE;
} else {
viewbox2d_x1 = x1;
viewbox2d_y1 = y1;
viewbox2d_x2 = x2;
viewbox2d_y2 = y2;
viewbox2d_drawflag = TRUE;
}
ViewLine2D( x1, y1, x1, y2, CURSOR_COLOR );
ViewLine2D( x1, y1, x2, y1, CURSOR_COLOR );
ViewLine2D( x2, y1, x2, y2, CURSOR_COLOR );
ViewLine2D( x1, y2, x2, y2, CURSOR_COLOR );
}