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 #include <vector>
00026
00027 #include "WCreateColorArraysThread.h"
00028
00029
00030 WCreateColorArraysThread::WCreateColorArraysThread( int left, int right, boost::shared_ptr< std::vector< float > >vertices,
00031 boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
00032 boost::shared_ptr< std::vector< size_t > > lineLengths,
00033 boost::shared_ptr< std::vector< float > > globalColors,
00034 boost::shared_ptr< std::vector< float > > localColors,
00035 boost::shared_ptr< std::vector< float > > tangents ):
00036 WThreadedRunner(),
00037 m_myThreadFinished( false ),
00038 m_left( left ),
00039 m_right( right ),
00040 m_vertices( vertices ),
00041 m_tangents( tangents ),
00042 m_globalColors( globalColors ),
00043 m_localColors( localColors ),
00044 m_lineStartIndexes( lineStartIndexes ),
00045 m_lineLengths( lineLengths )
00046 {
00047 }
00048
00049 WCreateColorArraysThread::~WCreateColorArraysThread()
00050 {
00051 }
00052
00053 void WCreateColorArraysThread::threadMain()
00054 {
00055 if( !m_vertices || !m_tangents || !m_globalColors || !m_localColors || !m_lineStartIndexes || !m_lineLengths )
00056 {
00057 return;
00058 }
00059
00060 if( !m_vertices->size() ||
00061 !m_tangents->size() ||
00062 !m_globalColors->size() ||
00063 !m_localColors->size() ||
00064 !m_lineStartIndexes->size() ||
00065 !m_lineLengths->size() )
00066 {
00067 return;
00068 }
00069
00070 int pc = 0;
00071 for( int i = 0; i < m_left; ++i )
00072 {
00073 pc += (*m_lineLengths)[i]*3;
00074 }
00075
00076 float r, g, b, rr, gg, bb;
00077 float x1, x2, y1, y2, z1, z2;
00078 float lastx, lasty, lastz;
00079 for( int i = m_left; i <= m_right; ++i )
00080 {
00081 x1 = (*m_vertices)[pc];
00082 y1 = (*m_vertices)[pc + 1];
00083 z1 = (*m_vertices)[pc + 2];
00084 x2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 3 ];
00085 y2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 2 ];
00086 z2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 1 ];
00087 r = ( x1 ) - ( x2 );
00088 g = ( y1 ) - ( y2 );
00089 b = ( z1 ) - ( z2 );
00090 if( r < 0.0 )
00091 r *= -1.0;
00092 if( g < 0.0 )
00093 g *= -1.0;
00094 if( b < 0.0 )
00095 b *= -1.0;
00096
00097 float norm = sqrt( r * r + g * g + b * b );
00098 r *= 1.0 / norm;
00099 g *= 1.0 / norm;
00100 b *= 1.0 / norm;
00101
00102 lastx = (*m_vertices)[pc] + ( (*m_vertices)[pc] - (*m_vertices)[pc + 3] );
00103 lasty = (*m_vertices)[pc+ 1] + ( (*m_vertices)[pc + 1] - (*m_vertices)[pc + 4] );
00104 lastz = (*m_vertices)[pc + 2] + ( (*m_vertices)[pc + 2] - (*m_vertices)[pc + 5] );
00105
00106 for( size_t j = 0; j < m_lineLengths->at( i ); ++j )
00107 {
00108 rr = lastx - (*m_vertices)[pc];
00109 gg = lasty - (*m_vertices)[pc + 1];
00110 bb = lastz - (*m_vertices)[pc + 2];
00111 lastx = (*m_vertices)[pc];
00112 lasty = (*m_vertices)[pc + 1];
00113 lastz = (*m_vertices)[pc + 2];
00114
00115 float norm = sqrt( rr * rr + gg * gg + bb * bb );
00116 rr *= 1.0 / norm;
00117 gg *= 1.0 / norm;
00118 bb *= 1.0 / norm;
00119 (*m_tangents)[pc] = rr;
00120 (*m_tangents)[pc+1] = gg;
00121 (*m_tangents)[pc+2] = bb;
00122
00123 if( rr < 0.0 )
00124 rr *= -1.0;
00125 if( gg < 0.0 )
00126 gg *= -1.0;
00127 if( bb < 0.0 )
00128 bb *= -1.0;
00129
00130 (*m_localColors)[pc] = rr;
00131 (*m_localColors)[pc+1] = gg;
00132 (*m_localColors)[pc+2] = bb;
00133
00134 (*m_globalColors)[pc] = r;
00135 (*m_globalColors)[pc+1] = g;
00136 (*m_globalColors)[pc+2] = b;
00137 pc += 3;
00138 }
00139 }
00140
00141 m_myThreadFinished = true;
00142 }
00143
00144 bool WCreateColorArraysThread::isFinished()
00145 {
00146 return m_myThreadFinished;
00147 }