Ejemplo n.º 1
0
int main(int argc, char* argv[])
{
  osg::ref_ptr<osg::Geode> quad(build_quad());

  osg::ref_ptr<osg::Image> image = osgDB::readImageFile("examples/pattern.dds");
  osg::ref_ptr<osg::Texture2D> tex = new osg::Texture2D;
  tex->setImage(image.get());
  boost::shared_ptr<shade::osg::Texture> shade_tex(new shade::osg::Texture(tex.get()));

  boost::shared_ptr<shade::GLSLWrapper> state(new shade::osg::Wrapper(quad->getOrCreateStateSet()));

  boost::shared_ptr<shade::Program> shader_program(setup_shading(state, shade_tex));
  shader_program->compile();
  state->make_current();
  shader_program->upload();

  run_viewer(quad.get());
}
Ejemplo n.º 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();
}