Hi Jean,

I modified the code in the functions and it seemed fairly straightforward. 

However, I notice that all the cylinders are rendered at the origin inspite of 
me doing a glTranslate (which I have commented out now).

When I opened the osg file I noticed that the matrix had its last column as 
0,0,0,1 -> which means that the cylinder did not translate prior to rendering.

I tried a number of different things like placing a matrix transform above each 
Geode while creating the scene graph. But even that didn't quite give any 
positive results.

I have mentioned my cylinder geometry creation code below which returns a Geode.

Thanks,
Sanat.


Code:
osg::ref_ptr<osg::Geode> createCylinderGeode(osg::ref_ptr<osg::Cylinder> 
cylinder)
//osg::ref_ptr<osg::Geode> createCylinderGeode(unsigned int numSegments, float 
RADIUS, float height,
//                                             osg::Vec3 center)
{
  unsigned int numSegments = 90;
  const float angleDelta = 2.0f*osg::PI/(float)numSegments;
  const float texCoordDelta = 1.0f/(float)numSegments;
    
  //const float r = RADIUS;
  //const float h = height;
  const float r = cylinder->getRadius();
  const float h = cylinder->getHeight();
    
  float basez = -h*0.5f;
  float topz = h*0.5f;
    
  float angle = 0.0f;
  float texCoord = 0.0f;
    
  bool drawFrontFace = true;
  bool drawBackFace = false;
    
  // The only difference between the font & back face loops is that the
  //  normals are inverted and the order of the vertex pairs is reversed.
  //  The code is mostly duplicated in order to hoist the back/front face 
  //  test out of the loop for efficiency
  osg::ref_ptr<osg::Geode> cylinderGeode = new osg::Geode();
  osg::ref_ptr<osg::Geometry> cylinderBody = new osg::Geometry();
  osg::ref_ptr<osg::Geometry> cylinderTop = new osg::Geometry();
  osg::ref_ptr<osg::Geometry> cylinderBottom = new osg::Geometry();
  osg::ref_ptr<osg::Vec3Array> bodyVertexArray = new osg::Vec3Array();
  osg::ref_ptr<osg::Vec3Array> bodyNormalArray = new osg::Vec3Array();
  osg::ref_ptr<osg::Vec3Array> topVertexArray = new osg::Vec3Array();
  osg::ref_ptr<osg::Vec3Array> topNormalArray = new osg::Vec3Array();
  osg::ref_ptr<osg::Vec3Array> bottomVertexArray = new osg::Vec3Array();
  osg::ref_ptr<osg::Vec3Array> bottomNormalArray = new osg::Vec3Array();

  //Draw BODY
  glBegin(GL_TRIANGLE_STRIP);
  if (drawFrontFace) 
  {
    for(unsigned int bodyi=0;
        bodyi<numSegments;
        ++bodyi,angle+=angleDelta,texCoord+=texCoordDelta)
    {
      float c = cosf(angle);
      float s = sinf(angle);
      bodyNormalArray->push_back(osg::Vec3(c,s,0.0f));
      //glTexCoord2f(texCoord,1.0f);
      bodyVertexArray->push_back(osg::Vec3(c*r,s*r,topz));
      //glTexCoord2f(texCoord,0.0f);
      bodyVertexArray->push_back(osg::Vec3(c*r,s*r,basez));
    }  
    // do last point by hand to ensure no round off errors.
    bodyNormalArray->push_back(osg::Vec3(1.0f,0.0f,0.0f));
    //glTexCoord2f(1.0f,1.0f);
    bodyVertexArray->push_back(osg::Vec3(r,0.0f,topz));
    //glTexCoord2f(1.0f,0.0f);
    bodyVertexArray->push_back(osg::Vec3(r,0.0f,basez));
  }//if (drawFrontFace) 
  glEnd(); //glBegin(GL_TRIANGLE_STRIP);  

  //create TOP
  glBegin(GL_TRIANGLE_FAN);
  topNormalArray->push_back(osg::Vec3(0.0f,0.0f,1.0f));
  //glTexCoord2f(0.5f,0.5f);
  topVertexArray->push_back(osg::Vec3(0.0f,0.0f,topz));

  angle = 0.0f;
  texCoord = 0.0f;
  for(unsigned int topi=0;
      topi<numSegments;
      ++topi,angle+=angleDelta,texCoord+=texCoordDelta)
  {
    float c = cosf(angle);
    float s = sinf(angle);

    //glTexCoord2f(c*0.5f+0.5f,s*0.5f+0.5f);
    topVertexArray->push_back(osg::Vec3(c*r,s*r,topz));
  }
  //glTexCoord2f(1.0f,0.5f);
  topVertexArray->push_back(osg::Vec3(r,0.0f,topz));
  glEnd();
  
  //create BOTTOM
  //glTranslatef(cylunder.center.x(), cylinder.center.y(), cylinder.center.z());
  glBegin(GL_TRIANGLE_FAN);
  bottomNormalArray->push_back(osg::Vec3(0.0f,0.0f,-1.0f));
  //glTexCoord2f(0.5f,0.5f);
  bottomVertexArray->push_back(osg::Vec3(0.0f,0.0f,basez));
  angle = osg::PI*2.0f;
  //texCoord = 1.0f;
  for(unsigned int bottomi=0;
      bottomi<numSegments;
      ++bottomi, angle -= angleDelta, texCoord -= texCoordDelta)
  {
    float c = cosf(angle);
    float s = sinf(angle);

    //glTexCoord2f(c*0.5f+0.5f,s*0.5f+0.5f);
    bottomVertexArray->push_back(osg::Vec3(c*r,s*r,basez));
  }
  //glTexCoord2f(1.0f,0.5f);
  bottomVertexArray->push_back(osg::Vec3(r,0.0f,basez));
  glEnd();
  glPopMatrix();

  
  //add vertex array to geometry
  cylinderBody->setVertexArray(bodyVertexArray.get());
  cylinderBody->setNormalArray(bodyNormalArray.get());
  cylinderTop->setVertexArray(topVertexArray.get());
  cylinderTop->setNormalArray(topNormalArray.get());
  cylinderBottom->setVertexArray(bottomVertexArray.get());
  cylinderBottom->setNormalArray(bottomNormalArray.get());
  //add primitives to geometry
  cylinderBody->addPrimitiveSet(new 
osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, 0 , 
bodyVertexArray->size()));
  cylinderTop->addPrimitiveSet(new 
osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN, 0, topVertexArray->size()));
  cylinderBottom->addPrimitiveSet(new 
osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN, 0, bottomVertexArray->size()));
  //add geometry to geode
  //cylinderGeode->addDrawable(cylinderTop.get());
  //cylinderGeode->addDrawable(cylinderBottom.get());
  cylinderGeode->addDrawable(cylinderBody.get());
  return cylinderGeode.get();
}



Thanks
Sanat

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





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

Reply via email to