Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2010, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00029 /* */ 00030 /**************************************************************************************/ 00031 00032 #include <vlGraphics/BezierSurface.hpp> 00033 00034 using namespace vl; 00035 00036 //----------------------------------------------------------------------------- 00070 void BezierPatch::resize(int x, int y) 00071 { 00072 if ( ((x-1)/3)*3+1 != x || ((y-1)/3)*3+1 != y ) 00073 { 00074 vl::Log::error("BezierPatch::resize(): illegal patch dimensions.\n"); 00075 mControlPoints.clear(); 00076 // in debug mode stop here 00077 VL_CHECK( ((x-1)/3)*3+1 == x ) 00078 VL_CHECK( ((y-1)/3)*3+1 == y ) 00079 return; 00080 } 00081 00082 mX = x; 00083 mY = y; 00084 mControlPoints.resize(mX*mY); 00085 } 00086 //----------------------------------------------------------------------------- 00087 void BezierSurface::updateBezierSurface(bool gen_tex_coords) 00088 { 00089 int patch_count = 0; 00090 for(unsigned ipatch=0; ipatch<patches().size(); ++ipatch) 00091 patch_count += ((patches()[ipatch]->x()-1)/3)*((patches()[ipatch]->y()-1)/3); 00092 00093 ref<ArrayFloat3> vert_array = cast<ArrayFloat3>(vertexArray()); 00094 if (!vert_array) 00095 { 00096 vert_array = new ArrayFloat3; 00097 setVertexArray(vert_array.get()); 00098 } 00099 vert_array->resize(detail()*detail()*patch_count); 00100 vert_array->setBufferObjectDirty(); 00101 00102 ref<ArrayFloat2> texc_array = cast<ArrayFloat2>(texCoordArray(0)); 00103 if ( gen_tex_coords ) 00104 { 00105 if (!texc_array) 00106 { 00107 texc_array = new ArrayFloat2; 00108 setTexCoordArray(0,texc_array.get()); 00109 } 00110 texc_array->resize(detail()*detail()*patch_count); 00111 texc_array->setBufferObjectDirty(); 00112 } 00113 00114 ref<DrawElementsUInt> de = drawCalls()->size() == 1 ? cast<DrawElementsUInt>(drawCalls()->at(0)) : NULL; 00115 if (!de) 00116 { 00117 drawCalls()->clear(); 00118 de = new DrawElementsUInt(PT_QUADS); 00119 drawCalls()->push_back(de.get()); 00120 } 00121 de->indexBuffer()->resize((detail()-1)*(detail()-1)*4*patch_count); 00122 de->indexBuffer()->setBufferObjectDirty(); 00123 00124 int ivert = 0; 00125 int iquad = 0; 00126 for(unsigned ipatch=0, patch_num=0; ipatch<patches().size(); ++ipatch) 00127 { 00128 const BezierPatch* p = patches()[ipatch].get(); 00129 for(int ix=0; ix<p->x()-3; ix+=3) 00130 for(int iy=0; iy<p->y()-3; iy+=3) 00131 { 00132 for(unsigned y=0; y<detail(); ++y) 00133 { 00134 // A B C D 00135 // ^ ^ ^ ^ 00136 // +---+---+---+ 00137 // | | | | 00138 // +---+---+---+ 00139 // | | | | 00140 // +---+---+---+ 00141 // | | | | 00142 // +---+---+---+ 00143 real v = (real)y/(detail()-1); 00144 real ty = 1.0f - v; 00145 real ty1 = 1.0f - ty; 00146 real k0 = ty*ty*ty; 00147 real k1 = 3*ty*ty*ty1; 00148 real k2 = 3*ty*ty1*ty1; 00149 real k3 = ty1*ty1*ty1; 00150 vec3 A = p->at(ix+0,iy+0)*k0 + p->at(ix+0,iy+1)*k1 + p->at(ix+0,iy+2)*k2 + p->at(ix+0,iy+3)*k3; 00151 vec3 B = p->at(ix+1,iy+0)*k0 + p->at(ix+1,iy+1)*k1 + p->at(ix+1,iy+2)*k2 + p->at(ix+1,iy+3)*k3; 00152 vec3 C = p->at(ix+2,iy+0)*k0 + p->at(ix+2,iy+1)*k1 + p->at(ix+2,iy+2)*k2 + p->at(ix+2,iy+3)*k3; 00153 vec3 D = p->at(ix+3,iy+0)*k0 + p->at(ix+3,iy+1)*k1 + p->at(ix+3,iy+2)*k2 + p->at(ix+3,iy+3)*k3; 00154 for(unsigned x=0; x<detail(); ++x, ++ivert) 00155 { 00156 real u = (real)x/(detail()-1); 00157 real tx = 1.0f - u; 00158 real tx1 = 1.0f - tx; 00159 vert_array->at(ivert) = (fvec3)(A*tx*tx*tx + B*3*tx*tx*tx1 + C*3*tx*tx1*tx1 + D*tx1*tx1*tx1); 00160 if(gen_tex_coords) 00161 { 00162 texc_array->at(ivert).x() = (float)u; 00163 texc_array->at(ivert).y() = (float)v; 00164 } 00165 } 00166 } 00167 00168 int istart = detail()*detail()*patch_num; 00169 for(unsigned y=0; y<detail()-1; ++y) 00170 { 00171 for(unsigned x=0; x<detail()-1; ++x) 00172 { 00173 de->indexBuffer()->at(iquad++) = istart + y *detail() + x; 00174 de->indexBuffer()->at(iquad++) = istart + y *detail() + x+1; 00175 de->indexBuffer()->at(iquad++) = istart + (y+1)*detail() + x+1; 00176 de->indexBuffer()->at(iquad++) = istart + (y+1)*detail() + x; 00177 } 00178 } 00179 ++patch_num; 00180 } 00181 } 00182 00183 #if defined(VL_OPENGL_ES1) || defined(VL_OPENGL_ES2) 00184 this->makeGLESFriendly(); 00185 #endif 00186 } 00187 //-----------------------------------------------------------------------------