Example #1
0
/**
  Walk recursively through a node and switch on all lights.

  \param node World object to search for lights
 */
void GLRenderInstance::applyLights(WorldObject& node)
{
  double M[16];
  int idx=0;

  WorldObject::ChildIterator it;
  for(it=node.childsBegin(); it!=node.childsEnd(); it++)
  {
    glPushMatrix();
    // Set the local transformation
    it->second->localTransform().toList(M);
    glMultMatrixd(M);

    // Check if the object is a light source...
    LightSource* lgt = dynamic_cast<LightSource*>(it->second.get());
    if (lgt!=0)
    {
      // Is it a point light?
      GLPointLight* pntlgt = dynamic_cast<GLPointLight*>(lgt);
      if (pntlgt!=0)
      {
        if (pntlgt->enabled.getValue())
        {
          pntlgt->applyGL(idx);
          glEnable(GL_LIGHT0+idx);
          idx++;
        }
      }
      // Is it a spot light?
      GLSpotLight* spotlgt = dynamic_cast<GLSpotLight*>(lgt);
      if (spotlgt!=0)
      {
        if (spotlgt->enabled.getValue())
        {
          spotlgt->applyGL(idx);
          glEnable(GL_LIGHT0+idx);
          idx++;
        }
      }
      // Is it a distant light?
      GLDistantLight* dstlgt = dynamic_cast<GLDistantLight*>(lgt);
      if (dstlgt!=0)
      {
        if (dstlgt->enabled.getValue())
        {
          dstlgt->applyGL(idx);
          glEnable(GL_LIGHT0+idx);
          idx++;
        }
      }
    }

    // Search the children for lights
    applyLights(*(it->second));
    glPopMatrix();
  }
}
Example #2
0
/**
  Draw a tree.

  \param node Node
  \param draw_blends If true, only objects that use blending are drawn. Otherwise objects with
         blending are not drawn.
  \return True if the tree contained one or more objects that use blending
 */
bool GLRenderInstance::drawNode(WorldObject& node, bool draw_blends)
{
  double M[16];
  BoundingBox bb;
  vec3d bmin, bmax, t, d;
  bool render_flag = true;
  bool res = false;

  WorldObject::ChildIterator it;
  for(it=node.childsBegin(); it!=node.childsEnd(); it++)
  {
    glPushMatrix();
    // Set the local transformation
    it->second->localTransform().toList(M);
    glMultMatrixd(M);
    // Draw the geom (if visible)
    boost::shared_ptr<GeomObject> geom = it->second->getGeom();
    if (geom.get()!=0 && (it->second->visible.getValue()))
    {
      // Render by default if draw_blends is false
      render_flag = !draw_blends;

      // Set material
      boost::shared_ptr<Material> mat = it->second->getMaterial();
      Material* bmat = dynamic_cast<Material*>(mat.get());
      // Check if the object should be postponed because blending is used...
      if (bmat!=0)
      {
        if (bmat->usesBlending())
        {
          res = true;
          render_flag = draw_blends;
        }
      }

      if (render_flag)
      {
        // Set material
        glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT);
        if (bmat!=0)
        {
          bmat->applyGL();
        }

        if (draw_solid)
        {
          clearGLError();
          geom->drawGL();
        }
        // Draw bounding box
        if (draw_bboxes)
        {
          bb = geom->boundingBox();
          if (!bb.isEmpty())
          {
            glDisable(GL_LIGHTING);
            bb.getBounds(bmin, bmax);
            t = 0.5*(bmax+bmin);
            d = bmax-bmin;
            glPushMatrix();
            glTranslated(t.x, t.y, t.z);
            drawWireCube(d.x, d.y, d.z);
            glPopMatrix();
            glEnable(GL_LIGHTING);
          }
        }
        glPopAttrib();  // restore material
      }
    }
    // Draw the children
    res |= drawNode(*(it->second), draw_blends);
    glPopMatrix();
  }
  return res;
}