PTdecode/CImg-1.3.0/examples/scene3d.cpp

changeset 5
1204ebf9340d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/PTdecode/CImg-1.3.0/examples/scene3d.cpp	Mon Aug 03 14:09:20 2009 +0100
     1.3 @@ -0,0 +1,161 @@
     1.4 +/*
     1.5 + #
     1.6 + #  File        : scene3d.cpp
     1.7 + #                ( C++ source file )
     1.8 + #
     1.9 + #  Description : A simple program that demonstrates the use of the
    1.10 + #                3D functions of CImg, in conjonction with the Board library.
    1.11 + #                This file is a part of the CImg Library project.
    1.12 + #                ( http://cimg.sourceforge.net )
    1.13 + #
    1.14 + #  Copyright   : David Tschumperle
    1.15 + #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
    1.16 + #
    1.17 + #  License     : CeCILL v2.0
    1.18 + #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
    1.19 + #
    1.20 + #  This software is governed by the CeCILL  license under French law and
    1.21 + #  abiding by the rules of distribution of free software.  You can  use,
    1.22 + #  modify and/ or redistribute the software under the terms of the CeCILL
    1.23 + #  license as circulated by CEA, CNRS and INRIA at the following URL
    1.24 + #  "http://www.cecill.info".
    1.25 + #
    1.26 + #  As a counterpart to the access to the source code and  rights to copy,
    1.27 + #  modify and redistribute granted by the license, users are provided only
    1.28 + #  with a limited warranty  and the software's author,  the holder of the
    1.29 + #  economic rights,  and the successive licensors  have only  limited
    1.30 + #  liability.
    1.31 + #
    1.32 + #  In this respect, the user's attention is drawn to the risks associated
    1.33 + #  with loading,  using,  modifying and/or developing or reproducing the
    1.34 + #  software by the user in light of its specific status of free software,
    1.35 + #  that may mean  that it is complicated to manipulate,  and  that  also
    1.36 + #  therefore means  that it is reserved for developers  and  experienced
    1.37 + #  professionals having in-depth computer knowledge. Users are therefore
    1.38 + #  encouraged to load and test the software's suitability as regards their
    1.39 + #  requirements in conditions enabling the security of their systems and/or
    1.40 + #  data to be ensured and,  more generally, to use and operate it in the
    1.41 + #  same conditions as regards security.
    1.42 + #
    1.43 + #  The fact that you are presently reading this means that you have had
    1.44 + #  knowledge of the CeCILL license and that you accept its terms.
    1.45 + #
    1.46 +*/
    1.47 +
    1.48 +// Uncomment the line below to use the Board library.
    1.49 +// ( You will need to link your code with the board library object ).
    1.50 +// ( Get the Board Library at : http://libboard.sourceforge.net/ )
    1.51 +//#define cimg_use_board
    1.52 +
    1.53 +#include "CImg.h"
    1.54 +using namespace cimg_library;
    1.55 +
    1.56 +// The lines below are necessary when using a non-standard compiler as visualcpp6.
    1.57 +#ifdef cimg_use_visualcpp6
    1.58 +#define std
    1.59 +#endif
    1.60 +#ifdef min
    1.61 +#undef min
    1.62 +#undef max
    1.63 +#endif
    1.64 +
    1.65 +#ifndef cimg_imagepath
    1.66 +#define cimg_imagepath "img/"
    1.67 +#endif
    1.68 +
    1.69 +//-------------------------
    1.70 +// Begin the main procedure
    1.71 +//-------------------------
    1.72 +int main() {
    1.73 +
    1.74 +  // Define a simple 3D scene, composed of various basic objects (torus, cone, cube, ...)
    1.75 +  //-------------------------------------------------------------------------------------
    1.76 +  std::fprintf(stderr," - Create 3D Scene.\n");
    1.77 +  CImg<float> scene_pts, scene_opacs;
    1.78 +  CImgList<unsigned int> scene_prims;
    1.79 +  CImgList<unsigned char> scene_cols;
    1.80 +
    1.81 +  CImgList<unsigned int>
    1.82 +    cube_prims,
    1.83 +    cone_prims,
    1.84 +    torus_prims,
    1.85 +    sphere_prims,
    1.86 +    plane_prims;
    1.87 +
    1.88 +  // Define objects vertices and faces.
    1.89 +  const CImg<float>
    1.90 +    cube_pts = CImg<>::cube3d(cube_prims,60).translate_object3d(-50,50,0),
    1.91 +    cone_pts = CImg<>::cone3d(cone_prims,30,40).translate_object3d(50,50,0),
    1.92 +    torus_pts = CImg<>::torus3d(torus_prims,30,10).translate_object3d(-50,-50,0),
    1.93 +    sphere_pts = CImg<>::sphere3d(sphere_prims,30).translate_object3d(50,-50,0),
    1.94 +    plane_pts = CImg<>::plane3d(plane_prims,200,200,20,20,true).translate_object3d(0,0,40);
    1.95 +
    1.96 +  // Define objects colors and textures.
    1.97 +  const CImgList<unsigned char>
    1.98 +    cone_cols = CImgList<unsigned char>(cone_prims.size,CImg<unsigned char>::vector(128,63,255)),
    1.99 +    torus_cols = CImgList<unsigned char>(torus_prims.size,CImg<unsigned char>::vector(255,55,163)),
   1.100 +    sphere_cols = CImgList<unsigned char>(sphere_prims.size,CImg<unsigned char>::vector(115,115,63)),
   1.101 +    plane_cols = CImgList<unsigned char>(plane_prims.size,CImg<unsigned char>::vector(60,120,180));
   1.102 +
   1.103 +  const CImg<unsigned char> texture = CImg<unsigned char>(cimg_imagepath "milla.bmp").resize(128,128);
   1.104 +  CImgList<unsigned char> cube_cols;
   1.105 +  cimglist_for(cube_prims,p) {
   1.106 +    cube_cols.insert(texture,~0U,true);
   1.107 +    cube_prims[p].append(CImg<unsigned int>::vector(0,0,127,0,127,127,0,127),'y');
   1.108 +  }
   1.109 +
   1.110 +  // Define objects opacities.
   1.111 +  const CImg<float>
   1.112 +    cube_opacs(cube_prims.size,1,1,1,1.0f),
   1.113 +    cone_opacs(cone_prims.size,1,1,1,0.8f),
   1.114 +    torus_opacs(torus_prims.size,1,1,1,0.6f),
   1.115 +    sphere_opacs(sphere_prims.size,1,1,1,0.4f),
   1.116 +    plane_opacs(plane_prims.size,1,1,1,0.4f);
   1.117 +
   1.118 +  // Append all object in a single scene.
   1.119 +  scene_pts.
   1.120 +    append_object3d(scene_prims,cube_pts,cube_prims).
   1.121 +    append_object3d(scene_prims,cone_pts,cone_prims).
   1.122 +    append_object3d(scene_prims,torus_pts,torus_prims).
   1.123 +    append_object3d(scene_prims,sphere_pts,sphere_prims).
   1.124 +    append_object3d(scene_prims,plane_pts,plane_prims);
   1.125 +  scene_cols<<cube_cols<<cone_cols<<torus_cols<<sphere_cols<<plane_cols;
   1.126 +  scene_opacs = (cube_opacs<<cone_opacs<<torus_opacs<<sphere_opacs<<plane_opacs).get_append('x');
   1.127 +
   1.128 +  // Display object3D in a user-interacted window and get final position matrix.
   1.129 +  std::fprintf(stderr," - Display 3D Scene.\n");
   1.130 +  const CImg<unsigned char> visu = CImg<unsigned char>(3,512,512,1).fill(230,230,255).permute_axes("yzvx");
   1.131 +  CImg<float> view_matrix = CImg<>::identity_matrix(4);
   1.132 +  visu.display_object3d("3D Scene",scene_pts,scene_prims,scene_cols,scene_opacs,true,4,4,false,
   1.133 +                        500.0f,0.5f,0.1f,true,view_matrix.ptr());
   1.134 +
   1.135 +  // Save object 3D as OFF file.
   1.136 +  std::fprintf(stderr," - Save .OFF 3D object file.\n");
   1.137 +  scene_pts.save_off("output.off",scene_prims,scene_cols);
   1.138 +
   1.139 +  // Save 3D view in SVG, EPS and FIG files.
   1.140 +  // (using the Board library : http://www.greyc.ensicaen.fr/~seb/board/ ).
   1.141 +#ifdef cimg_use_board
   1.142 +
   1.143 +  // Define a Board instance
   1.144 +  BoardLib::Board B;
   1.145 +
   1.146 +  // Set Background color of the board.
   1.147 +  B.clear(230,230,255);
   1.148 +
   1.149 +  // Draw object both in 'visu' and in the board.
   1.150 +  (view_matrix.crop(0,0,2,2))*=20;
   1.151 +  (+visu).draw_object3d(B,visu.dimx()/2,visu.dimy()/2,visu.dimz()/2,view_matrix*scene_pts,scene_prims,scene_cols,scene_opacs,3).
   1.152 +  display("Snapshot for Board");
   1.153 +
   1.154 +  // Save board into a vector graphics file format.
   1.155 +  std::fprintf(stderr," - Save .SVG, .EPS and .FIG snapshots\n");
   1.156 +  B.save("output.svg");
   1.157 +  B.save("output.eps");
   1.158 +  B.save("output.fig");
   1.159 +#endif
   1.160 +
   1.161 +  // Exit.
   1.162 +  std::fprintf(stderr," - Exit.\n");
   1.163 +  return 0;
   1.164 +}