File: display.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (313 lines) | stat: -rw-r--r-- 8,686 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
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
44
45
46
47
48
49
50
51
52
53
54
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* ==========================================================================
                            DISPLAY_C
=============================================================================

    FUNCTION NAMES

    void paint_muscles 			-- displays the muscles.
    void paint_polyline 		-- paint the polyline.
    paint_polygons			-- display the polygons.
    calculate_polygon_vertex_normal 	-- calculate the vertex norms.
    calc_normal 			-- calculate a normal.

    C SPECIFICATIONS

    void paint_muscles 			( HEAD *face )
    void paint_polyline 		( HEAD *face )
    paint_polygons      		( HEAD *face, int type, int normals )
    calculate_polygon_vertex_normal 	( HEAD *face ) 
    calc_normal 			( float *p1, float *p2, float *p3, 
                                          float *norm )

    DESCRIPTION
    
    This module is responsible for displaying the face geometry.
    This module comes as is with no warranties.  

    HISTORY
	16-Dec-94  Keith Waters (waters) at DEC's Cambridge Research Lab
	Created.

============================================================================ */

#include <math.h>           /* C header for any math functions               */
#include <stdio.h>          /* C header for standard I/O                     */
#include <string.h>         /* For String compare                            */
#include <stdlib.h>
#ifndef _WIN32
#include <sys/types.h>
#include <sys/file.h>
#endif
#include <GL/glut.h>	    /* OpenGl headers				     */

#include "head.h"	   /* local header for the face		             */

void calc_normal ( float *p1, float *p2, float *p3, float *norm );

/* ========================================================================= */  
/* paint_muscles                                                             */
/* ========================================================================= */  
/*
** Displays the face muscles.
**
*/

#define PAINT_MUSCLES_DEBUG 0
void paint_muscles ( HEAD *face )
{
  int i,j;
  float v1[3], v2[3] ;

  glLineWidth   ( 3.0 ) ;
  glColor3f     ( 100.0, 200.0, 200.0 ) ;

    for ( i=0; i<face->nmuscles; i++ ) {
    
      for (j=0; j<3; j++) {
      v1[j] = face->muscle[i]->head[j] ;
      v2[j] = face->muscle[i]->tail[j] ;
    }
      
#if PAINT_MUSCLES_DEBUG
    fprintf (stderr, "head x: %f y: %f z: %f\n",   v1[0], v1[1], v1[2] ) ;
    fprintf (stderr, "tail x: %f y: %f z: %f\n\n", v2[0], v2[1], v2[2] ) ;
#endif

      glBegin ( GL_LINE_STRIP ) ;
      glVertex3f ( v1[0], v1[1], v1[2] ) ;
      glVertex3f ( v2[0], v2[1], v2[2] ) ;
      glEnd ( ) ;
  }
  glLineWidth   ( 1.0 ) ;
}

/* ========================================================================= */  
/* paint_polyline                                                            */
/* ========================================================================= */  
/*
** Displays the polyline.
**
*/

void paint_polyline ( HEAD *face )
{
  int i,j,cnt ;
  float v1[3] ;
  static float r ;

  glClear    	( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
  glLineWidth   ( 1.0 ) ;
  glColor3f     ( 100.0, 100.0, 0.0 ) ;
  
  glPushMatrix  ( ) ;
  glRotatef	( r, 1.0, 1.0, 1.0 ) ;

  glBegin ( GL_LINE_STRIP ) ;
    for (cnt=0, i=0; i<face->npolylinenodes; i++ ) {
    
    for (j=0; j<3; j++, cnt++) 
      v1[j] = face->polyline[cnt] ;

#if PAINT_POLYLINE_DEBUG
    printf ("x: %f y: %f z: %f\n", v1[0], v1[1], v1[2] ) ;
#endif

      glVertex3f ( v1[0], v1[1], v1[2] ) ;
  }
  
  glEnd ( ) ;

  glPopMatrix	( ) ;
  glFlush	( ) ;
  
}


/* ========================================================================= */  
/* paint_polygons                                                            */
/* ========================================================================= */  
/*
** Paints the polygons of the face. 
** Type indicates if they are to be 
** drawn         (type=0), 
** flat shaded   (type=1),
** smooth shaded (type=2).
*/

void paint_polygons ( HEAD *face, int type, int normals )
{
  int    i, j ;
  float  v1[3], v2[3], v3[3] ;
  float  norm1[3], norm2[3], norm3[3] ;
  float  vn1[3], vn2[3], vn3[3] ;

  glLineWidth   ( 2.0 ) ;  

  for (i=0; i<face->npolygons; i++ )
    {
      for (j=0; j<3; j++) {
	v1[j] = face->polygon[i]->vertex[0]->xyz[j] ;
	v2[j] = face->polygon[i]->vertex[1]->xyz[j] ;
	v3[j] = face->polygon[i]->vertex[2]->xyz[j] ;
      }

      if ( type == 0 ) {
	
	for (j=0; j<3; j++) {
	  norm1[j] = face->polygon[i]->vertex[0]->norm[j] ;
	  norm2[j] = face->polygon[i]->vertex[1]->norm[j] ;
	  norm3[j] = face->polygon[i]->vertex[2]->norm[j] ;
	}
	glBegin ( GL_LINE_LOOP ) ; {
	  glNormal3f ( norm1[0], norm1[1], norm1[2] ) ;
	  glVertex3f ( v1[0],    v1[1],    v1[2]    ) ;
	  glNormal3f ( norm2[0], norm2[1], norm2[2] ) ;
	  glVertex3f ( v2[0],    v2[1],    v2[2]    ) ;
	  glNormal3f ( norm3[0], norm3[1], norm3[2] ) ;
	  glVertex3f ( v3[0],    v3[1],    v3[2]    ) ;
	}  glEnd ( ) ;

      } /* end if drawn */

      if ( type == 1 ) {
	
	for (j=0; j<3; j++) {
	  norm1[j] = face->polygon[i]->vertex[0]->norm[j] ;
	  norm2[j] = face->polygon[i]->vertex[1]->norm[j] ;
	  norm3[j] = face->polygon[i]->vertex[2]->norm[j] ;
	}
	glBegin ( GL_TRIANGLES ) ; {
	  glNormal3f ( norm1[0], norm1[1], norm1[2] ) ;
	  glVertex3f ( v1[0],    v1[1],    v1[2]    ) ;
	  glNormal3f ( norm2[0], norm2[1], norm2[2] ) ;
	  glVertex3f ( v2[0],    v2[1],    v2[2]    ) ;
	  glNormal3f ( norm3[0], norm3[1], norm3[2] ) ;
	  glVertex3f ( v3[0],    v3[1],    v3[2]    ) ;
	}  glEnd ( ) ;

      } /* end if drawn */


      else if ( type == 1) {
	for (j=0; j<3; j++) {
	  norm1[j] = face->polygon[i]->vertex[0]->norm[j] ;
	  norm2[j] = face->polygon[i]->vertex[1]->norm[j] ;
	  norm3[j] = face->polygon[i]->vertex[2]->norm[j] ;
	}
      } /* end if flat */

      else if ( type == 2 ) {

	averaged_vertex_normals ( face, i, norm1, norm2, norm3 ) ;

      } /* end if smoothed */

      if ( type ) {

	glBegin ( GL_TRIANGLES ) ; {
	  glNormal3f ( norm1[0], norm1[1], norm1[2] ) ;
	  glVertex3f ( v1[0],    v1[1],    v1[2]    ) ;
	  glNormal3f ( norm2[0], norm2[1], norm2[2] ) ;
	  glVertex3f ( v2[0],    v2[1],    v2[2]    ) ;
	  glNormal3f ( norm3[0], norm3[1], norm3[2] ) ;
	  glVertex3f ( v3[0],    v3[1],    v3[2]    ) ;
	}  glEnd ( ) ;
      } /* endif painted */

      if ( normals ) {
	for (j=0; j<3; j++) {
	  vn1[j] = face->polygon[i]->vertex[0]->xyz[j] + norm1[j] ;
	  vn2[j] = face->polygon[i]->vertex[1]->xyz[j] + norm2[j] ;
	  vn3[j] = face->polygon[i]->vertex[2]->xyz[j] + norm3[j] ;
	}

	glBegin ( GL_LINE_STRIP ) ; {
	  glVertex3f ( v1[0],    v1[1],    v1[2]     ) ;
	  glVertex3f ( vn1[0],  vn1[1],    vn1[2]    ) ;
	}  glEnd ( ) ;


	glBegin ( GL_LINES ) ; {
	  glVertex3f ( v2[0],    v2[1],    v2[2]     ) ;
	  glVertex3f ( vn2[0],  vn2[1],    vn2[2]    ) ;
	}  glEnd ( ) ;


	glBegin ( GL_LINES ) ; {
	  glVertex3f ( v3[0],    v3[1],    v3[2]     ) ;
	  glVertex3f ( vn3[0],  vn3[1],    vn3[2]    ) ;
	}  glEnd ( ) ;

      }
    }
  glLineWidth   ( 1.0 ) ;  
} 

/* ========================================================================= */  
/* calculate_polygon_vertex_normal.					     */
/* ========================================================================= */
/*
** As it says.
*/

void
calculate_polygon_vertex_normal ( HEAD *face ) 
{
  int i,j,k ;
  float p1[3], p2[3], p3[3] ;
  float norm[3] ;
  for (i=0; i<face->npolygons; i++ )
    {
      for (j=0; j<3; j++) 
	p1[j] = face->polygon[i]->vertex[0]->xyz[j] ;
      for (j=0; j<3; j++) 
	p2[j] = face->polygon[i]->vertex[1]->xyz[j] ;
      for (j=0; j<3; j++) 
	p3[j] = face->polygon[i]->vertex[2]->xyz[j] ;

      calc_normal ( p1, p2, p3, norm ) ;

      for (j=0; j<3; j++) 
	for (k=0; k<3; k++)
	  face->polygon[i]->vertex[j]->norm[k] = norm[k] ;
    }
}

/* ========================================================================= */  
/* calc_normal.					     			     */
/* ========================================================================= */
/*
** Calculates the normal vector from three vertices.
*/
void
calc_normal ( float *p1, float *p2, float *p3, float *norm )
{
  float coa, cob, coc ;
  float px1, py1, pz1 ;
  float px2, py2, pz2 ;
  float px3, py3, pz3 ;
  
  float absvec ;
  
  px1 = p1[0] ;
  py1 = p1[1] ;
  pz1 = p1[2] ;
  
  px2 = p2[0] ;
  py2 = p2[1] ;
  pz2 = p2[2] ;
  
  px3 = p3[0] ;
  py3 = p3[1] ;
  pz3 = p3[2] ;
  
  coa = -(py1 * (pz2-pz3) + py2*(pz3-pz1) + py3*(pz1-pz2)) ;
  cob = -(pz1 * (px2-px3) + pz2*(px3-px1) + pz3*(px1-px2)) ;
  coc = -(px1 * (py2-py3) + px2*(py3-py1) + px3*(py1-py2)) ;
  
  absvec = sqrt ((double) ((coa*coa) + (cob*cob) + (coc*coc))) ;
  
  norm[0] = coa/absvec ;
  norm[1] = cob/absvec ;
  norm[2] = coc/absvec ;
}