|
本帖最后由 bigdudu 于 2015-2-6 17:43 编辑
大神们帮我看个问题
" vec3 exitPoint = texture(backfaceTexture, gl_FragCoord.xy/800.0).xyz;\n"
" if(abs(exitPoint.x-0.5)<=0.002) exitPoint = vec3(0.0,0.0,0.0);\n"
"fragData = vec4(exitPoint,1.0);\n"
我片元里面这么写,画的立方体上就有一条黑色。。但是我把那个0.002改成0.0019,那条黑色就没有了
这个精度是多少啊
- #include <osg/MatrixTransform>
- #include <osg/Geometry>
- #include <osg/Geode>
- #include <osgViewer/Viewer>
- #include <osgUtil/SmoothingVisitor>
- #include <osg/CullFace>
- #include <osgGA/TrackballManipulator>
- #include <osg/Texture2D>
- char volume_backface_vert[] = "#version 450\n"
- "uniform mat4 osg_ModelViewProjectionMatrix;\n"
- "uniform mat3 osg_NormalMatrix;\n"
- "in vec3 osg_Vertex;\n"
- "in vec4 osg_Color;\n"
- "in vec3 osg_Normal;\n"
- "in vec4 osg_MultiTexCoord0; \n"
- "out vec4 color;\n"
- "\n"
- "void main(void)\n"
- "{\n"
- "color = osg_Color;\n"
- "gl_Position = osg_ModelViewProjectionMatrix * vec4(osg_Vertex, 1.0);\n"
- "}\n"
- "\n";
- char volume_backface_frag[] =
- "#version 450\n"
- //"uniform sampler3D baseTexture;\n"
- "in vec4 color;\n"
- "out vec4 fragData;\n"
- "void main(void)\n"
- "{ \n"
- " fragData = color;\n"
- "}\n"
- "\n";
- char volume_rendering_vert[] = "#version 450\n"
- "uniform mat4 osg_ModelViewProjectionMatrix;\n"
- "uniform mat4 oag_ModelViewMatrix;\n"
- "uniform mat3 osg_NormalMatrix;\n"
- "in vec3 osg_Vertex;\n"
- "in vec4 osg_Color;\n"
- "in vec3 osg_Normal;\n"
- "in vec4 osg_MultiTexCoord0; \n"
- "out vec3 entryPoint;\n"
- "out vec4 ldir0;\n"
- "\n"
- "void main(void)\n"
- "{\n"
- "ldir0 = (osg_ModelViewProjectionMatrix)*vec4(0.0,10.0,0.01,0.0);\n"
- "entryPoint = osg_Color.xyz;\n"
- "gl_Position = vec4(osg_Vertex, 1.0);\n"
- "}\n"
- "\n";
- char volume_rendering_frag[] =
- "#version 450\n"
- "highp uniform sampler2D backfaceTexture;\n"
- "uniform sampler2D frantfaceTexture;\n"
- "in vec3 entryPoint;\n"
- "out vec4 fragData;\n"
- "void main(void)\n"
- "{ \n"
- " vec3 exitPoint = texture(backfaceTexture, gl_FragCoord.xy/800.0).xyz;\n"
- " if(abs(exitPoint.x-0.5)<=0.002) exitPoint = vec3(0.0,0.0,0.0);\n"
- "fragData = vec4(exitPoint,1.0);\n"
- "return;\n"
- "}\n"
- "\n";
- int main()
- {
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- {
- osg::Geometry* geom = new osg::Geometry;
- osg::Vec3Array* coords = new osg::Vec3Array(8);
- (*coords)[0] = osg::Vec3d(-1.0, -1.0, -1.0);
- (*coords)[1] = osg::Vec3d(1.0, -1.0, -1.0);
- (*coords)[2] = osg::Vec3d(1.0, 1.0, -1.0);
- (*coords)[3] = osg::Vec3d(-1.0, 1.0, -1.0);
- (*coords)[4] = osg::Vec3d(-1.0, -1.0, 1.0);
- (*coords)[5] = osg::Vec3d(1.0, -1.0, 1.0);
- (*coords)[6] = osg::Vec3d(1.0, 1.0, 1.0);
- (*coords)[7] = osg::Vec3d(-1.0, 1.0, 1.0);
- geom->setVertexArray(coords);
- osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
- normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
- geom->setNormalArray(normals.get());
- geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
- osg::Vec4Array* colours = new osg::Vec4Array(8);
- //(*colours)[0].set(1.0f, 1.0f, 1.0, 1.0f);
- (*colours)[0] = osg::Vec4d(0.0f, 0.0f, 0.0f, 1.0f);
- (*colours)[1] = osg::Vec4d(1.0f, 0.0f, 0.0f, 1.0f);
- (*colours)[2] = osg::Vec4d(1.0f, 1.0f, 0.0f, 1.0f);
- (*colours)[3] = osg::Vec4d(0.0f, 1.0f, 0.0f, 1.0f);
- (*colours)[4] = osg::Vec4d(0.0f, 0.0f, 1.0f, 1.0f);
- (*colours)[5] = osg::Vec4d(1.0f, 0.0f, 1.0f, 1.0f);
- (*colours)[6] = osg::Vec4d(1.0f, 1.0f, 1.0f, 1.0f);
- (*colours)[7] = osg::Vec4d(0.0f, 1.0f, 1.0f, 1.0f);
- geom->setColorArray(colours, osg::Array::BIND_PER_VERTEX);
- osg::DrawElementsUShort* drawElements = new osg::DrawElementsUShort(GL_QUADS);
- // bottom
- drawElements->push_back(0);
- drawElements->push_back(1);
- drawElements->push_back(2);
- drawElements->push_back(3);
- // bottom
- drawElements->push_back(3);
- drawElements->push_back(2);
- drawElements->push_back(6);
- drawElements->push_back(7);
- // left
- drawElements->push_back(0);
- drawElements->push_back(3);
- drawElements->push_back(7);
- drawElements->push_back(4);
- // right
- drawElements->push_back(5);
- drawElements->push_back(6);
- drawElements->push_back(2);
- drawElements->push_back(1);
- // front
- drawElements->push_back(1);
- drawElements->push_back(0);
- drawElements->push_back(4);
- drawElements->push_back(5);
- // top
- drawElements->push_back(7);
- drawElements->push_back(6);
- drawElements->push_back(5);
- drawElements->push_back(4);
- geom->addPrimitiveSet(drawElements);
- osgUtil::SmoothingVisitor::smooth(*geom);
- osg::ref_ptr<osg::Geode> root = new osg::Geode;
- osg::StateSet* stateset = root->getOrCreateStateSet();
- osg::ref_ptr<osg::CullFace> cullFace = new osg::CullFace(osg::CullFace::BACK);
- stateset->setAttributeAndModes(cullFace.get(), osg::StateAttribute::ON);
- stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
- osg::Program* program = new osg::Program;
- // get shaders from source
- program->addShader(new osg::Shader(osg::Shader::VERTEX, volume_backface_vert));
- program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_backface_frag));
- //program->setParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 36 );
- program->setParameter(GL_GEOMETRY_INPUT_TYPE_EXT, GL_LINES_ADJACENCY_EXT);
- program->setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_LINE_STRIP);
- stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
- root->addDrawable(geom);
- osg::ref_ptr<osg::Texture2D> _backfacetexture;
- osg::ref_ptr<osg::Camera> _rttbackcamera;
- osg::ref_ptr<osg::MatrixTransform> rtttransform;
- int tex_width = 800, tex_height = 800;
- _backfacetexture = new osg::Texture2D;
- _backfacetexture->setTextureSize(tex_width, tex_height);
- _backfacetexture->setInternalFormat(GL_RGBA);
- _backfacetexture->setResizeNonPowerOfTwoHint(false);
- _backfacetexture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
- _backfacetexture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
- //_backfacetexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::CLAMP_TO_BORDER);
- _backfacetexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
- _backfacetexture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
- _rttbackcamera = new osg::Camera;
- //_rttbackcamera->setViewport(_camera->getViewport());
- _rttbackcamera->setClearColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.0f));
- _rttbackcamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- _rttbackcamera->setRenderOrder(osg::Camera::PRE_RENDER);
- _rttbackcamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
- _rttbackcamera->attach(osg::Camera::COLOR_BUFFER, _backfacetexture.get());
- rtttransform = new osg::MatrixTransform;
- rtttransform->addChild(root.get());
- _rttbackcamera->addChild(rtttransform.get());
- //_transform->addChild(_rttbackcamera.get());
- osg::ref_ptr<osg::Group> gp = new osg::Group;
- gp->addChild(_rttbackcamera);
- osg::Geometry* geom1 = new osg::Geometry;
- {
-
- const float w = 2.0 / 2.0f;
- const float h = 2.0 / 2.0f;
- osg::Vec3Array* vertices = new osg::Vec3Array;
- vertices->push_back(osg::Vec3(-w, -h, 0.0f));
- vertices->push_back(osg::Vec3(+w, -h, 0.0f));
- vertices->push_back(osg::Vec3(-w, +h, 0.0f));
- vertices->push_back(osg::Vec3(+w, +h, 0.0f));
- osg::Vec3Array* normals = new osg::Vec3Array;
- normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
- osg::Vec2Array* texCoords = new osg::Vec2Array;
- texCoords->push_back(osg::Vec2(0.0f, 0.0f));
- texCoords->push_back(osg::Vec2(1.0f, 0.0f));
- texCoords->push_back(osg::Vec2(0.0f, 1.0f));
- texCoords->push_back(osg::Vec2(1.0f, 1.0f));
- osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
- colors->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 0.5f));
- geom1->setVertexArray(vertices);
- geom1->setColorArray(colors.get());
- geom1->setColorBinding(osg::Geometry::BIND_OVERALL);
- geom1->setTexCoordArray(0, texCoords);
- geom1->setNormalArray(normals);
- geom1->setNormalBinding(osg::Geometry::BIND_OVERALL);
- geom1->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, 0, vertices->size()));
- //geometry->setUseDisplayList(false);
- geom1->setUseVertexBufferObjects(true);
- }
- geode->addDrawable(geom1);
- osg::StateSet* stateset1 = geode->getOrCreateStateSet();
- stateset1->setTextureAttributeAndModes(1, _backfacetexture, osg::StateAttribute::ON);
- osg::Uniform* backfaceTextureSampler = new osg::Uniform("backfaceTexture", 1);
- stateset1->addUniform(backfaceTextureSampler);
- ////----------------------------program------------------------------/////
- osg::Program* program1 = new osg::Program;
- // get shaders from source
- program1->addShader(new osg::Shader(osg::Shader::VERTEX, volume_rendering_vert));
- program1->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_rendering_frag));
- //program->setParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 36 );
- program1->setParameter(GL_GEOMETRY_INPUT_TYPE_EXT, GL_LINES_ADJACENCY_EXT);
- program1->setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_LINE_STRIP);
- stateset1->setAttributeAndModes(program1, osg::StateAttribute::ON);
- gp->addChild(geode.get());
-
- osgViewer::Viewer viewer;
- viewer.setCameraManipulator(new osgGA::TrackballManipulator);
- viewer.setSceneData(gp.get());
- viewer.setUpViewInWindow((800 - 700) / 2, (800 - 700) / 2, 800, 800);
- viewer.realize();
-
- osg::State* state = viewer.getCamera()->getGraphicsContext()->getState();
- state->setUseModelViewAndProjectionUniforms(true);
- state->setUseVertexAttributeAliasing(true);
- return viewer.run();
- }
- }
复制代码 |
-
|