00001 #include <esg/explorer/POVRayExporter.h>
00002 #include <esg/emittance/Emittance.h>
00003 #include <stdio.h>
00004
00005 using namespace esg;
00006
00007 void POVRayExporter::_write_mesh(Mesh & mesh, int texture)
00008 {
00009 fprintf(_file, "mesh {\n");
00010
00011 mesh.resetActSolid();
00012 do {
00013 mesh.resetActPlane();
00014 do {
00015 _indent(1); fprintf(_file, "smooth_triangle {\n");
00016 bool first = true;
00017 mesh.resetActEdge();
00018 do {
00019 Vector3 v(mesh.getActVert1(true));
00020 Vector3 n(mesh.getActVertNormal1(true));
00021 if (!first) fprintf(_file, ",\n");
00022 _indent(2); fprintf(_file, "<%f, %f, %f>, <%f, %f, %f>",
00023 v.x, v.y, v.z, n.x, n.y, n.z);
00024 first = false;
00025 } while (mesh.goToNextEdge());
00026 fprintf(_file, "\n"); _indent(1); fprintf(_file, "}\n");
00027 } while (mesh.goToNextPlane());
00028 } while (mesh.goToNextSolid());
00029
00030 if (texture >= 0) {
00031 fprintf(_file, "\n");
00032 _indent(1); fprintf(_file, "texture {Texture%i}\n", texture);
00033 }
00034 fprintf(_file, "}\n\n");
00035 }
00036
00037 void POVRayExporter::_write_sphere(const Sphere & sphere, int texture)
00038 {
00039 fprintf(_file, "sphere {\n");
00040 _indent(1); fprintf(_file, "<%f, %f, %f>, %f\n",
00041 sphere.centre().x,sphere.centre().y,sphere.centre().z,
00042 sphere.radius());
00043 if (texture >= 0) {
00044 _indent(1); fprintf(_file, "texture {Texture%i}\n", texture);
00045 }
00046 fprintf(_file, "}\n\n");
00047 }
00048
00049 void POVRayExporter::_write_texture(MatVisitor & visitor, unsigned number)
00050 {
00051 fprintf(_file, "#declare Texture%i = texture {\n", number);
00052 _indent(1); fprintf(_file, "finish {\n");
00053 _indent(2); fprintf(_file, "diffuse %f\n",
00054 (visitor.diffuse().x +
00055 visitor.diffuse().y +
00056 visitor.diffuse().z)/3);
00057 _indent(2); fprintf(_file, "ambient %f\n",
00058 (visitor.ambient().x +
00059 visitor.ambient().y +
00060 visitor.ambient().z)/3);
00061 _indent(2); fprintf(_file, "specular %f\n",
00062 (visitor.specular().x +
00063 visitor.specular().y +
00064 visitor.specular().z)/3);
00065 _indent(2); fprintf(_file, "roughness %f\n", visitor.roughness());
00066 _indent(1); fprintf(_file, "}\n");
00067 fprintf(_file, "}\n\n");
00068 }
00069
00070 void POVRayExporter::_write_light_source(Emittance& em)
00071 {
00072 Vector3 v;
00073 Color3f c;
00074
00075 if (!em.sourceLocation(v)) return;
00076 em.intensity(c);
00077 fprintf(_file, "light_source {\n");
00078 _indent(1); fprintf(_file, "<%f, %f, %f>\n", v.y, v.y, v.z);
00079 _indent(1); fprintf(_file, "color <%f, %f, %f>\n", c.x, c.y, c.z);
00080 fprintf(_file, "}\n\n");
00081 }
00082
00083 bool POVRayExporter::_process_leaf(SceneGraphObject& obj)
00084 {
00085 MatVisitor mVisitor;
00086
00087 obj.inspectMaterials(mVisitor);
00088
00089 Emittance * pEmittance = obj.emittance();
00090 if (pEmittance) _write_light_source(*pEmittance);
00091 else _write_texture(mVisitor, _oid);
00092
00093 Geometry * pGeom = obj.geometry();
00094 if (!pGeom) return false;
00095
00096 if (IS_INSTANCE_OF(*pGeom, Sphere)) {
00097 _write_sphere((Sphere&)(*pGeom), _oid++);
00098 } else {
00099 Mesh * pMesh = pGeom->mesh();
00100 if (pMesh) {
00101 _write_mesh(*pMesh, _oid++);
00102 delete pMesh;
00103 }
00104 }
00105
00106 return true;
00107 }