|
g_HeightMap = new osg::Texture2D();
g_HeightMap->setTextureSize(2048, 2048);
g_HeightMap->setInternalFormat(GL_RGBA);
g_HeightMap->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D:INEAR);
g_HeightMap->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
g_HeightMap->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP_TO_EDGE);
g_HeightMap->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP_TO_EDGE);
// Create its camera and render to it
model = osgDB::ReadNodeFile("D:\\xx.ive");
g_HeightMapCamera = new osg::Camera;
g_HeightMapCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
g_HeightMapCamera->setClearColor(osg::Vec4(-1000.0, -1000.0, -1000.0, 1.0f));
g_HeightMapCamera->setViewport(0, 0, 2048, 2048);
g_HeightMapCamera->setRenderOrder(osg::Camera:RE_RENDER);
g_HeightMapCamera->attach(osg::Camera::COLOR_BUFFER, g_HeightMap);
g_HeightMapCamera->addChild(model);
g_HeightMapCamera->setProjectionMatrix(osg::Matrix:rtho2D(-HEIGHT_MAP_AREA,HEIGHT_MAP_AREA,- HEIGHT_MAP_AREA,HEIGHT_MAP_AREA));
g_HeightMapCamera->setCullingActive(false);
g_HeightMapCamera->setReferenceFrame(osg::Camera::ABSOLUTE_RF_INHERIT_VIEWPOINT);
g_HeightMapCamera->setImplicitBufferAttachmentMask(0, 0);
g_HeightMapCamera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT );
g_HeightMapCamera->getOrCreateStateSet()->setMode(GL_ALPHA_TEST,
osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
osg::ref_ptr<osg::Program> heightMapProgram = new osg::Program;
heightMapProgram->addShader(new osg::Shader(osg::Shader::VERTEX, HeightMapVertSource));
heightMapProgram->addShader(new osg::Shader(osg::Shader::FRAGMENT, HeightMapFragSource));
其中的顶点着色器和片段着色器如下:
const char *HeightMapVertSource = {
"varying float height;\n"
"void main()\n"
"{\n"
" vec4 worldVert = gl_Vertex;\n"
" height = worldVert.z;\n"
" gl_Position = gl_ModelViewProjectionMatrix * worldVert;\n"
" gl_ClipVertex = gl_ModelViewMatrix * worldVert;\n"
" gl_FrontColor = gl_Color;\n"
"}\n"
};
const char *HeightMapFragSource = {
"varying vec4 gl_Color;\n"
"varying float height;\n"
"void main()\n"
"{\n"
" gl_FragColor = gl_Color;\n"
"}\n"
};
为什么渲染出来的纹理显示的不是正常的场景呢。只是一块白色 |
|