尝试使用在 textureUnit1 中分配的纹理加载 OSG 模型
Trying to load an OSG model with textures allocated in textureUnit1
我想在两个约束下渲染带有纹理的 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
您将纹理设置为 tex 单元 1 的方式是正确的,您确定您正在加载的模型在索引 1 处也包含纹理坐标吗?
只要确保有这样的 tex 坐标,否则尝试重新使用不同的 tex 坐标索引,如 gl_MultiTexCoord0
我想在两个约束下渲染带有纹理的 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
您将纹理设置为 tex 单元 1 的方式是正确的,您确定您正在加载的模型在索引 1 处也包含纹理坐标吗?
只要确保有这样的 tex 坐标,否则尝试重新使用不同的 tex 坐标索引,如 gl_MultiTexCoord0