Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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
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