Frederic Bouvier wrote:
> 
> > // load texture as an image
> > imgTexture = osgDB::readImageFile("model/BAKE_OVERRIDE.jpg");
> > // if the image is successfully loaded
> > if (imgTexture)
> > {
> > imgTexture->allocateImage(3500, 3500, 1, GL_RGBA,
> > GL_UNSIGNED_BYTE);
> > 
> 
> Why are you reallocating space for an image loaded from file ?
> Shouldn't the texture dimension be a power of 2 ?
> 

Ok, it is a mistake. The model I load (obj) has linked a texture that is loaded 
automatically.
I want to paint in another texture (size like models texture) in fragment 
shader....
I am following the example of osgMultiplerendertargets ... but I can not get 
good textures ...

Code:
osg::Group *scene = new osg::Group();
    modelTransf = new osg::PositionAttitudeTransform();
    node = osgDB::readNodeFile("model\\file.obj");
    node->setName("AVATAR");
    modelTransf->addChild(node);
    scene->addChild(modelTransf);
    setSceneData(scene);

    unsigned tex_width = 4096;
    unsigned tex_height = 4096;
    // textures to render to and to use for texturing of the final quad
    osg::TextureRectangle* textureRect[2] = {0,0};
    for (int i=0;i<2;i++) {
        textureRect[i] = new osg::TextureRectangle;
        textureRect[i]->setTextureSize(tex_width, tex_height);
        textureRect[i]->setInternalFormat(GL_RGBA);
        
textureRect[i]->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
        
textureRect[i]->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
    }
    //create the geometry of the quad
    { 
        osg::Geometry* polyGeom = new osg::Geometry();

        polyGeom->setSupportsDisplayList(false);

        osg::Vec3Array* vertices = new osg::Vec3Array;
        osg::Vec2Array* texcoords = new osg::Vec2Array;
        
        vertices->push_back(osg::Vec3d(0,0,0));
        texcoords->push_back(osg::Vec2(0,0));

        vertices->push_back(osg::Vec3d(200,0,0));
        texcoords->push_back(osg::Vec2(tex_width,0));

        vertices->push_back(osg::Vec3d(200,0,200));
        texcoords->push_back(osg::Vec2(tex_width,tex_height));

        vertices->push_back(osg::Vec3d(0,0,200));
        texcoords->push_back(osg::Vec2(0,tex_height));

        polyGeom->setVertexArray(vertices);
        polyGeom->setTexCoordArray(0,texcoords);

        osg::Vec4Array* colors = new osg::Vec4Array;
        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
        polyGeom->setColorArray(colors);
        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL);

        polyGeom->addPrimitiveSet(new 
osg::DrawArrays(osg::PrimitiveSet::QUADS,0,vertices->size()));

        // now we need to add the textures (generated by RTT) to the Drawable.
        osg::StateSet* stateset = new osg::StateSet;
        for (int i=0;i<2;i++) {
            stateset->setTextureAttributeAndModes(i, textureRect[i], 
osg::StateAttribute::ON);
        }
        
        polyGeom->setStateSet(stateset);
        static const char *shaderSource = {
                "uniform sampler2DRect textureID0;\n"
                "uniform sampler2DRect textureID1;\n"
                "void main(void)\n"
                "{\n"
                "    gl_FragData[0] = \n"
                "          vec4(texture2DRect( textureID0, gl_TexCoord[0].st 
).rgb, 1);  \n"
                "}\n"
            };
            osg::ref_ptr<osg::Shader> fshader = new osg::Shader( 
osg::Shader::FRAGMENT , shaderSource);
            osg::ref_ptr<osg::Program> program = new osg::Program;
            program->addShader( fshader.get());
            stateset->setAttributeAndModes( program.get(), 
osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
        stateset->addUniform(new osg::Uniform("textureID0", 0));
        stateset->addUniform(new osg::Uniform("textureID1", 1));
        osg::Geode* geode = new osg::Geode();
        geode->addDrawable(polyGeom);
        
        scene->addChild(geode);
    }

    getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
    getCamera()->setProjectionMatrixAsPerspective(30.0f,
        static_cast<double>(width())/static_cast<double>(height()), 1.0f, 
10000.0f);
    getCamera()->setGraphicsContext(getGraphicsWindow());

    // now create the camera to do the multiple render to texture
    {    
        osg::Camera* camera = new osg::Camera;

        // set up the background color and clear mask.
        camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
        camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // the camera is going to look at our input quad
        camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1));
        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        camera->setViewMatrix(osg::Matrix::identity());

        // set viewport
        camera->setViewport(0, 0, width(), height());

        // set the camera to render before the main camera.
        camera->setRenderOrder(osg::Camera::PRE_RENDER);

        // tell the camera to use OpenGL frame buffer objects
        
//camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);

        // attach the textures to use
        for (int i=0;i<2;i++) {
            
camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+i), 
textureRect[i]);
         }
        // add the subgraph to render
        camera->addChild(node);
        scene->addChild(camera);
    }  


and fragment shader of model node:

Code:

uniform sampler2D baseMap;
uniform vec3 interPos;

varying vec3 Normal;
varying vec4 color;
varying vec2 Texcoord;
varying vec3 position;

void main( void )
{
    float radio = 3.0;
    vec3 a = position - interPos;
    float dist = sqrt((a.x*a.x)+(a.y*a.y)+(a.z*a.z));    
    vec4 paintColor = vec4(1.0,0.0,0.0,1.0);
    if(dist<radio){
        gl_FragData[0] = texture2D( baseMap, Texcoord ) * color + paintColor;
        gl_FragData[1] = paintColor;
    }
    else{
        gl_FragData[0] = texture2D( baseMap, Texcoord ) * color;
        gl_FragData[1] = paintColor;    
    }
}


Assuming that in the shader of quad I put: vec4(texture2DRect( textureID1, 
gl_TexCoord[0].st ).rgb, 1);
it should give me a red texture, but its black...

Thanks for de answer!

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=32518#32518





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to