|
最近学习一下渲染到3D纹理,创建一个256*256深度为32的3D纹理如下
- osg::Texture3D* _texture = new osg::Texture3D;
- _texture->setTextureSize(256,256,32);
- _texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
- _texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
- _texture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE);
- _texture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);
- _texture->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE);
- _texture->setInternalFormat(GL_RGB16F_ARB);
- _texture->setSourceFormat (GL_RGB);
- _texture->setSourceType(GL_FLOAT);
-
复制代码
创建一个平面pannel_1节点,一个2D的rtt相机cameraRtt,
场景根节点下面添加cameraRtt,cameraRtt下面添加pannel_1,cameraRtt始终(0,0,1)看(0,0,0)方向(0,1,0),pannel_1添加shader,使用geometry shader
- shader = osg::Shader::readShaderFile(osg::Shader::GEOMETRY,filePath);
- pro->addShader(shader);
- pro->setParameter( GL_GEOMETRY_VERTICES_OUT_EXT, 3 );
- pro->setParameter( GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES );
- pro->setParameter( GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP );
复制代码
cameraRtt渲染到纹理,
- m_cameraRtt->attach(osg::Camera::COLOR_BUFFER0,m_RttTexture3D.get(),0,m_cameraRtt->FACE_CONTROLLED_BY_GEOMETRY_SHADER);
- m_cameraRtt->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
复制代码
在shader中传入layer对层更新,
-
- osg::StateSet *state = pannel_1->getOrCreateStateSet();
- #if 1
- {
- state->getOrCreateUniform("layer",osg::Uniform::INT)->set( m_layer);
- ++m_layer;
- if (m_layer==32)
- {
- m_layer = 0;
- }
- }
- #else
- for (int layer = 0; layer < 32; ++layer)
- {
- state->getOrCreateUniform("layer",osg::Uniform::INT)->set( m_layer);
- }
- #endif
复制代码
这里我也不太清楚是一更新设置一下layer还是一次更新设置多次设置layer的值,
vertex shader如下:
- void main()
- {
- gl_Position = ftransform();
- gl_TexCoord[0] = gl_MultiTexCoord0;
- gl_FrontColor = gl_Color;
- }
复制代码
frag shader如下:
- uniform sampler2D uTexture2D0;
- void main()
- {
- vec4 color = texture2D(uTexture2D0,gl_TexCoord[0]);
- gl_FragData[0] = color;
- }
复制代码
geometry shader如下:
- #version 120
- uniform int layer;
- #extension GL_EXT_geometry_shader4 : enable
- void main()
- {
- for(int i =0 ; i < gl_VerticesIn ; ++i)
- {
- gl_Position = gl_PositionIn[i];
- gl_TexCoord[0] = gl_TexCoordIn[i][0];
- gl_Layer = layer;
- EmitVertex();
- }
- EndPrimitive();
- }
复制代码
在主相机下面添加一个面板pannel_2 设置纹理为 Texture3D,使用shader传入layer 通过gl_TexCoord[0].z = (layer)/32.0 来获取纹理,最后发现只有pannel_2的layer=0.0时才有纹理。
想要实现rtt 到Texture3D,通过geometry shader的gl_Layer改变图层,然后在贴到平面上显示,结果并未如愿,不知道哪里有问题,谁会这个帮忙指导一下啊!
看了一下osg源码,源码中camera里貌似是一下过程
- void Camera::attach(BufferComponent buffer, osg::Texture* texture, unsigned int level, unsigned int face, bool mipMapGeneration,
- unsigned int multisampleSamples,
- unsigned int multisampleColorSamples)
- {
- _bufferAttachmentMap[buffer]._texture = texture;
- _bufferAttachmentMap[buffer]._level = level;
- _bufferAttachmentMap[buffer]._face = face;
- _bufferAttachmentMap[buffer]._mipMapGeneration = mipMapGeneration;
- _bufferAttachmentMap[buffer]._multisampleSamples = multisampleSamples;
- _bufferAttachmentMap[buffer]._multisampleColorSamples = multisampleColorSamples;
- }
复制代码
把attach对象存入map中,而后在FrameBufferObject中
- FrameBufferAttachment::FrameBufferAttachment(Camera::Attachment& attachment)
- {
- ........
- osg::Texture3D* texture3D = dynamic_cast<osg::Texture3D*>(texture);
- if (texture3D)
- {
- _ximpl = new Pimpl(Pimpl::TEXTURE3D, attachment._level);
- _ximpl->textureTarget = texture3D;
- _ximpl->zoffset = attachment._face;
- return;
- }
- .............
- }
复制代码
- FrameBufferAttachment::FrameBufferAttachment(Texture3D* target, unsigned int zoffset, unsigned int level)
- {
- _ximpl = new Pimpl(Pimpl::TEXTURE3D, level);
- _ximpl->textureTarget = target;
- _ximpl->zoffset = zoffset;
- }
复制代码
- void FrameBufferAttachment::attach(State &state, GLenum target, GLenum attachment_point, const FBOExtensions* ext) const
- {
- ........
- case Pimpl::TEXTURE3D:
- if (_ximpl->zoffset == Camera::FACE_CONTROLLED_BY_GEOMETRY_SHADER)
- {
- if (ext->glFramebufferTexture)
- {
- ext->glFramebufferTexture(target, attachment_point, tobj->id(), _ximpl->level);
- }
- }
- else
- ext->glFramebufferTexture3D(target, attachment_point, GL_TEXTURE_3D, tobj->id(), _ximpl->level, _ximpl->zoffset);
- break;
- ........
- }
复制代码
我设置attach应该也没错啊,使用了Camera::FACE_CONTROLLED_BY_GEOMETRY_SHADER,求大神指导并纠正错误····· |
|