vtkOglrPolyMapper.cxx 9.02 KB
Newer Older
1
2
3
/*=========================================================================

  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
  Module:    vtkOglrPolyMapper.cxx
5
6
7
8
9
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


10
Copyright (c) 1993-1996 Ken Martin, Will Schroeder, Bill Lorensen.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.

The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.

IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.


=========================================================================*/
#include <stdlib.h>
#include <math.h>

Ken Martin's avatar
Ken Martin committed
44
#include "vtkOglrPolyMapper.h"
45
46

#ifdef _WIN32
Ken Martin's avatar
Ken Martin committed
47
#include "vtkWin32OglrRenderWindow.h"
48
#else
Ken Martin's avatar
Ken Martin committed
49
#include "vtkOglrRenderWindow.h"
50
#endif
Ken Martin's avatar
Ken Martin committed
51
52
53
54
#include "vtkOglrRenderer.h"
#include "vtkPolyData.h"
#include "vtkPolygon.h"
#include "vtkTriangle.h"
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

// Description:
// Construct empty object.
vtkOglrPolyMapper::vtkOglrPolyMapper()
{
  this->Data = NULL; 
  this->Colors = NULL; 
}

// Description:
// Get the lmcolor property, this is a pretty important little 
// function.  It determines how vertex colors will be handled  
// in gl.  When a PolyMapper has vertex colors it will use this 
// method to determine what lmcolor mode to set.               
GLenum vtkOglrPolyMapper::GetLmcolorMode(vtkProperty *prop)
{
  if (prop->GetAmbient() > prop->GetDiffuse())
    {
    return GL_AMBIENT;
    }
  else
    {
    return GL_DIFFUSE;
    }
}

// Description:
// Build the data structure for the gl polygon PolyMapper.
void vtkOglrPolyMapper::Build(vtkPolyData *data, vtkColorScalars *c)
{
  this->Data = data;
  this->Colors = c;
  return;
}

// Description:
// Draw method for OpenGL.
void vtkOglrPolyMapper::Draw(vtkRenderer *aren, vtkActor *act)
{
  vtkOglrRenderer *ren = (vtkOglrRenderer *)aren;
  int npts, rep, j, interpolation, idx[3];
  float fclr[4], polyNorm[3], tran;
  short clr[4];
  GLenum glFunction[4], aGlFunction;
  vtkProperty *prop;
  vtkPoints *p;
  vtkCellArray *prims[4], *aPrim;
  vtkColorScalars *c;
  vtkNormals *n;
  int *pts;
  vtkTCoords *t;
  int tDim, primType;

  if ( ! this->Data || (npts=this->Data->GetNumberOfPoints()) < 1)
    {
    return;
    }
  
Ken Martin's avatar
Ken Martin committed
113
// make sure our window is current
114
115
116
117
118
119
120
121
122
123
124
125
#ifdef _WIN32
  ((vtkWin32OglrRenderWindow *)(ren->GetRenderWindow()))->MakeCurrent();
#else
  ((vtkOglrRenderWindow *)(ren->GetRenderWindow()))->MakeCurrent();
#endif

  // get the property 
  prop = act->GetProperty();

  // get the transparency 
  tran = prop->GetOpacity();
  
126
  // if the primitives are invisable then get out of here 
127
128
129
  if (tran <= 0.0) return;
  clr[3] = (short) ((float)tran*255);

130
  // get the representation (e.g., surface / wireframe / points)
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  rep = prop->GetRepresentation();

  switch (rep) 
    {
    case VTK_POINTS:
      glFunction[0]  = GL_POINTS;
      glFunction[1]  = GL_POINTS;
      glFunction[2]  = GL_POINTS;
      glFunction[3]  = GL_POINTS;
      break;
    case VTK_WIREFRAME:
      glFunction[0] = GL_POINTS;
      glFunction[1] = GL_LINE_STRIP;
      glFunction[2] = GL_LINE_STRIP;
      glFunction[3] = GL_LINE_LOOP;
      break;
    case VTK_SURFACE:
      glFunction[0] = GL_POINTS;
      glFunction[1] = GL_LINE_STRIP;
      glFunction[2] = GL_TRIANGLE_STRIP;
      glFunction[3] = GL_POLYGON;
      break;
    default: 
      vtkErrorMacro(<< "Bad representation sent\n");
      glFunction[0] = GL_POINTS;
      glFunction[1] = GL_LINE_STRIP;
      glFunction[2] = GL_TRIANGLE_STRIP;
      glFunction[3] = GL_POLYGON;
      break;
    }

  // get the shading interpolation 
  interpolation = prop->GetInterpolation();

  // and draw the display list
  p = this->Data->GetPoints();
  c = this->Colors;
  prims[0] = this->Data->GetVerts();
  prims[1] = this->Data->GetLines();
  prims[2] = this->Data->GetStrips();
  prims[3] = this->Data->GetPolys();

  t = this->Data->GetPointData()->GetTCoords();
  if ( t ) 
    {
    tDim = t->GetDimension();
    if (tDim != 2)
      {
      vtkDebugMacro(<< "Currently only 2d textures are supported.\n");
      t = NULL;
      }
    }

  n = this->Data->GetPointData()->GetNormals();
  if (interpolation == VTK_FLAT) n = 0;

  // if we are doing vertex colors then set lmcolor to adjust 
  // the current materials ambient and diffuse values using   
  // vertex color commands otherwise tell it not to.          
  glDisable( GL_COLOR_MATERIAL );
  if (c)
    {
    glColorMaterial( GL_FRONT_AND_BACK, this->GetLmcolorMode(prop));
    glEnable( GL_COLOR_MATERIAL );
    }
  
  for (primType = 0; primType < 4; primType++)
    {
    aPrim = prims[primType];
    aGlFunction = glFunction[primType];
    
    // for lines or points
    if (primType < 2)
      {
      if (!n)
	{
	glDisable( GL_LIGHTING);
	if (!c)
	  {
	  float *bg_color;
	  // if a line is being drawn without normals and with the  
	  // ambient intensity set to zero, then lets pretend that  
	  // the ambient intensity is 1.0 because otherwise the line
	  // would either not show up or be screwed up              
	  // get the color from the property and set it 
	  bg_color = prop->GetColor();
	  fclr[0] = bg_color[0]; 
	  fclr[1] = bg_color[1]; 
	  fclr[2] = bg_color[2];
	  fclr[3]  = tran;
	  glBegin( GL_POINTS );
	  glColor4fv(fclr);
	  glEnd();
	  }
	}
      }
    
    for (aPrim->InitTraversal(); aPrim->GetNextCell(npts,pts); )
      { 
      glBegin(aGlFunction);
      
      if ((primType > 1) && (!n))
233
234
235
236
        {
        if ( primType == 3 ) vtkPolygon::ComputeNormal(p,npts,pts,polyNorm);
	else vtkTriangle::ComputeNormal(p,3,pts,polyNorm);
        }
237
238
239
240
241
      
      for (j = 0; j < npts; j++) 
	{
	if (c) 
	  {
242
	  glColor4ubv(c->GetColor(pts[j]));
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
	  }
	
	if (t)
	  {
	  glTexCoord2fv(t->GetTCoord(pts[j]));
	  }
	
	if (n) 
	  {
	  glNormal3fv(n->GetNormal(pts[j]));
	  }
	else 
	  {
	  if (primType == 3) 
	    {
	    glNormal3fv(polyNorm);
	    }
	  if (primType == 2)
	    {
	    if ( j > 2)
	      {
	      if (j % 2)
		{
		idx[0] = pts[j-2]; idx[1] = pts[j]; idx[2] = pts[j-1]; 
267
		vtkTriangle::ComputeNormal(p, 3, idx, polyNorm);
268
269
270
271
		}
	      else
		{
		idx[0] = pts[j-2]; idx[1] = pts[j-1]; idx[2] = pts[j]; 
272
		vtkTriangle::ComputeNormal(p, 3, idx, polyNorm);
273
274
275
276
		}
	      }
	    else if ( j == 0 )
	      {
277
	      vtkTriangle::ComputeNormal(p, 3, pts, polyNorm);
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
	      }
	    glNormal3fv(polyNorm);
	    }
	  }
	
	glVertex3fv(p->GetPoint(pts[j]));
	}
      glEnd();
      
      // if its wireframe, then draw the top and bottom edges
      // of any tstrips
      if (primType == 2 && rep == VTK_WIREFRAME) 
	{
	// draw first line
	glBegin(GL_LINE_STRIP);
	for (j = 0; j < npts; j += 2) 
	  {
	  if (c) 
	    {
297
	    glColor4ubv(c->GetColor(pts[j]));
298
299
300
301
302
303
304
305
306
307
	    }
	  
	  if (n) 
	    {
	    glNormal3fv(n->GetNormal(pts[j]));
	    }
	  else 
	    {
	    if ( j && j < (npts-1) )
	      {
308
309
              idx[0] = pts[j-1]; idx[1] = pts[j]; idx[2] = pts[j+1]; 
              vtkTriangle::ComputeNormal(p, 3, idx, polyNorm);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
	      }
	    glNormal3fv(polyNorm);
	    }
	  
	  if (t)
	    {
	    glTexCoord2fv(t->GetTCoord(pts[j]));
	    }
	  
	  glVertex3fv(p->GetPoint(pts[j]));
	  }
	glEnd();
	
	// draw second line
	glBegin(GL_LINE_STRIP);
	for (j = 1; j < npts; j += 2) 
	  {
	  if (c) 
	    {
329
	    glColor4ubv(c->GetColor(pts[j]));
330
331
332
333
334
335
336
337
338
339
	    }
	  
	  if (n) 
	    {
	    glNormal3fv(n->GetNormal(pts[j]));
	    }
	  else 
	    {
	    if (j < npts-1)
	      {
340
341
              idx[0] = pts[j+1]; idx[1] = pts[j]; idx[2] = pts[j-1]; 
              vtkTriangle::ComputeNormal(p, 3, idx, polyNorm);
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
	      }
	    glNormal3fv(polyNorm);
	    }
	  if (t)
	    {
	    glTexCoord2fv(t->GetTCoord(pts[j]));
	    }
	  glVertex3fv(p->GetPoint(pts[j]));
	  }
	glEnd();
	}
      }
    if (primType < 2)
      {
      // reset the lighting if we turned it off
      if (!n) glEnable( GL_LIGHTING);
      }
    }
}