| 1 | /* $Id: bezierPatch.cpp,v 1.1 2000-02-09 08:49:00 jeroen Exp $ */
|
|---|
| 2 | /*
|
|---|
| 3 | ** License Applicability. Except to the extent portions of this file are
|
|---|
| 4 | ** made subject to an alternative license as permitted in the SGI Free
|
|---|
| 5 | ** Software License B, Version 1.0 (the "License"), the contents of this
|
|---|
| 6 | ** file are subject only to the provisions of the License. You may not use
|
|---|
| 7 | ** this file except in compliance with the License. You may obtain a copy
|
|---|
| 8 | ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
|
|---|
| 9 | ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
|
|---|
| 10 | **
|
|---|
| 11 | ** http://oss.sgi.com/projects/FreeB
|
|---|
| 12 | **
|
|---|
| 13 | ** Note that, as provided in the License, the Software is distributed on an
|
|---|
| 14 | ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
|
|---|
| 15 | ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
|
|---|
| 16 | ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
|
|---|
| 17 | ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
|
|---|
| 18 | **
|
|---|
| 19 | ** Original Code. The Original Code is: OpenGL Sample Implementation,
|
|---|
| 20 | ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
|
|---|
| 21 | ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
|
|---|
| 22 | ** Copyright in any portions created by third parties is as indicated
|
|---|
| 23 | ** elsewhere herein. All Rights Reserved.
|
|---|
| 24 | **
|
|---|
| 25 | ** Additional Notice Provisions: The application programming interfaces
|
|---|
| 26 | ** established by SGI in conjunction with the Original Code are The
|
|---|
| 27 | ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
|
|---|
| 28 | ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
|
|---|
| 29 | ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
|
|---|
| 30 | ** Window System(R) (Version 1.3), released October 19, 1998. This software
|
|---|
| 31 | ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
|
|---|
| 32 | ** published by SGI, but has not been independently verified as being
|
|---|
| 33 | ** compliant with the OpenGL(R) version 1.2.1 Specification.
|
|---|
| 34 | **
|
|---|
| 35 | ** $Date: 2000-02-09 08:49:00 $ $Revision: 1.1 $
|
|---|
| 36 | */
|
|---|
| 37 | /*
|
|---|
| 38 | ** $Header: /home/ktk/tmp/odin/2007/netlabs.cvs/odin32/src/opengl/glu/nurbs/interface/bezierPatch.cpp,v 1.1 2000-02-09 08:49:00 jeroen Exp $
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | #include "gluos.h"
|
|---|
| 42 | #include <stdlib.h>
|
|---|
| 43 | #include <stdio.h>
|
|---|
| 44 | #include <assert.h>
|
|---|
| 45 | #include "gl.h"
|
|---|
| 46 | #include "glu.h" /*for drawing bzier patch*/
|
|---|
| 47 | #include "bezierPatch.h"
|
|---|
| 48 | #include "bezierEval.h"
|
|---|
| 49 |
|
|---|
| 50 | /*
|
|---|
| 51 | *allocate an instance of bezierPatch. The control points are unknown. But
|
|---|
| 52 | *the space of this array is allocated with size of
|
|---|
| 53 | * uorder*vorder*dimension
|
|---|
| 54 | *
|
|---|
| 55 | */
|
|---|
| 56 | bezierPatch* bezierPatchMake(float umin, float vmin, float umax, float vmax, int uorder, int vorder, int dimension)
|
|---|
| 57 | {
|
|---|
| 58 | bezierPatch* ret = (bezierPatch*) malloc(sizeof(bezierPatch));
|
|---|
| 59 | assert(ret);
|
|---|
| 60 | ret->umin = umin;
|
|---|
| 61 | ret->vmin = vmin;
|
|---|
| 62 | ret->umax = umax;
|
|---|
| 63 | ret->vmax = vmax;
|
|---|
| 64 | ret->uorder = uorder;
|
|---|
| 65 | ret->vorder = vorder;
|
|---|
| 66 | ret->dimension = dimension;
|
|---|
| 67 | ret->ctlpoints = (float*) malloc(sizeof(float) * dimension * uorder * vorder);
|
|---|
| 68 | assert(ret->ctlpoints);
|
|---|
| 69 |
|
|---|
| 70 | ret->next = NULL;
|
|---|
| 71 |
|
|---|
| 72 | return ret;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | bezierPatch* bezierPatchMake2(float umin, float vmin, float umax, float vmax, int uorder, int vorder, int dimension, int ustride, int vstride, float* ctlpoints)
|
|---|
| 76 | {
|
|---|
| 77 | bezierPatch* ret = (bezierPatch*) malloc(sizeof(bezierPatch));
|
|---|
| 78 | assert(ret);
|
|---|
| 79 | ret->umin = umin;
|
|---|
| 80 | ret->vmin = vmin;
|
|---|
| 81 | ret->umax = umax;
|
|---|
| 82 | ret->vmax = vmax;
|
|---|
| 83 | ret->uorder = uorder;
|
|---|
| 84 | ret->vorder = vorder;
|
|---|
| 85 | ret->dimension = dimension;
|
|---|
| 86 | ret->ctlpoints = (float*) malloc(sizeof(float) * dimension * uorder * vorder);
|
|---|
| 87 | assert(ret->ctlpoints);
|
|---|
| 88 |
|
|---|
| 89 | /*copy the control points there*/
|
|---|
| 90 | int the_ustride = vorder * dimension;
|
|---|
| 91 | int the_vstride = dimension;
|
|---|
| 92 | for(int i=0; i<uorder; i++)
|
|---|
| 93 | for(int j=0; j<vorder; j++)
|
|---|
| 94 | for(int k=0; k<dimension; k++)
|
|---|
| 95 | ret->ctlpoints[i * the_ustride + j*the_vstride+k] = ctlpoints[i*ustride+j*vstride+k];
|
|---|
| 96 |
|
|---|
| 97 | ret->next = NULL;
|
|---|
| 98 |
|
|---|
| 99 | return ret;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /*
|
|---|
| 103 | *deallocate the space as allocated by Make
|
|---|
| 104 | */
|
|---|
| 105 | void bezierPatchDelete(bezierPatch *b)
|
|---|
| 106 | {
|
|---|
| 107 | free(b->ctlpoints);
|
|---|
| 108 | free(b);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /*delete the whole linked list
|
|---|
| 112 | */
|
|---|
| 113 | void bezierPatchDeleteList(bezierPatch *b)
|
|---|
| 114 | {
|
|---|
| 115 | bezierPatch *temp;
|
|---|
| 116 | for(temp = b; temp != NULL; temp = temp->next)
|
|---|
| 117 | bezierPatchDelete(temp);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | bezierPatch* bezierPatchInsert(bezierPatch *list, bezierPatch *b)
|
|---|
| 121 | {
|
|---|
| 122 | b->next = list;
|
|---|
| 123 | return b;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /*print the data stored in this patch*/
|
|---|
| 127 | void 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*/
|
|---|
| 136 | void bezierPatchPrintList(bezierPatch *list)
|
|---|
| 137 | {
|
|---|
| 138 | bezierPatch* temp;
|
|---|
| 139 | for(temp=list; temp != NULL; temp = temp->next)
|
|---|
| 140 | bezierPatchPrint(temp);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | void 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 | */
|
|---|
| 160 | void 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 |
|
|---|
| 176 | void 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 |
|
|---|
| 188 | void bezierPatchListDraw(bezierPatch *list, int u_reso, int v_reso)
|
|---|
| 189 | {
|
|---|
| 190 | bezierPatch *temp;
|
|---|
| 191 | glEnable(GL_LIGHTING);
|
|---|
| 192 | glEnable(GL_LIGHT0);
|
|---|
| 193 | glEnable(GL_MAP2_VERTEX_3);
|
|---|
| 194 | glEnable(GL_AUTO_NORMAL);
|
|---|
| 195 | glEnable(GL_NORMALIZE);
|
|---|
| 196 | glColor3f(1,0,0);
|
|---|
| 197 | #ifdef DEBUG
|
|---|
| 198 | printf("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 |
|
|---|
| 207 |
|
|---|