SGI SI GLU library
[mesa.git] / src / glu / sgi / libnurbs / interface / bezierPatch.cc
1 /*
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:
9 **
10 ** http://oss.sgi.com/projects/FreeB
11 **
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.
17 **
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.
23 **
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.
33 **
34 ** $Date: 2001/03/17 00:25:40 $ $Revision: 1.1 $
35 */
36 /*
37 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/interface/bezierPatch.cc,v 1.1 2001/03/17 00:25:40 brianp Exp $
38 */
39
40 #include "gluos.h"
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <assert.h>
44 #include <GL/glu.h> /*for drawing bzier patch*/
45 #include "bezierPatch.h"
46 #include "bezierEval.h"
47
48 /*
49 *allocate an instance of bezierPatch. The control points are unknown. But
50 *the space of this array is allocated with size of
51 * uorder*vorder*dimension
52 *
53 */
54 bezierPatch* bezierPatchMake(float umin, float vmin, float umax, float vmax, int uorder, int vorder, int dimension)
55 {
56 bezierPatch* ret = (bezierPatch*) malloc(sizeof(bezierPatch));
57 assert(ret);
58 ret->umin = umin;
59 ret->vmin = vmin;
60 ret->umax = umax;
61 ret->vmax = vmax;
62 ret->uorder = uorder;
63 ret->vorder = vorder;
64 ret->dimension = dimension;
65 ret->ctlpoints = (float*) malloc(sizeof(float) * dimension * uorder * vorder);
66 assert(ret->ctlpoints);
67
68 ret->next = NULL;
69
70 return ret;
71 }
72
73 bezierPatch* bezierPatchMake2(float umin, float vmin, float umax, float vmax, int uorder, int vorder, int dimension, int ustride, int vstride, float* ctlpoints)
74 {
75 bezierPatch* ret = (bezierPatch*) malloc(sizeof(bezierPatch));
76 assert(ret);
77 ret->umin = umin;
78 ret->vmin = vmin;
79 ret->umax = umax;
80 ret->vmax = vmax;
81 ret->uorder = uorder;
82 ret->vorder = vorder;
83 ret->dimension = dimension;
84 ret->ctlpoints = (float*) malloc(sizeof(float) * dimension * uorder * vorder);
85 assert(ret->ctlpoints);
86
87 /*copy the control points there*/
88 int the_ustride = vorder * dimension;
89 int the_vstride = dimension;
90 for(int i=0; i<uorder; i++)
91 for(int j=0; j<vorder; j++)
92 for(int k=0; k<dimension; k++)
93 ret->ctlpoints[i * the_ustride + j*the_vstride+k] = ctlpoints[i*ustride+j*vstride+k];
94
95 ret->next = NULL;
96
97 return ret;
98 }
99
100 /*
101 *deallocate the space as allocated by Make
102 */
103 void bezierPatchDelete(bezierPatch *b)
104 {
105 free(b->ctlpoints);
106 free(b);
107 }
108
109 /*delete the whole linked list
110 */
111 void bezierPatchDeleteList(bezierPatch *b)
112 {
113 bezierPatch *temp;
114 for(temp = b; temp != NULL; temp = temp->next)
115 bezierPatchDelete(temp);
116 }
117
118 bezierPatch* bezierPatchInsert(bezierPatch *list, bezierPatch *b)
119 {
120 b->next = list;
121 return b;
122 }
123
124 /*print the data stored in this patch*/
125 void bezierPatchPrint(bezierPatch *b)
126 {
127 printf("bezierPatch:\n");
128 printf("umin,umax=(%f,%f), (vmin, vmax)=(%f,%f)\n", b->umin, b->umax, b->vmin, b->vmax);
129 printf("uorder=%i, vorder=%i\n", b->uorder, b->vorder);
130 printf("idmension = %i\n", b->dimension);
131 }
132
133 /*print the whole list*/
134 void bezierPatchPrintList(bezierPatch *list)
135 {
136 bezierPatch* temp;
137 for(temp=list; temp != NULL; temp = temp->next)
138 bezierPatchPrint(temp);
139 }
140
141 void bezierPatchEval(bezierPatch *b, float u, float v, float ret[])
142 {
143 if( u >= b->umin && u<= b->umax
144 && v >= b->vmin && v<= b->vmax)
145 {
146
147 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);
148
149 }
150 else if(b->next != NULL)
151 bezierPatchEval(b->next, u,v, ret);
152 else
153 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);
154 }
155
156 /*the returned normal is normlized
157 */
158 void bezierPatchEvalNormal(bezierPatch *b, float u, float v, float ret[])
159 {
160 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);
161
162 if( u >= b->umin && u<= b->umax
163 && v >= b->vmin && v<= b->vmax)
164 {
165 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);
166 }
167 else if(b->next != NULL)
168 bezierPatchEvalNormal(b->next, u,v, ret);
169 else
170 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);
171
172 }
173
174 void bezierPatchDraw(bezierPatch *bpatch, int u_reso, int v_reso)
175 {
176 if(bpatch->dimension == 3)
177 glMap2f(GL_MAP2_VERTEX_3, bpatch->umin, bpatch->umax, 3*bpatch->vorder, bpatch->uorder, bpatch->vmin, bpatch->vmax,3, bpatch->vorder, (GLfloat*) bpatch->ctlpoints);
178 else
179 glMap2f(GL_MAP2_VERTEX_4, bpatch->umin, bpatch->umax, 4*bpatch->vorder, bpatch->uorder, bpatch->vmin, bpatch->vmax,3, bpatch->vorder, (GLfloat*) bpatch->ctlpoints);
180
181 glMapGrid2f(u_reso, bpatch->umin, bpatch->umax,
182 v_reso, bpatch->vmin, bpatch->vmax);
183 glEvalMesh2(GL_LINE, 0, u_reso, 0, v_reso);
184 }
185
186 void bezierPatchListDraw(bezierPatch *list, int u_reso, int v_reso)
187 {
188 bezierPatch *temp;
189 glEnable(GL_LIGHTING);
190 glEnable(GL_LIGHT0);
191 glEnable(GL_MAP2_VERTEX_3);
192 glEnable(GL_AUTO_NORMAL);
193 glEnable(GL_NORMALIZE);
194 glColor3f(1,0,0);
195 #ifdef DEBUG
196 printf("mapmap\n");
197 #endif
198
199
200 for(temp = list; temp != NULL; temp = temp->next)
201 bezierPatchDraw(temp, u_reso, v_reso);
202 }
203
204
205