//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void PointSprites::onKeyPressEvent(KeyEvent* keyEvent)
{
    char character = keyEvent->character();
    if (character == '+' || character == L'-')
    {
        if (character == '+')   m_fovScale += 0.1;
        else                    m_fovScale -= 0.1;
        m_fovScale = Math::clamp(m_fovScale, 0.1, 4.0);
    }

    if (character == ',' || character == L'.')
    {
        if (character == ',')   m_nearPlane += 0.5;
        else                    m_nearPlane -= 0.5;
        m_nearPlane = Math::clamp(m_nearPlane, 0.0001, 200.0);
    }

    if (m_usePerspective)
    {
        double fovDeg = m_fovScale*40.0;
        m_camera->setProjectionAsPerspective(fovDeg, m_nearPlane, m_camera->farPlane());
        Trace::show("Perspective: FOV set to %.1f   NEAR set to %.1f", fovDeg, m_nearPlane);
    }
    else
    {
        BoundingBox bb = m_renderSequence->boundingBox();
        double height = m_fovScale*bb.extent().length();
        m_camera->setProjectionAsOrtho(height, m_nearPlane, m_camera->farPlane());
        Trace::show("Ortho: HEIGHT set to %.1f   NEAR set to %.1f", height, m_nearPlane);
    }

    keyEvent->setRequestedAction(REDRAW);
}
Example #2
0
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
bool SingleBox::onInitialize()
{
    ref<DrawableGeo> geo1 = new DrawableGeo;
    {
        GeometryBuilderFaceList builder;
        GeometryUtils::createBox(Vec3f(0,0,0), 2.0, 2.0, 2.0, &builder);

        ref<Vec3fArray> vertices = builder.vertices();
        ref<UIntArray> faceList = builder.faceList();

        geo1->setVertexArray(vertices.p());
        geo1->setFromFaceList(*faceList);
        geo1->computeNormals();
    }

    ref<Part> part1 = new Part;
    part1->setDrawable(geo1.p());

    ref<Effect> eff1 = new Effect;
    eff1->setRenderState(new RenderStateMaterial_FF(Color3f(1, 0, 0)));

    part1->setEffect(eff1.p());

    ref<ModelBasicList> myModel = new ModelBasicList;
    myModel->addPart(part1.p());
    myModel->updateBoundingBoxesRecursive();

//     BoundingBox b = myModel->boundingBox();
//     Trace::show("%f %f %f  %f %f %f", b.min().x(), b.min().y(), b.min().z(), b.max().x(), b.max().y(), b.max().z());

    m_renderSequence->rendering(0)->scene()->addModel(myModel.p());

    BoundingBox bb = m_renderSequence->boundingBox();
    if (bb.isValid())
    {
        m_camera->setProjectionAsOrtho(bb.extent().length(), m_camera->nearPlane(), m_camera->farPlane());
    }
       
    m_camera->setFromLookAt(Vec3d(0,0,5), Vec3d(0,0,0), Vec3d(0,1,0));

    return true;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
bool PointSprites::onInitialize()
{
    ref<ModelBasicList> model = new ModelBasicList;

    bool useShaders = true;

    {
        GeometryBuilderDrawableGeo builder;
        GeometryUtils::createSphere(1, 10, 10, &builder);
        ref<DrawableGeo> geo = builder.drawableGeo();

        ref<Effect> eff = new Effect;

        if (useShaders)
        {
            cvf::ShaderProgramGenerator gen("SimpleHeadlight", cvf::ShaderSourceProvider::instance());
            gen.configureStandardHeadlightColor();
            ref<ShaderProgram> prog = gen.generate();
            eff->setShaderProgram(prog.p());
            eff->setUniform(new UniformFloat("u_color", Color4f(Color3::YELLOW)));
        }
        else
        {
            eff->setRenderState(new RenderStateMaterial_FF(RenderStateMaterial_FF::PURE_YELLOW));
        }

        ref<Part> part = new Part;
        part->setDrawable(geo.p());
        part->setEffect(eff.p());

        model->addPart(part.p());
    }

    {
        ref<Vec3fArray> vertices = new Vec3fArray;
        vertices->reserve(10);
        vertices->add(Vec3f(0, 0, 0));
        vertices->add(Vec3f(-3, 0, 0));
        vertices->add(Vec3f(3, 0, 0));

        ref<UIntArray> indices = new UIntArray(vertices->size());
        indices->setConsecutive(0);

        ref<PrimitiveSetIndexedUInt> primSet = new PrimitiveSetIndexedUInt(PT_POINTS);
        primSet->setIndices(indices.p());

        ref<DrawableGeo> geo = new DrawableGeo;
        geo->setVertexArray(vertices.p());
        geo->addPrimitiveSet(primSet.p());

        ref<Effect> eff = new Effect;

        if (useShaders)
        {
            bool useTextureSprite = true;

            cvf::ShaderProgramGenerator gen("PointSprites", cvf::ShaderSourceProvider::instance());
            gen.addVertexCode(ShaderSourceRepository::vs_DistanceScaledPoints);
            
            if (useTextureSprite)  gen.addFragmentCode(ShaderSourceRepository::src_TextureFromPointCoord);
            else                   gen.addFragmentCode(ShaderSourceRepository::src_Color);
            gen.addFragmentCode(ShaderSourceRepository::fs_CenterLitSpherePoints);

            ref<cvf::ShaderProgram> prog = gen.generate();
            eff->setShaderProgram(prog.p());
            eff->setUniform(new UniformFloat("u_pointRadius", 1.0f));

            if (useTextureSprite)
            {
                ref<Texture> tex = createTexture();
                ref<Sampler> sampler = new Sampler;
                sampler->setMinFilter(Sampler::LINEAR);
                sampler->setMagFilter(Sampler::NEAREST);
                sampler->setWrapModeS(Sampler::REPEAT);
                sampler->setWrapModeT(Sampler::REPEAT);
                ref<RenderStateTextureBindings> texBind = new RenderStateTextureBindings(tex.p(), sampler.p(), "u_texture2D");
                eff->setRenderState(texBind.p());
            }
            else
            {
                eff->setUniform(new UniformFloat("u_color", Color4f(Color3::RED)));
            }

            ref<RenderStatePoint> point = new RenderStatePoint(RenderStatePoint::PROGRAM_SIZE);
            point->enablePointSprite(true);
            eff->setRenderState(point.p());
        }
        else
        {
            eff->setRenderState(new RenderStateLighting_FF(false));
            eff->setRenderState(new RenderStateMaterial_FF(RenderStateMaterial_FF::PURE_MAGENTA));

            ref<RenderStatePoint> point = new RenderStatePoint(RenderStatePoint::FIXED_SIZE);
            point->enablePointSprite(true);
            point->setSize(600.0f);
            eff->setRenderState(point.p());
        }

        ref<Part> part = new Part;
        part->setDrawable(geo.p());
        part->setEffect(eff.p());

        model->addPart(part.p());
    }

    model->updateBoundingBoxesRecursive();
    m_renderSequence->firstRendering()->scene()->addModel(model.p());

    BoundingBox bb = model->boundingBox();
    if (bb.isValid())
    {
        m_camera->fitView(bb, -Vec3d::Z_AXIS, Vec3d::Y_AXIS);
        if (m_usePerspective)
        {
            m_camera->setProjectionAsPerspective(m_fovScale*40.0, m_nearPlane, m_camera->farPlane());
        }
        else
        {
            m_camera->setProjectionAsOrtho(m_fovScale*bb.extent().length(), m_nearPlane, m_camera->farPlane());
        }
    }

    return true;
}