我想要做的应该是非常简单的,我猜,但我没有得到我想要的东西(也许是因为我是这种工具的新手)。我只想通过顶点和片段着色器将某个纹理图像(例如典型的砖)映射到现有的cessna.osg中。为了完成这个任务,我选择了一个非常简单的顶点和片段着色器,在我的有限理解中,它应该可以工作,但事实并非如此。通过GLSL着色器的cessna.osg的纹理映射
下面是代码:
#include <osg/PositionAttitudeTransform>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
#include <osgGA/TrackballManipulator>
#include <osg/Program>
#include <osg/Shader>
#include <osgViewer/Viewer>
#include <iostream>
#include <osg/Texture2D>
static const char* VertexShader = {
"varying vec2 texCoords;\n"
"void main()\n"
"{\n"
" texCoords = gl_MultiTexCoord0.st;\n"
" gl_Position = ftransform();\n"
"}\n"
};
static const char* FragmentShader = {
"varying vec2 texCoords;\n"
"uniform sampler2D tex;\n"
"void main()\n"
"{\n"
" gl_FragColor = texture2D(tex, texCoords);\n"
"}\n"
};
int main(int argc, char **argv)
{
// Assembling scenegraph
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("cessna.osg");
// Assigning program
osg::ref_ptr<osg::StateSet> ss = model->getOrCreateStateSet();
osg::ref_ptr<osg::Program> program(new osg::Program);
osg::ref_ptr<osg::Shader> vShader(new osg::Shader(osg::Shader::VERTEX, VertexShader));
osg::ref_ptr<osg::Shader> fShader(new osg::Shader(osg::Shader::FRAGMENT, FragmentShader));
program->addShader(vShader);
program->addShader(fShader);
ss->setAttributeAndModes(program.get());
//Set Texture 1
osg::ref_ptr<osg::Texture2D> bodyTexture = new osg::Texture2D;
bodyTexture->setImage(osgDB::readImageFile("Images/Brick-Norman-Brown.TGA"));
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(0, bodyTexture.get());
ss->addUniform(new osg::Uniform("tex", 0));
// View the scene
osgViewer::Viewer viewer;
viewer.setSceneData(model);
viewer.setUpViewInWindow(0, 0, 512, 384);
return viewer.run();
return 0;
}
我得到的是塞斯纳模型用素色(纹理图像的主色),但我从来没有得到一个真正的“膨松”塞斯纳。
我很抱歉如果问题很愚蠢,但我真的很感激任何一种帮助继续前进的提示。
感谢
您使用OpenGL或OpenGL ES? –
在这个例子中,我使用的是OpenGL,但想法是学习如何在Open GL ES 2.0中完成同样的事情(但现在我对OpenGL更感兴趣) – user3417052
你确定'cessna.osg '包含纹理坐标? – SurvivalMachine