Exemplo n.º 1
0
Arquivo: vector.c Projeto: 198767/cyy2
int vect_strcmp(const vector vector1,const vector vector2)
{
	int ret = 0;
	vector vector_temp1;
	vector vector_temp2;
	if(!vector1 || !vector2) //空指针 出错
		return -1;
	if(vect_getelmtype(vector1) !=STRING || vect_getelmtype(vector2) !=STRING) //保存的不是字符串 出错
		return -1;
	if(vect_getlength(vector1) !=vect_getlength(vector2))	//保存的字符串个数不一致 肯定不相等
		return 1;	
	//拷贝副本
	vector_temp1=vect_copy(vector1);
	if(!vector_temp1)
		return -1;
	vector_temp2=vect_copy(vector2);
	if(!vector_temp2)
		return -1;
	vect_sortstr(vector_temp1);
	vect_sortstr(vector_temp2);

	ret = vect_strcmp_order(vector_temp1, vector_temp2);
	vect_free(vector_temp1);
	vect_free(vector_temp2);
	return (ret);
}
Exemplo n.º 2
0
void CWindModifier::m_Force (float4 framepos, AD_Vect3D *pos, AD_Vect3D *vel, AD_Vect3D *accel)
{
  AD_Vect3D posrel, tf, p, force, p2;
  float4 dist, freq, turb;

  freq=p_Frequency;
  turb=p_Turbolence;

  if (p_Mapping==FORCE_PLANAR)
  {
    if (p_Decay!=0.0f)
	{
	  vect_sub(pos, &p_CurrentPosition, &posrel);
	  dist=fabsf(vect_dot(&p_Force, &posrel));
      vect_scale(&p_ScaledForce, (float4)exp(-p_Decay*dist), accel);
	}
	else vect_copy(&p_ScaledForce, accel);
  } 
  else
  {
    vect_sub(pos, &p_CurrentPosition, &force);
    dist = vect_length(&force);
    if (dist != 0) vect_auto_scale(&force, 1.0f/dist);
    if (p_Decay != 0)
      vect_scale(&force, p_ScaledStrength*(float4)exp(-p_Decay*dist), accel);
	else
      vect_scale(&force, p_ScaledStrength, accel);
  }

  if (turb != 0)
  {
    vect_sub(pos, &p_CurrentPosition, &p2);
    freq *= 0.01f;

	vect_copy(&p2, &p);
    p.x  = freq * framepos;
    tf.x = noise3(p.x*p_Scale, p.y*p_Scale, p.z*p_Scale);

	vect_copy(&p2, &p);
    p.y  = freq * framepos;
    tf.y = noise3(p.x*p_Scale, p.y*p_Scale, p.z*p_Scale);

	vect_copy(&p2, &p);    
	p.z  = freq * framepos;
    tf.z = noise3(p.x*p_Scale, p.y*p_Scale, p.z*p_Scale);

    turb *= 0.0001f*forceScaleFactor;
    vect_auto_scale(&tf, turb);
	vect_add(accel, &tf, accel);
  }
}
Exemplo n.º 3
0
void AD_FFDModifier::Map2(AD_Vect3D *pos, AD_Vect3D *out)
{
	AD_Vect3D q, pp, v;
	float kw;
	
	mat_mulvect(&tm, pos, &pp);

	if (invol)
	{
       if (pp.x<-EPSILON || pp.x>1.0f+EPSILON)
	   {
		  vect_copy(pos, out);
          return;
	   }
       if (pp.y<-EPSILON || pp.y>1.0f+EPSILON)
	   {
		  vect_copy(pos, out);
          return;
	   }
       if (pp.z<-EPSILON || pp.z>1.0f+EPSILON)
	   {
		  vect_copy(pos, out);
          return;
	   }
	}


	q.x=q.y=q.z=0;
	// Compute the deformed point as a weighted average of all
	// 64 control points.
	for (int i=0; i<dim1; i++)
	{
		for (int j=0; j<dim2; j++)
		{
			for (int k=0; k<dim3; k++)
			{
				v=cpoints[GRIDINDEX22(i,j,k)];
				kw=BPoly2(i, pp.x)*BPoly2(j, pp.z)*BPoly2(k, pp.y);
				v.x=v.x*kw;
				v.y=v.y*kw;
				v.z=v.z*kw;
				vect_add_inline(&q, &v, &q);
			}
		}
	}
	
	mat_mulvect(&invtm, &q, out);
	return;
}
Exemplo n.º 4
0
void CGravityModifier::m_Force (float4 framepos, AD_Vect3D *pos, AD_Vect3D *vel, AD_Vect3D *accel)
{
    AD_Vect3D posrel, force;
    float4 dist;

    if (p_Mapping==FORCE_PLANAR)
	{
	  if (p_Decay!=0)
	  {
	    vect_sub(pos, &p_CurrentPosition, &posrel);
        dist = fabsf(vect_dot(&p_Force, &posrel));
		vect_scale(&p_ScaledForce, (float4)exp(-p_Decay*dist), accel);
	  }
	  else
	  vect_copy(&p_ScaledForce, accel);
	}
    else
	{
	  vect_sub(&p_CurrentPosition, pos, &force);
      dist = vect_length(&force);
      if (dist != 0) vect_auto_scale(&force, 1.0f/dist);
      if (p_Decay != 0) 
	  {
        vect_scale(&force, p_ScaledStrength*(float4)exp(-p_Decay*dist), accel);
	  }
	  else
	    vect_scale(&force, p_ScaledStrength, accel);
	}
}
Exemplo n.º 5
0
void CCamera::m_Ray (float4 screenX, float4 screenY, int32 space, Ray *out)
{
   // screenX e screenY vanno nel range [-1;1]
   AD_Vect3D cam_x, cam_y, cam_z;
   float4 fx, fy;

   if (!out) return;

   fx=(float4)tan(p_CurrentFov*0.5);
   fy=(float4)tan(p_CurrentFov*0.5);

   if (space==WORLDSPACE_RAY)
   {
      vect_copy(&p_CurrentPosition, &out->base);
      mat_get_row(&p_CurrentRotationMatrix, 0, &cam_x);
      mat_get_row(&p_CurrentRotationMatrix, 1, &cam_y);
      mat_get_row(&p_CurrentRotationMatrix, 2, &cam_z);
      vect_auto_scale(&cam_x, screenX*fx);
      vect_auto_scale(&cam_y, screenY*fy*480.0f/640.0f);
      vect_add(&cam_x, &cam_y, &out->direction);
      vect_auto_add(&out->direction, &cam_z);
      vect_auto_normalize(&out->direction);
   }
   else
   if (space==CAMERASPACE_RAY)
   {
      vect_set(&out->base, 0, 0, 0);
      vect_set(&out->direction, screenX*fx, screenY*fy, 1);
   }
   vect_auto_normalize(&out->direction);
}
Exemplo n.º 6
0
void AD_NoiseModifier::Map(AD_Vect3D *pos, AD_Vect3D *out)
{
   AD_Vect3D d, sp, p;

   p.x=pos->x-center.x;
   p.y=pos->y-center.y;
   p.z=pos->z-center.z;

   sp.x=(0.5f + p.x*scale);
   sp.y=(0.5f + p.y*scale);
   sp.z=(0.5f + p.z*scale);

   if (fractal)
   {
      /*
      d.x = fBm1(Point3(sp.y,sp.z,time),rough,2.0f,iterations);
      d.y = fBm1(Point3(sp.x,sp.z,time),rough,2.0f,iterations);
      d.z = fBm1(Point3(sp.x,sp.y,time),rough,2.0f,iterations);
	  */
      vect_copy(pos, out);
	  return;
   }
   else
   {
      d.x = noise3(sp.z, time, sp.y);
      d.z = noise3(sp.x, time, sp.y);
      d.y = noise3(sp.x, time, sp.z);
   }

   out->x = (p.x + d.x*strength.x) + center.x;
   out->y = (p.y + d.y*strength.y) + center.y;
   out->z = (p.z + d.z*strength.z) + center.z;
}
int32 CGeometricObject::m_CopyTEX1Vertex(int32 baseIndex, void *dest,
										 CMesh *mesh)
{
   FVFGeometryUV1 *l_dest;
   int32 i;

   l_dest=(FVFGeometryUV1 *)dest;
   /*if (!p_BaseMaterial->m_MapNeedUVTransform(1))
     for (i=0; i<mesh->p_NumDriverVertex; i++)
	 {
	    vect_copy(&mesh->p_DriverVertex[i].point, &l_dest[i+baseIndex].point);
	    vect_copy(&mesh->p_DriverVertex[i].normal, &l_dest[i+baseIndex].normal);
        l_dest[i+baseIndex].uv1.u=mesh->p_DriverVertex[i].uv1.u;
        l_dest[i+baseIndex].uv1.v=-mesh->p_DriverVertex[i].uv1.v;
	 }
   else*/
     for (i=0; i<mesh->p_NumDriverVertex; i++)
	 {
	    //vect_copy(&mesh->p_DriverVertex[i].point, &l_dest[i+baseIndex].point);
        vect_sub(&mesh->p_DriverVertex[i].point, &p_Pivot, &l_dest[i+baseIndex].point);
	    vect_copy(&mesh->p_DriverVertex[i].normal, &l_dest[i+baseIndex].normal);
        l_dest[i+baseIndex].uv1.u=mesh->p_DriverVertex[i].uv1.u;
        l_dest[i+baseIndex].uv1.v=mesh->p_DriverVertex[i].uv1.v;
	 }
   return (mesh->p_NumDriverVertex);
}
int32 CGeometricObject::m_Clone(CGeometricObject *source)
{
   if (!source) return(0);

   strcpy(p_FatherName, source->p_FatherName);
   
   p_Father=source->p_Father;
   p_HasChildrens=source->p_HasChildrens;

   p_BaseMaterial=source->p_BaseMaterial;
   for (int32 i=0; i<MAX_LODS; i++)
	  p_Lods[i]=source->p_Lods[i];
   p_NumLods=source->p_NumLods;

   p_VertexBuffer=source->p_VertexBuffer;
   p_StaticVertex=source->p_StaticVertex;

   p_PosTrack=source->p_PosTrack;
   p_RotTrack=source->p_RotTrack;
   p_ScaleTrack=source->p_ScaleTrack;

   vect_copy(&source->p_Pivot, &p_Pivot);
   vect_copy(&source->p_CurrentPosition, &p_CurrentPosition);
   quat_copy(&source->p_CurrentRotationQuaternion, &p_CurrentRotationQuaternion);
   mat_copy(&source->p_CurrentRotationMatrix, &p_CurrentRotationMatrix);
   vect_copy(&source->p_CurrentScale, &p_CurrentScale);
   vect_copy(&source->p_TotalScale, &p_TotalScale);
   mat_copy(&source->p_WorldMatrix, &p_WorldMatrix);

   if (source->p_SPHEREBoundVolume)
   {
      p_SPHEREBoundVolume=new CBoundVolume;
      source->p_SPHEREBoundVolume->m_Copy(CLONE, p_SPHEREBoundVolume);
   }
   if (source->p_AABBBoundVolume)
   {
      p_AABBBoundVolume=new CBoundVolume;
      source->p_AABBBoundVolume->m_Copy(CLONE, p_AABBBoundVolume);
   }
   if (source->p_OBBBoundVolume)
   {
      p_OBBBoundVolume=new CBoundVolume;
      source->p_OBBBoundVolume->m_Copy(CLONE, p_OBBBoundVolume);
   }
   return(1);
}
int32 CGeometricObject::m_CopyTEX1DOT3Vertex(int32 baseIndex, void *dest, CMesh *mesh)
{
   VSGeometryUV1DOT3 *l_dest;
   int32 i;

   l_dest=(VSGeometryUV1DOT3 *)dest;

   for (i=0; i<mesh->p_NumDriverVertex; i++)
   {
	 //vect_copy(&mesh->p_DriverVertex[i].point, &l_dest[i+baseIndex].point);
     vect_sub(&mesh->p_DriverVertex[i].point, &p_Pivot, &l_dest[i+baseIndex].point);
	 vect_copy(&mesh->p_DriverVertex[i].normal, &l_dest[i+baseIndex].normal);
     l_dest[i+baseIndex].uv1.u=mesh->p_DriverVertex[i].uv1.u;
     l_dest[i+baseIndex].uv1.v=mesh->p_DriverVertex[i].uv1.v;
     vect_copy(&mesh->p_DriverVertex[i].bumpspace.T, &l_dest[i+baseIndex].T);
   }
   return (mesh->p_NumDriverVertex);
}
Exemplo n.º 10
0
/* Transform the given vector */
void vect_transform (Vector v1, Vector v2, Matrix mat)
{
    Vector tmp;

    tmp[X] = (v2[X] * mat[0][0]) + (v2[Y] * mat[1][0]) + (v2[Z] * mat[2][0]) + mat[3][0];
    tmp[Y] = (v2[X] * mat[0][1]) + (v2[Y] * mat[1][1]) + (v2[Z] * mat[2][1]) + mat[3][1];
    tmp[Z] = (v2[X] * mat[0][2]) + (v2[Y] * mat[1][2]) + (v2[Z] * mat[2][2]) + mat[3][2];

    vect_copy (v1, tmp);
}
Exemplo n.º 11
0
void AD_WindModifier::build_objectmatrix (float4 framepos)
{
   AD_Vect3D postmp, stmp;
   AD_Quaternion objrot;
   AD_Matrix posttrans, scaling, maux;

   accum_scale.x=accum_scale.y=accum_scale.z=1.0f;
   mat_identity(&currentmatrix_rot);

   // estrazione dei dati col keyframer: niente di piu' facile col c++ !!!
   if (rotationtrack.numkey>0)
   {
     rotationtrack.get_data(framepos, &objrot);
     quat_rotquat_to_matrix(&objrot, &currentmatrix_rot);
   }
   mat_copy(&currentmatrix_rot, &currentmatrix);

   if (scaletrack.numkey>0)
   {
     scaletrack.get_data(framepos, &stmp);
     mat_setmatrix_of_scaling(&scaling, stmp.x, stmp.y, stmp.z);
	 accum_scale.x*=stmp.x;
	 accum_scale.y*=stmp.y;
	 accum_scale.z*=stmp.z;
   }
   else mat_identity(&scaling);
   
   if (positiontrack.numkey>0) positiontrack.get_data(framepos, &currentpos);
   mat_setmatrix_of_pretraslation(&posttrans, &currentpos);

   mat_mul(&scaling, &currentmatrix_rot, &maux);
   mat_mul(&posttrans, &maux, &currentmatrix);
 
   if (father!=(AD_Object3D *)NULL)
   {
     mat_mulaffine(&father->currentmatrix_rot, &currentmatrix_rot, &currentmatrix_rot);
	 mat_mul(&father->currentmatrix, &currentmatrix, &currentmatrix);
     mat_mulvect(&father->currentmatrix, &currentpos, &postmp);
	 vect_copy(&postmp, &currentpos);
	 
	 accum_scale.x*=father->accum_scale.x;
	 accum_scale.y*=father->accum_scale.y;
	 accum_scale.z*=father->accum_scale.z;
   }

   mat_transpose(&currentmatrix_rot, &inverse_rotmatrix);
   mat_get_row(&currentmatrix, 1, &forza);
   vect_auto_normalize(&forza);
   vect_scale(&forza, strenght*0.00016f*forceScaleFactor, &forza);
}
Exemplo n.º 12
0
void AD_PatchObject::build_objectmatrix (float4 framepos)
// costruisce la matrice di trasformazione, che servira' poi per trasformare
// i vertici dell'oggetto;
{
   AD_Vect3D postmp, stmp;
   AD_Quaternion objrot;
   AD_Matrix posttrans, scaling, maux;

   accum_scale.x=accum_scale.y=accum_scale.z=1.0f;
   mat_identity(&currentmatrix_rot);

   // estrazione dei dati col keyframer: niente di piu' facile col c++ !!!
   if (rotationtrack.numkey>0)
   {
     rotationtrack.get_data(framepos, &objrot);
     quat_rotquat_to_matrix(&objrot, &currentmatrix_rot);
   }
   mat_copy(&currentmatrix_rot, &currentmatrix);

   if (scaletrack.numkey>0)
   {
     scaletrack.get_data(framepos, &stmp);
     mat_setmatrix_of_scaling(&scaling, stmp.x, stmp.y, stmp.z);
	 accum_scale.x=accum_scale.x*stmp.x;
	 accum_scale.y=accum_scale.x*stmp.y;
	 accum_scale.z=accum_scale.x*stmp.z;
   }
   else mat_identity(&scaling);
   
   if (positiontrack.numkey>0) positiontrack.get_data(framepos, &currentpos);
   mat_setmatrix_of_pretraslation(&posttrans, &currentpos);

   mat_mul(&scaling, &currentmatrix_rot, &maux);
   mat_mul(&posttrans, &maux, &currentmatrix);
 
   if (father!=(AD_Object3D *)NULL)
   {
     mat_mulaffine(&father->currentmatrix_rot, &currentmatrix_rot, &currentmatrix_rot);
	 mat_mul(&father->currentmatrix, &currentmatrix, &currentmatrix);
     mat_mulvect(&father->currentmatrix, &currentpos, &postmp);
	 vect_copy(&postmp, &currentpos);
	 
	 accum_scale.x*=father->accum_scale.x;
	 accum_scale.y*=father->accum_scale.y;
	 accum_scale.z*=father->accum_scale.z;
   }

   mat_transpose(&currentmatrix_rot, &inverse_rotmatrix);
}
Exemplo n.º 13
0
void AD_StretchModifier::Map(AD_Vect3D *pos, AD_Vect3D *out)
{
    float fraction, normHeight;
    float xyScale, zScale, a, b, c;
	AD_Vect3D p;
    
    if (stretch == 0 || (heightMax - heightMin == 0))
	{
        vect_copy(pos, out);
		return;
	}
    
    mat_mulvect(&tm, pos, &p);
    
    if (dolim && p.y > uplim)
        normHeight = (uplim - heightMin) / (heightMax - heightMin);
    else if (dolim && p.y < lowlim)
        normHeight = (lowlim - heightMin) / (heightMax - heightMin);
    else
        normHeight = (p.y - heightMin) / (heightMax - heightMin);

    if (stretch < 0) {   // Squash
        xyScale = (amplify * -stretch + 1.0f);
        zScale = (-1.0f / (stretch - 1.0f));
    } else {           // Stretch
        xyScale = 1.0f / (amplify * stretch + 1.0f);
        zScale = stretch + 1.0f;
    }

    // a, b, and c are the coefficients of the quadratic function f(x)
    // such that f(0) = 1, f(1) = 1, and f(0.5) = xyScale
    a = 4.0F * (1.0F - xyScale);
    b = -4.0F * (1.0F - xyScale);
    c = 1.0F;
    fraction = (((a * normHeight) + b) * normHeight) + c;
    p.x *= fraction;
    p.z *= fraction;

    if (dolim && p.y < lowlim)
	p.y += (zScale - 1.0F) * lowlim;
    else if (dolim && p.y <= uplim)
	p.y *= zScale;
    else if (dolim && p.y > uplim)
	p.y += (zScale - 1.0F) * uplim;
    else
	p.y *= zScale;

    mat_mulvect(&invtm, &p, out);
}
Exemplo n.º 14
0
void AD_Object3D::get_vertex_normal (int16 quale, AD_Vect3D *vnorm)
{
  int16 tt;
  AD_Vect3D somma;
  AD_Vertex3D *quale_ptr;

  vect_set(&somma, 0, 0, 0);
  quale_ptr=&vertex3D[quale];
  for (tt=0; tt<num_tria; tt++)
  {  
    if ((tria[tt].v1==quale_ptr) || (tria[tt].v2==quale_ptr) || (tria[tt].v3==quale_ptr))
        vect_add(&somma, &tria[tt].normal, &somma);
  }
  vect_normalize(&somma);
  vect_copy(&somma, vnorm);
}
Exemplo n.º 15
0
void CGeometricObject::m_Update(float4 frame)
{
   AD_Vect3D postmp;
   int32 i;

   if (p_HideTrack)
     p_HideTrack->m_GetData(frame, &p_Visible);

   if (p_RotTrack)
     p_RotTrack->m_GetData(frame, &p_CurrentRotationQuaternion);

   vect_set(&p_TotalScale, 1, 1, 1);
   if (p_ScaleTrack)
   {
      p_ScaleTrack->m_GetData(frame, &p_CurrentScale);
      p_TotalScale.x*=p_CurrentScale.x;
      p_TotalScale.y*=p_CurrentScale.y;
      p_TotalScale.z*=p_CurrentScale.z;
   }

   if (p_PosTrack)
       p_PosTrack->m_GetData(frame, &p_CurrentPosition);

   m_BuildWorldMatrix();

   if (p_Father)
   {
     mat_mulvect(&p_Father->p_WorldMatrix, &p_CurrentPosition, &postmp);
     vect_copy(&postmp, &p_CurrentPosition);
   }

   // update degli OSMs
   for (i=0; i<p_NumOSMs; i++) p_OSMs[i]->m_Update(frame, &p_Pivot);

   // update e del volume gerarchico + fine di tutti
   if (p_SPHEREBoundVolume)
        p_Lods[0].Mesh->p_SPHEREBoundVolume.m_Update(&p_WorldMatrix, p_SPHEREBoundVolume);
   if (p_AABBBoundVolume)
        p_Lods[0].Mesh->p_AABBBoundVolume.m_Update(&p_WorldMatrix, p_AABBBoundVolume);
   if (p_OBBBoundVolume)
        p_Lods[0].Mesh->p_OBBBoundVolume.m_Update(&p_WorldMatrix, p_OBBBoundVolume);
}
Exemplo n.º 16
0
int32 CGeometricObject::m_CopyTEX2Vertex(int32 baseIndex, void *dest,
										 CMesh *mesh)
{
   FVFGeometryUV2 *l_dest;
   int32 i;

   l_dest=(FVFGeometryUV2 *)dest;

   for (i=0; i<mesh->p_NumDriverVertex; i++)
   {
	  //vect_copy(&mesh->p_DriverVertex[i].point, &l_dest[i+baseIndex].point);
      vect_sub(&mesh->p_DriverVertex[i].point, &p_Pivot, &l_dest[i+baseIndex].point);
	  vect_copy(&mesh->p_DriverVertex[i].normal, &l_dest[i+baseIndex].normal);
      l_dest[i+baseIndex].uv1.u=mesh->p_DriverVertex[i].uv1.u;
      l_dest[i+baseIndex].uv1.v=mesh->p_DriverVertex[i].uv1.v;
      l_dest[i+baseIndex].uv2.u=mesh->p_DriverVertex[i].uv2.u;
      l_dest[i+baseIndex].uv2.v=mesh->p_DriverVertex[i].uv2.v;
   }
   return (mesh->p_NumDriverVertex);
}
Exemplo n.º 17
0
void AD_TaperModifier::Map(AD_Vect3D *pos, AD_Vect3D *out)
{
	float f, z;
	AD_Vect3D p;

	if (l == 0)
	{
       vect_copy(pos, out);
       return;
	}
	mat_mulvect(&tm, pos, &p);

	if (dolim)
	{
		if (p.y<lowlim)
		{
			z = lowlim/l;
		}
		else 
		if (p.y>uplim)
		{
			z = uplim/l;
		}
		else
		{
			z = p.y/l;
		}
	}
	else
	{	
		z = p.y/l;
	}	

	if (symmetry && z<0.0f) z = -z;	
	f =  1 + z*amount + curve*z*(1-z);	
  	if (doX) p.x *= f;
  	if (doY) p.z *= f;

	mat_mulvect(&invtm, &p, out);
}
Exemplo n.º 18
0
void AD_Object3D::precalc_radius(void)
{
  int16 i;
  float4 aux=-1.0, mn;
  AD_Vect3D midp, paux;

  vect_set(&midp, 0, 0, 0);
  for (i=0; i<num_vertex3D; i++)
  {
	vect_add(&midp, &vertex3D[i].point, &midp);
  }
  vect_scale(&midp, 1.0f/(float)num_vertex3D, &midp);
  vect_copy(&midp, &mid_point);

  for (i=0; i<num_vertex3D; i++)
  {
	vect_sub(&vertex3D[i].point, &midp, &paux);
	mn=vect_lenght(&paux);
    if (mn>aux) aux=mn;
  }
  radius=aux;
}
Exemplo n.º 19
0
void AD_TwistModifier::Map(AD_Vect3D *pos, AD_Vect3D *out)
{
   AD_Vect3D v;
   float x, y, z, a, cosine, sine;

   if (angle==0.0f)
   {
	  vect_copy(pos, out);
	  return;
   }
   
   mat_mulvect (&tm, pos, &v);
   x = v.x;
   z = v.z;
	
   if (dolim)
   {
      if (v.y < lowlim)	y = lowlim;
	  else if (v.y > uplim) y = uplim;
	       else y = v.y;
   }
   else y = v.y;
	
   if (bias>0)
   {
      float u = y/height;
      a = angle * (float)pow(fabs(u), bias);
      if (u<0.0) a = -a;
   }
   else a = y * angleOverHeight;

   cosine = cosf(a);
   sine = sinf(a);
   v.x =  cosine*x + sine*z;
   v.z = -sine*x + cosine*z;

   mat_mulvect (&invtm, &v, out);
}
Exemplo n.º 20
0
void AD_BendModifier::Map(AD_Vect3D *pos, AD_Vect3D *out)
{
   AD_Vect3D p, v;
   float x, y, c, s, yr;
   
    if (r==0)
	{
	   vect_copy(pos, out);
       return;
	}
	
    mat_mulvect (&tm, pos, &p);
	if (dolim)
	{
       if (p.y < lowlim)
	   {
          mat_mulvect (&tmBelow, &p, &v);
          mat_mulvect (&invtm, &v, out);
          return;
	   }
	   else if (p.y > uplim)
	   {
          mat_mulvect (&tmAbove, &p, &v);
          mat_mulvect (&invtm, &v, out);
          return;
	   }
	}	

	x = p.x;
	y = p.y;
	yr = y/r;
	c = cosf((float)(M_PI-yr));
	s = sinf((float)(M_PI-yr));
	p.x = r*c + r - x*c;
	p.y = r*s - x*s;

    mat_mulvect (&invtm, &p, out);
}
Exemplo n.º 21
0
int32 CGeometricObject::m_CopyVertex(int32 baseIndex, void *dest,
									 CMesh *mesh)
{
   FVFOnlyGeometry *l_dest;
   int32 i;

//   if (p_ScaleTrack)
//	 p_ScaleTrack->m_GetData(0, &p_CurrentScale);

   l_dest=(FVFOnlyGeometry *)dest;
   for (i=0; i<mesh->p_NumDriverVertex; i++)
   {
	  //vect_copy(&mesh->p_DriverVertex[i].point, &l_dest[i+baseIndex].point);
      vect_sub(&mesh->p_DriverVertex[i].point, &p_Pivot, &l_dest[i+baseIndex].point);
	  vect_copy(&mesh->p_DriverVertex[i].normal, &l_dest[i+baseIndex].normal);
/*
	  l_dest[i+baseIndex].normal.x*=p_CurrentScale.x;
      l_dest[i+baseIndex].normal.y*=p_CurrentScale.y;
	  l_dest[i+baseIndex].normal.z*=p_CurrentScale.z;
*/
   }
   return (mesh->p_NumDriverVertex);
}
Exemplo n.º 22
0
void CGeometricObject::m_DoOSMs(int32 witchLod)
{
  int32 i, j, k;
  AD_Vect3D *point_tr, pp;
  DriverVertex *l_DriverVertex;
  HRESULT hr;
  uint32 start_lock, end_lock, vsize;
  BYTE *l_Data;

  k=p_Lods[witchLod].Mesh->p_NumDriverVertex;
  l_DriverVertex=p_Lods[witchLod].Mesh->p_DriverVertex;

  vsize=p_BaseMaterial->p_InputVertexFormat.vertexSize;
  start_lock=p_Lods[witchLod].VBStart*vsize;
  end_lock=p_Lods[witchLod].VBLong*vsize;

locka:
;
  hr=p_VertexBuffer->Lock(start_lock, end_lock,
                          (BYTE **)&l_Data, D3DLOCK_NOOVERWRITE );// 0);
  if (hr!=D3D_OK) goto locka;

  for (i=0; i<k; i++)
  {
	point_tr=(AD_Vect3D *)l_Data;
	//vect_copy(&l_DriverVertex[i].point, &pp);
    vect_sub(&l_DriverVertex[i].point, &p_Pivot, &pp);
	for (j=0; j<p_NumOSMs; j++)
	{
      p_OSMs[j]->m_Map(&pp, &pp);
	}
	vect_copy(&pp, point_tr);
	l_Data+=vsize;
  }

  hr=p_VertexBuffer->Unlock();
}
Exemplo n.º 23
0
void CParticleSystem::m_Paint(void *arg1, void *arg2, void *arg3)
{
  CRenderLib *RL;
  HRESULT hr;
  ParticleVBVertex *l_Particles;
  AD_Matrix l_identity;
  int32 numLive, i, alpha;
  DWORD zw, ab;

  RL=(CRenderLib *)arg1;
  if (!RL) return;

  hr=RL->p_Device->GetRenderState(D3DRS_ALPHABLENDENABLE, &ab);
  hr=RL->p_Device->GetRenderState(D3DRS_ZWRITEENABLE, &zw);

  // matrice identita', i particle vivono gia' nello spazio mondo!!!
  mat_identity(&l_identity);
  RL->m_SetWorldMatrix(&l_identity);

  hr=RL->p_Device->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
  hr=RL->p_Device->SetRenderState(D3DRS_POINTSCALEENABLE,  TRUE);
  hr=RL->p_Device->SetRenderState(D3DRS_POINTSIZE,     F2DW(p_Attenuation0));
  hr=RL->p_Device->SetRenderState(D3DRS_POINTSIZE_MIN, F2DW(0.0f));
  //RL->p_Device->SetRenderState(D3DRS_POINTSIZE_MAX, F2DW(50.0f));
  hr=RL->p_Device->SetRenderState(D3DRS_POINTSCALE_A,  F2DW(p_Attenuation1));
  hr=RL->p_Device->SetRenderState(D3DRS_POINTSCALE_B,  F2DW(p_Attenuation2));
  hr=RL->p_Device->SetRenderState(D3DRS_POINTSCALE_C,  F2DW(p_Attenuation3));
  hr=RL->p_Device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
  hr=RL->p_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  hr=RL->p_Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  hr=RL->p_Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
  // SERVE SENNO' SONO CAZZI
  hr=RL->p_Device->SetRenderState(D3DRS_LIGHTING, FALSE);
  hr=RL->p_Device->SetRenderState(D3DRS_FOGENABLE, FALSE);

  hr=RL->p_Device->SetTexture(0, p_Texture->p_HWSurfaces[0]);
  hr=RL->p_Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  hr=RL->p_Device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  hr=RL->p_Device->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_MODULATE);
  hr=RL->p_Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
  hr=RL->p_Device->SetTextureStageState(0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1);
  hr=RL->p_Device->SetTexture(1, NULL);
  hr=RL->p_Device->SetTextureStageState(1, D3DTSS_COLOROP,   D3DTOP_DISABLE);
  hr=RL->p_Device->SetTextureStageState(1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE);
  hr=RL->p_Device->SetTexture(2, NULL);
  hr=RL->p_Device->SetTextureStageState(2, D3DTSS_COLOROP,   D3DTOP_DISABLE);
  hr=RL->p_Device->SetTextureStageState(2, D3DTSS_ALPHAOP,   D3DTOP_DISABLE);

  // Set up the vertex buffer to be rendered
  RL->p_Device->SetStreamSource(0, p_VertexBuffer, sizeof(ParticleVBVertex));
  RL->p_Device->SetVertexShader(FVF_PARTICLE);

begin:
  //hr=p_VertexBuffer->Lock(0, 0, (BYTE **)&l_Particles, D3DLOCK_NOOVERWRITE);
  hr=p_VertexBuffer->Lock(0, 0, (BYTE **)&l_Particles, 0);
  if (hr!=0) goto begin;

  numLive=0;

  if (p_FadedParticles<=0)
    for (i=0; i<p_MaxParticles; i++)
	{
	  // lock e paint con drawprimitve
	  if (p_Particles[i].age > 0)
	  {
        vect_copy(&p_Particles[i].pos, &l_Particles[numLive].pos);
        l_Particles[numLive].color=0xffffffff;
        numLive++;
	  }
	}
  else
  {
	float4 h=p_FadedParticles*p_Life;
	float4 invLife=255.0f/(p_Life-h);
	float4 fStart=0.08f*p_Life;

    for (i=0; i<p_MaxParticles; i++)
	{
	  // lock e paint con drawprimitve
	  if (p_Particles[i].age > 0)
	  {
        vect_copy(&p_Particles[i].pos, &l_Particles[numLive].pos);
		if (p_Particles[i].age<=fStart)
		{
		   alpha=(int32)(255.0f*p_Particles[i].age/fStart);
		}
		else
		if (p_Particles[i].age<=h) alpha=255;
		else
		{
          alpha=((int32)((p_Particles[i].age-h)*invLife)) & 0xFF;
	      alpha=255-alpha;
		}
        //l_Particles[numLive].color=0x00ffffff | (alpha <<24);
        //l_Particles[numLive].color=(bb) | (gg<<8) | (rr<<16) | (alpha <<24);
        l_Particles[numLive].color=p_Particles[numLive].color | (alpha <<24);
        numLive++;
	  }
	}
  }
again:
  hr=p_VertexBuffer->Unlock();
  if (hr!=0) goto again;

  if (numLive>0)
	 hr=RL->p_Device->DrawPrimitive(D3DPT_POINTLIST, 0, numLive);
  hr=RL->p_Device->SetRenderState(D3DRS_POINTSPRITEENABLE, FALSE);
  hr=RL->p_Device->SetRenderState(D3DRS_POINTSCALEENABLE,  FALSE);
  hr=RL->p_Device->SetRenderState(D3DRS_ZWRITEENABLE, zw);
  hr=RL->p_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, ab);
}
Exemplo n.º 24
0
Arquivo: xml.c Projeto: arader/crt
void start( void *data, const char *el, const char **attr ) {
    parse_data *pdata = (parse_data*)data;
    float xyz[3];

    if( pdata == NULL ) {
        fprintf( stderr, "*** xml: parse data is NULL\n" );
        exit( 1 );
    }

    if( strcmp( el, "scene" ) == 0 ) {
    }
    else if( pdata->prim == NULL ) {
        pdata->prim = (primitive*)calloc( 1, sizeof( primitive ) );

        if( strcmp( el, "sphere" ) == 0 ) {
            pdata->prim->type = SPHERE;

            pdata->prim->intersect = &sphere_isect;
            pdata->prim->normal = &sphere_normal;

            pdata->prim->data = (sphere_data*)malloc( sizeof( sphere_data ) );

            parse_light( pdata->prim, attr );
            parse_xyz( xyz, attr );
            ((sphere_data*)pdata->prim->data)->radius = parse_attrf(
                "radius", attr, 0.0f );

            ((sphere_data*)pdata->prim->data)->center.x = xyz[0];
            ((sphere_data*)pdata->prim->data)->center.y = xyz[1];
            ((sphere_data*)pdata->prim->data)->center.z = xyz[2];

            vect_copy( &pdata->prim->center,
                    &((sphere_data*)pdata->prim->data)->center );
            
            /* set some defaults */
            pdata->prim->mat.col.x = 1.0f;
            pdata->prim->mat.col.y = 1.0f;
            pdata->prim->mat.col.z = 1.0f;

            pdata->prim->mat.diffuse = 1.0f;
            pdata->prim->mat.specular = 0.0f;
            pdata->prim->mat.refl = 0.0f;
            pdata->prim->mat.is_refr = 0;
        }
        else if( strcmp( el, "plane" ) == 0 ) {
            pdata->prim->type = PLANE;

            pdata->prim->intersect = &plane_isect;
            pdata->prim->normal = &plane_normal;

            pdata->prim->data = (plane_data*)malloc( sizeof( plane_data ) );

            parse_light( pdata->prim, attr );
            parse_xyz( xyz, attr );

            ((plane_data*)pdata->prim->data)->normal.x = xyz[0];
            ((plane_data*)pdata->prim->data)->normal.y = xyz[1];
            ((plane_data*)pdata->prim->data)->normal.z = xyz[2];
            ((plane_data*)pdata->prim->data)->dist = parse_attrf(
                "distance", attr, 0.0f );

            vect_copy( &pdata->prim->center,
                    &((plane_data*)pdata->prim->data)->normal );

            /* set some defaults */
            pdata->prim->mat.col.x = 1.0f;
            pdata->prim->mat.col.y = 1.0f;
            pdata->prim->mat.col.z = 1.0f;

            pdata->prim->mat.diffuse = 1.0f;
            pdata->prim->mat.specular = 0.0f;
            pdata->prim->mat.refl = 0.0f;
            pdata->prim->mat.is_refr = 0;
        }
        else if( strcmp( el, "box" ) == 0 ) {
            pdata->prim->type = BOX;

            pdata->prim->intersect = &box_isect;
            pdata->prim->normal = &box_normal;

            pdata->prim->data = (box_data*)malloc( sizeof( box_data ) );

            parse_xyz( xyz, attr );

            ((box_data*)pdata->prim->data)->point.x = xyz[0];
            ((box_data*)pdata->prim->data)->point.y = xyz[1];
            ((box_data*)pdata->prim->data)->point.z = xyz[2];
            ((box_data*)pdata->prim->data)->size.x = parse_attrf(
                "width", attr, 0.0f );
            ((box_data*)pdata->prim->data)->size.y = parse_attrf(
                "height", attr, 0.0f );
            ((box_data*)pdata->prim->data)->size.z = parse_attrf(
                "depth", attr, 0.0f );

            pdata->prim->center.x = xyz[0] +
                (0.5f * ((box_data*)pdata->prim->data)->size.x);
            pdata->prim->center.y = xyz[1] +
                (0.5f * ((box_data*)pdata->prim->data)->size.y);
            pdata->prim->center.z = xyz[2] +
                (0.5f * ((box_data*)pdata->prim->data)->size.z);

            parse_light( pdata->prim, attr );

            /* set some defaults */
            pdata->prim->mat.col.x = 1.0f;
            pdata->prim->mat.col.y = 1.0f;
            pdata->prim->mat.col.z = 1.0f;

            pdata->prim->mat.diffuse = 1.0f;
            pdata->prim->mat.specular = 0.0f;
            pdata->prim->mat.refl = 0.0f;
            pdata->prim->mat.is_refr = 0;
        }
        else {
            fprintf( stderr,
                    "*** xml: unknown primitive type \"%s\" at line %lu\n",
                    el, XML_GetCurrentLineNumber( pdata->parser ) );
            exit( 1 );
        }

        pdata->prim->next = pdata->head;
        pdata->head = pdata->prim;
    }
    else if( strcmp( el, "material" ) == 0 ) {
        pdata->prim->mat.col.x = parse_attrf( "r", attr, 1.0f );
        pdata->prim->mat.col.y = parse_attrf( "g", attr, 1.0f );
        pdata->prim->mat.col.z = parse_attrf( "b", attr, 1.0f );

        pdata->prim->mat.diffuse = parse_attrf( "diffuse", attr, 1.0f );
        pdata->prim->mat.specular = parse_attrf( "specular", attr, 0.0f );
        pdata->prim->mat.refl = parse_attrf( "reflectivity", attr, 0.0f );
        pdata->prim->mat.is_refr = parse_attrb( "refractive", attr, 0 );
        if( pdata->prim->mat.is_refr ) {
            pdata->prim->mat.refr = parse_attrf( "refractivity", attr, 1.0f );
        }
    }
    else if( strcmp( el, "light" ) == 0 ) {
    }
}
Exemplo n.º 25
0
void AD_Object3D::paint_modifiers(float4 pos, AD_Camera *telecamera, AD_Omnilight *omnilight)
{
  int i, j, w;
  AD_Matrix matrixrot_all, matrixtransform_all;
  AD_Matrix matrixtall_clip;
  AD_Vect3D p1, p2, vtmp, p;
  AD_Vertex3D *v;
  float invz;
  // float x1, y1, z1, x2, y2, z2;

  // calcolo delle matrici di traspfromazione tra spazi
  mat_mulaffine(&telecamera->currentmatrix_rot, &currentmatrix_rot, &matrixrot_all);
  mat_mul(&telecamera->currentmatrix, &currentmatrix, &matrixtall_clip);

  // calcolo della matrice di trasformazione con inclusi i
  // fattori di aspect ratio;
  mat_copy(&matrixtall_clip, &matrixtransform_all);
  for (w=0; w<4; w++)
  {
    matrixtransform_all.a[0][w]=matrixtransform_all.a[0][w]*telecamera->prospettivaX;
    matrixtransform_all.a[1][w]=matrixtransform_all.a[1][w]*telecamera->prospettivaY;
  }

/*
  for (j=0; j<num_OSMmods; j++)
  {
//     if (j>0)
//       OSMmods[j]->set_bbox(x1, y1, z1, x2, y2, z2);
	 OSMmods[j]->update(pos);

	 // calcolo bounding box per poi settarla negli OSM
     x1=y1=z1=1E10;
     x2=y2=z2=-1E10;
     for (i=0; i<num_vertex; i++)
	 {
       OSMmods[j]->Map(&points_tr[i], &points_tr[i]);
	   if (points_tr[i].x < x1) x1 = points_tr[i].x;
	   if (points_tr[i].y < y1) y1 = points_tr[i].y;
	   if (points_tr[i].z < z1) z1 = points_tr[i].z;
	   if (points_tr[i].x > x2) x2 = points_tr[i].x;
	   if (points_tr[i].y > y2) y2 = points_tr[i].y;
	   if (points_tr[i].z > z2) z2 = points_tr[i].z;
	 }
  }
*/
  for (j=0; j<num_OSMmods; j++)
	 OSMmods[j]->update(pos);

  for (i=0; i<num_vertex3D; i++)
  {
     // trasformazione tramite OSM di tutti i vertici e
     // salvataggio dentro points_tr (i points originali devono
     // essere preservati)
     vect_copy(&vertex3D[i].point, &vertex3D[i].tpoint);
	 vertex3D[i].flags=0;
	 vect_set(&normals[i], 0, 0, 0);
     for (j=0; j<num_OSMmods; j++)
	 {
       OSMmods[j]->Map(&vertex3D[i].tpoint, &vertex3D[i].tpoint);
	 }
  }

//  TRIA_PIPELINE_ENVRGB_MODIFIERS
//  TRIA_PIPELINE_RGB_MODIFIERS
  TRIA_PIPELINE_ENVMAP_MODIFIERS
  TRIA_PIPELINE_ELSE_MODIFIERS
}
Exemplo n.º 26
0
Arquivo: crt.c Projeto: arader/crt
color* trace( ray *aray, primitive *scene, int depth, float refr, float *dist,
        int shadows ){
    intersection *isect;
    primitive *prim, *iter;
    vector isect_pt, pn, lv, ln, tmpv1, tmpv2;
    ray tmpr;
    float tmpf1, tmpf2, tmpf3, shade;
    color *tmpc;
    color *c = (color*)malloc( sizeof( color ) );
    if( c == NULL ) {
        fprintf( stderr, "*** error: could not allocate color memory\n" );
        exit( 1 );
    }

    c->x = 0.0f;
    c->y = 0.0f;
    c->z = 0.0f;

    isect = intersect( aray, scene );

    if( isect == NULL ) {
        return c;
    }

    *dist = isect->dist;

    prim = isect->prim;

    if( prim->is_light ) {
        vect_copy( c, &(isect->prim->mat.col) );
        free( isect );
        return c;
    }

    vect_copy( &isect_pt, aray->dir );
    vect_multf( &isect_pt, isect->dist );
    vect_add( &isect_pt, aray->origin );

    prim->normal( prim, &isect_pt, &pn );

    iter = scene;
    while( iter != NULL ) {
        if( iter->is_light ) {
            vect_copy( &lv, &iter->center );
            vect_sub( &lv, &isect_pt );

            vect_copy( &ln, &lv );
            vect_normalize( &ln );

            shade = calc_shade( iter, &isect_pt, &lv, &ln, scene, shadows );

            if( shade > 0.0f ) {
                /* determine the diffuse component */
                tmpf1 = prim->mat.diffuse;
                if( tmpf1 > 0.0f ) {
                    tmpf2 = vect_dot( &pn, &ln );
                    if( tmpf2 > 0.0f ) {
                        tmpf1 *= tmpf2 * shade;
                        vect_copy( &tmpv1, &prim->mat.col );
                        vect_mult( &tmpv1, &iter->mat.col );
                        vect_multf( &tmpv1, tmpf1 );
                        vect_add( c, &tmpv1 );
                    }
                }

                /* determine the specular component */
                tmpf1 = prim->mat.specular;
                if( tmpf1 > 0.0f ) {
                    vect_copy( &tmpv1, &pn );
                    vect_copy( &tmpv2, &ln );
                    tmpf2 = 2.0f * vect_dot( &ln, &pn );
                    vect_multf( &tmpv1, tmpf2 );
                    vect_sub( &tmpv2, &tmpv1 );
                    tmpf2 = vect_dot( aray->dir, &tmpv2 );
                    if( tmpf2 > 0.0f ) {
                        tmpf1 = powf( tmpf2, 20.0f ) * tmpf1 * shade;
                        vect_copy( &tmpv1, &iter->mat.col );
                        vect_multf( &tmpv1, tmpf1 );
                        vect_add( c, &tmpv1 );
                    }
                }
            }
        }

        iter = iter->next;
    }

    /* calculate reflection */
    if( prim->mat.refl > 0.0f && depth < TRACE_DEPTH ) {
        vect_copy( &tmpv1, &pn );
        vect_multf( &tmpv1, 2.0f * vect_dot( &pn, aray->dir ) );
        vect_copy( &tmpv2, aray->dir );
        vect_sub( &tmpv2, &tmpv1 );

        vect_copy( &tmpv1, &tmpv2 );
        vect_multf( &tmpv1, EPSILON );
        vect_add( &tmpv1, &isect_pt );

        tmpr.origin = &tmpv1;
        tmpr.dir = &tmpv2;

        tmpc = trace( &tmpr, scene, depth + 1, refr, &tmpf1, shadows );
        vect_multf( tmpc, prim->mat.refl );

        vect_copy( &tmpv1, &prim->mat.col );
        vect_mult( &tmpv1, tmpc );
        vect_add( c, &tmpv1 );

        free( tmpc );
    }

    /* calculate refraction */
    if( prim->mat.is_refr && depth < TRACE_DEPTH ) {
        vect_copy( &tmpv1, &pn );
        if( isect->inside ) {
            vect_multf( &tmpv1, -1.0f );
        }

        tmpf1 = refr / prim->mat.refr;
        tmpf2 = -( vect_dot( &tmpv1, aray->dir ) );
        tmpf3 = 1.0f - tmpf1 * tmpf1 * (1.0f - tmpf2 * tmpf2);
        if( tmpf3 > 0.0f ) {
            vect_copy( &tmpv2, aray->dir );
            vect_multf( &tmpv2, tmpf1 );
            vect_multf( &tmpv1, tmpf1 * tmpf2 - sqrtf( tmpf3 ) );
            vect_add( &tmpv1, &tmpv2 );

            vect_copy( &tmpv2, &tmpv1 );
            vect_multf( &tmpv2, EPSILON );
            vect_add( &tmpv2, &isect_pt );

            tmpr.origin = &tmpv2;
            tmpr.dir = &tmpv1;

            tmpc = trace( &tmpr, scene, depth + 1, refr, &tmpf1, shadows );

            vect_copy( &tmpv1, &prim->mat.col );
            vect_multf( &tmpv1, prim->mat.absorb * tmpf1 );

            tmpv2.x = expf( -tmpv1.x );
            tmpv2.y = expf( -tmpv1.y );
            tmpv2.z = expf( -tmpv1.z );

            vect_mult( tmpc, &tmpv2 );
            vect_add( c, tmpc );

            free( tmpc );
        }
    }

    free( isect );

    if( c->x > 1.0f ) {
        c->x = 1.0f;
    }
    else if( c->x < 0.0f ) {
        c->x = 0.0f;
    }

    if( c->y > 1.0f ) {
        c->y = 1.0f;
    }
    else if( c->y < 0.0f ) {
        c->y = 0.0f;
    }

    if( c->z > 1.0f ) {
        c->z = 1.0f;
    }
    else if( c->z < 0.0f ) {
        c->z = 0.0f;
    }

    return c;
}
Exemplo n.º 27
0
Arquivo: crt.c Projeto: arader/crt
float calc_shade( primitive *light, vector *isect_pt, vector *lv, vector *ln,
        primitive *scene, int shadows ) {
    float shade = 1.0f;
    float l_dist;
    int i;
    ray r;
    vector o, dir;
    intersection *isect;
    primitive *iter;

    if( light->is_light == AREA_LIGHT && shadows > 1 && light->grid != NULL ) {
        for( i = 0; i < shadows; i++ ) {
            dir.x = light->grid[(i&63)*3] +
                ((float)rand()/RAND_MAX)*light->dx;
            dir.y = light->grid[(i&63)*3+1] +
                ((float)rand()/RAND_MAX)*light->dy;
            dir.z = light->grid[(i&63)*3+2] +
                ((float)rand()/RAND_MAX)*light->dz;

            vect_sub( &dir, isect_pt );
            l_dist = vect_length( &dir );
            vect_multf( &dir, 1.0f / l_dist );

            vect_copy( &o, &dir );
            vect_multf( &o, EPSILON );
            vect_add( &o, isect_pt );

            r.origin = &o;
            r.dir = &dir;

            for( iter = scene; iter != NULL; iter = iter->next ) {
                isect = iter->intersect( iter, &r );

                if( isect != NULL &&
                        !iter->is_light && isect->dist < l_dist ) {
                    shade -= 1.0f / shadows;
                    free( isect );
                    break;
                }

                free( isect );
            }
        }
    }
    else {
        /* setup the ray */
        vect_copy( &o, ln );
        vect_multf( &o, EPSILON );
        vect_add( &o, isect_pt );

        r.origin = &o;
        r.dir = ln;

        l_dist = vect_length( lv );

        for( iter = scene; iter != NULL; iter = iter->next ) {
            isect = iter->intersect( iter, &r );

            if( isect != NULL && !iter->is_light && isect->dist < l_dist ) {
                shade = 0.0f;
                free( isect );
                break;
            }

            free( isect );
        }
    }

    return shade;
}
Exemplo n.º 28
0
void AD_Object3D::init_normals(void)
// le normali dei triangoli devono essere gia' state calcolate
// vertici triangoli e smoothing groups devono già essare in memoria
{ 
   #define normal_err 0.0001f
   // indica l'errore massimo possibile x riciclare una normale: + e' alto il numero piu'
   // si perde qualita' visiva e si guadagna velocita': bisogna trovare un buon compromesso

  int *condivisi;
  int *smooth, *nosmooth; // array di triangoli condivisi da smoothare 
  AD_Vect3D *tempnormal;
  AD_Vect3D normadd;
  int num_condivisi, i, j, tr, num_smooth, num_nosmooth;
  int num_normal, norm;
  float maxerr, err;

  condivisi=new int[num_tria]; // nel caso peggiore tutti i triangoli sono condivisi 
  smooth=new int[num_tria];
  nosmooth=new int[num_tria];
  tempnormal=new AD_Vect3D[num_tria*3]; // nel caso peggiore ci sono 3 normali per triangolo
  num_normal=0;
      
  for (i=0; i<num_vertex3D; i++)
  {
	// trovo i triangoli che condividono il vertice i
	num_condivisi=0;
	for (j=0; j<num_tria; j++)
	{
	    if ((tria[j].v1==&vertex3D[i]) ||
			(tria[j].v2==&vertex3D[i]) ||
			(tria[j].v3==&vertex3D[i]))
	    {
		   condivisi[num_condivisi]=j;
		   num_condivisi++;          
	    }
	}

	while (num_condivisi>0)
	{
	   tr=condivisi[0]; // triangolo di riferimento
	   num_smooth=0;
	   num_nosmooth=0;
	   smooth[num_smooth]=tr;
	   num_smooth++;
	   for(j=1; j<num_condivisi; j++)
	   {
	     if ((triasmoothgroup[tr] & triasmoothgroup[condivisi[j]])!=0)
	     {
		    smooth[num_smooth]=condivisi[j];
		    num_smooth++;
	     }
		 else
		 {
		    nosmooth[num_nosmooth]=condivisi[j];
		    num_nosmooth++;
		 }
	   }

	   // cacolo la normale
	   vect_set(&normadd, 0, 0, 0);
	   for (j=0; j<num_smooth; j++)
          vect_add(&normadd, &tria[smooth[j]].normal, &normadd);
	   vect_normalize(&normadd);

	   // cerco se ne esiste gia' una molto simile
	   j=0;
	   norm=-1;
	   maxerr=normal_err;
	   while (j < num_normal)
	   { 
	      // trovo l'errore massimo della normale j
	      err=fmax(fmax(fabsf(normadd.x-tempnormal[j].x),
			            fabsf(normadd.y-tempnormal[j].y)),
			       fabsf(normadd.z-tempnormal[j].z));
	      if (err < maxerr)
		  { 
              // trovata normale + precisa
			  maxerr=err;
			  norm=j;
		  }
	      j++;
	   }
	   if (norm==-1)
	   { // non trovata: la creo
	      vect_copy(&normadd, &tempnormal[num_normal]);
		  norm=num_normal;
	      num_normal++;
	   }

	   for (j=0; j<num_smooth; j++)
	   {
	     // la assegno al triangolo (cercando il vertice giusto)
	     if (tria[smooth[j]].v1==&vertex3D[i]) tria[smooth[j]].n1=&tempnormal[norm];
	     if (tria[smooth[j]].v2==&vertex3D[i]) tria[smooth[j]].n2=&tempnormal[norm];
	     if (tria[smooth[j]].v3==&vertex3D[i]) tria[smooth[j]].n3=&tempnormal[norm];
	   }
	      
	   // tolgo quelli assegnati e ricompatto gli altri
	   for (j=0; j<num_nosmooth; j++) condivisi[j]=nosmooth[j];
	   num_condivisi-=num_smooth;
	}
  }
 
  // copio la parte usata di tempnormal in normal
  normals = new AD_Vect3D[num_normal];
  num_normals=num_normal;
  for (j=0; j<num_normal; j++)
  {
	  vect_copy(&tempnormal[j], &normals[j]);
	  for (i=0; i<num_tria; i++)
	  {
		 if (tria[i].n1==&tempnormal[j]) tria[i].n1=&normals[j];
		 if (tria[i].n2==&tempnormal[j]) tria[i].n2=&normals[j];
		 if (tria[i].n3==&tempnormal[j]) tria[i].n3=&normals[j];
	  }
  }
  delete [] condivisi;
  delete [] smooth;
  delete [] nosmooth;
  delete [] tempnormal;
  delete [] triasmoothgroup;  // non servono piu'
}
Exemplo n.º 29
0
int AD_Object3D::init(void)
{
//  AD_Lod lodder;
  int k;
  AD_Quaternion q;
  AD_Matrix M;
  //AD_Vect3D v;
  float x1, y1, z1, x2, y2, z2;
  AD_OSMObject *OSMaux[100];

// ############  INIZIALIZZAZIONI DELLE KEYFRAMER  ############
  if (positiontrack.init()==-1) return(-1);
  if (rotationtrack.init()==-1) return(-1);
  if (scaletrack.init()==-1)    return(-1);
  
  // cistruisco le quantita' che mi permettono di costruire una
  // eventuale matrice di skin se questo oggetto fosse usato come
  // osso
  if (rotationtrack.numkey>0)
  {
     rotationtrack.get_data(0, &q);
     quat_rotquat_to_matrix(&q, &M);
	 mat_transpose(&M, &rot0);
  }else mat_identity(&rot0);
  if (scaletrack.numkey>0) scaletrack.get_data(0, &scale0);
  else vect_set(&scale0, 1, 1, 1);
  if (positiontrack.numkey>0) positiontrack.get_data(0, &pos0);
  else vect_set(&pos0, 0, 0, 0);

/*
  if ((positiontrack.numkey==1) && (positiontrack.posizioni[0].posintrack==0) &&
	  (father==(AD_Object3D *)NULL) && (have_childrens<=0))
  {
	currentpos=positiontrack.posizioni[0].p;
	positiontrack.numkey=0;
  }
*/

  if (type==DUMMY)
  {
  // sistemo il flare connesso; devo creare i 2 triangoli,
  // i vertici 2D, 3D (solo per linkare i v2D), il materiale
  // trasparente
    if (flare!=(texture *)NULL)
	{
	  latoX=(float)flare->dimx;
	  latoY=(float)flare->dimy;

	  // setto le uv dei vertici per il flare; i vertici sono cosi'
	  // disposti
	  // 0----1
	  // |    |
	  // |    |
	  // 2----3

	  vertex2D[0].u=0.0f*TEXTURE_PRECISION;
	  vertex2D[0].v=0.0f*TEXTURE_PRECISION;
	  vertex2D[1].u=0.99f*TEXTURE_PRECISION;
	  vertex2D[1].v=0.0f*TEXTURE_PRECISION;
	  vertex2D[2].u=0.0f*TEXTURE_PRECISION;
	  vertex2D[2].v=0.99f*TEXTURE_PRECISION;
	  vertex2D[3].u=0.99f*TEXTURE_PRECISION;
	  vertex2D[3].v=0.99f*TEXTURE_PRECISION;

	
	  // i vertici 3D li setto sempre da non clippare visto
	  // che quando devo disegnare i flare sono sicuro che non
	  // devo clipparli e solo quando disegno li inserisco nella
	  // lista dei triangoli trasparenti
	  vertex3D[0].flags=0;
	  vertex3D[1].flags=0;
	  vertex3D[2].flags=0;
	  vertex3D[3].flags=0;
	
	  // setto il materiale
	  matflare.texture_ptr=flare;
	  matflare.flags=(0 | PAINT_TEXTURE | IS_TRASPARENT);
	  matflare.trasparencytype=MIXTYPE_ADD;
	  AddUpdate_Material(&matflare);

      // ATTENZIONE: non cambiare gli indici perche' a causa
	  // del settaggio di cull2D della scheda3D i triangoli
	  // devono essere in senso antiorario
	  // setto i triangoli
	  tria[0].materiale=&matflare;
	  tria[0].v1=&vertex3D[0];
	  tria[0].v2=&vertex3D[2];
	  tria[0].v3=&vertex3D[3];
	  tria[0].sp1=&vertex2D[0];
	  tria[0].sp2=&vertex2D[2];
	  tria[0].sp3=&vertex2D[3];
	  tria[1].materiale=&matflare;
	  tria[1].v1=&vertex3D[3];
	  tria[1].v2=&vertex3D[1];
	  tria[1].v3=&vertex3D[0];
	  tria[1].sp1=&vertex2D[3];
	  tria[1].sp2=&vertex2D[1];
	  tria[1].sp3=&vertex2D[0];
	}
	return(0);
  }
  if (type==BONE) return(0);

/* NON PIU' FATTIBILE A CAUSA DEGLI OSM
------------------------------------------
-----------------------------------------
  // OTTIMIZAZIONE !!!
  if ((rotationtrack.numkey==1) && (rotationtrack.rotazioni[0].posintrack==0) &&
	  (father==(AD_Object3D *)NULL) && (have_childrens<=0))
  {
	q=rotationtrack.rotazioni[0].rotquat;
    quat_rotquat_to_matrix(&q, &M);
	rotationtrack.numkey=0;
    mat_identity(&currentmatrix_rot);
	for (k=0; k<num_vertex; k++)
	{
	  mat_mulvect(&M, &points[k], &v);
	  vect_copy(&v, &points[k]);
	}
  }
  if ((scaletrack.numkey==1) && (scaletrack.posizioni[0].posintrack==0) &&
	  (father==(AD_Object3D *)NULL) && (have_childrens<=0))
  {
	for (k=0; k<num_vertex; k++)
	{
	  points[k].x*=scaletrack.posizioni[0].p.x;
	  points[k].y*=scaletrack.posizioni[0].p.y;
	  points[k].z*=scaletrack.posizioni[0].p.z;
	}
	scaletrack.numkey=0;
  }
*/

  // calcolo bounding box per poi settarla negli OSM
  x1=y1=z1=1E10;
  x2=y2=z2=-1E10;
  for (k=0; k<num_vertex3D; k++)
  {
	 if (vertex3D[k].point.x < x1) x1 = vertex3D[k].point.x;
	 if (vertex3D[k].point.y < y1) y1 = vertex3D[k].point.y;
	 if (vertex3D[k].point.z < z1) z1 = vertex3D[k].point.z;
	 if (vertex3D[k].point.x > x2) x2 = vertex3D[k].point.x;
	 if (vertex3D[k].point.y > y2) y2 = vertex3D[k].point.y;
	 if (vertex3D[k].point.z > z2) z2 = vertex3D[k].point.z;
  }

  // inverto l'ordine degli OSM perche' vanno applicati
  // al contrario
  for (k=0; k<(int)num_OSMmods; k++)
    OSMaux[k]=OSMmods[k];

  for (k=0; k<(int)num_OSMmods; k++)
    OSMmods[num_OSMmods-k-1]=OSMaux[k];

  // Inizializzazione OSM modifiers
  for (k=0; k<(int)num_OSMmods; k++)
  {
	OSMmods[k]->init();
	OSMmods[k]->set_bbox(x1, y1, z1, x2, y2, z2);
  }

  precalc_radius();  // precalcola raggio sfera circoscritta
  init_tria();       // calcola: normale, punto medio e raggio circoscritto
  init_texture_coordinate(); // rende positive le uv
  init_vertex();  // alloca e prepara le normali ai vertici tenendo
                  // conto degli smoothing groups; inoltre setta
                  // opportunamente i campi n1, n2, n3, sp1, sp2, sp3
                  // dei triangoli
  if (vertexUV!=(AD_VectUV *)NULL) delete [] vertexUV;
  
  if (bones_matrix!=(AD_Matrix **)NULL)
  for (k=0; k<num_vertex3D; k++)
  {
     // questo perchè in fase di lettura (UtilsA3D) i punti nello
	 // spazio delle bonez vengono messi in points_tr
	 vect_copy(&vertex3D[k].tpoint, &vertex3D[k].point);
  }


// NB: IL LOD VA INSERITO IN QUESTO PUNTO !!!!
// NE' PRIMA (perche' le uv ai vertici devono essere sistemate)
// NE' DOPO (perche' i tringoli devono eseere inizializzati
// tutti (anche quelli del modello semplificato) dopo)
// [ad]TURBO ROXXXXXXXXXXXXXXXXX   
/*
  lodder.apply_lod(vertex3D, num_vertex,
	                tria, num_tria,
					70,
					&vertex3D, &num_vertex,
					&tria, &num_tria,
					GEOMETRIC_LOD);
*/
  split_tria_list();
  return(0);
}
Exemplo n.º 30
0
void AD_Object3D::build_objectmatrix (float4 framepos)
// costruisce la matrice di trasformazione, che servira' poi per trasformare
// i vertici dell'oggetto;
{
   AD_Vect3D postmp, stmp, ptmp;
   AD_Quaternion objrot;
   AD_Matrix posttrans, scaling, maux, pretrans;

   accum_scale.x=accum_scale.y=accum_scale.z=1.0f;
   mat_identity(&currentmatrix_rot);

   if (rotationtrack.numkey>0)
   {
     rotationtrack.get_data(framepos, &objrot);
     quat_rotquat_to_matrix(&objrot, &currentmatrix_rot);
   }
   mat_copy(&currentmatrix_rot, &currentmatrix);

   if (scaletrack.numkey>0)
   {
     scaletrack.get_data(framepos, &stmp);
     mat_setmatrix_of_scaling(&scaling, stmp.x, stmp.y, stmp.z);
	 accum_scale.x*=stmp.x;
	 accum_scale.y*=stmp.y;
	 accum_scale.z*=stmp.z;
   } else mat_identity(&scaling);
   if (positiontrack.numkey>0) positiontrack.get_data(framepos, &currentpos);
   mat_setmatrix_of_pretraslation(&posttrans, &currentpos);

   if (type==BONE)
   {
      mat_mul(&scaling, &currentmatrix_rot, &maux);
      mat_mul(&posttrans, &maux, &currentmatrix);
   }
   else 
   {
      mat_mul(&currentmatrix_rot, &scaling, &maux);
      mat_mul(&posttrans, &maux, &currentmatrix);
   }

   // per le bone si costruiscono gia' le matrici comprese
   // di padre; per oggetti non bone invece si costruiscono
   // matrici relative e quindi devo 'accodarci' pure
   // la trasformazione del padre
   if (type!=BONE)
   {
      if (father!=(AD_Object3D *)NULL)
	  {
        mat_mulaffine(&father->currentmatrix_rot, &currentmatrix_rot, &currentmatrix_rot);
	    mat_mul(&father->currentmatrix, &currentmatrix, &currentmatrix);
        mat_mulvect(&father->currentmatrix, &currentpos, &postmp);
	    vect_copy(&postmp, &currentpos);
	    accum_scale.x*=father->accum_scale.x;
	    accum_scale.y*=father->accum_scale.y;
	    accum_scale.z*=father->accum_scale.z;
	  }
   }
   mat_transpose(&currentmatrix_rot, &inverse_rotmatrix);

   if (skinned_object!=(AD_Object3D *)NULL)
   {
      if (father==(AD_Object3D *)NULL)
	  {
		  vect_copy(&currentpos, &ptmp);
		  vect_neg(&ptmp, &ptmp);
          mat_setmatrix_of_pretraslation(&pretrans, &ptmp);
          vect_neg(&ptmp, &ptmp);
          mat_setmatrix_of_pretraslation(&posttrans, &ptmp);

          mat_copy(&pretrans, &maux);

		  mat_mul(&currentmatrix_rot, &maux, &maux);
          mat_mul(&rot0, &maux, &maux);
          mat_mul(&scaling, &maux, &maux);
          mat_setmatrix_of_scaling(&scaling, 1.0f/scale0.x, 1.0f/scale0.y, 1.0f/scale0.z);
          mat_mul(&scaling, &maux, &maux);
		  //vect_sub(&currentpos, &pos0);
		  mat_mul(&posttrans, &maux, &skin_matrix);
	  }
	  else
	  {
		  /*
		  if (positiontrack.numkey>0) positiontrack.get_data(framepos, &ptmp);
		  vect_neg(&ptmp, &ptmp);
          mat_setmatrix_of_pretraslation(&pretrans, &ptmp);
          vect_neg(&ptmp, &ptmp);
          mat_setmatrix_of_pretraslation(&posttrans, &ptmp);

          mat_mul(&father->inverse_rotmatrix, &currentmatrix_rot, &maux);
		  maux.a[0][3]=maux.a[1][3]=maux.a[2][3]=0;
		  mat_mul(&maux, &pretrans, &maux);
          mat_mul(&scaling, &maux, &maux);
          mat_mul(&posttrans, &maux, &skin_matrix);

	      mat_mul(&father->skin_matrix, &skin_matrix, &skin_matrix);
		  */
          mat_copy(&father->skin_matrix, &skin_matrix);
	  }
   }
}