Can't override texture unit 0

classic Classic list List threaded Threaded
3 messages Options
marlinrowley marlinrowley
Reply | Threaded
Open this post in threaded view
|

Can't override texture unit 0

Hello,

I am trying to create my own texture that overrides the base image layer.  Here is the code:

osg::Group* g = new osg::Group();
                const char* imageFile = "X:/OpenSceneGraph-Data-3.0.0/Images/red.bmp";
                osg::ref_ptr<osg::Image> img = osgDB::readImageFile(imageFile);
                osg::ref_ptr<osg::Texture2D> tex = new osg::Texture2D(img);
                tex->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP_TO_EDGE);
                tex->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP_TO_EDGE);
                tex->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR);
                tex->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR);
                tex->osg::Texture::setMaxAnisotropy(4.0);
                tex->setUnRefImageDataAfterApply(true);
                tex->setDataVariance(osg::Object::DYNAMIC);
                g->getOrCreateStateSet()->getOrCreateUniform("BaseTexSampler", osg::Uniform::SAMPLER_2D)->set(0);
                g->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex, osg::StateAttribute::OVERRIDE);
                g->getOrCreateStateSet()->setAttributeAndModes(setupShader(VertexShaderPath, FragmentShaderPath), osg::StateAttribute::OVERRIDE);
                g->addChild(earth);

Vertex shader:

#version 330 compatibility
#pragma vp_entryPoint   Compute_VS
#pragma vp_location     vertex_model
#pragma vp_order        1.0
out vec4 BaseTexCoords;

// OsgEarth shader code function
void Compute_VS(inout vec4 VertexMODEL)
{
    // transform our vertex into clip space
    gl_Position = gl_ModelViewProjectionMatrix * VertexMODEL;

    // get our base texture coordinates
    BaseTexCoords = gl_MultiTexCoord0;
}


Fragment shader:

#version 330 compatibility
#pragma vp_entryPoint   Compute_FS
#pragma vp_location     fragment_coloring
#pragma vp_order        1.0
uniform sampler2D BaseTexSampler;
in vec4 BaseTexCoords;

// OsgEarth shader code function
void Compute_FS(inout vec4 color)
{
    // Set the base texture (this could be the terrain texture as well).
    color = texture(BaseTexSampler, BaseTexCoords.st);
    color.a = 1;
}

This gets ignored if I used texture unit 0.  I have to switch to texture unit 1 in order for it to work.  How do I override texture unit 0?

Thanks,

-M
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|

Re: Can't override texture unit 0

Marlin,

You don't. It is reserved for use by the terrain engine at startup.

osgEarth keeps track of which texture image units are in use by the terrain engine. You can reserve a free one by calling TerrainEngineNode::getResources() and using the TerrainResources API.
Glenn Waldron / Pelican Mapping
marlinrowley marlinrowley
Reply | Threaded
Open this post in threaded view
|

Re: Can't override texture unit 0

Ok.  I was trying to keep my code backwards compatible with OSG.

Thanks for the info.

-M