Hey,
i've got question relating to a simple shader-implementation (#version 140) in
OSG. I just want to render a scene without any modifications but by passing a
vertex- and fragment-shader. Because I'm working with the third edition of the
Orange-Book I want to use this "newer" glsl-version.
here is my code so far:
.cpp
Code:
int _tmain(int argc, _TCHAR* argv[])
{
osg::setNotifyLevel( osg::WARN );
osg::ref_ptr<osg::GraphicsContext::Traits> gctraits = new
osg::GraphicsContext::Traits();
gctraits->x=50;
gctraits->y=50;
gctraits->width=1280;
gctraits->height=1024;
gctraits->windowDecoration=true;
gctraits->doubleBuffer=true;
osg::ref_ptr<osg::GraphicsContext> gc =
osg::GraphicsContext::createGraphicsContext(gctraits.get());
//set fixed functionality vars
gc->getState()->setUseModelViewAndProjectionUniforms(true);
gc->getState()->setUseVertexAttributeAliasing(true);
osg::ref_ptr<osg::Camera> cam = new osg::Camera();
cam->setGraphicsContext(gc.get());
cam->setViewport(new osg::Viewport(0,0,gctraits->width, gctraits->height));
cam->setClearMask(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
cam->setClearColor(osg::Vec4(0.2,0.2,0.4,1.0));
cam->setProjectionMatrixAsPerspective(30.0,(double)gctraits->width/gctraits->height,1.0,1000.0);
//setup scene
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::ref_ptr<osg::Node> cessna = new osg::Node();
osg::ref_ptr<osg::Node> landscape = new osg::Node();
osg::ref_ptr<osg::Geode> sphere = new osg::Geode();
osg::ref_ptr<osg::PositionAttitudeTransform> cessnatransform = new
osg::PositionAttitudeTransform();
osg::ref_ptr<osg::PositionAttitudeTransform> spheretransform = new
osg::PositionAttitudeTransform();
osg::ref_ptr<osg::PositionAttitudeTransform> landscapetransform = new
osg::PositionAttitudeTransform();
landscape = osgDB::readNodeFile("osgdata/lz.osg");
cessna = osgDB::readNodeFile("osgdata/cessna.osg");
osg::ref_ptr<osg::ShapeDrawable> sphereshape = new osg::ShapeDrawable(new
osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),5.0f));
sphereshape->setColor(osg::Vec4(0.0f,1.0f,0.0f,0.5f));
sphere->addDrawable(sphereshape.get());
cessnatransform->setPosition(osg::Vec3(100.0f,0.0f,150.0f));
spheretransform->setPosition(osg::Vec3(50.0f,0.0f,150.0f));
landscapetransform->setPosition(osg::Vec3(0.0f,0.0f,0.0f));
cessnatransform->addChild(cessna.get());
landscapetransform->addChild(landscape.get());
spheretransform->addChild(sphere.get());
root->addChild(landscapetransform.get());
root->addChild(cessnatransform.get());
root->addChild(spheretransform.get());
//setup shaders
osg::ref_ptr<osg::Shader> vertshader = new osg::Shader(osg::Shader::VERTEX);
if(!vertshader->loadShaderSourceFromFile(VSSRCFILE))
osg::notify(osg::WARN)<<"Error while loading vertexshader-sourcefile...";
osg::ref_ptr<osg::Shader> fragshader = new osg::Shader(osg::Shader::FRAGMENT);
if(!fragshader->loadShaderSourceFromFile(FSSRCFILE))
osg::notify(osg::WARN)<<"Error while loading fragmentshader-sourcefile...";
osg::ref_ptr<osg::Program> shaderprog = new osg::Program();
shaderprog->addShader(vertshader.get());
shaderprog->addShader(fragshader.get());
root->getOrCreateStateSet()->setAttributeAndModes(shaderprog.get());
osgViewer::Viewer viewer;
viewer.setCamera(cam.get());
viewer.setSceneData(root.get());
viewer.setCameraManipulator(new osgGA::TerrainManipulator);
viewer.getCameraManipulator()->setHomePosition(osg::Vec3(250.0,-200.0,143.0),osg::Vec3(23.0,8.0,143.0),osg::Vec3(0.0,0.0,1.0));
return viewer.run();
}
vertexshader
Code:
#version 140
uniform mat4 osg_ModelViewProjectionMatrix;
in vec4 osg_Color;
in vec4 osg_Vertex;
out vec4 fragcolor;
void main(){
fragcolor=osg_Color;
gl_Position=osg_ModelViewProjectionMatrix*osg_Vertex;
}
fragment-shader
Code:
#version 140
in vec4 fragcolor;
out vec4 fragData;
void main(){
fragData=fragcolor;
}
My problem is, that the rendering-result is not the same as I expected. I have
attached to pictures to this post. When I run the program with the use of the
methods "setUseModelViewAndProjectionUniforms(true)" and
"setUseVertexAttributeAliasing(true)" it seems that the vertices of the models
(texture-based fragments) were not processed by the shaders (because they are
white). The other picture visualizes the scene without the using of shaders.
Because of the fact, that in all probability my understanding of this topic is
not correct and my code is buggy, I would be glad, if someone could comment my
mistakes or missapprehensions.
Thank you!
regards,
Felix
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=48953#48953
Attachments:
http://forum.openscenegraph.org//files/shader_used_186.jpg
http://forum.openscenegraph.org//files/without_shader_696.jpg
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org