home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / egltk22.zip / sample2.cpp < prev    next >
Text File  |  1998-07-30  |  11KB  |  248 lines

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Sample Escape GL Custom Screen Saver
  4. //    Copyright (c) 1997, Snow Storm Software
  5. //    All Rights Reserved
  6. //
  7. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. #include "glcustom.hpp"
  10.  
  11. #include <glut.h>
  12.  
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. //
  15. // general instructions:
  16. //   - copy this file to another name eg. cool.cpp
  17. //   - modify all functions examples to provide the processing you desire
  18. //   - add your project to the 'TARGETS =' part of 'MAKEFILE.' eg. 'TARGETS = sample.egl cool.egl'
  19. //   - make sure the 'TOP =' part of 'MAKEFILE' correctly identifies the location of your opengl base directory
  20. //      eg. 'TOP = \opengl'
  21. //   - build your project using 'nmake'
  22. //   - copy your new screen saver module 'cool.egl' to any Escape GL sub-directory
  23. //   - reload Escape GL
  24. //
  25. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26.  
  27. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. //
  29. // new class definition
  30. //
  31. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32.  
  33. static class SampleModel
  34.    : public GLCustomModelV2
  35. {
  36.    public:
  37.       SampleModel();
  38.       virtual ~SampleModel();                                // virtual destructor
  39.       virtual void initialize();                               // virtual function to initialize the model
  40.       virtual void destroy();                                  // virtual function to destroy the model
  41.       virtual void animate( GLfloat fStep );                   // virtual function to animate the model
  42.       virtual void display();                                  // virtual function to display the model
  43.       virtual void reshape( long width, long height );         // virtual function to reshape the viewport
  44.       virtual void addSettings();                              // virtual function to add settings definitions
  45.    private:
  46.       GLfloat rotation;
  47.       GLboolean spin;
  48.       GLint quality;
  49.       char *text;
  50.       Point3D point;
  51. } sample;
  52.  
  53. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. //
  55. // global variables, constants, common functions, other classes
  56. //
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58.  
  59. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. //
  61. // constructor
  62. //   creates the model object
  63. //   do not perform any OpenGL commands in the constructor, they will not work
  64. //   the constructor is only called once, when the model is created the first time
  65. //
  66. //   GLCustomModel takes as its parameter the name of the module as it will appear
  67. //   in the drop down module list, this name must be unique and not match any
  68. //   existing modules, nor custom modules
  69. //
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71.  
  72. SampleModel :: SampleModel()
  73.    : GLCustomModelV2( "Cube Sample 2" )
  74.    , rotation( 0 )
  75. {
  76.    //
  77.    // all of the following settings are the defaults, only use to change the defaults
  78.    //
  79.    //setDoubleBuffered( GL_TRUE );                             // set double buffered flag
  80.    //setFPS( 10. );                                            // set the desired maximum frames per second
  81.    //setMaxStepSize( 0.25 );                                   // set the maximum step size (seconds)
  82. }
  83.  
  84. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. //
  86. // destructor
  87. //   destroys the object
  88. //   this is only called when the program is exited, not when the model is changed, use destroy to
  89. //     destroy anything related to the actual model use
  90. //
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92.  
  93. SampleModel :: ~SampleModel()
  94. {
  95. }
  96.  
  97. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  98. //
  99. // initialize
  100. //   initializes the opengl module you wish to display and is called once before the model is animated
  101. //   this function is called once to setup the module
  102. //
  103. //   retrieve any settings here, not in the constructor
  104. //
  105. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106.  
  107. void SampleModel :: initialize()
  108. {
  109.    glClearColor( 0.0, 0.0, 0.0, 0.0 );
  110.    glShadeModel( GL_FLAT );
  111.  
  112.    spin = getOnOffSetting( "Spin" );
  113.  
  114.    //
  115.    // the quality setting is not used for this sample, however is presented as an example.
  116.    //
  117.    quality = getNumericSetting( "Quality" );
  118.  
  119.    //
  120.    // the directory setting is not used for this sample, however is presented as an example.
  121.    //
  122.    char dir[256], file[256];
  123.    GLboolean inc;
  124.    getDirectorySetting( "Dir", dir, file, &inc );
  125.  
  126.    //
  127.    // the text setting is not used for this sample, however is presented as an example.
  128.    //
  129.    text = getTextSetting( "Text" );
  130.  
  131.    //
  132.    // the point setting is not used for this sample, however is presented as an example.
  133.    //
  134.    point = getPointSetting( "Color" );
  135.  
  136. }
  137.  
  138. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  139. //
  140. // destroy
  141. //   destroy display lists, objects etc. when the module has ended
  142. //
  143. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  144.  
  145. void SampleModel :: destroy()
  146. {
  147. }
  148.  
  149. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  150. //
  151. // animate
  152. //   animates and displays the module
  153. //   the parameter 'step' simply defines the time in seconds between time steps, use this to control the animation speed
  154. //    the size of the step is based on the speed of the target system and the complexity of the module, slower modules
  155. //    will take larger steps
  156. //
  157. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  158.  
  159. void SampleModel :: animate( GLfloat step )
  160. {
  161.    if ( spin )
  162.    {
  163.       rotation += step * 270.;                                 // rotate at 30 deg per second
  164.       if ( rotation > 360.0 )
  165.          rotation -= 360.;
  166.    }
  167. }
  168.  
  169. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  170. //
  171. // display
  172. //   display the model
  173. //
  174. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  175.  
  176. void SampleModel :: display()
  177. {
  178.    glClear( GL_COLOR_BUFFER_BIT );
  179.    glColor3f( 1.0, 1.0, 1.0 );
  180.  
  181.    glLoadIdentity();             /* clear the matrix */
  182.  
  183.    /* viewing transformation  */
  184.    gluLookAt( 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
  185.  
  186.    /* rotate */
  187.    glRotatef( rotation, 0, 1, 0 );
  188.    glutWireCube( 1.0 );
  189.  
  190.    // NOTE: swapping of the buffers is done automatically by Escape GL and should not be added here.
  191. }
  192.  
  193. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  194. //
  195. // reshape
  196. //   resizes the window
  197. //   use this function to define the viewport and projection transformations based on the window size
  198. //
  199. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  200.  
  201. void SampleModel :: reshape( GLint width, GLint height )
  202. {
  203.    glViewport( 0, 0, width, height );
  204.    glMatrixMode( GL_PROJECTION );
  205.    glLoadIdentity();
  206.    glFrustum( -1.0, 1.0, -1.0, 1.0, 1.5, 20.0 );
  207.    glMatrixMode( GL_MODELVIEW );
  208. }
  209.  
  210. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. //
  212. // addSettings
  213. //   add your custom settings here
  214. //   simply use the add* function to add the type of setting you want
  215. //   leave this function blank if no settings are required
  216. //   NOTE: settings are displayed in reverse order, so add the setting you want first, last
  217. //
  218. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  219.  
  220. void SampleModel :: addSettings()
  221. {
  222.    //
  223.    // sample settings, add or remove these as desired
  224.    //
  225.    addOnOffSetting( "Spin", "Spin the Model",                     // id, prompt
  226.                     "Spin:  indicates if the model will spin.",   // help
  227.                      1 );                                         // default (0 = off, 1 = on)
  228.  
  229.    addNumericSetting( "Quality", "Quality (1 lowest, 3 highest)", // id, prompt
  230.                       "Quality:  model quality.",                 // help
  231.                       1, 1, 3 );                                  // default, minimum, maximum
  232.  
  233.    addDirectorySetting( "Dir", "\\os2\\bitmap", "*", 0 );         // id, default directory, file default, include subdirectories default
  234.  
  235.    addDescription( "Module Description" );                        // this description shows up at the top of the help notebook page
  236.    setDefaultWindowSize( 7 );                                     // set the default window size
  237.    setDefaultFloatingWindow( 1 );                                 // set the default for floating windows
  238.  
  239.    addTextSetting( "Text", "Text String",                         // id, prompt
  240.                    "Text String: text help",                      // help
  241.                    "default text" );                              // default
  242.  
  243.    addPointSetting( "Color", "Color", "Color: color of object",   // id, prompt, help
  244.                     Point3D( 5, 5, 5 ),                           // default 3D point value
  245.                     0, 360 );                                     // minimum and maximum of values
  246. }
  247.  
  248.