Example #1
0
/*
 * Allocate an Element and store a duplicate of the data pointed
 * to by obj in the Element. Modules do not get duplicated. The
 * function needs to handle each type of object separately in a
 * case statement.
 */
Element *Element_init(ObjectType type, void *obj) {
  Element *e = Element_create();
  e->type = type;
  switch (type)
  {
    case ObjNone:
      break;
    case ObjLine:
      memcpy(&(e->obj.line), obj, sizeof(Line));
      break;
    case ObjPoint:
      memcpy(&(e->obj.point), obj, sizeof(Point));
      break;
    case ObjPolyline:
      Polyline_setNULL(&(e->obj.polyline));
      Polyline_copy(&(e->obj.polyline),(Polyline*)obj);
      break;
    case ObjPolygon:
      Polygon_setNULL(&(e->obj.polygon));
      Polygon_copy(&(e->obj.polygon),(Polygon*)obj);
      break;
    case ObjCircle:
      memcpy(&(e->obj.circle), obj, sizeof(Circle));
      break;
    case ObjSphere:
      memcpy(&(e->obj.sphere), obj, sizeof(Sphere));
    case ObjPlane:
      memcpy(&(e->obj.plane), obj, sizeof(Plane));
    case ObjIdentity:
      memcpy(&(e->obj.matrix), obj, sizeof(Matrix));
      break;
    case ObjMatrix:
      memcpy(&(e->obj.matrix), obj, sizeof(Matrix));
      break;
    case ObjColor:
      Color_copy(&(e->obj.color), obj);
      break;
    case ObjBodyColor:
      Color_copy(&(e->obj.color), obj);
      break;
    case ObjSurfaceColor:
      Color_copy(&(e->obj.color), obj);
      break;
    case ObjSurfaceCoeff:
      memcpy(&(e->obj.coeff), obj, sizeof(float));
      break;
    case ObjLight:
      //memcpy(&(e->obj.????), obj, sizeof(????));
      break;
    case ObjTexture:
      e->obj.tex = obj;
    case ObjModule:
      e->obj.module = obj;
      break;
    default:
      break;
  }
  return e;
}
Example #2
0
/*
 * Draw the module into the image using the given view transformation
 * matrix [VTM], Lighting and DrawState by traversing the list of
 * Elements. (For now, Lighting can be an empty structure.)
 */
void Module_draw(Module *md, Matrix *VTM, Matrix *GTM, DrawState *ds,
				Lighting *lighting, Image *src) {

  printf("module draw\n");

  // set the matrix LTM to identity
  Matrix LTM;
  Matrix_identity(&LTM);
  Line l;
  Point x;
  Polyline pl;
  Polygon pg;
  Circle circle;
  Matrix TM;  
  DrawState *tempDS = DrawState_create();

  Polygon_setNULL(&pg);
  Polyline_setNULL(&pl);
  
  // for each element E in the module md
  Element *e;
  e = md->head;
  
  while (e) {

    //if (e->type >= 13)
    //printf("e->type is NULL X_X\n");
    switch (e->type)
    {
      case ObjNone:
        printf("objNone\n");
        break;
        
      case ObjLine:
        printf("objline\n");
        // copy the line data in E to L
        memcpy(&l, &(e->obj.line), sizeof(Line));
        
        // transform L by the LTM, GTM, VTM
        Matrix_xformLine(&LTM, &l);
        Matrix_xformLine(GTM, &l);
        Matrix_xformLine(VTM, &l);
        
        // normalize L by the homogeneous coord
        Line_normalize(&l);
        
        // draw L using DS->color
        Line_draw(&l, src, ds->color);
        break;
        
      case ObjPoint:
        printf("objpoint\n");
        // copy the line data in E to X
        memcpy(&x, &(e->obj.point), sizeof(Point));
        
        //transform X by the LTM, GTM, VTM        
        Matrix_xformPoint(&LTM, &x, &x);
        Matrix_xformPoint(GTM, &x, &x);
        Matrix_xformPoint(VTM, &x, &x);
        
        // normalize X by the homogeneous coord
        Point_normalize(&x);
        
        // draw X using DS->color (if X is in the image)
        Point_draw(&x, src, ds->color);
        break;
        
      case ObjPolyline:
        printf("objpolyline\n");
        // copy the polyline data in E to P
        Polyline_copy(&pl, &(e->obj.polyline));
        
        //transform P by the LTM, GTM, VTM
        Matrix_xformPolyline(&LTM, &pl);
        Matrix_xformPolyline(GTM, &pl);
        Matrix_xformPolyline(VTM, &pl);
        
        //normalize P by the homogeneous coord
        Polyline_normalize(&pl);
        
        // draw P using DS->color
        Polyline_drawFrame(&pl, src, ds->color);
        break;
        
      case ObjPolygon:
        printf("objpolygon\n");
        // copy the polygon data in E to P
        Polygon_copy(&pg, &(e->obj.polygon));
        //transform P by the LTM, GTM,
        Matrix_xformPolygon(&LTM, &pg);
        Matrix_xformPolygon(GTM, &pg);
        
        // if shadePhong, store world coordinate into appropriate fields     
        if (ds->shade == ShadePhong) {
          printf("before setworld \n");
          Polygon_setWorld(&pg,pg.nVertex);
          printf("after setworld \n");
        }
	    //printf("l->nLights %d \n", lighting->nLights);
        if ((ds->shade == ShadeGouraud) || (ds->shade == ShadeFlat)) {
          // call Polygon_shade to calculate color at each vertex using p
          printf("before polygon shade \n");
          Polygon_shade(&pg, lighting, ds);
          printf("after polygon shade \n");
        }
        
        // transform by VTM
        Matrix_xformPolygon(VTM, &pg);        
        
        //Homogenize the X and Y coordinates
        Polygon_normalize(&pg);
		
		//Polygon_drawFrame(&pg,src,ds->color);
        printf("before drawshade \n");
		Polygon_drawShade(&pg, src, ds, lighting);
		printf("after drawshade \n");
		
        break;
        
        
    case ObjCircle:
        printf("objcircle\n");
        // copy the polyline data in E to P
        memcpy(&circle, &(e->obj.circle), sizeof(Circle));        
        
        //Matrix temp;
        //Matrix_identity(&temp);
        //Matrix_multiply(GTM,&LTM,&temp);
        //Matrix_multiply(&temp,VTM,&temp);
        
        Circle_drawXForm(&circle, src, ds->color, VTM);
        //Circle_draw(&circle,src,ds->color);
        break;
        
      case ObjIdentity:
        printf("identity\n");
        // LTM = I
        Matrix_identity(&LTM);
        break;
        
      case ObjMatrix:
      	printf("objmatrix\n");
        //LTM = (Matrix field of E) * LTM
        Matrix_multiply(&(e->obj.matrix), &LTM, &LTM);
        break;
        
      case ObjColor:
        printf("objcolor\n");
        Color_copy(&(ds->color),&(e->obj.color));
        break;
        
      case ObjBodyColor:
        printf("objbodycolor\n");
        Color_copy(&(ds->body),&(e->obj.color));
        //ds->body = e->obj.color;
        break;
        
      case ObjSurfaceColor:
        printf("objsurfacecolor\n");
        Color_copy(&(ds->surface),&(e->obj.color));
        //ds->surface = e->obj.color;
        break;
        
      case ObjSurfaceCoeff:
        printf("objsurfacecoeff\n");
        ds->surfaceCoeff = e->obj.coeff;
        break;
        
      case ObjLight:
      	printf("objlight\n");
        //memcpy(&(e->obj.????), (char*)obj, sizeof(????));
        break;
        
      case ObjTexture:
        printf("objtexture\n");
        ds->tex = e->obj.tex;
        break;
        
      case ObjModule:
        printf("objmodule\n");
        //TM = GTM * LTM
        Matrix_multiply(GTM, &LTM, &TM);
        
        //tempDS = DS
        DrawState_copy(tempDS, ds);
        //Module_draw( (Module field of E), VTM, TM, tempDS, Light, src );
        
        Module_draw(e->obj.module, VTM, &TM, tempDS, lighting, src);
        break;
        
      default:
        printf("default\n");
        break;
    }
    
    e = e->next;
  }
}
static PyObject *Color_deepcopy(ColorObject *self, PyObject *args)
{
	if (!mathutils_deepcopy_args_check(args))
		return NULL;
	return Color_copy(self);
}