我想在两个约束条件下渲染具有纹理的Openscenegraph模型:
1)使用着色器(opengles20) 2)将纹理上传到GPU上的textureUnit1(不是默认的textureUnit0)
我认为我做得对,但我仍然得到非纹理模型(只有网格)。
以下是着色器(请注意我使用了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,例如:
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
答案 0 :(得分:0)
将纹理设置为tex单元1的方式是正确的,您确定要加载的模型还包含索引1处的纹理坐标吗?
确保有这样的tex coords,否则尝试重用不同的tex coord索引,如gl_MultiTexCoord0