PTdecode/CImg-1.3.0/examples/scene3d.cpp

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