Scene3D Simple Scene Viewer
Scene3D.h
1 
5 #ifndef SCENE3D_H
6 #define SCENE3D_H
7 
8 #include <vector>
9 #include <map>
10 #include <fstream>
11 #include <string>
12 #include <sstream>
13 #include <iostream>
14 #include <math.h>
15 #define PI 3.14159265
16 
17 using namespace std;
18 
19 #define HTML_PREFIX "<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"utf-8\"/>\n </head>\n <body>\n <!-- three.js scripts -->\n <!-- startup three.js -->\n <script src=\"jsmodules/three.min.js\"></script>\n <script src=\"jsmodules/three.module.js\"></script>\n <script src=\"jsmodules/gif.js\"></script>\n <!-- load models and look at them-->\n <script src=\"jsmodules/OBJLoader.js\"></script>\n <script src=\"jsmodules/MTLLoader.js\"></script>\n <!-- postprocessing -->\n <script src=\"jsmodules/CopyShader.js\"></script>\n <script src=\"jsmodules/Pass.js\"></script>\n <script src=\"jsmodules/ShaderPass.js\"></script>\n <script src=\"jsmodules/MaskPass.js\"></script>\n <script src=\"jsmodules/EffectComposer.js\"></script>\n <script src=\"jsmodules/RenderPass.js\"></script>\n <script src=\"jsmodules/DigitalGlitch.js\"></script>\n <script src=\"jsmodules/GlitchPass.js\"></script>\n\n <!--Other outside libraries -->\n <script type=\"text/javascript\" src=\"jsmodules/jquery-3.5.1.min.js\"></script>\n <script type=\"text/javascript\" src=\"jsmodules/dat.gui.min.js\"></script>\n <script type=\"text/javascript\" src=\"jsmodules/gl-matrix-min.js\"></script>\n\n <!-- Our code -->\n <script type=\"text/javascript\" src=\"cameras3d.js\"></script>\n <script type=\"text/javascript\" src=\"scenecanvas.js\"></script>\n\n\n";
20 
21 class Scene3D {
22  private:
23  stringstream sceneCode;
24 
25  public:
26  Scene3D() {
27  sceneCode << "let canvas = new SceneCanvas();\n";
28  }
29 
47  void addBox(double cx, double cy, double cz, double xlen,
48  double ylen, double zlen,
49  double r, double g, double b,
50  double roughness, double metalness,
51  double rx, double ry, double rz) {
52  sceneCode << "canvas.addBox(" << cx << "," << cy << "," << cz << "," << xlen << "," << ylen << "," << zlen << "," << r << "," << g << "," << b << "," << roughness << "," << metalness << "," << rx << "," << ry << "," << rz << ");\n";
53  }
54 
69  void addBox(double cx, double cy, double cz,
70  double xlen, double ylen, double zlen,
71  double r, double g, double b,
72  double roughness, double metalness) {
73  addBox(cx, cy, cz, xlen, ylen, zlen, r, g, b, roughness, metalness, 0, 0, 0);
74  }
75 
95  void addCylinder(double cx, double cy, double cz, double radius,
96  double height, double r, double g, double b,
97  double roughness, double metalness,
98  double rx, double ry, double rz,
99  double sx, double sy, double sz) {
100  sceneCode << "canvas.addCylinder(" << cx << "," << cy << "," << cz << "," << radius << "," << height << "," << r << "," << g << "," << b << "," << roughness << "," << metalness << "," << rx << "," << ry << "," << rz << "," << sx << "," << sy << "," << sz << ");\n";
101  }
102 
116  void addCylinder(double cx, double cy, double cz, double radius,
117  double height, double r, double g, double b,
118  double roughness, double metalness) {
119  addCylinder(cx, cy, cz, radius, height, r, g, b, roughness, metalness, 0, 0, 0, 1, 1, 1);
120  }
121 
141  void addCone(double cx, double cy, double cz, double radius,
142  double height, double r, double g, double b,
143  double roughness, double metalness,
144  double rx, double ry, double rz,
145  double sx, double sy, double sz) {
146  sceneCode << "canvas.addCone(" << cx << "," << cy << "," << cz << "," << radius << "," << height << "," << r << "," << g << "," << b << "," << roughness << "," << metalness << "," << rx << "," << ry << "," << rz << "," << sx << "," << sy << "," << sz << ");";
147  }
148 
162  void addCone(double cx, double cy, double cz, double radius,
163  double height, double r, double g, double b,
164  double roughness, double metalness) {
165  addCone(cx, cy, cz, radius, height, r, g, b, roughness, metalness, 0, 0, 0, 1, 1, 1);
166  }
167 
185  void addEllipsoid(double cx, double cy, double cz,
186  double radx, double rady, double radz,
187  double r, double g, double b,
188  double roughness, double metalness,
189  double rx, double ry, double rz) {
190  sceneCode << "canvas.addEllipsoid(" << cx << "," << cy << "," << cz << "," << radx << "," << rady << "," << radz << "," << r << "," << g << "," << b << "," << roughness << "," << metalness << "," << rx << "," << ry << "," << rz << ");\n";
191  }
192 
207  void addEllipsoid(double cx, double cy, double cz,
208  double radx, double rady, double radz,
209  double r, double g, double b,
210  double roughness, double metalness) {
211  addEllipsoid(cx, cy, cz, radx, rady, radz, r, g, b, roughness, metalness, 0, 0, 0);
212  }
213 
226  void addSphere(double cx, double cy, double cz, double radius,
227  double r, double g, double b,
228  double roughness, double metalness) {
229  addEllipsoid(cx, cy, cz, radius, radius, radius, r, g, b, roughness, metalness);
230  }
231 
251  void addMesh(string path,
252  double cx, double cy, double cz,
253  double rx, double ry, double rz,
254  double sx, double sy, double sz,
255  double r, double g, double b,
256  double roughness, double metalness) {
257  sceneCode << "canvas.addMesh(\"" << path << "\"," << cx << "," << cy << "," << cz << "," << rx << "," << ry << "," << rz << "," << sx << "," << sy << "," << sz << "," << r << "," << g << "," << b << "," << roughness << "," << metalness << ");\n";
258  }
259 
276  void addTexturedMesh(string path, string matpath,
277  double cx, double cy, double cz,
278  double rx, double ry, double rz,
279  double sx, double sy, double sz,
280  double shininess) {
281  sceneCode << "canvas.addTexturedMesh(\"" << path << "\",\"" << matpath << "\"," << cx << "," << cy << "," << cz << "," << rx << "," << ry << "," << rz << "," << sx << "," << sy << "," << sz << "," << shininess << ");\n";
282  }
283 
284 
292  void addCamera(double x, double y, double z, double rot) {
293  sceneCode << "canvas.addCamera(" << x << "," << y << "," << z << "," << rot << ");\n";
294  }
295 
307  void addPointLight(double x, double y, double z, double r, double g, double b, double intensity) {
308  sceneCode << "canvas.addPointLight(" << x << "," << y << "," << z << "," << r << "," << g << "," << b << "," << intensity << ");\n";
309  }
310 
323  void addDirectionalLight(double x, double y, double z, double r, double g, double b, double intensity) {
324  sceneCode << "canvas.addDirectionalLight(" << x << "," << y << "," << z << "," << r << "," << g << "," << b << "," << intensity << ");\n";
325  }
326 
332  void saveScene(string filename, string sceneName) {
333  std::ofstream out(filename.c_str());
334  out << HTML_PREFIX;
335  out << "<script>\n";
336  out << sceneCode.str();
337  out << "canvas.name = \"" << sceneName << "\";\n";
338  out << "canvas.repaint();\n</script>\n </body>\n</html>";
339  out.close();
340  }
341 };
342 
343 #endif
Definition: Scene3D.h:21
void addPointLight(double x, double y, double z, double r, double g, double b, double intensity)
Definition: Scene3D.h:307
void addCylinder(double cx, double cy, double cz, double radius, double height, double r, double g, double b, double roughness, double metalness, double rx, double ry, double rz, double sx, double sy, double sz)
Definition: Scene3D.h:95
void addEllipsoid(double cx, double cy, double cz, double radx, double rady, double radz, double r, double g, double b, double roughness, double metalness)
Definition: Scene3D.h:207
void addDirectionalLight(double x, double y, double z, double r, double g, double b, double intensity)
Definition: Scene3D.h:323
void addCamera(double x, double y, double z, double rot)
Definition: Scene3D.h:292
void addCylinder(double cx, double cy, double cz, double radius, double height, double r, double g, double b, double roughness, double metalness)
Definition: Scene3D.h:116
void addSphere(double cx, double cy, double cz, double radius, double r, double g, double b, double roughness, double metalness)
Definition: Scene3D.h:226
void addMesh(string path, double cx, double cy, double cz, double rx, double ry, double rz, double sx, double sy, double sz, double r, double g, double b, double roughness, double metalness)
Definition: Scene3D.h:251
void addEllipsoid(double cx, double cy, double cz, double radx, double rady, double radz, double r, double g, double b, double roughness, double metalness, double rx, double ry, double rz)
Definition: Scene3D.h:185
void addBox(double cx, double cy, double cz, double xlen, double ylen, double zlen, double r, double g, double b, double roughness, double metalness, double rx, double ry, double rz)
Definition: Scene3D.h:47
void saveScene(string filename, string sceneName)
Definition: Scene3D.h:332
void addTexturedMesh(string path, string matpath, double cx, double cy, double cz, double rx, double ry, double rz, double sx, double sy, double sz, double shininess)
Definition: Scene3D.h:276
void addCone(double cx, double cy, double cz, double radius, double height, double r, double g, double b, double roughness, double metalness)
Definition: Scene3D.h:162
void addBox(double cx, double cy, double cz, double xlen, double ylen, double zlen, double r, double g, double b, double roughness, double metalness)
Definition: Scene3D.h:69
void addCone(double cx, double cy, double cz, double radius, double height, double r, double g, double b, double roughness, double metalness, double rx, double ry, double rz, double sx, double sy, double sz)
Definition: Scene3D.h:141