Thanks Robert!!

I got it worked. There is some minor issue though .

The mouse interaction is not functional. I checked the simplegl3 example and 
the mouse interaction is working fine there. What i could have missed again ?

Here goes the whole source so that you can try it in your end.


Code:

#include <osg/ref_ptr>
#include <osgDB/Registry>
#include <osgDB/WriteFile>
#include <osg/Notify>
#include <osg/Geode>
#include <osg/GraphicsContext>
#include <osg/Geometry>
#include <osg/Shader>
#include <osg/Program>
#include <osg/Camera>
#include <osgViewer/Viewer>

#include <iostream>

using std::endl;

osg::ref_ptr<osg::Node> createSceneGraph();

int main()
{
  osg::ref_ptr<osg::Node> root = createSceneGraph();

  const std::string version("3.1");
  osg::ref_ptr<osg::GraphicsContext::Traits> traits = new 
osg::GraphicsContext::Traits();
  traits->x = 20;
  traits->y = 30;
  traits->width = 800;
  traits->height = 600;
  traits->windowDecoration = true;
  traits->doubleBuffer = true;
  traits->glContextVersion = version;


  osg::ref_ptr< osg::GraphicsContext > gc = 
osg::GraphicsContext::createGraphicsContext( traits.get() );

  if( !gc.valid() )
   {
       osg::notify( osg::FATAL ) << "Unable to create OpenGL v" << version << " 
context." << std::endl;
       return( 1 );
   }

  osg::ref_ptr<osg::Camera> camera = new osg::Camera;
  camera->setGraphicsContext(gc.get());
  camera->setViewport(new osg::Viewport(0,0,traits->width,traits->height));

  osgViewer::Viewer viewer;
  viewer.setCamera(camera.get());
  viewer.setSceneData(root.get());

  return viewer.run() ;
}

osg::ref_ptr<osg::Node> createSceneGraph()
{
  //create an object to store the geometry in
  osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;

  //create an array of four vertices
  osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;

  v->push_back(osg::Vec3(-0.8f, -0.8f, 0.0f));
  v->push_back(osg::Vec3(0.8f, -0.8f, 0.0f));
  v->push_back(osg::Vec3(0.0f,  0.8f, 0.0f));

//  geom->setVertexArray(v.get());

  //make sure that vertex array is used
  geom->setUseDisplayList(false);
  geom->setVertexAttribArray(0,v.get());
  geom->setVertexAttribBinding(0,osg::Geometry::BIND_PER_VERTEX);

  //create an array of three colors
  osg::ref_ptr<osg::Vec3Array> c = new osg::Vec3Array;
  c->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
  c->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
  c->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));

//  geom->setColorArray(c.get());
  geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
  geom->setVertexAttribArray(1,c.get());
  geom->setVertexAttribBinding(1,geom->getColorBinding());


  //draw a three-vertex tringle from the stored data
  geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,3));

  //add the geometry to the geode and return the geode
  osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  geode->addDrawable(geom.get());


  osg::ref_ptr<osg::StateSet> primitiveState = geode->getOrCreateStateSet();


  osg::ref_ptr<osg::Program> basicPrimitiveShaderProg = new osg::Program;

  osg::ref_ptr<osg::Shader> 
primitiveVertexShader(osg::Shader::readShaderFile(osg::Shader::VERTEX,"shader/basic.vert"));
  osg::ref_ptr<osg::Shader> 
elephantFragmentShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT,"shader/basic.frag"));

  basicPrimitiveShaderProg->addShader(primitiveVertexShader.get());
  basicPrimitiveShaderProg->addShader(elephantFragmentShader.get());

  basicPrimitiveShaderProg->addBindAttribLocation("VertexPosition",0);
  basicPrimitiveShaderProg->addBindAttribLocation("VertexColor",1);

  
primitiveState->setAttributeAndModes(basicPrimitiveShaderProg.get(),osg::StateAttribute::ON);


  return geode.get();
}




And the shaders are :


Code:

#version 400

in vec3 VertexPosition;
in vec3 VertexColor;

out vec3 Color;

void main()
{
    Color = VertexColor;

    gl_Position = vec4(VertexPosition,1.0);
}




And the fragment shader


Code:

#version 400

in vec3 Color;

out vec4 FragColor;

void main()
{
        FragColor = vec4(Color,1.0);
}




Please try it on your end and let me know!!

Regards
Sajjadul

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=56633#56633





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to