2012年7月20日 星期五

Ogre Compositor

main.cpp


#include "Ogre\ExampleApplication.h"
class CompositorListener1 : public Ogre::CompositorInstance::Listener
{
    public:
    CompositorListener1()
    {
      number = 125.0f;
    }
    void notifyMaterialRender(uint32 pass_id, MaterialPtr &mat)
    {
      mat->getBestTechnique()->getPass(pass_id)->getFragmentProgramParameters()->setNamedConstant("numpixels",number);
    }
    void setNumber(float num)
    {
        number = num;
    }
    float getNumber()
    {
        return number;
    }
    private:
    float number;
};

class NeoLastener : public Ogre::FrameListener
{
public:
    NeoLastener(Ogre::RenderWindow* win, CompositorListener1* listener)
    {
       _comListener = listener;
       size_t windowHan = 0;
       std::stringstream windowHanStr;
       win->getCustomAttribute("WINDOW",&windowHan);
       windowHanStr << windowHan;
       OIS::ParamList pl;
       pl.insert(std::make_pair(std::string("WINDOW"), windowHanStr.str()));
       _man = OIS::InputManager::createInputSystem(pl);
       _key = static_cast(_man->createInputObject(OIS::OISKeyboard,false));
    }
    bool frameStarted(const Ogre::FrameEvent &ent)
    {
        _key->capture();
        if(_key->isKeyDown(OIS::KC_SPACE))
        {
            return false;
        }
        if(_key->isKeyDown(OIS::KC_EQUALS))
        {
            float num = _comListener->getNumber();
            num++;
            _comListener->setNumber(num);
            std::cout << num << std::endl;
        }
        if(_key->isKeyDown(OIS::KC_MINUS))
        {
            float num = _comListener->getNumber();
            num--;
            _comListener->setNumber(num);
            std::cout << num << std::endl;
        }
        return true;
    }
private:
    OIS::InputManager* _man;
    OIS::Keyboard* _key;
     CompositorListener1* _comListener;
};
class Example : public ExampleApplication
{
public:
      void createScene()
      {
        Ogre::Entity* ent = mSceneMgr->createEntity("E1","Sinbad.mesh");
        mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
        Ogre::CompositorManager::getSingleton().addCompositor(mCamera->getViewport(),"Compositor1");
        Ogre::CompositorManager::getSingleton().setCompositorEnabled(mCamera->getViewport(),"Compositor1",true);
        Ogre::CompositorInstance* comp = Ogre::CompositorManager::getSingleton().getCompositorChain(mCamera->getViewport())->getCompositor("Compositor1");
        CompositorListener1* compListener = new CompositorListener1();
        comp->addListener(compListener);
       Ogre::FrameListener* FrameListener = new NeoLastener(mWindow,compListener);
       mRoot->addFrameListener(FrameListener);
    }

};
int main (void)
{
    Example app;
    app.go();
    return 0;
}


\media\materials\scripts\neo.compositor

compositor Compositor1
{ 
      technique 
        { 
                texture scene target_width target_height PF_R8G8B8 
                target scene 
                { 
                        input previous 
                } 
     
                target_output 
                { 
                        input none 
                        pass render_quad 
                        { 
                                material neo/Comp1
                                input 0 scene 
                        } 
                } 
        } 
} 

\media\materials\scripts\neo.material

fragment_program MyFragmentShader1 cg
{ 
    source neo.cg         
    entry_point MyFragmentShader1       
    profiles ps_1_1    arbfp1 
 default_params                                         
    {                 
      param_named numpixels float 50  
    } 
}

material neo/Comp1
{ 
    technique 
        { 
                pass 
                { 
    fragment_program_ref MyFragmentShader1       
                     { 
                     } 
       texture_unit 
                        { 
                        } 
                } 
        } 
}

\media\materials\programs\neo.cg

void MyFragmentShader1(float2 uv : TEXCOORD0,out float4 color: COLOR,uniform sampler2D texture,  uniform float numpixels)
{  
float num = numpixels; 
float stepsize = 1.0/ num; 
float2 fragment = float2(stepsize * floor(uv.x * num),stepsize *    floor(uv.y * num)); 
color = tex2D(texture, fragment); 
}

meterial還能加vertex

vertex_program MyVertexShader1 cg 
{
    source neo.cg     
    entry_point MyVertexShader1       
    profiles vs_1_1 arbvp1                                           
    default_params                                         
    {                 
        param_named_auto worldViewMatrix worldviewproj_matrix   
 param_named_auto pulseTime time        
    }  
}
material neo/Comp1
{ 
    technique 
        { 
                pass 
                { 
                     fragment_program_ref MyFragmentShader1 
                     { 
                     } 
                    vertex_program_ref MyVertexShader1
                    { 
                    } 
                    texture_unit 
                        { 
                        } 
                } 
        } 
}

cg部分vertex加

void MyVertexShader1( uniform float pulseTime, float4 position : POSITION, out float4 oPosition : POSITION, float2 uv : TEXCOORD0,out float2 oUv : TEXCOORD0,uniform float4x4 worldViewMatrix) 
{ 
       oPosition = mul(worldViewMatrix, position); 
    oUv = uv;
    oPosition.x *= (2+sin(pulseTime)); 
}

灰階合成器neo.cg

void MyFragmentShader1(float2 uv : TEXCOORD0,out float4 color: COLOR,uniform sampler2D texture)
{  
 float4 temp_color = tex2D(texture,uv);  
 float greyvalue = temp_color.r * 0.3 + temp_color.g * 0.59 + temp_color.b * 0.11; 
 color = float4( greyvalue,  greyvalue,greyvalue,0); 
} 

沒有留言:

張貼留言