2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
10 ** http://oss.sgi.com/projects/FreeB
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
42 #include <GL/glu.h> /*for drawing bzier patch*/
43 #include "bezierPatch.h"
44 #include "bezierEval.h"
47 *allocate an instance of bezierPatch. The control points are unknown. But
48 *the space of this array is allocated with size of
49 * uorder*vorder*dimension
52 bezierPatch
* bezierPatchMake(float umin
, float vmin
, float umax
, float vmax
, int uorder
, int vorder
, int dimension
)
54 bezierPatch
* ret
= (bezierPatch
*) malloc(sizeof(bezierPatch
));
62 ret
->dimension
= dimension
;
63 ret
->ctlpoints
= (float*) malloc(sizeof(float) * dimension
* uorder
* vorder
);
64 assert(ret
->ctlpoints
);
71 bezierPatch
* bezierPatchMake2(float umin
, float vmin
, float umax
, float vmax
, int uorder
, int vorder
, int dimension
, int ustride
, int vstride
, float* ctlpoints
)
73 bezierPatch
* ret
= (bezierPatch
*) malloc(sizeof(bezierPatch
));
81 ret
->dimension
= dimension
;
82 ret
->ctlpoints
= (float*) malloc(sizeof(float) * dimension
* uorder
* vorder
);
83 assert(ret
->ctlpoints
);
85 /*copy the control points there*/
86 int the_ustride
= vorder
* dimension
;
87 int the_vstride
= dimension
;
88 for(int i
=0; i
<uorder
; i
++)
89 for(int j
=0; j
<vorder
; j
++)
90 for(int k
=0; k
<dimension
; k
++)
91 ret
->ctlpoints
[i
* the_ustride
+ j
*the_vstride
+k
] = ctlpoints
[i
*ustride
+j
*vstride
+k
];
99 *deallocate the space as allocated by Make
101 void bezierPatchDelete(bezierPatch
*b
)
107 /*delete the whole linked list
109 void bezierPatchDeleteList(bezierPatch
*b
)
115 bezierPatchDelete(temp
);
119 bezierPatch
* bezierPatchInsert(bezierPatch
*list
, bezierPatch
*b
)
125 /*print the data stored in this patch*/
126 void bezierPatchPrint(bezierPatch
*b
)
128 printf("bezierPatch:\n");
129 printf("umin,umax=(%f,%f), (vmin, vmax)=(%f,%f)\n", b
->umin
, b
->umax
, b
->vmin
, b
->vmax
);
130 printf("uorder=%i, vorder=%i\n", b
->uorder
, b
->vorder
);
131 printf("idmension = %i\n", b
->dimension
);
134 /*print the whole list*/
135 void bezierPatchPrintList(bezierPatch
*list
)
138 for(temp
=list
; temp
!= NULL
; temp
= temp
->next
)
139 bezierPatchPrint(temp
);
142 void bezierPatchEval(bezierPatch
*b
, float u
, float v
, float ret
[])
144 if( u
>= b
->umin
&& u
<= b
->umax
145 && v
>= b
->vmin
&& v
<= b
->vmax
)
148 bezierSurfEval(b
->umin
, b
->umax
, b
->uorder
, b
->vmin
, b
->vmax
, b
->vorder
, b
->dimension
, b
->ctlpoints
, b
->dimension
* b
->vorder
, b
->dimension
, u
, v
, ret
);
151 else if(b
->next
!= NULL
)
152 bezierPatchEval(b
->next
, u
,v
, ret
);
154 bezierSurfEval(b
->umin
, b
->umax
, b
->uorder
, b
->vmin
, b
->vmax
, b
->vorder
, b
->dimension
, b
->ctlpoints
, b
->dimension
* b
->vorder
, b
->dimension
, u
, v
, ret
);
157 /*the returned normal is normlized
159 void bezierPatchEvalNormal(bezierPatch
*b
, float u
, float v
, float ret
[])
161 bezierSurfEvalNormal(b
->umin
, b
->umax
, b
->uorder
, b
->vmin
, b
->vmax
, b
->vorder
, b
->dimension
, b
->ctlpoints
, b
->dimension
* b
->vorder
, b
->dimension
, u
, v
, ret
);
163 if( u
>= b
->umin
&& u
<= b
->umax
164 && v
>= b
->vmin
&& v
<= b
->vmax
)
166 bezierSurfEvalNormal(b
->umin
, b
->umax
, b
->uorder
, b
->vmin
, b
->vmax
, b
->vorder
, b
->dimension
, b
->ctlpoints
, b
->dimension
* b
->vorder
, b
->dimension
, u
, v
, ret
);
168 else if(b
->next
!= NULL
)
169 bezierPatchEvalNormal(b
->next
, u
,v
, ret
);
171 bezierSurfEvalNormal(b
->umin
, b
->umax
, b
->uorder
, b
->vmin
, b
->vmax
, b
->vorder
, b
->dimension
, b
->ctlpoints
, b
->dimension
* b
->vorder
, b
->dimension
, u
, v
, ret
);
175 void bezierPatchDraw(bezierPatch
*bpatch
, int u_reso
, int v_reso
)
177 if(bpatch
->dimension
== 3)
178 glMap2f(GL_MAP2_VERTEX_3
, bpatch
->umin
, bpatch
->umax
, 3*bpatch
->vorder
, bpatch
->uorder
, bpatch
->vmin
, bpatch
->vmax
,3, bpatch
->vorder
, (GLfloat
*) bpatch
->ctlpoints
);
180 glMap2f(GL_MAP2_VERTEX_4
, bpatch
->umin
, bpatch
->umax
, 4*bpatch
->vorder
, bpatch
->uorder
, bpatch
->vmin
, bpatch
->vmax
,3, bpatch
->vorder
, (GLfloat
*) bpatch
->ctlpoints
);
182 glMapGrid2f(u_reso
, bpatch
->umin
, bpatch
->umax
,
183 v_reso
, bpatch
->vmin
, bpatch
->vmax
);
184 glEvalMesh2(GL_LINE
, 0, u_reso
, 0, v_reso
);
187 void bezierPatchListDraw(bezierPatch
*list
, int u_reso
, int v_reso
)
190 glEnable(GL_LIGHTING
);
192 glEnable(GL_MAP2_VERTEX_3
);
193 glEnable(GL_AUTO_NORMAL
);
194 glEnable(GL_NORMALIZE
);
201 for(temp
= list
; temp
!= NULL
; temp
= temp
->next
)
202 bezierPatchDraw(temp
, u_reso
, v_reso
);