void daeElement::getAttributes(daeTArray<attr>& attrs) { attrs.clear(); for (size_t i = 0; i < getAttributeCount(); i++) { std::string value; getAttribute(i, value); attrs.append(attr(getAttributeName(i), value)); } }
static void classifyInputs(const daeTArray<T>& inputs, T sortedInputs[], U32 *maxOffset=0) { if (maxOffset) *maxOffset = 0; // Clear output array for (int i = 0; i < NumStreams; i++) sortedInputs[i] = 0; // Separate inputs by type, and sort by set (ie. lowest TEXCOORD set becomes UV, // next TEXCOORD set becomes UV2 etc) for (int iInput = 0; iInput < inputs.getCount(); iInput++) { const T& input = inputs[iInput]; const daeString semantic = input->getSemantic(); if (dStrEqual(semantic, "VERTEX")) { domVertices* vertices = daeSafeCast<domVertices>(findInputSource(input)); // The <vertices> element may contain multiple inputs (eg. POSITION, NORMAL etc) domInputLocalRef verticesInputs[NumStreams]; classifyInputs(vertices->getInput_array(), verticesInputs); for (int iStream = 0; iStream < NumStreams; iStream++) { if (verticesInputs[iStream] != 0) sortedInputs[iStream] = input; } } else if (dStrEqual(semantic, "POSITION")) selectInput(input, sortedInputs, Points); else if (dStrEqual(semantic, "NORMAL")) selectInput(input, sortedInputs, Normals); else if (dStrEqual(semantic, "COLOR")) selectInput(input, sortedInputs, Colors); else if (dStrEqual(semantic, "TEXCOORD")) selectInput(input, sortedInputs, UVs, UV2s); else if (dStrEqual(semantic, "JOINT")) selectInput(input, sortedInputs, Joints); else if (dStrEqual(semantic, "WEIGHT")) selectInput(input, sortedInputs, Weights); else if (dStrEqual(semantic, "INV_BIND_MATRIX")) selectInput(input, sortedInputs, InvBindMatrices); if (maxOffset) { const domInputLocalOffset* localOffset = daeSafeCast<domInputLocalOffset>(input); domUint offset = localOffset ? localOffset->getOffset() : 0; if (offset > (*maxOffset)) *maxOffset = offset; } } }
void daeElement::deleteCMDataArray(daeTArray<daeCharArray*>& cmData) { for (unsigned int i = 0; i < cmData.getCount(); i++) delete cmData.get(i); cmData.clear(); }
void processNode(domNodeRef node, DAE &dae, const char *cmdlntexfn) { daeTArray<daeElementRef> nodeChildren = node->getChildren(); for (size_t j = 0; j < nodeChildren.getCount(); j++) { const daeElementRef nodeChild = nodeChildren[j]; const daeString nodeChildKind = nodeChild->getElementName(); if (!strcmp(nodeChildKind, "node")) processNode(daeSafeCast<domNode>(nodeChild), dae, cmdlntexfn); else if (!strcmp(nodeChildKind, "instance_geometry")) { const daeTArray<daeElementRef> geometryChildren = daeSafeCast<domInstance_geometry>(nodeChild)->getUrl().getElement()->getChildren(); for (size_t k = 0; k < geometryChildren.getCount(); k++) { const daeElementRef geometryChild = geometryChildren[k]; const daeString geometryChildKind = geometryChild->getElementName(); if (!strcmp(geometryChildKind, "mesh")) { const domMeshRef mesh = daeSafeCast<domMesh>(geometryChild); const domTriangles_Array trianglesArray = mesh->getTriangles_array(); for (size_t i = 0; i < trianglesArray.getCount(); i++) { char *effect = NULL; string windex; string texturefnstr; const char *texturefn = cmdlntexfn; daeElement *squeegee = nodeChild->getChild("bind_material"); if (squeegee) { squeegee = squeegee->getChild("technique_common"); if (squeegee) { squeegee = squeegee->getChild("instance_material"); if (squeegee) { windex = squeegee->getAttribute("target"); effect = (char*)malloc(windex.length()+1); if (!effect) { perror("malloc"); exit(0); } strcpy(effect, windex.c_str()); daeElement *effectel = daeSafeCast<domInstance_material>(squeegee)->getTarget().getElement(); if (effectel) { daeElement *profile_COMMON = effectel->getChild("profile_COMMON"); if (profile_COMMON) { daeElement *technique = profile_COMMON->getChild("technique"); if (technique) { daeElement *phong = technique->getChild("phong"); if (phong) { daeElement *diffuse = phong->getChild("diffuse"); if (diffuse) { daeElement *texture = diffuse->getChild("texture"); if (texture) { const string samplerSid = texture->getAttribute("texture"); daeElement *samplernewparam = daeSidRef(samplerSid, effectel, "COMMON").resolve().elt; if (samplernewparam) { daeElement *sampler2D = samplernewparam->getChild("sampler2D"); if (sampler2D) { daeElement *samplersrcel = sampler2D->getChild("source"); daeElement *minfilterel = sampler2D->getChild("minfilter"); daeElement *magfilterel = sampler2D->getChild("magfilter"); if (samplersrcel && minfilterel && magfilterel) { const string surfSid = samplersrcel->getCharData(); daeElement *surfnewparam = daeSidRef(surfSid, effectel, "COMMON").resolve().elt; if (surfnewparam) { daeElement *surface = surfnewparam->getChild("surface"); if (surface) { daeElement *init_from = surface->getChild("init_from"); if (init_from) { daeElement *imageel = dae.getDatabase()->idLookup(init_from->getCharData(), init_from->getDocument()); if (imageel) { daeElement *imageinit_from = imageel->getChild("init_from"); if (imageinit_from) { texturefnstr = imageinit_from->getCharData(); texturefn = texturefnstr.c_str(); } } } } } } } } } } } } } } } } } TriangleMesh x(trianglesArray[i], effect); x.transform(texturefn); } } } } } }
int main(int argc, const char *argv[]) { if (argc != 6) { if (fprintf(stderr, "Usage: %s [dimension] [bgR] [bgG] [bgB] [texture]\n", argv[0]) < 0) perror("fprintf"); return 0; } buf = new FrameBuffer(atoi(argv[1]), atof(argv[2]), atof(argv[3]), atof(argv[4])); #ifdef WIN32 const char *fn = _tempnam("C:\tmp", "transform"); if (!fn) { perror("_tempnam"); return 0; } #else const char *fn = tmpnam(NULL); if (!fn) { perror("tmpnam"); return 0; } #endif FILE *tmpfile = fopen(fn, "w"); if (!tmpfile) { perror("fopen"); return 0; } int c; while ((c = getchar()) != EOF) if (fputc(c, tmpfile) == EOF) { perror("fputc"); return 0; } if (ferror(tmpfile)) { perror("getchar"); return 0; } if (fclose(tmpfile) == EOF) { perror("fclose"); return 0; } DAE dae; daeElement *root = dae.open(fn); if (!root) { if (fprintf(stderr, "Problem opening temporary file.\n") < 0) perror("fprintf"); return 0; } const daeTArray<daeElementRef> visualSceneChildren = daeSafeCast<domInstanceWithExtra>(root->getChild("scene")->getChild("instance_visual_scene")) ->getUrl().getElement()->getChildren(); for (size_t i = 0; i < visualSceneChildren.getCount(); i++) { const daeElementRef node = visualSceneChildren[i]; if (!strcmp(node->getElementName(), "node")) processNode(daeSafeCast<domNode>(node), dae, argv[5]); } dae.clear(); buf->write(dae, fn); tmpfile = fopen(fn, "r"); if (!tmpfile) { perror("fopen"); return 0; } while ((c = fgetc(tmpfile)) != EOF) if (putchar(c) == EOF) { perror("putchar"); return 0; } if (ferror(tmpfile)) { perror("fgetc"); return 0; } if (fclose(tmpfile) == EOF) { perror("fclose"); return 0; } if (remove(fn)) { perror("remove"); return 0; } }
void processNode(domNodeRef node, Matrix4 m_trans, vector<AmbientLight> *ambientLights, vector<PointLight> *pointLights, vector<Cube> *cubes, vector<Sphere> *spheres, vector<Cylinder> *cylinders, vector<Cone> *cones, vector<Torus> *toruses, vector<PartialTorus> *partialToruses, vector<TaperedCylinder> *taperedCylinders, vector<Square> *squares) { daeTArray<daeElementRef> nodeChildren = node->getChildren(); for (size_t j = 0; j < nodeChildren.getCount(); j++) { const daeElementRef nodeChild = nodeChildren[j]; const daeString nodeChildKind = nodeChild->getElementName(); if (!strcmp(nodeChildKind, "node")) processNode(daeSafeCast<domNode>(nodeChild), m_trans, ambientLights, pointLights, cubes, spheres, cylinders, cones, toruses, partialToruses, taperedCylinders, squares); else if (!strcmp(nodeChildKind, "translate")) { const domFloat3 val = daeSafeCast<domTranslate>(nodeChild)->getValue(); m_trans = m_trans * trans_mat(Vector3(val[0], val[1], val[2])); } else if (!strcmp(nodeChildKind, "rotate")) { const domFloat3 val = daeSafeCast<domRotate>(nodeChild)->getValue(); m_trans = m_trans * rot_mat(Point3(), Vector3(val[0], val[1], val[2]), val[3]); } else if (!strcmp(nodeChildKind, "scale")) { const domFloat3 val = daeSafeCast<domScale>(nodeChild)->getValue(); m_trans = m_trans * scale_mat(Vector3(val[0], val[1], val[2])); } else if (!strcmp(nodeChildKind, "instance_geometry")) { const daeTArray<daeElementRef> geometryChildren = daeSafeCast<domInstance_geometry>(nodeChild)->getUrl().getElement()->getChildren(); for (size_t k = 0; k < geometryChildren.getCount(); k++) { const daeElementRef geometryChild = geometryChildren[k]; const daeString geometryChildKind = geometryChild->getElementName(); double KdR = 0.8, KdG = 0.8, KdB = 0.8, KsR = 1.f, KsG = 1.f, KsB = 1.f, shiny = 25.f, reflectivity = 1.f, transparency = 0.f, refractiveIndex = 1.f; daeElement *squeegee = nodeChild->getChild("bind_material"); if (squeegee) { squeegee = squeegee->getChild("technique_common"); if (squeegee) { squeegee = squeegee->getChild("instance_material"); if (squeegee) { squeegee = daeSafeCast<domInstance_material>(squeegee)->getTarget().getElement(); if (squeegee) { squeegee = squeegee->getChild("instance_effect"); if (squeegee) { daeElement *effectel = daeSafeCast<domInstance_effect>(squeegee)->getUrl().getElement(); if (effectel) { daeElement *profile_COMMON = effectel->getChild("profile_COMMON"); if (profile_COMMON) { daeElement *technique = profile_COMMON->getChild("technique"); if (technique) { daeElement *phong = technique->getChild("phong"); if (phong) { daeElement *diffuse = phong->getChild("diffuse"); daeElement *specular = phong->getChild("specular"); daeElement *shininess = phong->getChild("shininess"); daeElement *reflectivityel = phong->getChild("reflectivity"); daeElement *transparencyel = phong->getChild("transparency"); daeElement *index_of_refraction = phong->getChild("index_of_refraction"); if (diffuse && specular && shininess && reflectivityel && transparencyel && index_of_refraction) { daeElement *diffusecolorel = diffuse->getChild("color"); daeElement *specularcolorel = specular->getChild("color"); daeElement *shininessfloatel = shininess->getChild("float"); daeElement *reflectivityfloatel = reflectivityel->getChild("float"); daeElement *transparencyfloatel = transparencyel->getChild("float"); daeElement *index_of_refractionfloatel = index_of_refraction->getChild("float"); if (diffusecolorel && specularcolorel && shininessfloatel && reflectivityfloatel && transparencyfloatel && index_of_refractionfloatel) { istringstream parsediffusecolorel(diffusecolorel->getCharData()); parsediffusecolorel >> KdR; parsediffusecolorel >> KdG; parsediffusecolorel >> KdB; istringstream parsespecularcolorel(specularcolorel->getCharData()); parsespecularcolorel >> KsR; parsespecularcolorel >> KsG; parsespecularcolorel >> KsB; istringstream parseshininessfloatel(shininessfloatel->getCharData()); parseshininessfloatel >> shiny; istringstream parsereflectivityfloatel(reflectivityfloatel->getCharData()); parsereflectivityfloatel >> reflectivity; istringstream parsetransparencyfloatel(transparencyfloatel->getCharData()); parsetransparencyfloatel >> transparency; istringstream parseindex_of_refractionfloatel(index_of_refractionfloatel->getCharData()); parseindex_of_refractionfloatel >> refractiveIndex; } } } } } } } } } } } #define INJECTMESH \ mesh.transform(m_trans); \ mesh.KdR = KdR; \ mesh.KdG = KdG; \ mesh.KdB = KdB; \ mesh.KsR = KsR; \ mesh.KsG = KsG; \ mesh.KsB = KsB; \ mesh.shiny = shiny; \ mesh.reflectivity = reflectivity; \ mesh.transparency = transparency; \ mesh.refractiveIndex = refractiveIndex; \ buf->addObject(mesh); if (!strcmp(geometryChildKind, "block")) { const domBlockRef block = daeSafeCast<domBlock>(geometryChild); cubes->push_back(Cube(block->getEdgelen(), KdR, KdG, KdB, KsR, KsG, KsB, shiny, reflectivity, transparency, refractiveIndex, m_trans)); TriangleMesh mesh(block); INJECTMESH } else if (!strcmp(geometryChildKind, "ball")) { const domBallRef ball = daeSafeCast<domBall>(geometryChild); spheres->push_back(Sphere(ball->getRadius(), KdR, KdG, KdB, KsR, KsG, KsB, shiny, reflectivity, transparency, refractiveIndex, m_trans)); TriangleMesh mesh(ball); INJECTMESH } else if (!strcmp(geometryChildKind, "rod")) {