<commit_before>/*
    Copyright (C) 2001 by <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "cal3d/cal3d.h"
#include <stdio.h>
#include <stdarg.h>

#include "qsqrt.h"

void ifprintf(FILE *f, int indent, const char *fmt, ...) {
  for(int i=0; i<indent; i++) {
    fputc(' ', f);
  }

  va_list va;
  va_start(va, fmt);
  vfprintf(f, fmt, va);
  va_end(va);
}

void StripExt(char *dst, const char *src) {
  strcpy(dst, src);
  char *tmp=strrchr(dst, '.');
  if(tmp)
    *tmp=0;
}

void ReplaceExt(char *dst, const char *src, const char *ext) {
  StripExt(dst, src);
  strcat(dst, ext);
}

void ToLower(char *dst, const char *src) {
  char *d=dst;
  const char *s=src;
  for(; *s; s++, d++) {
    *d=tolower(*s);
  }
  *d=0;
}

void WriteBone(FILE *f, int ind, float scale, CalCoreSkeleton *skel, CalCoreBone *bone) {
  ifprintf(f, ind, "LIMB '%s' (\n", bone->getName().c_str());
#if 1
  const CalVector &pos=bone->getTranslation();
  const CalQuaternion &rot=bone->getRotation();
  ifprintf(f, ind, "TRANSFORM(V(%f,%f,%f)Q(%f,%f,%f,%f))\n", pos.x*scale, pos.y*scale, pos.z*scale, rot.x, rot.y, rot.z, rot.w);
#endif

  int *ptr=(int*)bone->getUserData();
  if(ptr) {
    ifprintf(f, ind, "VERTICES (");
    for(int i=0; i<ptr[0]; i++) {
      fprintf(f, "%d,", ptr[i+1]);
    }
    fprintf(f, ")\n");
  }

  std::list<int>::iterator iteratorChildId;
  for(iteratorChildId = bone->getListChildId().begin(); iteratorChildId != bone->getListChildId().end(); ++iteratorChildId)
  {
    CalCoreBone *child=skel->getCoreBone(*iteratorChildId);
    WriteBone(f, ind+1, scale, skel, child);
  }

  ifprintf(f, ind, ")\n");
}

void CalQuatInverse(CalQuaternion &qdest, const CalQuaternion &qin) {
  float inverselen=1.0f / qsqrt(qin[0]*qin[0]+qin[1]*qin[1]+qin[2]*qin[2]+qin[3]*qin[3]);
  qdest[0]=-inverselen*qin[0];
  qdest[1]=-inverselen*qin[1];
  qdest[2]=-inverselen*qin[2];
  qdest[3]=inverselen*qin[3];
}

int ExportSprite(const char* filename, float scale, float timescale, CalCoreModel &calCoreModel, CalModel & calModel) {
  char name[255];
  StripExt(name, filename);

  FILE *f=fopen(filename, "w");
  int ind=0;
  ifprintf(f, ind++, "LIBRARY 'soldier' (\n");

  ifprintf(f, ind++, "PLUGINS (\n");
  ifprintf(f, ind, "   PLUGIN 'sprite_3d_factory' ('crystalspace.mesh.loader.factory.sprite.3d')\n");
  ifprintf(f, --ind, ")\n");

//Write Texture Information
  ifprintf(f, ind++, "TEXTURES (\n");
  int i;
  for(i=0; i<calCoreModel.getCoreMaterialCount(); i++) {
    CalCoreMaterial *mat=calCoreModel.getCoreMaterial(i);
    for(int j=0; j<mat->getMapCount(); j++) {
      char texfile[255];
      ToLower(texfile, mat->getMapFilename(j).c_str());
      char texname[255];
      StripExt(texname, mat->getMapFilename(j).c_str());
      ifprintf(f, ind, "TEXTURE 'T_%s' (FILE (/lev/flarge/%s))\n", texname, texfile);
    }
  }
  ifprintf(f, --ind, ")\n");

//Write Material Information
  ifprintf(f, ind++, "MATERIALS (\n");
  for(i=0; i<calCoreModel.getCoreMaterialCount(); i++) {
    CalCoreMaterial *mat=calCoreModel.getCoreMaterial(i);
    const char *texfile=mat->getMapFilename(0).c_str();
    char texname[255];
    StripExt(texname, texfile);
    ifprintf(f, ind, "MATERIAL 'M_%s' (TEXTURE ('T_%s'))\n", texname, texname);
  }
  ifprintf(f, --ind, ")\n");

  CalCoreMesh *mesh=calCoreModel.getCoreMesh(0);
  if(!mesh) {
    printf("Core Mesh not found!\n");
    return 1;
  }
  CalCoreSubmesh *submesh=mesh->getCoreSubmesh(0);
  if(!submesh) {
    printf("Core Submesh not found!\n");
    return 1;
  }

//Write Mesh Information
  ifprintf(f, ind++, "MESHFACT '%s' (\n", name);
  ifprintf(f, ind, "PLUGIN ('sprite_3d_factory')\n");
  ifprintf(f, ind++, "PARAMS (\n");

  char texname[255];
  StripExt(texname, calCoreModel.getCoreMaterial(0)->getMapFilename(0).c_str());
  ifprintf(f, ind, "MATERIAL ('M_%s')\n", texname);

  calModel.update(1);

//Write Vertices
  ifprintf(f, ind++, "FRAME 'base' (\n");
  for(i=0; i<submesh->getVertexCount(); i++) {
    CalCoreSubmesh::Vertex *v=&submesh->getVectorVertex()[i];
    CalCoreSubmesh::TextureCoordinate *t=&submesh->getVectorVectorTextureCoordinate()[0][i];

//CS doesn't support per-vertex influences yet, so pick the biggest one
    CalCoreSubmesh::Influence *useinf=NULL;
    float useweight=0.0f;
    for(unsigned int j=0; j<v->vectorInfluence.size(); j++) {
      CalCoreSubmesh::Influence *i=&v->vectorInfluence[j];
      if(i->weight>=useweight) {
        useweight=i->weight;
        useinf=i;
      }
    }

//Store xyz
#if 1 //TODO Azverkan this is for trying to export the model in its "standard pose"
    CalBone *bone=calModel.getSkeleton()->getVectorBone()[useinf->boneId];
    CalVector pos(v->position);
    pos *= bone->getCoreBone()->getRotationBoneSpace();
    pos += bone->getCoreBone()->getTranslationBoneSpace();

    ifprintf(f, ind, "V(%f,%f,%f:%f,%f)\n", pos.x*scale, pos.y*scale, pos.z*scale, t->u, 1.0f-t->v);
#else
    ifprintf(f, ind, "V(%f,%f,%f:%f,%f)\n", v->position.x*scale, v->position.y*scale, v->position.z*scale, t->u, 1.0f-t->v);
#endif

//Add to bone list
    CalCoreBone *coreBone=calCoreModel.getCoreSkeleton()->getVectorCoreBone()[useinf->boneId];
    int *ptr=(int*)coreBone->getUserData();
    if(!ptr) {
      ptr=(int*)malloc((submesh->getVertexCount()+1)*sizeof(int)); //SEMI-HARDCODED
      coreBone->setUserData(ptr);
      ptr[0]=0; //Store size in ptr[0]
    }<commit_msg>only write texture coord info if its available<commit_after>/*
    Copyright (C) 2001 by <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "cal3d/cal3d.h"
#include <stdio.h>
#include <stdarg.h>

#include "qsqrt.h"

void ifprintf(FILE *f, int indent, const char *fmt, ...) {
  for(int i=0; i<indent; i++) {
    fputc(' ', f);
  }

  va_list va;
  va_start(va, fmt);
  vfprintf(f, fmt, va);
  va_end(va);
}

void StripExt(char *dst, const char *src) {
  strcpy(dst, src);
  char *tmp=strrchr(dst, '.');
  if(tmp)
    *tmp=0;
}

void ReplaceExt(char *dst, const char *src, const char *ext) {
  StripExt(dst, src);
  strcat(dst, ext);
}

void ToLower(char *dst, const char *src) {
  char *d=dst;
  const char *s=src;
  for(; *s; s++, d++) {
    *d=tolower(*s);
  }
  *d=0;
}

void WriteBone(FILE *f, int ind, float scale, CalCoreSkeleton *skel, CalCoreBone *bone) {
  ifprintf(f, ind, "LIMB '%s' (\n", bone->getName().c_str());
#if 1
  const CalVector &pos=bone->getTranslation();
  const CalQuaternion &rot=bone->getRotation();
  ifprintf(f, ind, "TRANSFORM(V(%f,%f,%f)Q(%f,%f,%f,%f))\n", pos.x*scale, pos.y*scale, pos.z*scale, rot.x, rot.y, rot.z, rot.w);
#endif

  int *ptr=(int*)bone->getUserData();
  if(ptr) {
    ifprintf(f, ind, "VERTICES (");
    for(int i=0; i<ptr[0]; i++) {
      fprintf(f, "%d,", ptr[i+1]);
    }
    fprintf(f, ")\n");
  }

  std::list<int>::iterator iteratorChildId;
  for(iteratorChildId = bone->getListChildId().begin(); iteratorChildId != bone->getListChildId().end(); ++iteratorChildId)
  {
    CalCoreBone *child=skel->getCoreBone(*iteratorChildId);
    WriteBone(f, ind+1, scale, skel, child);
  }

  ifprintf(f, ind, ")\n");
}

void CalQuatInverse(CalQuaternion &qdest, const CalQuaternion &qin) {
  float inverselen=1.0f / qsqrt(qin[0]*qin[0]+qin[1]*qin[1]+qin[2]*qin[2]+qin[3]*qin[3]);
  qdest[0]=-inverselen*qin[0];
  qdest[1]=-inverselen*qin[1];
  qdest[2]=-inverselen*qin[2];
  qdest[3]=inverselen*qin[3];
}

int ExportSprite(const char* filename, float scale, float timescale, CalCoreModel &calCoreModel, CalModel & calModel) {
  char name[255];
  StripExt(name, filename);

  FILE *f=fopen(filename, "w");
  int ind=0;
  ifprintf(f, ind++, "LIBRARY 'soldier' (\n");

  ifprintf(f, ind++, "PLUGINS (\n");
  ifprintf(f, ind, "   PLUGIN 'sprite_3d_factory' ('crystalspace.mesh.loader.factory.sprite.3d')\n");
  ifprintf(f, --ind, ")\n");

//Write Texture Information
  ifprintf(f, ind++, "TEXTURES (\n");
  int i;
  for(i=0; i<calCoreModel.getCoreMaterialCount(); i++) {
    CalCoreMaterial *mat=calCoreModel.getCoreMaterial(i);
    for(int j=0; j<mat->getMapCount(); j++) {
      char texfile[255];
      ToLower(texfile, mat->getMapFilename(j).c_str());
      char texname[255];
      StripExt(texname, mat->getMapFilename(j).c_str());
      ifprintf(f, ind, "TEXTURE 'T_%s' (FILE (/lev/flarge/%s))\n", texname, texfile);
    }
  }
  ifprintf(f, --ind, ")\n");

//Write Material Information
  ifprintf(f, ind++, "MATERIALS (\n");
  for(i=0; i<calCoreModel.getCoreMaterialCount(); i++) {
    CalCoreMaterial *mat=calCoreModel.getCoreMaterial(i);
    const char *texfile=mat->getMapFilename(0).c_str();
    char texname[255];
    StripExt(texname, texfile);
    ifprintf(f, ind, "MATERIAL 'M_%s' (TEXTURE ('T_%s'))\n", texname, texname);
  }
  ifprintf(f, --ind, ")\n");

  CalCoreMesh *mesh=calCoreModel.getCoreMesh(0);
  if(!mesh) {
    printf("Core Mesh not found!\n");
    return 1;
  }
  CalCoreSubmesh *submesh=mesh->getCoreSubmesh(0);
  if(!submesh) {
    printf("Core Submesh not found!\n");
    return 1;
  }

//Write Mesh Information
  ifprintf(f, ind++, "MESHFACT '%s' (\n", name);
  ifprintf(f, ind, "PLUGIN ('sprite_3d_factory')\n");
  ifprintf(f, ind++, "PARAMS (\n");

  char texname[255];
  StripExt(texname, calCoreModel.getCoreMaterial(0)->getMapFilename(0).c_str());
  ifprintf(f, ind, "MATERIAL ('M_%s')\n", texname);

  calModel.update(1);

//Write Vertices
  ifprintf(f, ind++, "FRAME 'base' (\n");
  for(i=0; i<submesh->getVertexCount(); i++) {
    CalCoreSubmesh::Vertex *v=&submesh->getVectorVertex()[i];
    CalCoreSubmesh::TextureCoordinate *t=NULL;
    std::vector<std::vector<CalCoreSubmesh::TextureCoordinate> > &tc=submesh->getVectorVectorTextureCoordinate();
    if(tc.size()>0) {
      if(tc[0].size()>(unsigned)i) {
        t=&submesh->getVectorVectorTextureCoordinate()[0][i];
      }
    }

//CS doesn't support per-vertex influences yet, so pick the biggest one
    CalCoreSubmesh::Influence *useinf=NULL;
    float useweight=0.0f;
    for(unsigned int j=0; j<v->vectorInfluence.size(); j++) {
      CalCoreSubmesh::Influence *i=&v->vectorInfluence[j];
      if(i->weight>=useweight) {
        useweight=i->weight;
        useinf=i;
      }
    }

    //Grab texcoord if available
    char texcoord[255];
    if(t) {
      snprintf(texcoord, 255, ":%f,%f", t->u, 1.0f-t->v);
    } else {
      texcoord[0]=0;
    }

//Store xyz
#if 1 //TODO Azverkan this is for trying to export the model in its "standard pose"
    CalBone *bone=calModel.getSkeleton()->getVectorBone()[useinf->boneId];
    CalVector pos(v->position);
    pos *= bone->getCoreBone()->getRotationBoneSpace();
    pos += bone->getCoreBone()->getTranslationBoneSpace();

    ifprintf(f, ind, "V(%f,%f,%f%s)\n", pos.x*scale, pos.y*scale, pos.z*scale, texcoord);
#else
    ifprintf(f, ind, "V(%f,%f,%f%s)\n", v->position.x*scale, v->position.y*scale, v->position.z*scale, texcoord);
#endif

//Add to bone list
    CalCoreBone *coreBone=calCoreModel.getCoreSkeleton()->getVectorCoreBone()[useinf->boneId];
    int *ptr=(int*)coreBone->getUserData();
    if(!ptr) {
      ptr=(int*)malloc((submesh->getVertexCount()+1)*sizeof(int)); //SEMI-HARDCODED
      coreBone->setUserData(ptr);
      ptr[0]=0; //Store size in ptr[0]
    }
{'fraction_non_alphanumeric': 0.18202801655523718, 'fraction_numerical': 0.014167462591531359, 'mean_word_length': 4.124388254486134, 'pattern_counts': {'":': 1, '<': 31, '<?xml version=': 0, '>': 98, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4488637', 'n_tokens_mistral': 4883, 'n_tokens_neox': 4705, 'n_words': 1072}