PTdecode/CImg-1.3.0/examples/scene3d.cpp

Mon, 03 Aug 2009 14:09:20 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 03 Aug 2009 14:09:20 +0100
changeset 5
1204ebf9340d
permissions
-rwxr-xr-x

added P-touch decoder source

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