Thanks for your reply!
Yes, I am using osg_ suffix instead gl_. The only gl_ variable that I am using
is gl_Position, but I think this is OK - correct me if I am wrong.
Actually my shader is very simple. I am just trying to draw the raw normals of
the cow.
The problem with the textures cames with the elements that don't use any
shader. In the example, the quads that you can see in the background have a
yellow and gray texture respectively don't use any shader. However, If I use
setUseVertexAttributeAliasing(true), the quads lose the textures and acquire
this weird dark-green-red color.
Vertex shader:
Code:
#version 400
uniform mat4 osg_ModelViewProjectionMatrix;
in vec4 osg_Vertex;
in vec3 osg_Normal;
out vec3 myNormal;
void main()
{
myNormal = osg_Normal;
gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;
}
Fragment shader:
Code:
#version 400
in vec3 myNormal;
void main()
{
vec3 nNormal = normalize(myNormal);
outColor = vec4(nNormal,1.0);
}
main code:
Code:
Geode* drawQuad(osg::Vec3 A, osg::Vec3 B, osg::Vec3 C, osg::Vec3 D, std::string
texture_path)
{
osg::Vec2 tcA(0,0);
osg::Vec2 tcB(1,0);
osg::Vec2 tcC(1,1);
osg::Vec2 tcD(0,1);
Geode* geode = new osg::Geode();
//*****TRIANGLE 1*****//
osg::Geometry* triGeometry1 = new osg::Geometry();
//Vertices
osg::Vec3Array* triVertices1 = new osg::Vec3Array();
triVertices1->push_back(A); // bottom left
triVertices1->push_back(B); // bottom right
triVertices1->push_back(D); // top left
triGeometry1->setVertexArray( triVertices1 );
//Order
osg::DrawElementsUInt* triBase1 = new
osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
triBase1->push_back(0);
triBase1->push_back(1);
triBase1->push_back(2);
triGeometry1->addPrimitiveSet(triBase1);
//Text coord
osg::Vec2Array* texcoords1 = new osg::Vec2Array(3);
(*texcoords1)[0].set(tcA.x(),tcA.y()); // tex coord for vertex 0 (A)
(*texcoords1)[1].set(tcB.x(),tcB.y()); // tex coord for vertex 1 (B)
(*texcoords1)[2].set(tcD.x(),tcD.y()); // tex coord for vertex 2 (D)
triGeometry1->setTexCoordArray(0,texcoords1);
geode->addDrawable(triGeometry1);
//*****TRIANGLE 2*****//
osg::Geometry* triGeometry2 = new osg::Geometry();
//Vertices
osg::Vec3Array* triVertices2 = new osg::Vec3Array();
triVertices2->push_back(B); // bottom right
triVertices2->push_back(C); // top right
triVertices2->push_back(D); // top left
triGeometry2->setVertexArray( triVertices2 );
//Order
osg::DrawElementsUInt* triBase2 = new
osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
triBase2->push_back(0);
triBase2->push_back(1);
triBase2->push_back(2);
triGeometry2->addPrimitiveSet(triBase2);
//Text coord
osg::Vec2Array* texcoords2 = new osg::Vec2Array(3);
(*texcoords2)[0].set(tcB.x(),tcB.y()); // tex coord for vertex 0 (B)
(*texcoords2)[1].set(tcC.x(),tcC.y()); // tex coord for vertex 1 (C)
(*texcoords2)[2].set(tcD.x(),tcD.y()); // tex coord for vertex 2 (D)
triGeometry2->setTexCoordArray(0,texcoords2);
geode->addDrawable(triGeometry2);
//****TEXTURE****//
//Load image
osg::Image* image = osgDB::readImageFile(texture_path);
//Set up texture
texture = new osg::Texture2D(image);
texture->setDataVariance(osg::Object::DYNAMIC);
texture->setInternalFormat(GL_RGBA);
texture->setResizeNonPowerOfTwoHint(false);
texture->setFilter(osg::Texture::MIN_FILTER,
osg::Texture::LINEAR_MIPMAP_LINEAR);
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);
//Apply texture
osg::StateSet* ss = new osg::StateSet();
ss->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
geode->setStateSet(ss);
return geode;
}
int main()
{
osgViewer::Viewer viewer;
osg::Group* root = new osg::Group();
root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
root->getOrCreateStateSet()->setMode( GL_CULL_FACE, osg::StateAttribute::ON );
//Set up viewer
viewer.setSceneData( root );
osgGA::TrackballManipulator* cm = new osgGA::TrackballManipulator();
viewer.setCameraManipulator(cm);
viewer.getCamera()->setClearColor(osg::Vec4(0.0,0.0,0.0,1));
viewer.realize();
//Graphics Context
osg::State* state = viewer.getCamera()->getGraphicsContext()->getState();
state->setUseModelViewAndProjectionUniforms(true);
state->setUseVertexAttributeAliasing(true);
//Build scene
/* A,B,C,D are osg::Vec3 */
root->addChild(drawQuad(A,B,C,D,"texture1.gif"));
root->addChild(drawQuad(E,F,G,H"texture2.gif"));
osg::Group* cowG = new osg::Group();
cowG ->addChild(osgDB::readNodeFile("cow.osg"));
root->addChild(cowG);
//Shaders
osg::StateSet* shaderState = cowG->getOrCreateStateSet();
osg::Program* program = new osg::Program;
osg::Shader* vertexShader = new osg::Shader( osg::Shader::VERTEX );
osg::Shader* fragmentShader = new osg::Shader( osg::Shader::FRAGMENT );
program->addShader( vertexShader );
program->addShader( fragmentShader );
loadShaderSource( vertexShader, "Shaders/showNormals.vert" );
loadShaderSource( fragmentShader, "Shaders/showNormals.frag" );
shaderState->setAttributeAndModes(program, osg::StateAttribute::ON);
//Main loop
while( !viewer.done() )
{
viewer.frame();
}
return 0;
}
I had to make the code simpler, so probably I miss something. If you find
something confusing, feel free to ask :)
Best wishes
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60652#60652
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org