home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Resource Library: Graphics
/
graphics-16000.iso
/
general
/
raytrace
/
renaisnc
/
delaunay.lha
/
Delaunay
/
starbase.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-01-04
|
7KB
|
314 lines
/*
* Null device for animation. Provides stubs but does nothing.
*/
#include <stdio.h>
#include "k-Dtree.h"
#include "Delaunay.h"
#include <starbase.c.h>
#include <sbdl.c.h>
#ifndef FALSE
# define FALSE 0
#endif
#ifndef TRUE
# define TRUE 1
#endif
/* Options that can be changed by the user */
static int ShowTriangles = 1;
static int ShowCells = 0;
static int AnimationFrozen = 0;
static int filedes;
static int fdmouse;
static int Outline=false;
AnimateInitialize( XL,YL,XH,YH)
double XL, XH, YL, YH;
{
/* Set up the user coordinate system.
* XL is the x minimum
* XH is the x maximum
* ...
*
* These are in the range [-1,1] as I recall (don't quote me)
*/
float pl[2][3];
float r[3];
float p1[3];
float p2[3];
int cmap_size;
float xl,xh,yl,yh;
float size;
inquire_sizes(filedes,pl,r,p1,p2,&cmap_size);
xl = pl[0][0]; yl = pl[0][1];
xh = pl[1][0]; yh = pl[1][1];
if ( (xh-xl)*r[0] < (yh-yl)*r[1] )
size = fabs(xh-xl)*r[0];
else
size = fabs(yh-yl)*r[1];
set_p1_p2(filedes, METRIC, 10., 10., 10., size-10.0, size-10.0, 20.);
vdc_extent(filedes, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0);
}
static void SetPoint(i, cl)
int i;
float cl[];
{
float clist[20];
perPoint* pd;
Point* p;
pd = PointData + i;
p = Points + i;
cl[0] = p->x;
cl[1] = p->y;
cl[2] = pd->r; cl[2] /= 255.;
cl[3] = pd->g; cl[3] /= 255.;
cl[4] = pd->b; cl[4] /= 255.;
}
static void SetPoint2(x,y,r,g,b,cl)
float x,y;
short r,g,b;
float cl[];
{
cl[0] = x;
cl[1] = y;
cl[2] = r; cl[2] /= 255.;
cl[3] = g; cl[3] /= 255.;
cl[4] = b; cl[4] /= 255.;
}
AnimateShadeTriangle( i, j, k )
register i,j,k;
{
/* Shade a single triangle
** i,j,k are vertex indicies.
** Points[ i|j|k ] contains the vertex coordinates
** PointData[ i|j|k ] contains the colors
*/
float clist[20];
if (!ANIMATING || AnimationFrozen || !ShowTriangles) return;
SetPoint(i, clist);
SetPoint(j, clist+5);
SetPoint(k, clist+10);
polygon2d(filedes, clist, 3, 0 );
if ( Outline ) {
SetPoint(i, clist+15);
clist[2] = clist[3] = clist[4] =
clist[7] = clist[8] = clist[9] =
clist[12] = clist[13] = clist[14] =
clist[17] = clist[18] = clist[19] = 1.;
polyline2d(filedes,clist,4,0);
}
}
AnimateShadeCell( Boundary, Sample )
RectType *Boundary;
SampleData *Sample;
{
/* Fill a rectangular cell with a single color value */
/* PointData[ Sample->index ] contains the color. */
/* Boundary->{xL,yL,xH,yH} contain the rectangle bounds. */
float clist[20];
perPoint* pd;
if (!ANIMATING || AnimationFrozen || !ShowCells) return;
pd = PointData + Sample->index;
SetPoint2(Boundary->xL,Boundary->yL,pd->r,pd->g,pd->b,clist);
SetPoint2(Boundary->xL,Boundary->yH,pd->r,pd->g,pd->b,clist+5);
SetPoint2(Boundary->xH,Boundary->yH,pd->r,pd->g,pd->b,clist+10);
SetPoint2(Boundary->xH,Boundary->yL,pd->r,pd->g,pd->b,clist+15);
polygon2d(filedes, clist, 4, 0 );
}
void Animate (Animation)
char *Animation;
{
char* indev="/dev/hil_0.7";
char* s;
ANIMATING = true;
/* Initialize the graphics hardware */
filedes = gopen("/dev/crt0",OUTDEV,"hp98731",INIT);
if ( filedes < 0 ) {
fprintf(stderr,"gopen failed.\n");
exit(1);
}
shade_mode(filedes, CMAP_FULL|INIT, FALSE);
mapping_mode(filedes, ISOTROPIC);
double_buffer(filedes, 0, 24);
vertex_format(filedes, 3, 3, 1, 0, 1);
if(s=getenv("SB_INDEV")) indev=s;
fdmouse = gopen(indev,INDEV,"hp-hil",0);
if ( fdmouse < 0 ) {
fprintf(stderr,"Can't open mouse.\n");
}
}
static void TrianglesReDraw()
{
register TrianglePointer T;
/* Step through all the triangles calling Animate Shade Triangle
* for each one. These gives us a way to redraw on an expose event.
*/
T = Triangles;
do
{
AnimateShadeTriangle( T->P1, T->P2, T->P3 );
T = T->Next;
}
while (T != Triangles);
}
static void CellsReDraw()
{
/*
* Step through the kD-tree redrawing all leaf cells.
*/
extern HierarchicalRegion Root;
extern RectType RootBoundary;
register HierarchicalRegion *Node;
RectType LeftBoundary, RightBoundary, Boundary, Overlap;
/* This stack is used to avoid the expense of recursion */
HierarchicalRegion *NodeStack[MAX_DEPTH];
RectType BoundaryStack[MAX_DEPTH];
int levelStack[MAX_DEPTH];
int nStack, level;
/* Start with the root node on the stack */
nStack = 0;
NodeStack[nStack] = &Root;
BoundaryStack[nStack] = RootBoundary;
levelStack[nStack++] = 0;
while (nStack > 0) {
/* Get the next node to process from the stack */
Node = NodeStack[--nStack];
Boundary = BoundaryStack[nStack];
level = levelStack[nStack];
/* Follow the tree down a leaf, pushing the side branches.
*/
while (! IsLeaf(Node)) {
SplitBoundary( TRUE, Node->splitDirection, Node->splitValue,
&Boundary, &LeftBoundary );
if (Node->Left) {
NodeStack[nStack] = Node->Left;
BoundaryStack[nStack] = LeftBoundary;
levelStack[nStack++] = level+1;
}
SplitBoundary( FALSE, Node->splitDirection, Node->splitValue,
&Boundary, &RightBoundary );
if (Node->Right) {
if (nStack >= MAX_DEPTH) {
fprintf( stderr, "Filter stack overflow!\n" );
abort();
}
NodeStack[nStack] = Node->Right;
BoundaryStack[nStack] = RightBoundary;
levelStack[nStack++] = level+1;
}
/* Get the next node to process from the stack */
Node = NodeStack[--nStack];
Boundary = BoundaryStack[nStack];
level = levelStack[nStack];
}
if (Node == NULL)
continue; /* Dead end, go back to the stack loop */
AnimateShadeCell( &Boundary, Node->Sample );
}
}
static void AnimateReDraw()
{
/* Force a redraw */
if (ShowTriangles) TrianglesReDraw();
if (ShowCells) CellsReDraw();
}
AnimateDoEvent()
{
double range;
if (!ANIMATING) return;
/* Respond to input events */
}
int AnimateCheckInput()
{
int valid;
int button;
int update=0;
static int last=0;
/* Poll event queue and call AnimateDo Event if one is pending */
/* Look at mouse buttons */
if ( fdmouse >= 0 ) {
sample_choice(fdmouse,1,&valid,&button);
if ( button != 0 ) {
switch(button){
case 1:/* left */
if ( button != last )
Outline = !Outline;
break;
case 2:/* middle */
if ( button != last )
AnimationFrozen = !AnimationFrozen;
break;
case 3:/* right */
if ( button != last ) {
ShowCells = !ShowCells;
ShowTriangles = !ShowTriangles;
clear_view_surface(filedes);
}
break;
default:
break;
}
update = 1;
}
last = button;
}
if ( update && !AnimationFrozen ) {
AnimateReDraw();
}
}
AnimateExit()
{
/* Call AnimateDoEvent until the user chooses to stop by ^C or picking
QUIT from the menu.
*/
do {AnimateCheckInput();} while (1);
gclose(filedes);
}