//binds data to VBO buffers void BoundingBox::setupVBOs(){ HandleGLError("setup VBOs a "); VBOPos bb_verts[8]; VBOIndexedEdge bb_edges[12]; bb_verts[0] = VBOPos(Vec(minimum.x(),minimum.y(),minimum.z())); bb_verts[1] = VBOPos(Vec(minimum.x(),minimum.y(),maximum.z())); bb_verts[2] = VBOPos(Vec(minimum.x(),maximum.y(),minimum.z())); bb_verts[3] = VBOPos(Vec(minimum.x(),maximum.y(),maximum.z())); bb_verts[4] = VBOPos(Vec(maximum.x(),minimum.y(),minimum.z())); bb_verts[5] = VBOPos(Vec(maximum.x(),minimum.y(),maximum.z())); bb_verts[6] = VBOPos(Vec(maximum.x(),maximum.y(),minimum.z())); bb_verts[7] = VBOPos(Vec(maximum.x(),maximum.y(),maximum.z())); bb_edges[ 0] = VBOIndexedEdge(0,1); bb_edges[ 1] = VBOIndexedEdge(1,3); bb_edges[ 2] = VBOIndexedEdge(3,2); bb_edges[ 3] = VBOIndexedEdge(2,0); bb_edges[ 4] = VBOIndexedEdge(0,4); bb_edges[ 5] = VBOIndexedEdge(1,5); bb_edges[ 6] = VBOIndexedEdge(2,6); bb_edges[ 7] = VBOIndexedEdge(3,7); bb_edges[ 8] = VBOIndexedEdge(4,5); bb_edges[ 9] = VBOIndexedEdge(5,7); bb_edges[10] = VBOIndexedEdge(7,6); bb_edges[11] = VBOIndexedEdge(6,4); HandleGLError("setup VBOs b "); glBindBuffer(GL_ARRAY_BUFFER,bb_verts_VBO); HandleGLError("setup VBOs c "); glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPos)*8,bb_verts,GL_STATIC_DRAW); HandleGLError("setup VBOs d "); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,bb_edge_indices_VBO); HandleGLError("setup VBOs e "); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(VBOIndexedEdge)*12,bb_edges,GL_STATIC_DRAW); HandleGLError("setup VBOs bf "); }
void PhotonMapping::setupVBOs() { photon_verts.clear(); photon_direction_indices.clear(); kdtree_verts.clear(); kdtree_edge_indices.clear(); // initialize the data int dir_count = 0; int edge_count = 0; BoundingBox *bb = mesh->getBoundingBox(); double max_dim = bb->maxDim(); if (kdtree == NULL) return; std::vector<const KDTree*> todo; todo.push_back(kdtree); while (!todo.empty()) { const KDTree *node = todo.back(); todo.pop_back(); if (node->isLeaf()) { const std::vector<Photon> &photons = node->getPhotons(); int num_photons = photons.size(); for (int i = 0; i < num_photons; i++) { const Photon &p = photons[i]; Vec3f energy = p.getEnergy()*args->num_photons_to_shoot; const Vec3f &position = p.getPosition(); Vec3f other = position + p.getDirectionFrom()*0.02*max_dim; photon_verts.push_back(VBOPosColor(position,energy)); photon_verts.push_back(VBOPosColor(other,energy)); photon_direction_indices.push_back(VBOIndexedEdge(dir_count,dir_count+1)); dir_count+=2; } // initialize kdtree vbo const Vec3f& min = node->getMin(); const Vec3f& max = node->getMax(); kdtree_verts.push_back(VBOPos(Vec3f(min.x(),min.y(),min.z()))); kdtree_verts.push_back(VBOPos(Vec3f(min.x(),min.y(),max.z()))); kdtree_verts.push_back(VBOPos(Vec3f(min.x(),max.y(),min.z()))); kdtree_verts.push_back(VBOPos(Vec3f(min.x(),max.y(),max.z()))); kdtree_verts.push_back(VBOPos(Vec3f(max.x(),min.y(),min.z()))); kdtree_verts.push_back(VBOPos(Vec3f(max.x(),min.y(),max.z()))); kdtree_verts.push_back(VBOPos(Vec3f(max.x(),max.y(),min.z()))); kdtree_verts.push_back(VBOPos(Vec3f(max.x(),max.y(),max.z()))); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count ,edge_count+1)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+1,edge_count+3)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+3,edge_count+2)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+2,edge_count )); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+4,edge_count+5)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+5,edge_count+7)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+7,edge_count+6)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+6,edge_count+4)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count ,edge_count+4)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+1,edge_count+5)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+2,edge_count+6)); kdtree_edge_indices.push_back(VBOIndexedEdge(edge_count+3,edge_count+7)); edge_count += 8; } else { todo.push_back(node->getChild1()); todo.push_back(node->getChild2()); } } assert (2*photon_direction_indices.size() == photon_verts.size()); int num_directions = photon_direction_indices.size(); int num_edges = kdtree_edge_indices.size(); // cleanup old buffer data (if any) cleanupVBOs(); // copy the data to each VBO if (num_directions > 0) { glBindBuffer(GL_ARRAY_BUFFER,photon_verts_VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(VBOPosColor) * num_directions * 2, &photon_verts[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,photon_direction_indices_VBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(VBOIndexedEdge) * num_directions, &photon_direction_indices[0], GL_STATIC_DRAW); } if (num_edges > 0) { glBindBuffer(GL_ARRAY_BUFFER,kdtree_verts_VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(VBOPos) * num_edges * 2, &kdtree_verts[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,kdtree_edge_indices_VBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(VBOIndexedEdge) * num_edges, &kdtree_edge_indices[0], GL_STATIC_DRAW); } }
void Mesh::SetupLight(Vec3f light_position) { light_vert.push_back(VBOPos(light_position)); //light_vert.push_back(VBOPos(planeReflect(light_position, getMirrorCenter()))); glBindBuffer(GL_ARRAY_BUFFER,light_vert_VBO); glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPos)*2,&light_vert[0],GL_STATIC_DRAW); }
void Fluid::setupVBOs() { HandleGLError("in setup fluid VBOs"); fluid_particles.clear(); fluid_velocity_vis.clear(); fluid_face_velocity_vis.clear(); fluid_pressure_vis.clear(); fluid_cell_type_vis.clear(); // ===================================================================================== // setup the particles // ===================================================================================== for (int x = 0; x < nx; x++) { for (int y = 0; y < ny; y++) { for (int z = 0; z < nz; z++) { Cell *cell = getCell(x,y,z); std::vector<FluidParticle*> &particles = cell->getParticles(); for (unsigned int iter = 0; iter < particles.size(); iter++) { FluidParticle *p = particles[iter]; Vec3f v = p->getPosition(); fluid_particles.push_back(VBOPos(v)); } } } } // ===================================================================================== // visualize the velocity // ===================================================================================== if (args->dense_velocity == 0) { // one velocity vector per cell, at the centroid for (int i = 0; i < nx; i++) { for (int j = 0; j < ny; j++) { for (int k = 0; k < nz; k++) { Vec3f cell_center((i+0.5)*dx,(j+0.5)*dy,(k+0.5)*dz); Vec3f direction(get_u_avg(i,j,k),get_v_avg(i,j,k),get_w_avg(i,j,k)); Vec3f pt2 = cell_center+100*args->timestep*direction; fluid_velocity_vis.push_back(VBOPosColor(cell_center,Vec3f(1,0,0))); fluid_velocity_vis.push_back(VBOPosColor(pt2,Vec3f(1,1,1))); } } } } else if (args->dense_velocity == 1) { double z = nz*dz / 2.0; for (double x = 0; x <= (nx+0.01)*dx; x+=0.25*dx) { for (double y = 0; y <= (ny+0.01)*dy; y+=0.25*dy) { Vec3f vel = getInterpolatedVelocity(Vec3f(x,y,z)); Vec3f pt1(x,y,z); Vec3f pt2 = pt1 + 100*args->timestep*vel; fluid_velocity_vis.push_back(VBOPosColor(pt1,Vec3f(1,0,0))); fluid_velocity_vis.push_back(VBOPosColor(pt2,Vec3f(1,1,1))); } } } else if (args->dense_velocity == 2) { double y = ny*dy / 2.0; for (double x = 0; x <= (nx+0.01)*dx; x+=0.25*dx) { for (double z = 0; z <= (nz+0.01)*dz; z+=0.25*dz) { Vec3f vel = getInterpolatedVelocity(Vec3f(x,y,z)); Vec3f pt1(x,y,z); Vec3f pt2 = pt1 + 100*args->timestep*vel; fluid_velocity_vis.push_back(VBOPosColor(pt1,Vec3f(1,0,0))); fluid_velocity_vis.push_back(VBOPosColor(pt2,Vec3f(1,1,1))); } } } else if (args->dense_velocity == 3) { double x = nx*dx / 2.0; for (double y = 0; y <= (ny+0.01)*dy; y+=0.25*dy) { for (double z = 0; z <= (nz+0.01)*dz; z+=0.25*dz) { Vec3f vel = getInterpolatedVelocity(Vec3f(x,y,z)); Vec3f pt1(x,y,z); Vec3f pt2 = pt1 + 100*args->timestep*vel; fluid_velocity_vis.push_back(VBOPosColor(pt1,Vec3f(1,0,0))); fluid_velocity_vis.push_back(VBOPosColor(pt2,Vec3f(1,1,1))); } } } // ===================================================================================== // visualize the face velocity // render stubby triangles to visualize the u, v, and w velocities between cell faces // ===================================================================================== for (int i = 0; i < nx; i++) { for (int j = 0; j < ny; j++) { for (int k = 0; k < nz; k++) { double dt = args->timestep; double u = get_u_plus(i,j,k)*100*dt; double v = get_v_plus(i,j,k)*100*dt; double w = get_w_plus(i,j,k)*100*dt; double x = i*dx; double y = j*dy; double z = k*dz; if (u < -10*dt) { Vec3f pts[5] = { Vec3f(x+dx+u,y+0.5*dy,z+0.5*dz), Vec3f(x+dx,y+0.55*dy,z+0.55*dz), Vec3f(x+dx,y+0.55*dy,z+0.45*dz), Vec3f(x+dx,y+0.45*dy,z+0.45*dz), Vec3f(x+dx,y+0.45*dy,z+0.55*dz) }; setupConeVBO(pts,Vec3f(1,0,0),fluid_face_velocity_vis); } else if (u > 10*dt) { Vec3f pts[5] = { Vec3f(x+dx+u,y+0.5*dy,z+0.5*dz), Vec3f(x+dx,y+0.45*dy,z+0.45*dz), Vec3f(x+dx,y+0.55*dy,z+0.45*dz), Vec3f(x+dx,y+0.55*dy,z+0.55*dz), Vec3f(x+dx,y+0.45*dy,z+0.55*dz) }; setupConeVBO(pts,Vec3f(1,0,0),fluid_face_velocity_vis); } if (v < -10*dt) { Vec3f pts[5] = { Vec3f(x+0.5*dx,y+dy+v,z+0.5*dz), Vec3f(x+0.45*dx,y+dy,z+0.45*dz), Vec3f(x+0.55*dx,y+dy,z+0.45*dz), Vec3f(x+0.55*dx,y+dy,z+0.55*dz), Vec3f(x+0.45*dx,y+dy,z+0.55*dz) }; setupConeVBO(pts,Vec3f(0,1,0),fluid_face_velocity_vis); } else if (v > 10*dt) { Vec3f pts[5] = { Vec3f(x+0.5*dx,y+dy+v,z+0.5*dz), Vec3f(x+0.55*dx,y+dy,z+0.55*dz), Vec3f(x+0.55*dx,y+dy,z+0.45*dz), Vec3f(x+0.45*dx,y+dy,z+0.45*dz), Vec3f(x+0.45*dx,y+dy,z+0.55*dz) }; setupConeVBO(pts,Vec3f(0,1,0),fluid_face_velocity_vis); } if (w < -10*dt) { Vec3f pts[5] = { Vec3f(x+0.5*dx,y+0.5*dy,z+dz+w), Vec3f(x+0.55*dx,y+0.55*dy,z+dz), Vec3f(x+0.55*dx,y+0.45*dy,z+dz), Vec3f(x+0.45*dx,y+0.45*dy,z+dz), Vec3f(x+0.45*dx,y+0.55*dy,z+dz) }; setupConeVBO(pts,Vec3f(0,0,1),fluid_face_velocity_vis); } else if (w > 10*dt) { Vec3f pts[5] = { Vec3f(x+0.5*dx,y+0.5*dy,z+dz+w), Vec3f(x+0.45*dx,y+0.45*dy,z+dz), Vec3f(x+0.55*dx,y+0.45*dy,z+dz), Vec3f(x+0.55*dx,y+0.55*dy,z+dz), Vec3f(x+0.45*dx,y+0.55*dy,z+dz) }; setupConeVBO(pts,Vec3f(0,0,1),fluid_face_velocity_vis); } } } } // ===================================================================================== // visualize the cell pressure // ===================================================================================== for (int i = 0; i < nx; i++) { for (int j = 0; j < ny; j++) { for (int k = 0; k < nz; k++) { Vec3f pts[8] = { Vec3f((i+0.1)*dx,(j+0.1)*dy,(k+0.1)*dz), Vec3f((i+0.1)*dx,(j+0.1)*dy,(k+0.9)*dz), Vec3f((i+0.1)*dx,(j+0.9)*dy,(k+0.1)*dz), Vec3f((i+0.1)*dx,(j+0.9)*dy,(k+0.9)*dz), Vec3f((i+0.9)*dx,(j+0.1)*dy,(k+0.1)*dz), Vec3f((i+0.9)*dx,(j+0.1)*dy,(k+0.9)*dz), Vec3f((i+0.9)*dx,(j+0.9)*dy,(k+0.1)*dz), Vec3f((i+0.9)*dx,(j+0.9)*dy,(k+0.9)*dz) }; double p = getCell(i,j,k)->getPressure(); p *= 0.1; if (p > 1) p = 1; if (p < -1) p = -1; assert(p >= -1 && p <= 1); Vec3f color; if (p < 0) { color = Vec3f(1+p,1+p,1); } else { color = Vec3f(1,1-p,1-p); } setupCubeVBO(pts,color,fluid_pressure_vis); } } } // ===================================================================================== // render the MAC cells (FULL, SURFACE, or EMPTY) // ===================================================================================== for (int i = 0; i < nx; i++) { for (int j = 0; j < ny; j++) { for (int k = 0; k < nz; k++) { Vec3f pts[8] = { Vec3f((i+0.1)*dx,(j+0.1)*dy,(k+0.1)*dz), Vec3f((i+0.1)*dx,(j+0.1)*dy,(k+0.9)*dz), Vec3f((i+0.1)*dx,(j+0.9)*dy,(k+0.1)*dz), Vec3f((i+0.1)*dx,(j+0.9)*dy,(k+0.9)*dz), Vec3f((i+0.9)*dx,(j+0.1)*dy,(k+0.1)*dz), Vec3f((i+0.9)*dx,(j+0.1)*dy,(k+0.9)*dz), Vec3f((i+0.9)*dx,(j+0.9)*dy,(k+0.1)*dz), Vec3f((i+0.9)*dx,(j+0.9)*dy,(k+0.9)*dz) }; Cell *cell = getCell(i,j,k); Vec3f color; if (cell->getStatus() == CELL_FULL) { color = Vec3f(1,0,0); } else if (cell->getStatus() == CELL_SURFACE) { color=Vec3f(0,0,1); } else { continue; } setupCubeVBO(pts,color,fluid_cell_type_vis); } } } // cleanup old buffer data (if any) cleanupVBOs(); // copy the data to each VBO glBindBuffer(GL_ARRAY_BUFFER,fluid_particles_VBO); glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPos)*fluid_particles.size(),&fluid_particles[0],GL_STATIC_DRAW); if (fluid_velocity_vis.size() > 0) { glBindBuffer(GL_ARRAY_BUFFER,fluid_velocity_vis_VBO); glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPosColor)*fluid_velocity_vis.size(),&fluid_velocity_vis[0],GL_STATIC_DRAW); } if (fluid_face_velocity_vis.size() > 0) { glBindBuffer(GL_ARRAY_BUFFER,fluid_face_velocity_vis_VBO); glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPosNormalColor)*fluid_face_velocity_vis.size(),&fluid_face_velocity_vis[0],GL_STATIC_DRAW); } glBindBuffer(GL_ARRAY_BUFFER,fluid_pressure_vis_VBO); glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPosNormalColor)*fluid_pressure_vis.size(),&fluid_pressure_vis[0],GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER,fluid_cell_type_vis_VBO); glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPosNormalColor)*fluid_cell_type_vis.size(),&fluid_cell_type_vis[0],GL_STATIC_DRAW); HandleGLError("leaving setup fluid"); // ===================================================================================== // setup a marching cubes representation of the surface // ===================================================================================== for (int i = 0; i <= nx; i++) { for (int j = 0; j <= ny; j++) { for (int k = 0; k <= nz; k++) { marchingCubes->set(i,j,k,interpolateIsovalue(Vec3f((i-0.5),(j-0.5),(k-0.5)))); } } } marchingCubes->setupVBOs(); }
void Mesh::SetupLight(Vec3f light_position) { light_vert.push_back(VBOPos(light_position)); glBindBuffer(GL_ARRAY_BUFFER,light_vert_VBO); glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPos)*1,&light_vert[0],GL_STATIC_DRAW); }