Ejemplo n.º 1
0
void TransformState::applyAccumulatedOffset() {
  LayoutSize offset = m_accumulatedOffset;
  m_accumulatedOffset = LayoutSize();
  if (!offset.isZero()) {
    if (m_accumulatedTransform) {
      translateTransform(offset);
      flatten();
    } else {
      translateMappedCoordinates(offset);
    }
  }
}
Ejemplo n.º 2
0
void TransformState::move(const LayoutSize& offset, TransformAccumulation accumulate)
{
    if (accumulate == FlattenTransform || !m_accumulatedTransform)
        m_accumulatedOffset += offset;
    else {
        applyAccumulatedOffset();
        if (m_accumulatingTransform && m_accumulatedTransform) {
            // If we're accumulating into an existing transform, apply the translation.
            translateTransform(offset);

            // Then flatten if necessary.
            if (accumulate == FlattenTransform)
                flatten();
        } else
            // Just move the point and/or quad.
            translateMappedCoordinates(offset);
    }
    m_accumulatingTransform = accumulate == AccumulateTransform;
}
Ejemplo n.º 3
0
//==========Functions=============
void readFile(char *fname)
{
  char filename[1024]; //holds filename
  FILE *SceneFile; //points to the file
  char cmd[512]; // holds a command
  char Buff[2048];
  int line=1; //keep track of line #s

  GLdouble x, y, z, angle, ni;
  char axis;
    
  //Initialize number of lights & objects
  numLights = 0; 
  numObjs = 0;

  //Start reading scene with identity transform
  loadIdentityTransform(&currentTransform[curGroupLevel]);

  //Open the file
  if ((SceneFile = fopen(fname, "r")) == NULL) 
  {
      printf("Error opening scene file \n");
      exit(1);
  }
  fscanf(SceneFile, "%s", cmd);  //stores first word into cmd

  //Loop to read contents of the file
  while (!feof(SceneFile))
  {
      if (!strcmp(cmd, "view")) //compares string to view
      {
          fscanf(SceneFile, "%d", &view.size);
          //printf("View size: %d\n", view.size);
          fscanf(SceneFile, "%lf", &view.d);
          //printf("View distance to corner of image plane: %f\n", view.d);
      }
      else if (!strncmp(cmd, "#", 1)) 
      {
          /* Comment, let's ignore! */
          fgets(Buff, 2048, SceneFile); //takes in till \n is found
      }
      else if (!strcmp(cmd, "group")) 
      {
          ++curGroupLevel;
          //printf("Current group level: %d\n", curGroupLevel);

          // get transform for previous group level
          currentTransform[curGroupLevel] = currentTransform[curGroupLevel-1]; 
      }
      else if (!strcmp(cmd, "groupend")) 
      {
          --curGroupLevel;
          //printf("Current group level: %d\n", curGroupLevel);
      }
      else if (!strcmp(cmd, "background"))
      {
          fscanf(SceneFile, "%lf", &backgroundColor.red);
          fscanf(SceneFile, "%lf", &backgroundColor.green);
          fscanf(SceneFile, "%lf", &backgroundColor.blue);
      }
      else if (!strcmp(cmd, "ambient"))
      {
          fscanf(SceneFile, "%lf", &ambient_light.red);
          fscanf(SceneFile, "%lf", &ambient_light.green);
          fscanf(SceneFile, "%lf", &ambient_light.blue);
      }
      else if (!strcmp(cmd, "light"))
      {
          if (numLights>=MAX_LIGHTS)
          {
              fprintf(stderr, "Error: max number of lights exceeded in description file\n");
              fprintf(stderr, "Line %d\n", line);
              exit(-1);
          }
          fscanf(SceneFile,"%lf", &lightSources[numLights].color.red);
          fscanf(SceneFile,"%lf", &lightSources[numLights].color.green);
          fscanf(SceneFile,"%lf", &lightSources[numLights].color.blue);
          fscanf(SceneFile,"%lf", &lightSources[numLights].position.x);
          fscanf(SceneFile,"%lf", &lightSources[numLights].position.y);
          fscanf(SceneFile,"%lf", &lightSources[numLights].position.z);
          //printf("Color red: %lf\n", lightSources[numLights].color.red);
          //printf("Color green: %lf\n", lightSources[numLights].color.green);
          //printf("Color blue: %lf\n", lightSources[numLights].color.blue);
          //printf("Position x: %lf\n", lightSources[numLights].position.x);
          //printf("Position y: %lf\n", lightSources[numLights].position.y);
          //printf("Position z: %lf\n", lightSources[numLights].position.z);

          numLights++;
      }
      else if (!strcmp(cmd, "sphere"))
      {
          objects[numObjs].transform = currentTransform[curGroupLevel];
          objects[numObjs].material = currentMaterial;
          numObjs++;
          if (numObjs>MAX_OBJECTS)
          {
              fprintf(stderr, "Error: max number of objects exceeded in description file\n");
              fprintf(stderr, "Line %d\n", line);
              exit(-1);
          }
      }
      else if (!strcmp(cmd, "material"))
      {
          fscanf(SceneFile, "%lf", &currentMaterial.diffuse.red);
          fscanf(SceneFile, "%lf", &currentMaterial.diffuse.green);
          fscanf(SceneFile, "%lf", &currentMaterial.diffuse.blue);
          fscanf(SceneFile, "%lf", &currentMaterial.specular.red);
          fscanf(SceneFile, "%lf", &currentMaterial.specular.green);
          fscanf(SceneFile, "%lf", &currentMaterial.specular.blue);
          fscanf(SceneFile, "%lf", &currentMaterial.shininess);
          // What do I do with this?
          // you need to put this into the object
      }
      else if (!strcmp(cmd, "refraction"))
      {
          //needs to be setup later
      }
      else if (!strcmp(cmd, "move"))
      {
        fscanf(SceneFile, "%lf", &x);
        fscanf(SceneFile, "%lf", &y);
        fscanf(SceneFile, "%lf", &z);

        //save current matrix off to the side
        tempSave = currentTransform[curGroupLevel];

        //set current matrix to identity (loadIdentityTransoform b/c 
        // we can't send in our current matrix)
        loadIdentityTransform(&currentTransform[curGroupLevel]);

        //apply passed in move to identity. so that it's first (reversing
        //order)
        applyTransform(translateTransform(x,y,z),&currentTransform[curGroupLevel]);  

        //now put save matrix on to transform passed in. (reversing order)
        applyTransform( tempSave, &currentTransform[curGroupLevel]); 
      }
      else if (!strcmp(cmd, "scale"))
      {
        //printf("Scale\n");
        fscanf(SceneFile, "%lf", &x);
        fscanf(SceneFile, "%lf", &y);
        fscanf(SceneFile, "%lf", &z);

        //save current matrix off to the side
        tempSave = currentTransform [curGroupLevel];

        //set current matrix to identity (loadIdentTransform b/c we can't send in
        //our current matrix)
        loadIdentityTransform(&currentTransform[curGroupLevel]);
        //apply passed in scale to identity. so that it's first (reversing
        //order)
        applyTransform( scaleTransform(x, y, z), &currentTransform[curGroupLevel]); 

        //now put save matrix on to transform passed in. (reversing order)
        applyTransform( tempSave, &currentTransform[curGroupLevel]);  
      }
      else if (!strcmp(cmd, "rotate"))
      {
        fscanf(SceneFile, "%lf", &angle);
        fscanf(SceneFile, "%lf", &x);
        fscanf(SceneFile, "%lf", &y);
        fscanf(SceneFile, "%lf", &z);

        //save current matrix off to the side
        tempSave = currentTransform[curGroupLevel];

        //set current matrix to identity (loadIdentTransform b/c we can't send in
        //our current matrix)
        loadIdentityTransform(&currentTransform[curGroupLevel]);

        //apply passed in rotate to identity. so that it's first (reversing
        //order)
        applyTransform(rotateTransform(angle,x,y,z), &currentTransform[curGroupLevel]); 
        //now put save matrix on to transform passed in. (reversing order)
        applyTransform( tempSave, &currentTransform[curGroupLevel]);  
      }
      else
      {
          fprintf(stderr, "Error: Unknown cmd '%s' in description file\n", cmd);
          fprintf(stderr, "Line %d\n", line);
          exit(-1);
      }
      fscanf(SceneFile, "%s", cmd); //get next command
      line++;
  }

  fclose(SceneFile); //close the scene file
}