Ejemplo n.º 1
0
void SoFCCSysDragger::finishDragCB(void *data, SoDragger *)
{
    SoFCCSysDragger *sudoThis = reinterpret_cast<SoFCCSysDragger *>(data);

    // note: when creating a second view of the document and then closing
    // the first viewer it deletes the camera. However, the attached field
    // of the cameraSensor will be detached automatically.
    SoField* field = sudoThis->cameraSensor.getAttachedField();
    if (field)
    {
        SoCamera* camera = static_cast<SoCamera*>(field->getContainer());
        if (camera->getTypeId() == SoPerspectiveCamera::getClassTypeId())
            cameraCB(sudoThis, nullptr);
    }
}
int main(int argc, char **argv)
{

  using namespace std;
  int numObjects;
  long nRows, nCols;



  char **error;

			 
  if (argc < 4)
    {
      fprintf(stderr,"Usage: lab1 <input file> <output file> <rows>\n");
      return -1;
    }

  filename = (char*)malloc(sizeof(char)*strlen(argv[2]));
  strcpy(filename, argv[2]);

  error = (char**)malloc(sizeof(char**)*10);
  nRows = strtol(argv[3],error,10);
  
  if (**error)
    {
      fprintf(stderr,"Error! use a number for rows!\n");
      return -2;
    }
  
  free(error);
  
  

  SoDB::init();
  OSUInventorScene *scene = new OSUInventorScene(argv[1]);


  //check to see if there are objects
  if (numObjects = scene->Objects.getLength() < 1)
    {
      fprintf(stderr,"Error, no objects");
      return -2;
    }
  
  //get the objects and put them into a list
  for (int i = 0; i < scene->Objects.getLength(); i++)
    {
      OSUObjectData *obj = (OSUObjectData *)scene->Objects[i];
      /*
      if (!obj->Check())
	{
	  fprintf(stderr,"Error detected in OSUObjectData for object %i.\n",i);
	  return 20;
	}
      */
      

      SoType shape_type = obj->shape->getTypeId(); 

      if (shape_type == SoSphere::getClassTypeId()) 
	{ 
	  Sphere *sp = new Sphere;


	  SoSphere * sphere = (SoSphere *) obj->shape; 
	  SoTransform * transformation = obj->transformation; 
	  SbVec3f scale_vector = transformation->scaleFactor.getValue(); 
	  SbVec3f translation_vector = transformation->translation.getValue();
	
	  sp->radius = 1;
	  sp->center = Point(0,0,0,1);
	  
	  //do the materials stuff
	  SoMaterial * material = obj->material;
	  
	  sp->color = rgb(material->diffuseColor[0][0],material->diffuseColor[0][1],material->diffuseColor[0][2]);
	  sp->specular = rgb(material->specularColor[0][0], material->specularColor[0][1],material->specularColor[0][2]);
	  sp->ambient = rgb(material->ambientColor[0][0], material->ambientColor[0][1], material->ambientColor[0][2]);
	  sp->shininess = material->shininess[0];
	  sp->trans = material->transparency[0];
			  
	  printf("Object %d has shininess %f\n", sp->object_number, sp->shininess);
	  //let's get our translation space matrix
	  //void getTranslationSpaceMatrix(SbMatrix &mat, SbMatrix &inv) const
	  SbMatrix tmat, tinv, smat, sinv, rmat, rinv;
	  
	  transformation->getTranslationSpaceMatrix(tmat, tinv);
	  //	  transformation->getScaleSpaceMatrix(smat,sinv);
	  transformation->getRotationSpaceMatrix(rmat, rinv);

	  //	  SbMatrix & multRight(const SbMatrix &m)
	  sp->M =   tmat;
	  sp->Mi =  tinv;
	  
	  sp->M.transpose();
	  sp->Mi.transpose();
	 

	  sp->type = eSPHERE;
	  

	  //texture data
    

	  SoSFImage  image;
	  int      nc;
	  SbVec2s  size;
	  const unsigned char *texelArray;

	  // get properties of object
	  //obj = (OSUObjectData *)worldptr->scene->Objects[i];
	 
	  if (obj->texture != NULL)
	    {
	  
	      image = obj->texture->image;
	      
	      
	      texelArray = image.getValue(size,nc);     // sets 'size', 'nc', 'texelArray'
	      sp->image = texelArray[0];
	      printf("\nimage value = %d\n",texelArray[0]);
	    }


	

	  
	  objects.push_back(sp);
	  sp->object_number = i;
	  
	}

      if (shape_type == SoCube::getClassTypeId()) 
	{ 
	  Box *sp = new Box;
	  
	      
	  SoSphere * sphere = (SoSphere *) obj->shape; 
	  SoTransform * transformation = obj->transformation; 
	  SbVec3f scale_vector = transformation->scaleFactor.getValue(); 
	  SbVec3f translation_vector = transformation->translation.getValue();
	  sp->center = Point(0,0,0,1);
	  
	  //do the materials stuff
	  SoMaterial * material = obj->material;
	  
	  sp->color = rgb(material->diffuseColor[0][0],material->diffuseColor[0][1],material->diffuseColor[0][2]);
	  sp->specular = rgb(material->specularColor[0][0], material->specularColor[0][1],material->specularColor[0][2]);
	  sp->ambient = rgb(material->ambientColor[0][0], material->ambientColor[0][1], material->ambientColor[0][2]);
	      sp->shininess = material->shininess[0];
	      sp->trans = material->transparency[0];
	      
	      printf("Object %d has shininess %f\n", sp->object_number, sp->shininess);
	      //let's get our translation space matrix
	      //void getTranslationSpaceMatrix(SbMatrix &mat, SbMatrix &inv) const
	      SbMatrix tmat, tinv, smat, sinv, rmat, rinv;
	      
	      transformation->getTranslationSpaceMatrix(tmat, tinv);
	      //	  transformation->getScaleSpaceMatrix(smat,sinv);
	      transformation->getRotationSpaceMatrix(rmat, rinv);
	       // list triangles
       
	    
	      //	  SbMatrix & multRight(const SbMatrix &m)
	      sp->M =   tmat;
	      sp->Mi =  tinv;
	      
	      sp->M.transpose();
	      sp->Mi.transpose();
	    

	  SoSFImage  image;
	  int      nc;
	  SbVec2s  size;
	  const unsigned char *texelArray;
	  
	  // get properties of object
	  //obj = (OSUObjectData *)worldptr->scene->Objects[i];
	  if (obj->texture != NULL)
	    {
	      
	      image = obj->texture->image;
	      
	      
	      texelArray = image.getValue(size,nc);     // sets 'size' // list triangles
	    printf("\nimage value = %d\n",texelArray[0]);
	    	      sp->image = texelArray[0];
	  }
		  
	
	  
	  
	  objects.push_back(sp);
	  sp->type = eCUBE;
	  sp->object_number = i;
	}
      
      
      if (shape_type == SoIndexedTriangleStripSet::getClassTypeId()) 
	{
	  SoIndexedTriangleStripSet * triangle_strip_set =
	    (SoIndexedTriangleStripSet *) obj->shape;
	  
	  if (obj->points == NULL)
	    {
	      cout << "  Error: Points missing for indexed triangle strip set.";
	    }
	  else if (obj->points->getTypeId() != SoCoordinate3::getClassTypeId()) 
	    {
	      cout << "  Error: Incorrect format for point list." << endl;
	      cout << "         Expected list of 3D coordinates.";
	    }
	  else 
	    {
	      SoCoordinate3 * coord = (SoCoordinate3 *) obj->points;
	      int numv = coord->point.getNum();
	      cout << "  Surface has " << numv << " vertices." << endl;
	      for (int i = 0; i < numv; i++) 
		{
		  cout << "  Vertex " << i << " = ("
		       << coord->point[i][0] << ", "
		       << coord->point[i][1] << ", "
		       << coord->point[i][2] << ")." << endl;
		}
	  
	      if (obj->normals == NULL) 
		{
		  cout << "  No stored normals." << endl;
		}
	      else 
		{
		  int num_normals = obj->normals->vector.getNum();
		  if (num_normals != numv) 
		    {
		      // assume PER_VERTEX normal binding
		      cout << "Error: Number of normals does not equal number of vertices." << endl;
		    }
		  else 
		    {
		      
		      for (int i = 0; i < num_normals; i++) {
			cout << "  Normal " << i << " = ("
			     << obj->normals->vector[i][0] << ", "
			     << obj->normals->vector[i][1] << ", "
			     << obj->normals->vector[i][2] << ")." << endl;
		      }
		    }
		}
	      /*
	      // list triangles
	      int itriangle = 0;
	      int icoord = 0;
	      int coord_list_length = triangle_strip_set->coordIndex.getNum();
	      while (icoord < coord_list_length) {
		int c0 = SO_END_STRIP_INDEX;
		int c1 = SO_END_STRIP_INDEX;
		int c2 = SO_END_STRIP_INDEX;
		while (icoord < coord_list_length &&
		       triangle_strip_set->coordIndex[icoord] != SO_END_STRIP_INDEX) {            c2 = triangle_strip_set->coordIndex[icoord];
		       if (c0 != SO_END_STRIP_INDEX && c1 != SO_END_STRIP_INDEX) {
			 cout << "  Triangle " << itriangle
			      << " coordinate indices = ( "
			      << c0 << ", " << c1 << ", " << c2 << " )." << endl;
			 itriangle++;
		       };
		       icoord++;
		       c0 = c1;
		       c1 = c2;
		       
		}
	      }
	      */
	    }	
	      
	}
      free(obj);
    }
  



  printf("Number of objects seen %d\n", objects.size());
  printf("Setting up camera\n");
  

  /*********************************************************************
   *   Camera Setup
   *
   *********************************************************************/


  SoCamera * cam = scene->Camera; 
  
  
  if (scene->Camera == NULL)
    {
      printf("No camera found.  Setting Default/n");
      camera.aspect = 1;
      camera.position = Point(0,0,0,1);
      camera.n = Vector(0,0,-1,0);
      camera.v = Vector(0,1,0,0);
      camera.u = cross(camera.n,camera.v);
      camera.height_angle = M_PI/2;
      
    }
  else 
    {
      SbVec3f camera_position = cam->position.getValue(); 
      SbRotation cam_orientation = cam->orientation.getValue(); 
      SbVec3f camera_rotation_axis; 
      float cam_rotation_angle; 
      cam_orientation.getValue(camera_rotation_axis, cam_rotation_angle); 
      float cam_aspect_ratio = cam->aspectRatio.getValue(); 
      SoType cam_type = cam->getTypeId(); 
      
      
      
      //Let's grab the aspect ratio   
      nCols =(int)( nRows*cam_aspect_ratio);   
      camera.aspect = cam_aspect_ratio;   
      printf("Number of rows is %d columns is %d with an aspect ratio of %f\n",nRows,nCols,cam_aspect_ratio);   
      
      
      // calculate camera direction and camera up direction 
      SbVec3f camera_direction, camera_up; 
      cam_orientation.multVec(SbVec3f(0, 0, -1), camera_direction); 
      cam_orientation.multVec(SbVec3f(0, 1, 0), camera_up); 
      
      camera.n = -1*Vector(camera_direction[0],camera_direction[1],camera_direction[2],0); 
      camera.v = -1*Vector(camera_up[0],camera_up[1],camera_up[2],0); 
      
      
      camera.position = Point(camera_position[0],camera_position[1],camera_position[2],1); 
      
      camera.u = cross(camera.n,camera.v);  
      
      if (cam_type == SoPerspectiveCamera::getClassTypeId()) {
	
	// camera is a perspective camera

	SoPerspectiveCamera * perspective_camera = (SoPerspectiveCamera *) cam;
	float camera_height_angle = perspective_camera->heightAngle.getValue();
	camera.height_angle = camera_height_angle;
	
      }
    }

  N = 1; 
  pixH = 2*tan(camera.height_angle/2)/nCols; 
  pixW = 2*tan(camera.height_angle/2)/nRows; 
     
  
  printf("Camera position is %f %f %f\n",camera.position.x,camera.position.y, camera.position.z);   
  printf("Camera n is %f %f %f\n",camera.n.x,camera.n.y, camera.n.z);  
  printf("Camera v is %f %f %f\n",camera.v.x,camera.v.y, camera.v.z);  
  printf("Camera u is %f %f %f\n",camera.u.x,camera.u.y, camera.u.z); 
  printf("N is %f\n",N); 
 
  
 
  //setup for lights

  // list lights
  for (int j = 0; j < scene->Lights.getLength(); j++) {
    SoLight * flight = (SoLight *) scene->Lights[j];
    SoType light_type = flight->getTypeId();
    SoSFColor lightColor;
    SbColor   lightClr;
    float     lightr,lightg,lightb;
    
    lightColor = flight->color;

    light *l = new light;
    l->color = rgb(lightClr[0],lightClr[1],lightClr[2]);
    l->intensity = flight->intensity.getValue();

    if (light_type == SoPointLight::getClassTypeId())
      {
	SoPointLight * point_light = (SoPointLight *) flight; 
	SbVec3f location = point_light->location.getValue();
    	l->center = Point(location[0],location[1],location[2],1);
	l->type = ePOINT;
      }

    else if (light_type == SoSpotLight::getClassTypeId())
      {
	
	SoSpotLight * spot_light = (SoSpotLight *) flight;
	SbVec3f location = spot_light->location.getValue();

	l->center = Point(location[0], location[1], location[2],1);
	l->theta = spot_light->cutOffAngle.getValue();
	l->drop_off = spot_light->dropOffRate.getValue();
	
	SbVec3f direction = spot_light->direction.getValue();
	Vector lightDir(direction[0], direction[1], direction[2],0);
	l->dir = lightDir;
	l->type=eHOOD;
      }
    
    else if (light_type == SoDirectionalLight::getClassTypeId())
      {
	
	l->type = eDIR;
	SoDirectionalLight * dir_light = (SoDirectionalLight *) flight;
	
	SbVec3f direction = dir_light->direction.getValue();
	Vector lightDir(direction[0], direction[1], direction[2],0);
	l->dir = lightDir;
	
      }
    

    lights.push_back(l);
  }

  //start our tracer
  trace(objects, nRows, nCols, 1);
  

  //let's free our vector pointers
  int x = objects.size() - 1;
  fprintf(stderr,"Freeing objects\n");
  while (x >= 0)
    {
      delete objects[x];
      //  printf("freeing object %d\n",x);
      x--;
    }


  x = lights.size() - 1;
  fprintf(stderr,"Freeing lights\n");
  while (x >= 0)
    {
      delete lights[x];
      x--;
    }
  
    
}
Ejemplo n.º 3
0
/*!
  The string returned from this function is only valid until the next variable is
  requested.
*/
const char *
SoScXMLStateMachine::getVariable(const char * key) const
{
  if (strncmp(key, "_event.", 7) == 0) {
    // printf("scan for key '%s'\n", key);
    const char * subkey = key + 7;
    const ScXMLEvent * ev = this->getCurrentEvent();
    if (ev->isOfType(SoScXMLEvent::getClassTypeId())) {
      const SoScXMLEvent * soev = static_cast<const SoScXMLEvent *>(ev);
      const SoEvent * coinev = soev->getSoEvent();

      if (strcmp(subkey, "getTime()") == 0) {
        SbTime timeval = coinev->getTime();
        double doubletime = timeval.getValue();
        PRIVATE(this)->varstring = SbStringConvert::toString(doubletime);
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "getPosition().x") == 0) {
        SbVec2s pos = coinev->getPosition();
        PRIVATE(this)->varstring = SbStringConvert::toString(static_cast<double>(pos[0]));
        return PRIVATE(this)->varstring.getString();
      }
      else if (strcmp(subkey, "getPosition().y") == 0) {
        SbVec2s pos = coinev->getPosition();
        PRIVATE(this)->varstring = SbStringConvert::toString(static_cast<double>(pos[1]));
        return PRIVATE(this)->varstring.getString();
      }
      else if (strcmp(subkey, "getPosition()") == 0) {
        SbVec2s pos = coinev->getPosition();
        PRIVATE(this)->varstring = SbStringConvert::toString(pos);
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "getNormalizedPosition().x") == 0) {
        SbVec2f pos = coinev->getNormalizedPosition(this->getViewportRegion());
        PRIVATE(this)->varstring = SbStringConvert::toString(static_cast<double>(pos[0]));
        return PRIVATE(this)->varstring.getString();
      }
      else if (strcmp(subkey, "getNormalizedPosition().y") == 0) {
        SbVec2f pos = coinev->getNormalizedPosition(this->getViewportRegion());
        PRIVATE(this)->varstring = SbStringConvert::toString(static_cast<double>(pos[1]));
        return PRIVATE(this)->varstring.getString();
      }
      else if (strcmp(subkey, "getNormalizedPosition()") == 0) {
        SbVec2f pos = coinev->getNormalizedPosition(this->getViewportRegion());
        PRIVATE(this)->varstring = SbStringConvert::toString(pos);
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "wasShiftDown()") == 0) {
        SbBool wasdown = coinev->wasShiftDown();
        PRIVATE(this)->varstring = SbStringConvert::toString<bool>(wasdown);
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "wasCtrlDown()") == 0) {
        SbBool wasdown = coinev->wasCtrlDown();
        PRIVATE(this)->varstring = SbStringConvert::toString<bool>(wasdown);
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "wasAltDown()") == 0) {
        SbBool wasdown = coinev->wasAltDown();
        PRIVATE(this)->varstring = SbStringConvert::toString<bool>(wasdown);
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "getState()") == 0 && coinev->isOfType(SoButtonEvent::getClassTypeId())) {
        const SoButtonEvent * bevent = coin_assert_cast<const SoButtonEvent *>(coinev);
        SbString enumname;
        SoButtonEvent::enumToString(bevent->getState(), enumname);
        PRIVATE(this)->varstring.sprintf("'%s'", enumname.getString());
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "getKey()") == 0 && coinev->isOfType(SoKeyboardEvent::getClassTypeId())) {
        const SoKeyboardEvent * kbevent = coin_assert_cast<const SoKeyboardEvent *>(coinev);
        SbString enumname;
        SoKeyboardEvent::enumToString(kbevent->getKey(), enumname);
        PRIVATE(this)->varstring.sprintf("'%s'", enumname.getString());
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "getPrintableCharacter()") == 0 && coinev->isOfType(SoKeyboardEvent::getClassTypeId())) {
        const SoKeyboardEvent * kbevent = coin_assert_cast<const SoKeyboardEvent *>(coinev);
        char printable = kbevent->getPrintableCharacter();
        PRIVATE(this)->varstring.sprintf("'%c'", printable);
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "getButton()") == 0 && coinev->isOfType(SoMouseButtonEvent::getClassTypeId())) {
        const SoMouseButtonEvent * mbevent = coin_assert_cast<const SoMouseButtonEvent *>(coinev);
        SbString enumname;
        SoMouseButtonEvent::enumToString(mbevent->getButton(), enumname);
        PRIVATE(this)->varstring.sprintf("'%s'", enumname.getString());
        return PRIVATE(this)->varstring.getString();
      }

      else if (strcmp(subkey, "getButton()") == 0 && coinev->isOfType(SoSpaceballButtonEvent::getClassTypeId())) {
        const SoSpaceballButtonEvent * mbevent = coin_assert_cast<const SoSpaceballButtonEvent *>(coinev);
        SbString enumname;
        SoSpaceballButtonEvent::enumToString(mbevent->getButton(), enumname);
        PRIVATE(this)->varstring.sprintf("'%s'", enumname.getString());
        return PRIVATE(this)->varstring.getString();
      }

      // FIXME: x., .y, .z
      else if (strcmp(subkey, "getTranslation()") == 0 && coinev->isOfType(SoMotion3Event::getClassTypeId())) {
        const SoMotion3Event * m3event = coin_assert_cast<const SoMotion3Event *>(coinev);
        SbVec3f translation = m3event->getTranslation();
        PRIVATE(this)->varstring = SbStringConvert::toString(translation);
        return PRIVATE(this)->varstring.getString();
      }

      // FIXME: .angle, .axis
      else if (strcmp(subkey, "getRotation()") == 0 && coinev->isOfType(SoMotion3Event::getClassTypeId())) {
        const SoMotion3Event * m3event = coin_assert_cast<const SoMotion3Event *>(coinev);
        SbRotation rotation = m3event->getRotation();
        PRIVATE(this)->varstring = SbStringConvert::toString(rotation);
        return PRIVATE(this)->varstring.getString();
      }

      // FIXME: make this into a evaluator-level RayPick(SbVec2f) function instead
      else if (strcmp(key + 7, "pickposition3") == 0) {
        SbVec2s location2 = coinev->getPosition();
        SoRayPickAction rpa(this->getViewportRegion());
        rpa.setPoint(location2);
        rpa.apply(this->getSceneGraphRoot());
        SoPickedPoint * pp = rpa.getPickedPoint();
        if (pp) {
          SbVec3f pickpos = pp->getPoint();
          PRIVATE(this)->varstring = SbStringConvert::toString(pickpos);
        } else {
          PRIVATE(this)->varstring.sprintf("FALSE"); // need a valid undefined-value
        }
        return PRIVATE(this)->varstring.getString();
      }
    }
  }

  else if (strncmp(key, "coin:", 5) == 0) {
    const char * subkey = key + 5;
    if (strncmp(subkey, "camera.", 7) == 0) {
      SoCamera * camera = this->getActiveCamera();
      if (!camera) {
        SoDebugError::post("SoScXMLStateMachine::getVariable",
                           "queried for camera, but no camera is set.");
        return NULL;
      }
      const char * detail = subkey + 7;
      if (strcmp(detail, "getTypeId()") == 0) {
        PRIVATE(this)->varstring.sprintf("'%s'", camera->getTypeId().getName().getString());
        return PRIVATE(this)->varstring.getString();
      }
    }

    // get generic field access working and intercept for more So-specific stuff
    // coin:viewport
    // coin:camera
    // coin:scene
  }

  //else {
  //}

  // couldn't resolve the symbol - try parent class to get '_data' and other '_event'
  // locations resolved
  return inherited::getVariable(key);
}