PTdecode/CImg-1.3.0/examples/scene3d.cpp

Wed, 05 Aug 2009 17:10:56 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 05 Aug 2009 17:10:56 +0100
changeset 17
cf9d239ac1c9
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

add README

     1 /*
     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.sourceforge.net )
    10  #
    11  #  Copyright   : David Tschumperle
    12  #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
    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 */
    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
    50 #include "CImg.h"
    51 using namespace cimg_library;
    53 // The lines below are necessary when using a non-standard compiler as visualcpp6.
    54 #ifdef cimg_use_visualcpp6
    55 #define std
    56 #endif
    57 #ifdef min
    58 #undef min
    59 #undef max
    60 #endif
    62 #ifndef cimg_imagepath
    63 #define cimg_imagepath "img/"
    64 #endif
    66 //-------------------------
    67 // Begin the main procedure
    68 //-------------------------
    69 int main() {
    71   // Define a simple 3D scene, composed of various basic objects (torus, cone, cube, ...)
    72   //-------------------------------------------------------------------------------------
    73   std::fprintf(stderr," - Create 3D Scene.\n");
    74   CImg<float> scene_pts, scene_opacs;
    75   CImgList<unsigned int> scene_prims;
    76   CImgList<unsigned char> scene_cols;
    78   CImgList<unsigned int>
    79     cube_prims,
    80     cone_prims,
    81     torus_prims,
    82     sphere_prims,
    83     plane_prims;
    85   // Define objects vertices and faces.
    86   const CImg<float>
    87     cube_pts = CImg<>::cube3d(cube_prims,60).translate_object3d(-50,50,0),
    88     cone_pts = CImg<>::cone3d(cone_prims,30,40).translate_object3d(50,50,0),
    89     torus_pts = CImg<>::torus3d(torus_prims,30,10).translate_object3d(-50,-50,0),
    90     sphere_pts = CImg<>::sphere3d(sphere_prims,30).translate_object3d(50,-50,0),
    91     plane_pts = CImg<>::plane3d(plane_prims,200,200,20,20,true).translate_object3d(0,0,40);
    93   // Define objects colors and textures.
    94   const CImgList<unsigned char>
    95     cone_cols = CImgList<unsigned char>(cone_prims.size,CImg<unsigned char>::vector(128,63,255)),
    96     torus_cols = CImgList<unsigned char>(torus_prims.size,CImg<unsigned char>::vector(255,55,163)),
    97     sphere_cols = CImgList<unsigned char>(sphere_prims.size,CImg<unsigned char>::vector(115,115,63)),
    98     plane_cols = CImgList<unsigned char>(plane_prims.size,CImg<unsigned char>::vector(60,120,180));
   100   const CImg<unsigned char> texture = CImg<unsigned char>(cimg_imagepath "milla.bmp").resize(128,128);
   101   CImgList<unsigned char> cube_cols;
   102   cimglist_for(cube_prims,p) {
   103     cube_cols.insert(texture,~0U,true);
   104     cube_prims[p].append(CImg<unsigned int>::vector(0,0,127,0,127,127,0,127),'y');
   105   }
   107   // Define objects opacities.
   108   const CImg<float>
   109     cube_opacs(cube_prims.size,1,1,1,1.0f),
   110     cone_opacs(cone_prims.size,1,1,1,0.8f),
   111     torus_opacs(torus_prims.size,1,1,1,0.6f),
   112     sphere_opacs(sphere_prims.size,1,1,1,0.4f),
   113     plane_opacs(plane_prims.size,1,1,1,0.4f);
   115   // Append all object in a single scene.
   116   scene_pts.
   117     append_object3d(scene_prims,cube_pts,cube_prims).
   118     append_object3d(scene_prims,cone_pts,cone_prims).
   119     append_object3d(scene_prims,torus_pts,torus_prims).
   120     append_object3d(scene_prims,sphere_pts,sphere_prims).
   121     append_object3d(scene_prims,plane_pts,plane_prims);
   122   scene_cols<<cube_cols<<cone_cols<<torus_cols<<sphere_cols<<plane_cols;
   123   scene_opacs = (cube_opacs<<cone_opacs<<torus_opacs<<sphere_opacs<<plane_opacs).get_append('x');
   125   // Display object3D in a user-interacted window and get final position matrix.
   126   std::fprintf(stderr," - Display 3D Scene.\n");
   127   const CImg<unsigned char> visu = CImg<unsigned char>(3,512,512,1).fill(230,230,255).permute_axes("yzvx");
   128   CImg<float> view_matrix = CImg<>::identity_matrix(4);
   129   visu.display_object3d("3D Scene",scene_pts,scene_prims,scene_cols,scene_opacs,true,4,4,false,
   130                         500.0f,0.5f,0.1f,true,view_matrix.ptr());
   132   // Save object 3D as OFF file.
   133   std::fprintf(stderr," - Save .OFF 3D object file.\n");
   134   scene_pts.save_off("output.off",scene_prims,scene_cols);
   136   // Save 3D view in SVG, EPS and FIG files.
   137   // (using the Board library : http://www.greyc.ensicaen.fr/~seb/board/ ).
   138 #ifdef cimg_use_board
   140   // Define a Board instance
   141   BoardLib::Board B;
   143   // Set Background color of the board.
   144   B.clear(230,230,255);
   146   // Draw object both in 'visu' and in the board.
   147   (view_matrix.crop(0,0,2,2))*=20;
   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).
   149   display("Snapshot for Board");
   151   // Save board into a vector graphics file format.
   152   std::fprintf(stderr," - Save .SVG, .EPS and .FIG snapshots\n");
   153   B.save("output.svg");
   154   B.save("output.eps");
   155   B.save("output.fig");
   156 #endif
   158   // Exit.
   159   std::fprintf(stderr," - Exit.\n");
   160   return 0;
   161 }