Trying to load an OSG model with textures allocated in textureUnit1

半世苍凉 提交于 2019-12-12 03:34:33

问题


I want to render an Openscenegraph model with textures under two constraints:

1) Using shaders (opengles20) 2) Uploading the textures to the textureUnit1 on the GPU (NO the default textureUnit0)

I thought I was doing right but still I am getting non textured models (only the mesh).

Here are the shaders (notice that I use gl_MultiTexCoord1):

static const char gVertexShader1[] = {
    "varying vec2 texCoords;\n"
    "void main()\n"
    "{\n"
    "   texCoords = gl_MultiTexCoord1.st;\n"
    "   gl_Position = ftransform();\n"
    "}\n"
};

static const char gFragmentShader1[] = {
    "varying vec2 texCoords;\n"
    "uniform sampler2D tex;\n"
    "void main()\n"
    "{\n"
    "   gl_FragColor = texture2D(tex, texCoords);\n"
    "}\n"
};

The loaded osg model also specifies texture unit 1 in all the texture images, for instance:

  textureUnit 1 {
    GL_TEXTURE_2D ON
    Texture2D {
      UniqueID Texture2D_1
      file "/storage/sdcard0/osg/textures/p51d-jw-05.png"
      wrap_s REPEAT
      wrap_t REPEAT
      wrap_r CLAMP
      min_filter LINEAR_MIPMAP_LINEAR
      mag_filter LINEAR
      maxAnisotropy 1
      borderColor 0 0 0 0
      borderWidth 0
      useHardwareMipMapGeneration TRUE
      unRefImageDataAfterApply TRUE
      internalFormatMode USE_IMAGE_DATA_FORMAT
      resizeNonPowerOfTwo TRUE
    }

And finally the C++ code:

        //Load model
        osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(newModel.filename);

        //Assign program
        osg::ref_ptr<osg::StateSet> ss = loadedModel->getOrCreateStateSet();
        osg::Shader * vshader = new osg::Shader(osg::Shader::VERTEX, gVertexShader1 );
        osg::Shader * fshader = new osg::Shader(osg::Shader::FRAGMENT, gFragmentShader1 );
        osg::Program * prog = new osg::Program;
        prog->addShader ( vshader );
        prog->addShader ( fshader );
        ss->setAttributeAndModes(prog);

        //Uniforms
        osg::ref_ptr<osg::Texture2D> bodyTexture = new osg::Texture2D;
        bodyTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
        bodyTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
        bodyTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
        bodyTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
        ss->setTextureAttributeAndModes(1, bodyTexture.get());
        ss->addUniform(new osg::Uniform("tex", 1));

Any idea?

Thanks,

JM


回答1:


The way you set the texture to tex unit 1 is correct, are you sure the model you're loading contains texture coordinates at index 1 as well?

Just make sure there are such tex coords, otherwise try reusing a different tex coord index like gl_MultiTexCoord0



来源:https://stackoverflow.com/questions/39178777/trying-to-load-an-osg-model-with-textures-allocated-in-textureunit1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!