Пример #1
0
void ParseOBJFile(const char *pFilePtr)
{
	const char *pToken = GetNextToken(pFilePtr);

	while(*pToken != 0)
	{
		if(!MFString_CaseCmp(pToken, "o"))
		{
			const char *pName = GetRestOfLine(pFilePtr);

			pModel->name = pName;
		}
		else if(!MFString_CaseCmp(pToken, "g"))
		{
			const char *pName = GetRestOfLine(pFilePtr);

			if(!vertsInGroup)
			{
				// we'll just rename the current subobject, since theres nothing in it..
				F3DSubObject &sub = pModel->GetMeshChunk()->subObjects[subObject];
				sub.name = pName;
			}
			else
			{
				// probably wanna copy vertex data in at this point..
				// and subtract the min from each of the components indices..
				CopyDataIntoSubobject(subObject);

				++subObject;

				matSub = 0;

				minVertIndex = -1;
				minUVIndex = -1;
				minNormIndex = -1;
				maxVertIndex = -1;
				maxUVIndex = -1;
				maxNormIndex = -1;

				vertsInGroup = false;
				vertsInMatSub = false;

				F3DSubObject &sub = pModel->GetMeshChunk()->subObjects[subObject];
				sub.name = pName;
			}
		}
		else if(!MFString_CaseCmp(pToken, "v"))
		{
			const char *pX = GetNextToken(pFilePtr);
			const char *pY = GetNextToken(pFilePtr);
			const char *pZ = GetNextToken(pFilePtr);
			pFilePtr = MFSeekNewline(pFilePtr);

			MFVector v;
			v.x = (float)atof(pX);
			v.y = (float)atof(pY);
			v.z = (float)atof(pZ);
			v.w = 1.0f;

			verts.push(v);
		}
		else if(!MFString_CaseCmp(pToken, "vt"))
		{
			const char *pU = GetNextToken(pFilePtr);
			const char *pV = GetNextToken(pFilePtr);
			pFilePtr = MFSeekNewline(pFilePtr);

			MFVector v;
			v.x = (float)atof(pU);
			v.y = (float)atof(pV);
			v.z = 0.0f;
			v.w = 1.0f;

			uvs.push(v);
		}
		else if(!MFString_CaseCmp(pToken, "vn"))
		{
			const char *pX = GetNextToken(pFilePtr);
			const char *pY = GetNextToken(pFilePtr);
			const char *pZ = GetNextToken(pFilePtr);
			pFilePtr = MFSeekNewline(pFilePtr);

			MFVector v;
			v.x = (float)atof(pX);
			v.y = (float)atof(pY);
			v.z = (float)atof(pZ);
			v.w = 1.0f;

			normals.push(v);
		}
		else if(!MFString_CaseCmp(pToken, "f"))
		{
			vertsInGroup = true;
			vertsInMatSub = true;

			F3DSubObject &sub = pModel->GetMeshChunk()->subObjects[subObject];

			const char *pRestOfLine = GetRestOfLine(pFilePtr);

			int firstVert = (int)sub.matSubobjects[matSub].vertices.size();

			pToken = GetNextToken(pRestOfLine);

			while(*pToken)
			{
				const char *pPos = GetNextIndex(pToken);
				const char *pUV = GetNextIndex(pToken);
				const char *pNorm = GetNextIndex(pToken);

				int posid = atoi(pPos);
				int texid = atoi(pUV);
				int normid = atoi(pNorm);

				if(posid < 0)
					posid = (int)verts.size() - posid;
				else
					posid = posid - 1;

				if(texid < 0)
					texid = (int)uvs.size() - texid;
				else
					texid = texid - 1;

				if(normid < 0)
					normid = (int)normals.size() - normid;
				else
					normid = normid - 1;

				minVertIndex = minVertIndex == -1 ? posid : MFMin(minVertIndex, posid);
				minUVIndex = minUVIndex == -1 ? texid : MFMin(minUVIndex, texid);
				minNormIndex = minNormIndex == -1 ? normid : MFMin(minNormIndex, normid);
				maxVertIndex = minVertIndex == -1 ? posid : MFMax(maxVertIndex, posid);
				maxUVIndex = maxUVIndex == -1 ? texid : MFMax(maxUVIndex, texid);
				maxNormIndex = maxNormIndex == -1 ? normid : MFMax(maxNormIndex, normid);

				int vi = (int)sub.matSubobjects[matSub].vertices.size();
				int f = vi - firstVert;

				F3DVertex &vert = sub.matSubobjects[matSub].vertices[firstVert + f];
				vert.position = posid;
				vert.uv[0] = texid;
				vert.normal = normid;

				// add a triangle if we are up to the third vert or beyond
				if(f >= 2)
				{
					F3DTriangle &tri = sub.matSubobjects[matSub].triangles.push();

					tri.v[0] = firstVert;
					tri.v[1] = vi-1;
					tri.v[2] = vi;
				}

				pToken = GetNextToken(pRestOfLine);
			}
		}
		else if(!MFString_CaseCmp(pToken, "usemtl"))
		{
			F3DSubObject &sub = pModel->GetMeshChunk()->subObjects[subObject];

			if(vertsInGroup && vertsInMatSub)
			{
				++matSub;
				vertsInMatSub = false;
			}

			const char *pName = GetRestOfLine(pFilePtr);

			sub.matSubobjects[matSub].materialIndex = GetMaterialID(pName);
		}
		else if(!MFString_CaseCmp(pToken, "mtllib"))
		{
			// load material info?
			//..
			
			pFilePtr = MFSeekNewline(pFilePtr);
		}
		else if(pToken[0] == '#')
		{
			pFilePtr = MFSeekNewline(pFilePtr);
		}
		else
		{
			MFDebug_Warn(2, MFStr("Unknown token encountered in obj file '%s'!", pToken));
			pFilePtr = MFSeekNewline(pFilePtr);
		}

		pToken = GetNextToken(pFilePtr);
	}

	// want to copy vertex data into the last subobject at this point...
	if(vertsInGroup)
	{
		CopyDataIntoSubobject(subObject);
	}
}
Пример #2
0
struct bwb_line *
bwb_OPEN(struct bwb_line * l)
{
   /* OPEN filename [FOR mode] AS filenumber [LEN reclen] */
   char            filename[BasicStringLengthMax + 1];
   char            mode[BasicStringLengthMax + 1];
   char            filenumber[BasicStringLengthMax + 1];
   char            reclen[BasicStringLengthMax + 1];
   char            OutputBuffer[BasicStringLengthMax + 1];
   int             p;

   bwx_DEBUG(__FUNCTION__);

   /* OPEN filename [FOR mode] AS */
   if (GetKeyword(l, filename, " FOR "))
   {
      /* OPEN filename FOR */
      if (GetKeyword(l, mode, " AS "))
      {
         /* FOR mode AS */
      }
      else
      {
         bwb_error("syntax error");
      }
   }
   else
   if (GetKeyword(l, filename, " AS "))
   {
      /* OPEN filename AS */
      strcpy(mode, "BINARY"); /* default for structured OPEN */
   }
   else
   {
      bwb_error("syntax error");
   }

   /* AS filenumber [LEN reclen] */
   if (GetKeyword(l, filenumber, " LEN "))
   {
      /* AS filenumber LEN reclen */
      GetRestOfLine(l, reclen);
   }
   else
   {
      /* AS filenumber */
      GetRestOfLine(l, filenumber);
      strcpy(reclen, "128");  /* default for structured OPEN */
   }

   OutputBuffer[0] = '\0';
   strcat(OutputBuffer, "OPEN(");
   strcat(OutputBuffer, "\"");
   strcat(OutputBuffer, mode);
   strcat(OutputBuffer, "\"");
   strcat(OutputBuffer, ",");
   strcat(OutputBuffer, filenumber);
   strcat(OutputBuffer, ",");
   strcat(OutputBuffer, filename);
   strcat(OutputBuffer, ",");
   strcat(OutputBuffer, reclen);
   strcat(OutputBuffer, ")");


   p = 0;
   bwb_exp(OutputBuffer, FALSE, &p);
   if (ERROR_PENDING)
   {
      /* oops */
   }
   return bwb_zline(l);
}
Пример #3
0
void ReadLinkageParameterFile(void)
{
  int i;
  int linenr, locinumber;
  int numloci, risk, xlink, program;
  int locus_type, num_alleles, nmarkers;
  char *traitname;
  char tmpbuf[256];
  FreqList *fl;
  namelist *nl;
  

  printf("Name of parameter file: ");
  InputLine(buf, BUFFERSIZE);

  // Deletes old info
  FreeDataInfo();

  DeletePedigreeData();
  DeleteParameterData();

  cfopen (buf,"r");

  linenr = 0;
  locinumber = 1;
  nmarkers = 0;


  // Read line 1
  ReadNextNonEmptyLine();
  numloci = atoip (igetstr (buf));
  risk = geti();
  xlink = geti();
  program = geti();

  if (numloci==0)
  {
    printf("WARNING: No loci in parameter file\n");
    return;
  }

  InitializeFrequencies(numloci);
  freelist(markernames);
  markernames = 0;

  // Read line 2 and 3. Not used yet
  ReadNextNonEmptyLine();

  ReadNextNonEmptyLine();
  // Saves the order for later
  strncpy(tmpbuf, buf, sizeof(tmpbuf));

  // Read all locus/traits
  while (locinumber <= numloci)
  {
    ReadNextNonEmptyLine();

    locus_type = atoip (igetstr (buf));
    num_alleles = geti();
    traitname = GetRestOfLine();

    switch (locus_type)
    {
      case 1 : break;
      case 3 : // Read a marker locus
               fl = FrequencyNumber(locinumber);

               fl->num_alleles = num_alleles;

               // Read in the allele frequencies
	       ReadNextNonEmptyLine();
               fl->frequency[1] = atof(igetstr(buf));
               for (i=2; i<= num_alleles; i++)
               {
                 fl->frequency[i] = getf();
               }

               // Increase the number of markers in the dataset
               nmarkers++;

               // Saves the marker name
               nl = cmalloc(sizeof(namelist));
               memset(nl, 0, sizeof(namelist));
               
               if (strlen(traitname)==0)
                 sprintf(nl->name, "Marker %d",locinumber);
               else
               {
                 // remove initial # if any
                 traitname +=strspn(traitname, "# ") ;
                 strncpy(nl->name, traitname, sizeof(nl->name));
               }
               addlist(&markernames, nl);
               break;
      case 4 : // Read a quantitative trait
               break;
      default: printf("Unknown locus type (%d)\nExiting\n", locus_type);
    }
    locinumber++;

    #ifdef debug
    printf("Found locus (%s) type %d with %d alleles\n", nl->name, locus_type, num_alleles);
    #endif
  }

  ReadNextNonEmptyLine();
  // Now reads the recombination fractions for markers
  ReadNextNonEmptyLine();
  if (nmarkers>1)
  {
    distance = vector(1,nmarkers-1);
    distance[1] = atof(igetstr(buf));
    for (i = 2; i<nmarkers; i++)
      distance[i] = atof(getstr());
  }

  ReadNextNonEmptyLine();
  ReadNextNonEmptyLine();

  fclose(F);

  // Fixes order
  order = ivector(1,nmarkers);
  invorder = ivector(1,nmarkers);
  OrderMarkers(tmpbuf);

}