blob: f7ec4d101b28a4139b34dd3aa0e11d579233bba2 [file] [log] [blame]
Austin Schuh8c794d52019-03-03 21:17:37 -08001/*
2 #
3 # File : scene3d.cpp
4 # ( C++ source file )
5 #
6 # Description : A simple program that demonstrates the use of the
7 # 3D functions of CImg, in conjonction with the Board library.
8 # This file is a part of the CImg Library project.
9 # ( http://cimg.eu )
10 #
11 # Copyright : David Tschumperle
12 # ( http://tschumperle.users.greyc.fr/ )
13 #
14 # License : CeCILL v2.0
15 # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
16 #
17 # This software is governed by the CeCILL license under French law and
18 # abiding by the rules of distribution of free software. You can use,
19 # modify and/ or redistribute the software under the terms of the CeCILL
20 # license as circulated by CEA, CNRS and INRIA at the following URL
21 # "http://www.cecill.info".
22 #
23 # As a counterpart to the access to the source code and rights to copy,
24 # modify and redistribute granted by the license, users are provided only
25 # with a limited warranty and the software's author, the holder of the
26 # economic rights, and the successive licensors have only limited
27 # liability.
28 #
29 # In this respect, the user's attention is drawn to the risks associated
30 # with loading, using, modifying and/or developing or reproducing the
31 # software by the user in light of its specific status of free software,
32 # that may mean that it is complicated to manipulate, and that also
33 # therefore means that it is reserved for developers and experienced
34 # professionals having in-depth computer knowledge. Users are therefore
35 # encouraged to load and test the software's suitability as regards their
36 # requirements in conditions enabling the security of their systems and/or
37 # data to be ensured and, more generally, to use and operate it in the
38 # same conditions as regards security.
39 #
40 # The fact that you are presently reading this means that you have had
41 # knowledge of the CeCILL license and that you accept its terms.
42 #
43*/
44
45// Uncomment the line below to use the Board library.
46// ( You will need to link your code with the board library object ).
47// ( Get the Board Library at : http://libboard.sourceforge.net/ )
48//#define cimg_use_board
49
50#include "CImg.h"
51using namespace cimg_library;
52#ifndef cimg_imagepath
53#define cimg_imagepath "img/"
54#endif
55
56// Main procedure
57//----------------
58int main() {
59
60 // Define a simple 3D scene, composed of various basic objects (torus, cone, cube, ...)
61 //-------------------------------------------------------------------------------------
62 std::fprintf(stderr," - Create 3D Scene.\n");
63
64 // Define objects vertices and primitives.
65 CImgList<unsigned int> cube_prims, cone_prims, torus_prims, sphere_prims, plane_prims, scene_prims;
66 const CImg<float>
67 cube_pts = CImg<>::box3d(cube_prims,60,60,60).shift_object3d().shift_object3d(-50,50,0),
68 cone_pts = CImg<>::cone3d(cone_prims,30,40).shift_object3d().shift_object3d(50,50,0),
69 torus_pts = CImg<>::torus3d(torus_prims,30,10).shift_object3d().shift_object3d(-50,-50,0),
70 sphere_pts = CImg<>::sphere3d(sphere_prims,30).shift_object3d().shift_object3d(50,-50,0),
71 plane_pts = CImg<>::plane3d(plane_prims,200,200,20,20).shift_object3d().shift_object3d(0,0,40);
72 plane_prims.insert(plane_prims.get_reverse_object3d());
73
74 // Define objects colors and textures.
75 const CImgList<unsigned char>
76 cone_cols = CImgList<unsigned char>(cone_prims.size(),CImg<unsigned char>::vector(128,63,255)),
77 torus_cols = CImgList<unsigned char>(torus_prims.size(),CImg<unsigned char>::vector(255,55,163)),
78 sphere_cols = CImgList<unsigned char>(sphere_prims.size(),CImg<unsigned char>::vector(115,115,63)),
79 plane_cols = CImgList<unsigned char>(plane_prims.size(),CImg<unsigned char>::vector(60,120,180));
80 const CImg<unsigned char> texture = CImg<unsigned char>(cimg_imagepath "milla.bmp").resize(128,128);
81 CImgList<unsigned char> cube_cols;
82 cimglist_for(cube_prims,p) {
83 cube_cols.insert(texture,~0U,true);
84 cube_prims[p].append(CImg<unsigned int>::vector(0,0,127,0,127,127,0,127),'y');
85 }
86
87 // Define objects opacities.
88 const CImg<float>
89 cube_opacs(cube_prims.size(),1,1,1,1.0f),
90 cone_opacs(cone_prims.size(),1,1,1,0.8f),
91 torus_opacs(torus_prims.size(),1,1,1,0.6f),
92 sphere_opacs(sphere_prims.size(),1,1,1,0.4f),
93 plane_opacs(plane_prims.size(),1,1,1,0.4f);
94
95 // Append all object in a single 3D scene.
96 const CImg<float> scene_pts = CImg<float>().
97 append_object3d(scene_prims,cube_pts,cube_prims).
98 append_object3d(scene_prims,cone_pts,cone_prims).
99 append_object3d(scene_prims,torus_pts,torus_prims).
100 append_object3d(scene_prims,sphere_pts,sphere_prims).
101 append_object3d(scene_prims,plane_pts,plane_prims);
102 const CImgList<unsigned char> scene_cols = (+cube_cols,cone_cols,torus_cols,sphere_cols,plane_cols);
103 const CImg<float> scene_opacs = (cube_opacs,cone_opacs,torus_opacs,sphere_opacs,plane_opacs)>'x';
104
105 // Display object3D in a user-interacted window and get final position matrix.
106 std::fprintf(stderr," - Display 3D Scene.\n");
107 const CImg<unsigned char> visu = CImg<unsigned char>(3,512,512,1).fill(230,230,255).permute_axes("yzcx");
108 CImg<float> view_matrix = CImg<>::identity_matrix(4);
109 visu.display_object3d("3D Scene",scene_pts,scene_prims,scene_cols,scene_opacs,true,4,4,false,
110 500.0f,0,0,-5000,0.5f,0.1f,true,view_matrix.data());
111
112 // Save object 3D as OFF file.
113 std::fprintf(stderr," - Save .OFF 3D object file.\n");
114 scene_pts.save_off(scene_prims,scene_cols,"output.off");
115
116 // Save 3D view in SVG, EPS and FIG files.
117 // (using the Board library : https://github.com/c-koi/libboard ).
118#ifdef cimg_use_board
119
120 // Define a Board instance
121 LibBoard::Board B;
122
123 // Set Background color of the board.
124 B.clear(230,230,255);
125
126 // Draw object both in 'visu' and in the board.
127 (view_matrix.crop(0,0,2,2))*=2;
128 (+visu).draw_object3d(B,visu.width()/2,visu.height()/2,visu.depth()/2,
129 view_matrix*scene_pts,scene_prims,scene_cols,scene_opacs,3).
130 display("Snapshot for Board");
131
132 // Save board into a vector graphics file format.
133 std::fprintf(stderr," - Save .SVG, .EPS and .FIG snapshots\n");
134 B.save("output.svg");
135 B.save("output.eps");
136 B.save("output.fig");
137#endif
138
139 // Exit.
140 std::fprintf(stderr," - Exit.\n");
141 return 0;
142}