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*/
37f220fa62Smrg
38f220fa62Smrg#include "gluos.h"
39f220fa62Smrg#include <stdlib.h>
40f220fa62Smrg#include <stdio.h>
41f220fa62Smrg#include <GL/gl.h>
42f220fa62Smrg#include "zlassert.h"
43f220fa62Smrg#include "gridWrap.h"
44f220fa62Smrg
45f220fa62Smrg
46f220fa62Smrg/*******************grid structure****************************/
47f220fa62Smrgvoid gridWrap::print()
48f220fa62Smrg{
49f220fa62Smrg  printf("n_ulines = %i\n", n_ulines);
50f220fa62Smrg  printf("n_vlines = %i\n", n_vlines);
51f220fa62Smrg  printf("u_min=%f, umax=%f, vmin=%f, vmax=%f\n", u_min, u_max, v_min, v_max);
52f220fa62Smrg}
53f220fa62Smrg
54f220fa62SmrggridWrap::gridWrap(Int nUlines, Real* uvals,
55f220fa62Smrg		   Int nVlines, Real* vvals)
56f220fa62Smrg{
57f220fa62Smrg  assert(nUlines>=2);
58f220fa62Smrg  assert(nVlines>=2);
59f220fa62Smrg
60f220fa62Smrg  is_uniform = 0;
61f220fa62Smrg  n_ulines = nUlines;
62f220fa62Smrg  n_vlines = nVlines;
63f220fa62Smrg  u_min = uvals[0];
64f220fa62Smrg  u_max = uvals[nUlines-1];
65f220fa62Smrg  v_min = vvals[0];
66f220fa62Smrg  v_max = vvals[nVlines-1];
67f220fa62Smrg  u_values = (Real*) malloc(sizeof(Real) * n_ulines);
68f220fa62Smrg  assert(u_values);
69f220fa62Smrg  v_values = (Real*) malloc(sizeof(Real) * n_vlines);
70f220fa62Smrg  assert(v_values);
71f220fa62Smrg
72f220fa62Smrg  Int i;
73f220fa62Smrg  for(i=0; i<n_ulines; i++)
74f220fa62Smrg    u_values[i] = uvals[i];
75f220fa62Smrg  for(i=0; i<n_vlines; i++)
76f220fa62Smrg    v_values[i] = vvals[i];
77f220fa62Smrg}
78f220fa62Smrg
79f220fa62SmrggridWrap::gridWrap(Int nUlines, Int nVlines,
80f220fa62Smrg		   Real uMin, Real uMax,
81f220fa62Smrg		   Real vMin, Real vMax
82f220fa62Smrg		   )
83f220fa62Smrg{
84f220fa62Smrg  is_uniform = 1;
85f220fa62Smrg  n_ulines = nUlines;
86f220fa62Smrg  n_vlines = nVlines;
87f220fa62Smrg  u_min = uMin;
88f220fa62Smrg  u_max = uMax;
89f220fa62Smrg  v_min = vMin;
90f220fa62Smrg  v_max = vMax;
91f220fa62Smrg  u_values = (Real*) malloc(sizeof(Real) * n_ulines);
92f220fa62Smrg  assert(u_values);
93f220fa62Smrg  v_values = (Real*) malloc(sizeof(Real) * n_vlines);
94f220fa62Smrg  assert(v_values);
95f220fa62Smrg
96f220fa62Smrg  Int i;
97f220fa62Smrg  assert(nUlines>=2);
98f220fa62Smrg  assert(nVlines>=2);
99f220fa62Smrg  Real du = (uMax-uMin)/(nUlines-1);
100f220fa62Smrg  Real dv = (vMax-vMin)/(nVlines-1);
101f220fa62Smrg
102f220fa62Smrg  float tempu=uMin;
103f220fa62Smrg  u_values[0] = tempu;
104f220fa62Smrg  for(i=1; i<nUlines; i++)
105f220fa62Smrg    {
106f220fa62Smrg      tempu += du;
107f220fa62Smrg      u_values[i] = tempu;
108f220fa62Smrg    }
109f220fa62Smrg  u_values[nUlines-1] = uMax;
110f220fa62Smrg
111f220fa62Smrg  float tempv=vMin;
112f220fa62Smrg  v_values[0] = tempv;
113f220fa62Smrg  for(i=1; i<nVlines; i++)
114f220fa62Smrg    {
115f220fa62Smrg      tempv += dv;
116f220fa62Smrg      v_values[i] = tempv;
117f220fa62Smrg    }
118f220fa62Smrg  v_values[nVlines-1] = vMax;
119f220fa62Smrg}
120f220fa62Smrg
121f220fa62SmrggridWrap::~gridWrap()
122f220fa62Smrg{
123f220fa62Smrg  free(u_values);
124f220fa62Smrg  free(v_values);
125f220fa62Smrg}
126f220fa62Smrg
127f220fa62Smrgvoid gridWrap::draw()
128f220fa62Smrg{
129f220fa62Smrg  int i,j;
130f220fa62Smrg  glBegin(GL_POINTS);
131f220fa62Smrg  for(i=0; i<n_ulines; i++)
132f220fa62Smrg    for(j=0; j<n_vlines; j++)
133f220fa62Smrg      glVertex2f(get_u_value(i), get_v_value(j));
134f220fa62Smrg  glEnd();
135f220fa62Smrg}
136f220fa62Smrg
137f220fa62Smrgvoid gridWrap::outputFanWithPoint(Int v, Int uleft, Int uright, Real vert[2], primStream* pStream)
138f220fa62Smrg{
139f220fa62Smrg  Int i;
140f220fa62Smrg  if(uleft >= uright)
141f220fa62Smrg    return; //no triangles to output.
142f220fa62Smrg
143f220fa62Smrg  pStream->begin();
144f220fa62Smrg  pStream->insert(vert);
145f220fa62Smrg
146f220fa62Smrg  assert(vert[1] != v_values[v]); //don't output degenerate triangles
147f220fa62Smrg
148f220fa62Smrg  if(vert[1] > v_values[v]) //vertex is above this grid line: notice the orientation
149f220fa62Smrg    {
150f220fa62Smrg      for(i=uleft; i<=uright; i++)
151f220fa62Smrg	pStream->insert(u_values[i], v_values[v]);
152f220fa62Smrg    }
153f220fa62Smrg  else //vertex is below the grid line
154f220fa62Smrg    {
155f220fa62Smrg      for(i=uright; i>= uleft; i--)
156f220fa62Smrg	pStream->insert(u_values[i], v_values[v]);
157f220fa62Smrg    }
158f220fa62Smrg
159f220fa62Smrg  pStream->end(PRIMITIVE_STREAM_FAN);
160f220fa62Smrg}
161f220fa62Smrg
162f220fa62Smrg
163f220fa62Smrg
164f220fa62Smrg/*each chain stores a number of consecutive
165f220fa62Smrg *V-lines within a grid.
166f220fa62Smrg *There is one grid vertex on each V-line.
167f220fa62Smrg * The total number of V-lines is:
168f220fa62Smrg *   nVlines.
169f220fa62Smrg * with respect to the grid, the index of the first V-line is
170f220fa62Smrg *   firstVlineIndex.
171f220fa62Smrg * So with respect to the grid, the index of the ith V-line is
172f220fa62Smrg *   firstVlineIndex-i.
173f220fa62Smrg * the grid-index of the uline at the ith vline (recall that each vline has one grid point)
174f220fa62Smrg * is ulineIndices[i]. The u_value is cached in ulineValues[i], that is,
175f220fa62Smrg *   ulineValues[i] = grid->get_u_value(ulineIndices[i])
176f220fa62Smrg */
177f220fa62SmrggridBoundaryChain::gridBoundaryChain(
178f220fa62Smrg				     gridWrap* gr,
179f220fa62Smrg				     Int first_vline_index,
180f220fa62Smrg				     Int n_vlines,
181f220fa62Smrg				     Int* uline_indices,
182f220fa62Smrg				     Int* inner_indices
183f220fa62Smrg				     )
184f220fa62Smrg: grid(gr), firstVlineIndex(first_vline_index), nVlines(n_vlines)
185f220fa62Smrg{
186f220fa62Smrg  ulineIndices = (Int*) malloc(sizeof(Int) * n_vlines);
187f220fa62Smrg  assert(ulineIndices);
188f220fa62Smrg
189f220fa62Smrg  innerIndices = (Int*) malloc(sizeof(Int) * n_vlines);
190f220fa62Smrg  assert(innerIndices);
191f220fa62Smrg
192f220fa62Smrg  vertices = (Real2*) malloc(sizeof(Real2) * n_vlines);
193f220fa62Smrg  assert(vertices);
194f220fa62Smrg
195f220fa62Smrg
196f220fa62Smrg
197f220fa62Smrg  Int i;
198f220fa62Smrg  for(i=0; i<n_vlines; i++){
199f220fa62Smrg    ulineIndices[i] = uline_indices[i];
200f220fa62Smrg    innerIndices[i] = inner_indices[i];
201f220fa62Smrg  }
202f220fa62Smrg
203f220fa62Smrg  for(i=0; i<n_vlines; i++){
204f220fa62Smrg    vertices[i][0] = gr->get_u_value(ulineIndices[i]);
205f220fa62Smrg    vertices[i][1] = gr->get_v_value(first_vline_index-i);
206f220fa62Smrg  }
207f220fa62Smrg}
208f220fa62Smrg
209f220fa62Smrgvoid gridBoundaryChain::draw()
210f220fa62Smrg{
211f220fa62Smrg  Int i;
212f220fa62Smrg  glBegin(GL_LINE_STRIP);
213f220fa62Smrg  for(i=0; i<nVlines; i++)
214f220fa62Smrg    {
215f220fa62Smrg      glVertex2fv(vertices[i]);
216f220fa62Smrg    }
217f220fa62Smrg  glEnd();
218f220fa62Smrg}
219f220fa62Smrg
220f220fa62Smrgvoid gridBoundaryChain::drawInner()
221f220fa62Smrg{
222f220fa62Smrg  Int i;
223f220fa62Smrg  for(i=1; i<nVlines; i++)
224f220fa62Smrg    {
225f220fa62Smrg      glBegin(GL_LINE_STRIP);
226f220fa62Smrg      glVertex2f(grid->get_u_value(innerIndices[i]), get_v_value(i-1) );
227f220fa62Smrg      glVertex2f(grid->get_u_value(innerIndices[i]), get_v_value(i) );
228f220fa62Smrg      glEnd();
229f220fa62Smrg    }
230f220fa62Smrg}
231f220fa62Smrg
232f220fa62SmrgInt gridBoundaryChain::lookfor(Real v, Int i1, Int i2)
233f220fa62Smrg{
234f220fa62Smrg  Int mid;
235f220fa62Smrg  while(i1 < i2-1)
236f220fa62Smrg    {
237f220fa62Smrg      mid = (i1+i2)/2;
238f220fa62Smrg      if(v > vertices[mid][1])
239f220fa62Smrg	{
240f220fa62Smrg	  i2 = mid;
241f220fa62Smrg	}
242f220fa62Smrg      else
243f220fa62Smrg	i1 = mid;
244f220fa62Smrg    }
245f220fa62Smrg  return i1;
246f220fa62Smrg}
247f220fa62Smrg
248f220fa62Smrg/*output the fan of the right end between grid line i-1 and grid line i*/
249f220fa62Smrgvoid gridBoundaryChain::rightEndFan(Int i, primStream* pStream)
250f220fa62Smrg{
251f220fa62Smrg  Int j;
252f220fa62Smrg  if(getUlineIndex(i) > getUlineIndex(i-1))
253f220fa62Smrg    {
254f220fa62Smrg      pStream->begin();
255f220fa62Smrg      pStream->insert(get_vertex(i-1));
256f220fa62Smrg      for(j=getUlineIndex(i-1); j<= getUlineIndex(i); j++)
257f220fa62Smrg	pStream->insert(grid->get_u_value(j), get_v_value(i));
258f220fa62Smrg      pStream->end(PRIMITIVE_STREAM_FAN);
259f220fa62Smrg    }
260f220fa62Smrg  else if(getUlineIndex(i) < getUlineIndex(i-1))
261f220fa62Smrg    {
262f220fa62Smrg      pStream->begin();
263f220fa62Smrg      pStream->insert(get_vertex(i));
264f220fa62Smrg      for(j=getUlineIndex(i-1); j>= getUlineIndex(i); j--)
265f220fa62Smrg	pStream->insert(grid->get_u_value(j), get_v_value(i-1));
266f220fa62Smrg      pStream->end(PRIMITIVE_STREAM_FAN);
267f220fa62Smrg    }
268f220fa62Smrg  //otherside, the two are equal, so there is no fan to output
269f220fa62Smrg}
270f220fa62Smrg
271f220fa62Smrg
272f220fa62Smrg/*output the fan of the left end between grid line i-1 and grid line i*/
273f220fa62Smrgvoid gridBoundaryChain::leftEndFan(Int i, primStream* pStream)
274f220fa62Smrg{
275f220fa62Smrg  Int j;
276f220fa62Smrg  if(getUlineIndex(i) < getUlineIndex(i-1))
277f220fa62Smrg    {
278f220fa62Smrg      pStream->begin();
279f220fa62Smrg      pStream->insert(get_vertex(i-1));
280f220fa62Smrg      for(j=getUlineIndex(i); j<= getUlineIndex(i-1); j++)
281f220fa62Smrg	pStream->insert(grid->get_u_value(j), get_v_value(i));
282f220fa62Smrg      pStream->end(PRIMITIVE_STREAM_FAN);
283f220fa62Smrg    }
284f220fa62Smrg  else if(getUlineIndex(i) > getUlineIndex(i-1))
285f220fa62Smrg    {
286f220fa62Smrg      pStream->begin();
287f220fa62Smrg      pStream->insert(get_vertex(i));
288f220fa62Smrg      for(j=getUlineIndex(i); j>= getUlineIndex(i-1); j--)
289f220fa62Smrg	pStream->insert(grid->get_u_value(j), get_v_value(i-1));
290f220fa62Smrg      pStream->end(PRIMITIVE_STREAM_FAN);
291f220fa62Smrg    }
292f220fa62Smrg  /*otherwisem, the two are equal, so there is no fan to outout*/
293f220fa62Smrg}
294