📄 bezierpatchmesh.cc
字号:
/*** License Applicability. Except to the extent portions of this file are** made subject to an alternative license as permitted in the SGI Free** Software License B, Version 1.1 (the "License"), the contents of this** file are subject only to the provisions of the License. You may not use** this file except in compliance with the License. You may obtain a copy** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:** ** http://oss.sgi.com/projects/FreeB** ** Note that, as provided in the License, the Software is distributed on an** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.** ** Original Code. The Original Code is: OpenGL Sample Implementation,** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.** Copyright in any portions created by third parties is as indicated** elsewhere herein. All Rights Reserved.** ** Additional Notice Provisions: The application programming interfaces** established by SGI in conjunction with the Original Code are The** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X** Window System(R) (Version 1.3), released October 19, 1998. This software** was created using the OpenGL(R) version 1.2.1 Sample Implementation** published by SGI, but has not been independently verified as being** compliant with the OpenGL(R) version 1.2.1 Specification.**** $Date: 2001/03/17 00:25:40 $ $Revision: 1.1 $*//*** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/interface/bezierPatchMesh.cc,v 1.1 2001/03/17 00:25:40 brianp Exp $*/#include "gluos.h"#include <stdlib.h>#include <stdio.h>#include <assert.h>#include <GL/gl.h>#include "bezierEval.h"#include "bezierPatchMesh.h"static int isDegenerate(float A[2], float B[2], float C[2]);void drawStrips(float *vertex_array, float *normal_array, int *length_array, GLenum *type_array, int num_strips){ int i,j,k; k=0; /*k is the index of the first component of the current vertex*/ for(i=0; i<num_strips; i++) { glBegin(type_array[i]); for(j=0; j<length_array[i]; j++) { glNormal3fv(normal_array+k); glVertex3fv(vertex_array+k); k += 3; } glEnd(); }}void bezierPatchMeshListDelDeg(bezierPatchMesh* list){ bezierPatchMesh* temp; for(temp=list; temp != NULL; temp = temp->next) { bezierPatchMeshDelDeg(temp); }}void bezierPatchMeshListDelete(bezierPatchMesh *list){ if(list == NULL) return; bezierPatchMeshListDelete(list->next); bezierPatchMeshDelete(list); }bezierPatchMesh* bezierPatchMeshListReverse(bezierPatchMesh* list){ bezierPatchMesh* ret=NULL; bezierPatchMesh* temp; bezierPatchMesh* nextone; for(temp = list; temp != NULL; temp = nextone) { nextone = temp->next; ret=bezierPatchMeshListInsert(ret, temp); } return ret;}/*maptype is either GL_MAP2_VERTEX_3 or GL_MAP2_VERTEX_4 */bezierPatchMesh *bezierPatchMeshMake(int maptype, float umin, float umax, int ustride, int uorder, float vmin, float vmax, int vstride, int vorder, float *ctlpoints, int size_UVarray, int size_length_array){ int i,j,k; int dimension; int the_ustride; int the_vstride; bezierPatchMesh *ret = (bezierPatchMesh*) malloc(sizeof(bezierPatchMesh)); assert(ret); ret->bpatch = NULL; ret->bpatch_normal = NULL; ret->bpatch_color = NULL; ret->bpatch_texcoord = NULL; if(maptype == GL_MAP2_VERTEX_3) dimension = 3; else if (maptype==GL_MAP2_VERTEX_4) dimension = 4; else { fprintf(stderr, "error in inMap2f, maptype=%i is wrong, maptype,map is invalid\n", maptype); return NULL; } ret->bpatch = bezierPatchMake(umin, vmin, umax, vmax, uorder, vorder, dimension); /*copy the control points there*/ the_ustride = vorder * dimension; the_vstride = dimension; for(i=0; i<uorder; i++) for(j=0; j<vorder; j++) for(k=0; k<dimension; k++) ret->bpatch->ctlpoints[i * the_ustride + j*the_vstride+k] = ctlpoints[i*ustride+j*vstride+k]; ret->size_UVarray = size_UVarray; ret->size_length_array = size_length_array; ret->UVarray = (float*) malloc(sizeof(float) * size_UVarray); assert(ret->UVarray); ret->length_array = (int *)malloc(sizeof(int) * size_length_array); assert(ret->length_array); ret->type_array = (GLenum *)malloc(sizeof(GLenum) * size_length_array); assert(ret->type_array); ret->index_UVarray = 0; ret->index_length_array = 0; ret->vertex_array = NULL; ret->normal_array = NULL; ret->color_array = NULL; ret->texcoord_array = NULL; ret->next = NULL; return ret;}bezierPatchMesh *bezierPatchMeshMake2(int size_UVarray, int size_length_array){ bezierPatchMesh *ret = (bezierPatchMesh*) malloc(sizeof(bezierPatchMesh)); assert(ret); ret->bpatch = NULL; ret->bpatch_normal = NULL; ret->bpatch_color = NULL; ret->bpatch_texcoord = NULL; ret->size_UVarray = size_UVarray; ret->size_length_array = size_length_array; ret->UVarray = (float*) malloc(sizeof(float) * size_UVarray); assert(ret->UVarray); ret->length_array = (int *)malloc(sizeof(int) * size_length_array); assert(ret->length_array); ret->type_array = (GLenum *)malloc(sizeof(GLenum) * size_length_array); assert(ret->type_array); ret->index_UVarray = 0; ret->index_length_array = 0; ret->vertex_array = NULL; ret->normal_array = NULL; ret->color_array = NULL; ret->texcoord_array = NULL; ret->next = NULL; return ret;}void bezierPatchMeshPutPatch(bezierPatchMesh *bpm, int maptype, float umin, float umax, int ustride, int uorder, float vmin, float vmax, int vstride, int vorder, float *ctlpoints){ switch(maptype){ case GL_MAP2_VERTEX_3: bpm->bpatch = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 3, ustride, vstride, ctlpoints); break; case GL_MAP2_VERTEX_4: bpm->bpatch = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 4,ustride, vstride, ctlpoints ); break; case GL_MAP2_NORMAL: bpm->bpatch_normal = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 3, ustride, vstride, ctlpoints); break; case GL_MAP2_INDEX: bpm->bpatch_color = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 1, ustride, vstride, ctlpoints); break; case GL_MAP2_COLOR_4: bpm->bpatch_color = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 4, ustride, vstride, ctlpoints); break; case GL_MAP2_TEXTURE_COORD_1: bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 1, ustride, vstride, ctlpoints); break; case GL_MAP2_TEXTURE_COORD_2: bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 2, ustride, vstride, ctlpoints); break; case GL_MAP2_TEXTURE_COORD_3: bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 3, ustride, vstride, ctlpoints); break; case GL_MAP2_TEXTURE_COORD_4: bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 4, ustride, vstride, ctlpoints); break; default: fprintf(stderr, "error in bezierPatchMeshPutPatch, maptype=%i is wrong, maptype,map is invalid\n", maptype); }} /*delete everything including the arrays. So if you want to output the *pointers of the arrays, you should not use this function to deallocate space. *you should dealocate manually */void bezierPatchMeshDelete(bezierPatchMesh *bpm){ if(bpm->bpatch != NULL) bezierPatchDelete(bpm->bpatch); if(bpm->bpatch_normal != NULL) bezierPatchDelete(bpm->bpatch_normal); if(bpm->bpatch_color != NULL) bezierPatchDelete(bpm->bpatch_color); if(bpm->bpatch_texcoord != NULL) bezierPatchDelete(bpm->bpatch_texcoord); free(bpm->UVarray); free(bpm->length_array); free(bpm->vertex_array); free(bpm->normal_array); free(bpm->type_array); free(bpm);} /*begin a strip *type is the primitive type: */void bezierPatchMeshBeginStrip(bezierPatchMesh *bpm, GLenum type){ bpm->counter = 0; bpm->type = type;}/*signal the end of the current strip*/void bezierPatchMeshEndStrip(bezierPatchMesh *bpm){ int i; /*if there are no vertices in this strip, then nothing needs to be done*/ if(bpm->counter == 0) return; /*if the length_array is full, it should be expanded*/ if(bpm->index_length_array >= bpm->size_length_array) { int *temp = (int*) malloc(sizeof(int) * (bpm->size_length_array*2 + 1)); assert(temp); GLenum *temp_type = (GLenum*) malloc(sizeof(GLenum) * (bpm->size_length_array*2 + 1)); assert(temp_type); /*update the size*/ bpm->size_length_array = bpm->size_length_array*2 + 1; /*copy*/ for(i=0; i<bpm->index_length_array; i++) { temp[i] = bpm->length_array[i]; temp_type[i] = bpm->type_array[i]; } /*deallocate old array*/ free(bpm->length_array); free(bpm->type_array); /*point to the new array which is twice as bigger*/ bpm->length_array = temp; bpm->type_array = temp_type; } bpm->type_array[bpm->index_length_array] = bpm->type; bpm->length_array[bpm->index_length_array++] = bpm->counter;}/*insert (u,v) */void bezierPatchMeshInsertUV(bezierPatchMesh *bpm, float u, float v){ int i; /*if the UVarray is full, it should be expanded*/ if(bpm->index_UVarray+1 >= bpm->size_UVarray) { float *temp = (float*) malloc(sizeof(float) * (bpm->size_UVarray * 2 + 2)); assert(temp); /*update the size*/ bpm->size_UVarray = bpm->size_UVarray*2 + 2; /*copy*/ for(i=0; i<bpm->index_UVarray; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -