尝试使用在TextureUnit1中分配的纹理加载OSG模型 [英] Trying to load an OSG model with textures allocated in textureUnit1

查看:295
本文介绍了尝试使用在TextureUnit1中分配的纹理加载OSG模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用两个约束来渲染具有纹理的Openscenegraph模型:

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

1)使用着色器(opengles20) 2)将纹理上传到GPU上的textureUnit1(默认为textureUnit0)

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).

以下是着色器(请注意,我使用gl_MultiTexCoord1):

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"
};

加载的osg模型还在所有纹理图像中指定纹理单位1,例如:

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
    }

最后是C ++代码:

        //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));

有什么主意吗?

谢谢

JM

推荐答案

将纹理设置为tex单元1的方式是正确的,您确定要加载的模型也包含索引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?

只需确保有这样的tex坐标,否则尝试重用不同的tex坐标索引,例如gl_MultiTexCoord0

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

这篇关于尝试使用在TextureUnit1中分配的纹理加载OSG模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆