Particles(GLuint n) : count(n) , sort_nw(n) { Context gl; const GLfloat irm = 1.0f/RAND_MAX; std::vector<GLfloat> pos_data(count*3); for(GLuint p=0; p!=count; ++p) { for(GLuint c=0; c!=3; ++c) { pos_data[p*3+c] = 2.0f*(0.5f-std::rand()*irm); } } gl.Bound(BufferTarget::Array, positions).Data(pos_data); gl.Bound(BufferTarget::Uniform, distances).Data<GLfloat>( count, nullptr, BufferUsage::DynamicDraw ); gl.Bound(BufferTarget::CopyRead, indices_f).Data<GLuint>( count, nullptr, BufferUsage::DynamicRead ); gl.Bound(BufferTarget::CopyWrite,indices_d).Data<GLuint>( count, nullptr, BufferUsage::DynamicDraw ); }
void cMeshUtil::BuildDiskMesh(int slices, std::unique_ptr<cDrawMesh>& out_mesh) { const float r = 1; const int num_verts = 2 + slices; const int pos_size = 3; const int norm_size = 3; const int coord_size = 2; std::vector<float> pos_data(num_verts * pos_size); std::vector<float> norm_data(num_verts * norm_size); std::vector<float> coord_data(num_verts * coord_size); std::vector<int> idx_data(num_verts); pos_data[0] = 0; pos_data[1] = 0; pos_data[2] = 0; norm_data[0] = 0; norm_data[1] = 0; norm_data[2] = 1; coord_data[0] = 0; coord_data[1] = 0; for (int i = 0; i <= slices; ++i) { float theta = (i * 2 * M_PI) / slices; int pos_offset = (i + 1) * pos_size; int norm_offset = (i + 1) * norm_size; int coord_offset = (i + 1) * coord_size; pos_data[pos_offset + 0] = std::cos(theta); pos_data[pos_offset + 1] = std::sin(theta); pos_data[pos_offset + 2] = 0; norm_data[norm_offset + 0] = 0; norm_data[norm_offset + 1] = 0; norm_data[norm_offset + 2] = 1; coord_data[coord_offset + 0] = 0; coord_data[coord_offset + 1] = 0; } for (int i = 0; i < static_cast<int>(idx_data.size()); ++i) { idx_data[i] = i; } out_mesh = std::unique_ptr<cDrawMesh>(new cDrawMesh()); BuildDrawMesh(pos_data.data(), static_cast<int>(pos_data.size()), norm_data.data(), static_cast<int>(norm_data.size()), coord_data.data(), static_cast<int>(coord_data.size()), idx_data.data(), static_cast<int>(idx_data.size()), out_mesh.get()); }
bool IntersenseAPI::sample() { // If we are not active, then don't try to sample. if (!isActive()) { return false; } // Check to see if we have new data to pull if ( ! mTracker.updateData() ) { vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL) << clrOutBOLD(clrRED, "[gadget::IntersenseAPI::sample()]") << ": Could not read data from InterSense API driver!\n" << vprDEBUG_FLUSH; return false; } // This is the some code for the beginnings of trying to eliminate // the sleep in the control loop. Needs more testing. /* bool has_new_data(false); for ( unsigned int i = 0 ; i < mStations.size() ; ++i ) { // Make sure station is enabled and tracker has updated data. if( mStations[i].enabled && mTracker.hasData(mStations[i].stationIndex) ) { has_new_data = true; break; } } // If there wasn't any new data then reliquish control to the cpu and return if( ! has_new_data ) { vpr::Thread::yield(); vpr::System::msleep(10); } */ // Create the data buffers to put the new data into. std::vector<gadget::PositionData> cur_pos_samples(mStations.size()); std::vector<gadget::DigitalData> cur_digital_samples; std::vector<gadget::AnalogData> cur_analog_samples; // get an initial timestamp for this entire sample. we'll copy it into // each PositionData for this sample. if ( ! cur_pos_samples.empty() ) { cur_pos_samples[0].setTime(); } for ( unsigned int i = 0 ; i < mStations.size() ; ++i ) { // Get the station index for the given station. int stationIndex = mStations[i].stationIndex; // Set the time of each PositionData to match the first. cur_pos_samples[i].setTime( cur_pos_samples[0].getTime() ); // Don't process data from disabled stations if( ! mStations[i].enabled ) { continue; } gmtl::Matrix44f& pos_data(cur_pos_samples[i].editValue()); gmtl::identity(pos_data); // If the Intersense is returning data in Euler format. Otherwise we // assume that it is returning data in quaternion format. if ( mTracker.getAngleFormat(stationIndex) == ISD_EULER ) { gmtl::EulerAngleZYXf euler( gmtl::Math::deg2Rad(mTracker.zRot(stationIndex)), gmtl::Math::deg2Rad(mTracker.yRot(stationIndex)), gmtl::Math::deg2Rad(mTracker.xRot(stationIndex)) ); gmtl::setRot(pos_data, euler); } else { gmtl::Quatf quatValue(mTracker.xQuat(stationIndex), mTracker.yQuat(stationIndex), mTracker.zQuat(stationIndex), mTracker.wQuat(stationIndex)); gmtl::setRot(pos_data, quatValue); } gmtl::setTrans(pos_data, gmtl::Vec3f(mTracker.xPos(stationIndex), mTracker.yPos(stationIndex), mTracker.zPos(stationIndex))); // We start at the index of the first digital item (set in the config // files) and we copy the digital data from this station to the // InterSense device for range (min -> min+count-1). if (mStations[i].useDigital) { for ( int j = 0; j < mStations[i].dig_num; ++j ) { DigitalData new_digital(mTracker.buttonState(stationIndex, j)); new_digital.setTime(); cur_digital_samples.push_back(new_digital); } } // Analog works the same as the digital if (mStations[i].useAnalog) { for ( int j = 0; j < mStations[i].ana_num; ++j ) { AnalogData new_analog(mTracker.analogData(stationIndex, j)); new_analog.setTime(); cur_analog_samples.push_back(new_analog); } } } // Lock and then swap the buffers. addAnalogSample(cur_analog_samples); addDigitalSample(cur_digital_samples); addPositionSample(cur_pos_samples); return true; }
void cMeshUtil::BuildCylinder(int slices, std::unique_ptr<cDrawMesh>& out_mesh) { const float r = 1.f; const float h = 1.f; const int num_verts = 12 * slices; const int pos_size = 3; const int norm_size = 3; const int coord_size = 2; std::vector<float> pos_data(num_verts * pos_size); std::vector<float> norm_data(num_verts * norm_size); std::vector<float> coord_data(num_verts * coord_size); std::vector<int> idx_data(num_verts); for (int i = 0; i < slices; ++i) { double theta0 = (i * 2 * M_PI) / slices; double theta1 = ((i + 1) * 2 * M_PI) / slices; double x0 = r * std::cos(theta0); double z0 = r * std::sin(-theta0); double u0 = static_cast<float>(i) / slices; double x1 = r * std::cos(theta1); double z1 = r * std::sin(-theta1); double u1 = static_cast<float>(i + 1) / slices; tVector n0 = tVector(x0, 0, z0, 0).normalized(); tVector n1 = tVector(x1, 0, z1, 0).normalized(); int pos_offset = i * 12 * pos_size; int norm_offset = i * 12 * norm_size; int coord_offset = i * 12 * coord_size; pos_data[pos_offset] = x0; pos_data[pos_offset + 1] = -0.5 * h; pos_data[pos_offset + 2] = z0; pos_data[pos_offset + 3] = x1; pos_data[pos_offset + 4] = -0.5 * h; pos_data[pos_offset + 5] = z1; pos_data[pos_offset + 6] = x1; pos_data[pos_offset + 7] = 0.5 * h; pos_data[pos_offset + 8] = z1; pos_data[pos_offset + 9] = x1; pos_data[pos_offset + 10] = 0.5 * h; pos_data[pos_offset + 11] = z1; pos_data[pos_offset + 12] = x0; pos_data[pos_offset + 13] = 0.5 * h; pos_data[pos_offset + 14] = z0; pos_data[pos_offset + 15] = x0; pos_data[pos_offset + 16] = -0.5 * h; pos_data[pos_offset + 17] = z0; norm_data[norm_offset] = n0[0]; norm_data[norm_offset + 1] = n0[1]; norm_data[norm_offset + 2] = n0[2]; norm_data[norm_offset + 3] = n1[0]; norm_data[norm_offset + 4] = n1[1]; norm_data[norm_offset + 5] = n1[2]; norm_data[norm_offset + 6] = n1[0]; norm_data[norm_offset + 7] = n1[1]; norm_data[norm_offset + 8] = n1[2]; norm_data[norm_offset + 9] = n1[0]; norm_data[norm_offset + 10] = n1[1]; norm_data[norm_offset + 11] = n1[2]; norm_data[norm_offset + 12] = n1[0]; norm_data[norm_offset + 13] = n1[1]; norm_data[norm_offset + 14] = n1[2]; norm_data[norm_offset + 15] = n0[0]; norm_data[norm_offset + 16] = n0[1]; norm_data[norm_offset + 17] = n0[2]; coord_data[coord_offset] = u0; coord_data[coord_offset + 1] = 0.f; coord_data[coord_offset + 2] = u1; coord_data[coord_offset + 3] = 0.f; coord_data[coord_offset + 4] = u1; coord_data[coord_offset + 5] = 1; coord_data[coord_offset + 6] = u1; coord_data[coord_offset + 7] = 1.f; coord_data[coord_offset + 8] = u0; coord_data[coord_offset + 9] = 1.f; coord_data[coord_offset + 10] = u0; coord_data[coord_offset + 11] = 0.f; pos_data[pos_offset + 18] = x0; pos_data[pos_offset + 19] = 0.5 * h; pos_data[pos_offset + 20] = z0; pos_data[pos_offset + 21] = x1; pos_data[pos_offset + 22] = 0.5 * h; pos_data[pos_offset + 23] = z1; pos_data[pos_offset + 24] = 0.f; pos_data[pos_offset + 25] = 0.5 * h; pos_data[pos_offset + 26] = 0.f; pos_data[pos_offset + 27] = 0.f; pos_data[pos_offset + 28] = -0.5 * h; pos_data[pos_offset + 29] = 0.f; pos_data[pos_offset + 30] = x1; pos_data[pos_offset + 31] = -0.5 * h; pos_data[pos_offset + 32] = z1; pos_data[pos_offset + 33] = x0; pos_data[pos_offset + 34] = -0.5 * h; pos_data[pos_offset + 35] = z0; norm_data[norm_offset + 18] = 0.f; norm_data[norm_offset + 19] = 1.f; norm_data[norm_offset + 20] = 0.f; norm_data[norm_offset + 21] = 0.f; norm_data[norm_offset + 22] = 1.f; norm_data[norm_offset + 23] = 0.f; norm_data[norm_offset + 24] = 0.f; norm_data[norm_offset + 25] = 1.f; norm_data[norm_offset + 26] = 0.f; norm_data[norm_offset + 27] = 0.f; norm_data[norm_offset + 28] = -1.f; norm_data[norm_offset + 29] = 0.f; norm_data[norm_offset + 30] = 0.f; norm_data[norm_offset + 31] = -1.f; norm_data[norm_offset + 32] = 0.f; norm_data[norm_offset + 33] = 0.f; norm_data[norm_offset + 34] = -1.f; norm_data[norm_offset + 35] = 0.f; coord_data[coord_offset + 12] = u0; coord_data[coord_offset + 13] = 1.f; coord_data[coord_offset + 14] = u1; coord_data[coord_offset + 15] = 1.f; coord_data[coord_offset + 16] = u1; coord_data[coord_offset + 17] = 1.f; coord_data[coord_offset + 18] = u0; coord_data[coord_offset + 19] = 0.f; coord_data[coord_offset + 20] = u1; coord_data[coord_offset + 21] = 0.f; coord_data[coord_offset + 22] = u0; coord_data[coord_offset + 23] = 0.f; } for (int i = 0; i < static_cast<int>(idx_data.size()); ++i) { idx_data[i] = i; } out_mesh = std::unique_ptr<cDrawMesh>(new cDrawMesh()); BuildDrawMesh(pos_data.data(), static_cast<int>(pos_data.size()), norm_data.data(), static_cast<int>(norm_data.size()), coord_data.data(), static_cast<int>(coord_data.size()), idx_data.data(), static_cast<int>(idx_data.size()), out_mesh.get()); }
Field(const images::Image& map, const Program& prog) : gl() { const GLuint w = map.Width(); const GLuint h = map.Height(); const GLuint d = map.Depth(); const GLuint c = 3; radius = std::sqrt(GLfloat(w*w+h*h+d*d))*0.5f; vert_count = w*h*d; std::vector<GLfloat> pos_data(w*h*d*c); std::vector<GLuint> tec_data(w*h*d*c); GLfloat xo = w * 0.5f; GLfloat yo = h * 0.5f; GLfloat zo = d * 0.5f; for(GLuint z=0; z!=d; ++z) { for(GLuint y=0; y!=h; ++y) { for(GLuint x=0; x!=w; ++x) { GLuint k = z*w*h*c+y*w*c+x*c; pos_data[k+0] = x-xo; pos_data[k+1] = y-yo; pos_data[k+2] = z-zo; tec_data[k+0] = x; tec_data[k+1] = y; tec_data[k+2] = z; } } } positions.Data(pos_data); texcoords.Data(tec_data); DSAVertexArrayAttribEXT(vao, prog, "Position") .Setup<Vector<GLfloat, 3>>(positions) .Enable(); DSAVertexArrayAttribEXT(vao, prog, "TexCoord") .Setup<Vector<GLuint, 3>>(texcoords) .Enable(); ProgramUniformSampler(prog, "Pattern").Set(1); pattern.BindMulti(1, Texture::Target::_3D); pattern.Image3D(map); pattern.MinFilter(TextureMinFilter::Nearest); pattern.MagFilter(TextureMagFilter::Nearest); pattern.WrapS(TextureWrap::ClampToBorder); pattern.WrapT(TextureWrap::ClampToBorder); pattern.WrapR(TextureWrap::ClampToBorder); pattern.BorderColor(Vec4f(0,0,0,1)); ProgramUniformSampler(prog, "FadeMap").Set(2); fademap.BindMulti(2, Texture::Target::_3D); fademap.Image3D(images::RandomRedUByte( map.Width(), map.Height(), map.Depth() )); fademap.MinFilter(TextureMinFilter::Nearest); fademap.MagFilter(TextureMagFilter::Nearest); fademap.WrapS(TextureWrap::ClampToBorder); fademap.WrapT(TextureWrap::ClampToBorder); fademap.WrapR(TextureWrap::ClampToBorder); fademap.BorderColor(Vec4f(0,0,0,1)); }