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