1f220fa62Smrg/*
2f220fa62Smrg** License Applicability. Except to the extent portions of this file are
3f220fa62Smrg** made subject to an alternative license as permitted in the SGI Free
4f220fa62Smrg** Software License B, Version 1.1 (the "License"), the contents of this
5f220fa62Smrg** file are subject only to the provisions of the License. You may not use
6f220fa62Smrg** this file except in compliance with the License. You may obtain a copy
7f220fa62Smrg** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8f220fa62Smrg** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9f220fa62Smrg**
10f220fa62Smrg** http://oss.sgi.com/projects/FreeB
11f220fa62Smrg**
12f220fa62Smrg** Note that, as provided in the License, the Software is distributed on an
13f220fa62Smrg** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14f220fa62Smrg** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15f220fa62Smrg** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16f220fa62Smrg** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17f220fa62Smrg**
18f220fa62Smrg** Original Code. The Original Code is: OpenGL Sample Implementation,
19f220fa62Smrg** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20f220fa62Smrg** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21f220fa62Smrg** Copyright in any portions created by third parties is as indicated
22f220fa62Smrg** elsewhere herein. All Rights Reserved.
23f220fa62Smrg**
24f220fa62Smrg** Additional Notice Provisions: The application programming interfaces
25f220fa62Smrg** established by SGI in conjunction with the Original Code are The
26f220fa62Smrg** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27f220fa62Smrg** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28f220fa62Smrg** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29f220fa62Smrg** Window System(R) (Version 1.3), released October 19, 1998. This software
30f220fa62Smrg** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31f220fa62Smrg** published by SGI, but has not been independently verified as being
32f220fa62Smrg** compliant with the OpenGL(R) version 1.2.1 Specification.
33f220fa62Smrg*/
34f220fa62Smrg
35f220fa62Smrg/*
36f220fa62Smrg * quilt.c++
37f220fa62Smrg *
38f220fa62Smrg */
39f220fa62Smrg
40f220fa62Smrg#include "glimports.h"
41f220fa62Smrg#include "mystdio.h"
42f220fa62Smrg#include "myassert.h"
43f220fa62Smrg#include "quilt.h"
44f220fa62Smrg#include "backend.h"
45f220fa62Smrg#include "mapdesc.h"
46f220fa62Smrg#include "flist.h"
47f220fa62Smrg#include "patchlist.h"
48f220fa62Smrg#include "simplemath.h" //min()
49f220fa62Smrg
50f220fa62Smrg/* local preprocessor definitions */
51f220fa62Smrg#define DEF_PATCH_STEPSIZE	.4
52f220fa62Smrg#define fsizeof(x)		(sizeof(x)/sizeof(REAL))
53f220fa62Smrg
54f220fa62Smrg
55f220fa62SmrgQuilt::Quilt( Mapdesc *_mapdesc )
56f220fa62Smrg{
57f220fa62Smrg    mapdesc = _mapdesc;
58f220fa62Smrg}
59f220fa62Smrg
60f220fa62Smrgvoid
61f220fa62SmrgQuilt::deleteMe( Pool& p )
62f220fa62Smrg{
63f220fa62Smrg    for( Quiltspec *q=qspec; q != eqspec; q++ ) {
64f220fa62Smrg#if 1
65f220fa62Smrg	if( q->breakpoints) delete[] q->breakpoints;  q->breakpoints = 0;
66f220fa62Smrg#else
67f220fa62Smrg	if( q->breakpoints) {
68f220fa62Smrg	   delete[] q->breakpoints;
69f220fa62Smrg	   q->breakpoints = 0;
70f220fa62Smrgprintf("in here\n");
71f220fa62Smrg        }
72f220fa62Smrg#endif
73f220fa62Smrg    }
74f220fa62Smrg    if( cpts ) delete[] cpts;
75f220fa62Smrg    cpts = 0;
76f220fa62Smrg    PooledObj::deleteMe( p );
77f220fa62Smrg}
78f220fa62Smrg
79f220fa62Smrgvoid
80f220fa62SmrgQuilt::show( void )
81f220fa62Smrg{
82f220fa62Smrg#ifndef NDEBUG
83f220fa62Smrg    int nc = mapdesc->getNcoords();
84f220fa62Smrg    REAL *ps  = cpts;
85f220fa62Smrg    ps += qspec[0].offset;
86f220fa62Smrg    ps += qspec[1].offset;
87f220fa62Smrg    for( int i=0; i!= qspec[0].order * qspec[0].width; i++ ) {
88f220fa62Smrg	for( int j = 0; j!= qspec[1].order * qspec[1].width; j++ ) {
89f220fa62Smrg	    for( int k=0; k < nc; k++ )
90f220fa62Smrg		_glu_dprintf(  "%g ", ps[i*qspec[0].stride + j*qspec[1].stride + k] );
91f220fa62Smrg	    _glu_dprintf(  "\n" );
92f220fa62Smrg	}
93f220fa62Smrg	_glu_dprintf(  "\n" );
94f220fa62Smrg    }
95f220fa62Smrg    _glu_dprintf( "\n" );
96f220fa62Smrg#endif
97f220fa62Smrg}
98f220fa62Smrg
99f220fa62Smrg/*--------------------------------------------------------------------------
100f220fa62Smrg * Quilt::select - find which map in each quilt contains the points
101f220fa62Smrg *			pta and ptb with pta[i] < ptb[i]
102f220fa62Smrg *--------------------------------------------------------------------------
103f220fa62Smrg */
104f220fa62Smrg
105f220fa62Smrgvoid
106f220fa62SmrgQuilt::select( REAL *pta, REAL *ptb )
107f220fa62Smrg{
108f220fa62Smrg    int dim = eqspec - qspec;
109f220fa62Smrg    int i, j;
110f220fa62Smrg    for( i=0; i<dim; i++) {
111f220fa62Smrg	for( j=qspec[i].width-1; j>=0; j-- )
112f220fa62Smrg	    if( (qspec[i].breakpoints[j] <= pta[i]   ) &&
113f220fa62Smrg	    	(ptb[i] <= qspec[i].breakpoints[j+1] ) )
114f220fa62Smrg		 break;
115f220fa62Smrg	assert( j != -1 );
116f220fa62Smrg	qspec[i].index = j;
117f220fa62Smrg    }
118f220fa62Smrg}
119f220fa62Smrg
120f220fa62Smrgvoid
121f220fa62SmrgQuilt::download( Backend &backend )
122f220fa62Smrg{
123f220fa62Smrg    if( getDimension() == 2 ) {
124f220fa62Smrg	REAL *ps  = cpts;
125f220fa62Smrg	ps += qspec[0].offset;
126f220fa62Smrg	ps += qspec[1].offset;
127f220fa62Smrg	ps += qspec[0].index * qspec[0].order * qspec[0].stride;
128f220fa62Smrg	ps += qspec[1].index * qspec[1].order * qspec[1].stride;
129f220fa62Smrg	backend.surfpts( mapdesc->getType(), ps,
130f220fa62Smrg		  qspec[0].stride,
131f220fa62Smrg		  qspec[1].stride,
132f220fa62Smrg		  qspec[0].order,
133f220fa62Smrg		  qspec[1].order,
134f220fa62Smrg		  qspec[0].breakpoints[qspec[0].index],
135f220fa62Smrg		  qspec[0].breakpoints[qspec[0].index+1],
136f220fa62Smrg		  qspec[1].breakpoints[qspec[1].index],
137f220fa62Smrg		  qspec[1].breakpoints[qspec[1].index+1] );
138f220fa62Smrg    } else {
139f220fa62Smrg	REAL *ps  = cpts;
140f220fa62Smrg	ps += qspec[0].offset;
141f220fa62Smrg	ps += qspec[0].index * qspec[0].order * qspec[0].stride;
142f220fa62Smrg	backend.curvpts( mapdesc->getType(), ps,
143f220fa62Smrg		      qspec[0].stride,
144f220fa62Smrg		      qspec[0].order,
145f220fa62Smrg		      qspec[0].breakpoints[qspec[0].index],
146f220fa62Smrg		      qspec[0].breakpoints[qspec[0].index+1] );
147f220fa62Smrg    }
148f220fa62Smrg}
149f220fa62Smrg
150f220fa62Smrg/*--------------------------------------------------------------------------
151f220fa62Smrg * Quilt::downloadAll - download each map that contains the current patch
152f220fa62Smrg *--------------------------------------------------------------------------
153f220fa62Smrg */
154f220fa62Smrg
155f220fa62Smrgvoid
156f220fa62SmrgQuilt::downloadAll( REAL *pta, REAL *ptb, Backend &backend )
157f220fa62Smrg{
158f220fa62Smrg    for( Quilt *m = this; m; m=m->next ) {
159f220fa62Smrg	m->select( pta, ptb );
160f220fa62Smrg	m->download( backend );
161f220fa62Smrg    }
162f220fa62Smrg}
163f220fa62Smrg
164f220fa62Smrg/*--------------------------------------------------------------------------
165f220fa62Smrg  * Quilt::isCulled - determine if an entire quilt is trivially rejected.
166f220fa62Smrg *--------------------------------------------------------------------------
167f220fa62Smrg */
168f220fa62Smrg
169f220fa62Smrgint
170f220fa62SmrgQuilt::isCulled( void )
171f220fa62Smrg{
172f220fa62Smrg    if( mapdesc->isCulling() )
173f220fa62Smrg	return mapdesc->xformAndCullCheck( cpts + qspec[0].offset + qspec[1].offset,
174f220fa62Smrg			qspec[0].order * qspec[0].width, qspec[0].stride,
175f220fa62Smrg			qspec[1].order * qspec[1].width, qspec[1].stride );
176f220fa62Smrg    else
177f220fa62Smrg	return CULL_ACCEPT;
178f220fa62Smrg}
179f220fa62Smrg
180f220fa62Smrg/*---------------------------------------------------------------------------
181f220fa62Smrg * Quilt::getRange - retrieve the valid paramater range of a set of quilts
182f220fa62Smrg *---------------------------------------------------------------------------
183f220fa62Smrg */
184f220fa62Smrgvoid
185f220fa62SmrgQuilt::getRange( REAL *from, REAL *to, Flist& slist, Flist &tlist )
186f220fa62Smrg{
187f220fa62Smrg    getRange( from, to, 0, slist );
188f220fa62Smrg    getRange( from, to, 1, tlist );
189f220fa62Smrg}
190f220fa62Smrg
191f220fa62Smrg/*---------------------------------------------------------------------------
192f220fa62Smrg * Quilt::getRange - retrieve the valid paramater range of a set of quilts
193f220fa62Smrg *---------------------------------------------------------------------------
194f220fa62Smrg */
195f220fa62Smrgvoid
196f220fa62SmrgQuilt::getRange( REAL *from, REAL *to, int i, Flist &list )
197f220fa62Smrg{
198f220fa62Smrg    Quilt *maps = this;
199f220fa62Smrg    from[i] = maps->qspec[i].breakpoints[0];
200f220fa62Smrg    to[i]   = maps->qspec[i].breakpoints[maps->qspec[i].width];
201f220fa62Smrg    int maxpts = 0;
202f220fa62Smrg    Quilt_ptr m;
203f220fa62Smrg    for( m=maps; m; m=m->next ) {
204f220fa62Smrg	if( m->qspec[i].breakpoints[0] > from[i] )
205f220fa62Smrg	    from[i] = m->qspec[i].breakpoints[0];
206f220fa62Smrg	if( m->qspec[i].breakpoints[m->qspec[i].width] < to[i] )
207f220fa62Smrg	    to[i] = m->qspec[i].breakpoints[m->qspec[i].width];
208f220fa62Smrg	maxpts += m->qspec[i].width + 1;
209f220fa62Smrg    }
210f220fa62Smrg
211f220fa62Smrg    list.grow( maxpts );
212f220fa62Smrg
213f220fa62Smrg    for( m=maps; m; m=m->next )
214f220fa62Smrg	for( int j=0; j<=m->qspec[i].width; j++ ) {
215f220fa62Smrg	    list.add( m->qspec[i].breakpoints[j] );
216f220fa62Smrg	}
217f220fa62Smrg
218f220fa62Smrg    list.filter( );
219f220fa62Smrg    list.taper( from[i], to[i] );
220f220fa62Smrg}
221f220fa62Smrg
222f220fa62Smrgvoid
223f220fa62SmrgQuilt::getRange( REAL *from, REAL *to, Flist& slist )
224f220fa62Smrg{
225f220fa62Smrg    getRange( from, to, 0, slist );
226f220fa62Smrg}
227f220fa62Smrg
228f220fa62Smrgvoid
229f220fa62SmrgQuilt::findRates( Flist& slist, Flist& tlist, REAL rate[2] )
230f220fa62Smrg{
231f220fa62Smrg    findSampleRates( slist, tlist );
232f220fa62Smrg    rate[0] = qspec[0].step_size;
233f220fa62Smrg    rate[1] = qspec[1].step_size;
234f220fa62Smrg
235f220fa62Smrg    for( Quilt *q = next; q; q = q->next ) {
236f220fa62Smrg	q->findSampleRates( slist, tlist );
237f220fa62Smrg	if( q->qspec[0].step_size < rate[0] )
238f220fa62Smrg	    rate[0] = q->qspec[0].step_size;
239f220fa62Smrg	if( q->qspec[1].step_size < rate[1] )
240f220fa62Smrg	    rate[1] = q->qspec[1].step_size;
241f220fa62Smrg    }
242f220fa62Smrg}
243f220fa62Smrg
244f220fa62Smrgvoid
245f220fa62SmrgQuilt::findSampleRates( Flist& slist, Flist& tlist )
246f220fa62Smrg{
247f220fa62Smrg    qspec[0].step_size = DEF_PATCH_STEPSIZE *
248f220fa62Smrg	    (qspec[0].breakpoints[qspec[0].width] - qspec[0].breakpoints[0]);
249f220fa62Smrg    qspec[1].step_size = DEF_PATCH_STEPSIZE *
250f220fa62Smrg	    (qspec[1].breakpoints[qspec[1].width] - qspec[1].breakpoints[0]);
251f220fa62Smrg
252f220fa62Smrg    for( int i = slist.start; i < slist.end-1; i++ ) {
253f220fa62Smrg	for( int j = tlist.start; j < tlist.end-1; j++ ) {
254f220fa62Smrg
255f220fa62Smrg	    REAL pta[2], ptb[2];
256f220fa62Smrg	    pta[0] = slist.pts[i];
257f220fa62Smrg	    ptb[0] = slist.pts[i+1];
258f220fa62Smrg	    pta[1] = tlist.pts[j];
259f220fa62Smrg	    ptb[1] = tlist.pts[j+1];
260f220fa62Smrg	    Patchlist patchlist( this, pta, ptb );
261f220fa62Smrg	    patchlist.getstepsize();
262f220fa62Smrg
263f220fa62Smrg	    {
264f220fa62Smrg	    float edge_len_s = min(glu_abs(ptb[0]-pta[0]),1.0);
265f220fa62Smrg	    float edge_len_t = min(glu_abs(ptb[1]-pta[1]),1.0);
266f220fa62Smrg
267f220fa62Smrg	    if( patchlist.getStepsize(0)/edge_len_s < qspec[0].step_size )
268f220fa62Smrg	       qspec[0].step_size = patchlist.getStepsize(0)/edge_len_s;
269f220fa62Smrg	    if( patchlist.getStepsize(1)/edge_len_t < qspec[1].step_size )
270f220fa62Smrg	      qspec[1].step_size = patchlist.getStepsize(1)/edge_len_t;
271f220fa62Smrg	    }
272f220fa62Smrg	}
273f220fa62Smrg    }
274f220fa62Smrg}
275