Example #1
0
static collision sphere_collide_mesh_space(sphere s, vec3 v, cmesh* cm, mat4 world, mat3 world_normal, mat3 space, mat3 space_normal) {
  
  if ( !cm->is_leaf ) {
  
    plane div = cm->division;
    div = plane_transform(div, world, world_normal);
    div = plane_transform_space(div, space, space_normal);
  
         if ( sphere_swept_inside_plane(s, v, div)  ) { return sphere_collide_mesh_space(s, v, cm->back,  world, world_normal, space, space_normal); }
    else if ( sphere_swept_outside_plane(s, v, div) ) { return sphere_collide_mesh_space(s, v, cm->front, world, world_normal, space, space_normal); }
    else {
    
      collision c0 = sphere_collide_mesh_space(s, v, cm->back,  world, world_normal, space, space_normal);
      collision c1 = sphere_collide_mesh_space(s, v, cm->front, world, world_normal, space, space_normal);
      return collision_merge(c0, c1);
      
    }
  }
  
  collision col = collision_none();
  
  for (int i = 0; i < cm->triangles_num; i++) {
    ctri ct = cm->triangles[i];
    ct = ctri_transform(ct, world, world_normal);
    ct = ctri_transform_space(ct, space, space_normal);
    
    /* This does not work for some reason */
    //if (sphere_swept_outside_sphere(s, v, ct.bound)) continue;
    col = collision_merge(col, sphere_collide_ctri(s, v, ct));
  }
  
  return col;

}
Example #2
0
int main(int argc, char* argv[])
{
    osg::ref_ptr<osg::Group> root(new osg::Group);

    osg::ref_ptr<osg::Geode> sphere(build_sphere());
    root->addChild(sphere.get());

    osg::ref_ptr<osg::Group> satellites_geometry(build_satellites());
    osg::ref_ptr<osg::MatrixTransform> satellites(new osg::MatrixTransform);
    satellites->addChild(satellites_geometry.get());
    satellites->setUpdateCallback(new RotateCallback(osg::Vec3(0., 1., 0.), 0.001));
    root->addChild(satellites.get());

    osg::ref_ptr<osg::Geode> plane(build_quad());
    osg::ref_ptr<osg::MatrixTransform> plane_transform(new osg::MatrixTransform);
    plane_transform->addChild(plane.get());
    osg::Matrix plane_transform_matrix;
    plane_transform_matrix.makeTranslate(0., -2., 0.);
    plane_transform->setMatrix(plane_transform_matrix);
    root->addChild(plane_transform.get());

    osg::ref_ptr<osg::Light> light(new osg::Light);
    light->setLightNum(1);
    light->setPosition(osg::Vec4(0., 0., 0., 1.));
    osg::ref_ptr<osg::LightSource> light_source(new osg::LightSource);
    light_source->setLight(light.get());
    osg::ref_ptr<osg::MatrixTransform> transform(new osg::MatrixTransform);
    transform->addChild(light_source.get());
    osg::Matrix matrix;
    matrix.makeTranslate(osg::Vec3(10., 0., 0.));
    transform->setMatrix(matrix);
    root->addChild(transform.get());

    osg::ref_ptr<osg::Texture2D> tex = new osg::Texture2D;
    int env_width = 1024;
    int env_height = 1024;
    tex->setTextureSize(env_width, env_height);
    tex->setInternalFormat(GL_RGBA);
    tex->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
    tex->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
    boost::shared_ptr<shade::osg::Texture> shade_tex(new shade::osg::Texture(tex.get()));

    osg::ref_ptr<osg::Camera> env_camera = new osg::Camera;
    env_camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    env_camera->setClearColor(osg::Vec4(0.5, 0.5, 0.5, 1.));
    env_camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    env_camera->setViewport(0, 0, env_width, env_height);
    env_camera->setRenderOrder(osg::Camera::PRE_RENDER);
    env_camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT, osg::Camera::FRAME_BUFFER);
    env_camera->attach(osg::Camera::COLOR_BUFFER, tex, 0, 0, true);
    env_camera->addChild(sphere);
    env_camera->addChild(satellites);
    osg::ref_ptr<osg::StateSet> env_camera_state(env_camera->getOrCreateStateSet());
    osg::FrontFace* cf = new osg::FrontFace(osg::FrontFace::CLOCKWISE);
    env_camera_state->setAttributeAndModes(cf);

    root->addChild(env_camera);

    setup_plane_shading(plane->getOrCreateStateSet(), shade_tex);

    osg::ref_ptr<osg::TextureCubeMap> osg_cube_tex = new osg::TextureCubeMap;
    int cube_map_size = 512;
    osg_cube_tex->setTextureSize(cube_map_size, cube_map_size);
    osg_cube_tex->setInternalFormat(GL_RGBA);
    struct
    {
        osg::Vec3 center;
        osg::Vec3 up;
    } cube_cameras[] = {
        {osg::Vec3(+1., 0., 0.), osg::Vec3(0., -1., 0.)},
        {osg::Vec3(-1., 0., 0.), osg::Vec3(0., -1., 0.)},
        {osg::Vec3(0., +1., 0.), osg::Vec3(0., 0., +1.)},
        {osg::Vec3(0., -1., 0.), osg::Vec3(0., 0., +1.)},
        {osg::Vec3(0., 0., +1.), osg::Vec3(0., -1., 0.)},
        {osg::Vec3(0., 0., -1.), osg::Vec3(0., -1., 0.)},
    };
    for(int i = 0; i != 6; ++i)
    {
        osg::ref_ptr<osg::Camera> cm_camera = new osg::Camera;
        cm_camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        cm_camera->setClearColor(osg::Vec4(0.3, 0.3, 0.3, 1.));
        cm_camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        cm_camera->setViewport(0, 0, cube_map_size, cube_map_size);
        cm_camera->setRenderOrder(osg::Camera::PRE_RENDER);
        cm_camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT, osg::Camera::FRAME_BUFFER);
        cm_camera->attach(osg::Camera::COLOR_BUFFER, osg_cube_tex, 0, i, true);

        cm_camera->setProjectionMatrixAsPerspective(90., 1., 0.1, 10.);
        cm_camera->setViewMatrixAsLookAt(osg::Vec3(0., 0., 0.), cube_cameras[i].center, cube_cameras[i].up);

        cm_camera->addChild(satellites);
        root->addChild(cm_camera);
    }
    boost::shared_ptr<shade::osg::Texture> cube_tex(new shade::osg::Texture(osg_cube_tex.get()));

    setup_sphere_shading(sphere->getOrCreateStateSet(), cube_tex);

    osg::ref_ptr<osgViewer::Viewer> viewer(build_viewer(root.get()));

    env_camera->setDataVariance(osg::Object::DYNAMIC);
    env_camera->setUpdateCallback(new MirrorCamera(viewer->getCamera(), plane_transform_matrix));

    viewer->run();
}