Examples (MXS creation)
Example 1
Basic pseudo-code of a MXS exporter:
Cmaxwell* pMaxwell = new Cmaxwell( ); pMaxwell->setSkyConstant( [. . .] ); // optional pMaxwell->createCamera( [. . .] ); pMaxwell->createMaterial( [. . .] ); // create materials pMaxwell->createMesh( [. . .] ); // create mesh [loop in number of vertices] pMaxwell->setVertex( [. . .] ); [end loop] [loop in number of normals] pMaxwell->setNormal( [. . .] ); [end loop] [loop in number of triangles] pMaxwell->setTriangle( [. . .] ); [end loop] [loop in number of triangles] pMaxwell->setTriangleUVW( [. . .] ); // UVW map values [end loop] pMaxwell->setRenderParameter( [. . .] ); pMaxwell->writeMXS( mxsfile ); // write MXS
Example 2
//define an error callback function to instance Cmaxwell dword id = 0; byte pErrorCallBack (byte isError, const char *pMethod, const char *pError, const void *pValue) { If (isError) { } return 0; } Cmaxwell * maxwellScene = new Cmaxwell (pErrorCallBack); // Transform Axis maxwellScene->setInputDataType ("ZXY"); // SKY maxwellScene->setSkyConstant (MXskyDomeColor, MXskyIntensity); or maxwellScene->setPhysicalSky (..); maxwellScene->setActiveSky( "PHYSICAL" ); // CAMERAS // Add several cameras for (long k = 0; k < nCameras; k++) { Cmaxwell::Ccamera curCamera = maxwellScene->addCamera (cameraNameChar, 1, (1/MXshutter), width, height, iso, diaph, angle, nBlades, fps, xRes, yRes, pixelAspect); byte camOK = curCamera.setStep (0, defaultOrigin, focalPoint, upVector, (focalLength/1000),MXfstop); if (this view is the active one) { curCamera.setActive(); } } // MATERIALS // Create a default material for all the objects Crgb defaultRgb; defaultRgb.assign (0.7, 0.7, 0.7); Cmaxwell::Cmaterial defaultMaterial = maxwellScene->createMaterial ("default", true); // GEOMETRY pMaxwell->createMesh ( [. . .] ); // create mesh [loop in number of vertices] pMaxwell->setVertex ( [. . .] ); [end loop] [loop in number of normals] pMaxwell->setNormal ( [. . .] ); [end loop] [loop in number of triangles] pMaxwell->setTriangle ( [. . .] ); [end loop] // RENDER OPTIONS maxwellScene->setRenderParameter ("STOP TIME", sizeof (dword),&maxTime); maxwellScene->setRenderParameter ("SAMPLING LEVEL", sizeof(dword), &sLevel); maxwellScene->setRenderParameter ("NUM THREADS", sizeof(dword),& nThreads); maxwellScene->setToneMapping (gamma, burn); maxwellScene->writeMXS (outputMxsPath); // Launch Maxwell Render as a separate app Sprintf (buffer, "start mxcl.exe -mxs:\"%s\" -display -o:\"%s\"", mxsPath, imagePath); system (buffer);
Example 3
Sample pseudo-code for a basic geometry exporter.
#include "maxwell.h" byte error_callback (byte iserror, const char *pmethod, const char *string, const void *data) { ...; } // Create Maxwell instance Cmaxwell *maxwell = new Cmaxwell (error_callback); // set target axes maxwell->setInputDataType ("YXZ"); // set single precision for geometry maxwell->setSinglePrecisionOfGeometry ( ); // create camera // (multiple cameras can be created) Cmaxwell::Ccamera curCamera = maxwell->addCamera (const char *pName, // camera name dword nSteps, // number of steps (usually 1, more for motion blur cases) real shutter, // shutter speed in seconds real filmWidth, // film width = 0.036 real filmHeight, // film height = 0.024 real iso, // ISO value (100, 200...) const char *pDiaphragmType, // "Polygonal", "Circular" real angle, // blade angle for "polygonal" type dword nBlades, // number of blades for "polygonal" type dword fps, // frames per second dword xRes, // x resolution dword yRes, // y resolution real pixelAspect, // pixel aspect // set active camera (necessary when there are several cameras) curCamera.setActive(); // set camera step (usually you need only one step except in motionblur cases) curCamera.setStep (0, // step index origin, // origin of the camera (x,y,z) focalPoint, // "look at" point (x,y,z) up, // sky vector focalLength, // focal length fStop); // fStop value (aperture) // create an object Cmaxwell::Cobject object = maxwell->createMesh (const char *pName, // object name dword nVertexes, // number of vertices dword nNormals, // number of normals (*) dword nTriangles, // number of triangles dword nPositionsPerVertex); // 1 for normal cases; 2 for motion blur // (*) Normals are assigned when the faces are created, one per vertex. // enable all lighting properties object.setProperties (true, true, true, true); // Caustics on / off // assign material (*) // (*) materials can be assigned at object-level or face-level object.setMaterial (Cmaxwell::Cmaterial material); // vertex definition for (int i = 0; i < nVertexes; i++) { Cpoint vertex; object.setVertex (i, 0, vertex); } // face definition for (int i = 0; i < nTriangles; i++) { // normal vectors for each point in the triangle object.setNormal (iNormal, 0, nor0); object.setNormal (iNormal + 1, 0, nor1); object.setNormal (iNormal + 2, 0, nor2); // create face (triangle) object.setTriangle (i, // face index iv, jv, kv, // vertex index iNormal, iNormal + 1, iNormal + 2); // normal index // (*) if only one normal per face is known, just call one SetNormal here, and set the 3 components of iNormal the same in setTriangle. The total number of normals would be = nTriangles at createMesh. // assign material (optional) object.setTriangleMaterial (i, // face index material); // material } // write MXS file to disk maxwell->writeMXS (char* filename); // free memory and delete maxwell->freeScene(); delete maxwell; UV mapping sample This is the code to assign UVW values to the faces of the object. Once the channel exists, the materials containing texture mapping will use that channel. In the material editor, the textures are assigned tospecific UV channels. Multiple channels can coexist. // add uv channel byte testchannel = object.addChannelUVW (channel); if (testchannel == 0) { // handle error } // fill UVW values for (int i = 0; i < nTriangles; i++) { object.setTriangleUVW (i, // face index channel, // UV channel u1, v1, w1, // UVW values for the triangle u2, v2, w2, u3, v3, w3); } Setting a texture in a material property We will see here how to set a texture to the reflectance property. // get the bsdf of the material if needed Cmaxwell::CmaterialBasic layer = material.getLayer ( 0 ); Cmaxwell::Cbsdf bsdf = layer.getBsdf (0); // get the reflectance Cmaxwell::Creflectance reflectance = bsdf.getReflectance (); // set the texture property mp.pID = "color"; mp.pType = "bitmap"; mp.pFileName = filename; // name of the texture bitmap file mp.uvwChannel = 0; // uv channel used mp.uIsTiled = 1; // U tiling (yes, no) mp.vIsTiled = 1; // V tiling (yes, no) mp.scale = scale; // texture scaling mp.offset = offset; // texture offset result = reflectance.setMultiValue ( mp ); result = reflectance.setActiveMultiValue ( mp ); Sample pseudo-code for basic material handling. Create a simple diffuse material // create material Cmaxwell::CmultiValue mv; Cmaxwell::Cmaterial newMaterial = maxwell->createMaterial( matname, false ); // Add one layer and one bsdf inside Cmaxwell::CmaterialBasic layer = newMaterial.addLayer(); layer.setName( " layer 1" ); Cmaxwell::Cbsdf newBsdf = layer.addBsdf(); // Set a basic diffuse reflectance color Cmaxwell::Creflectance reflectance = bsdf.getReflectance(); mv.pID = "color"; mv.pType = "rgb"; Crgb color; color.assign( r, g b ); mv.pParameter = & color; reflectance.setMultiValue( mv ); Sample pseudo-code for basic material handling. Create an emitter by color and power. Cmaxwell::Cmaterial newMaterial = maxwell->createMaterial( matname, false ); // Add one layer and one emitter inside Cmaxwell::CmaterialBasic layer = newMaterial.addLayer(); Cmaxwell::CmaterialEmitter emitter = layer.createEmitter(); // Set the active emission type to pair (Color+Luminance) Cmaxwell::CmultiValue mv; multivalue.pType = "pair" emitter.setActiveMultiValue( mv ); // Set the color and luminance Cmaxwell::CmultiValue::CemitterPair emitterPair; emitterPair.rgb.assign( color ); emitterPair.watts = 100.0; // Set the active pair type Cmaxwell::CmaterialEmitter::Cpair emitterActivePair; emitterActivePair.pEmissionColor = "rgb"; emitterActivePair.pLuminance = "wattsAndLuminousEfficacy";