home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / openglso.zip / gr.c < prev    next >
C/C++ Source or Header  |  2000-02-16  |  6KB  |  213 lines

  1. /*========================================================================= 
  2.  *                                 CS 488/688                                
  3.  *                          Introduction to Graphics                        
  4.  *                                                                         
  5.  *                           Assignment 1: OpenGL                         
  6.  *                                                                       
  7.  *=========================================================================*/
  8. #include <string.h>
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #ifdef __EMX__
  13. #include <float.h>
  14. #else
  15. #include <values.h>
  16. #endif /* __EMX__ */
  17.  
  18. #include "util.h"
  19. #include "gr.h"
  20. #include "Draw.h"
  21.  
  22. /*========================================================================= 
  23.  * Tcl/Gr commands:
  24.  *    These commands change the internal state of Gr, but do not
  25.  *    directly request any rendering (they can't, since they have no
  26.  *    access to the Togl handle, and may be called before an OpenGL
  27.  *    context is even available).
  28.  *
  29.  *    A pointer to a statically allocated string should be returned in
  30.  *    case of an error, (char *)0 in case of success.
  31.  *=========================================================================*/
  32.  
  33. /*-------------------------------------------------------------------------
  34.  * gr_mode:
  35.  *-------------------------------------------------------------------------*/
  36. char*
  37. gr_mode (
  38.     char mode
  39. ) {
  40.     fprintf(stderr,"Called: gr_mode(mode=\"%c\")\n",mode);
  41.       return "Not implemented";
  42. }
  43.  
  44. /*-------------------------------------------------------------------------
  45.  * gr_buffer:
  46.  *   Turn double buffering on or off
  47.  *-------------------------------------------------------------------------*/
  48. char*
  49. gr_buffer (
  50.     char mode
  51. ) {
  52.     fprintf(stderr,"Called: gr_buffer(mode=\"%c\")\n",mode);
  53.       return "Not implemented";
  54. }
  55.  
  56. /*-------------------------------------------------------------------------
  57.  * gr_lighting:
  58.  *   Set the lighting mode
  59.  *-------------------------------------------------------------------------*/
  60. char*
  61. gr_lighting (
  62.     char* lighting
  63. ) {
  64.     fprintf(stderr,"Called: gr_lighting(lighting=\"%s\")\n",lighting);
  65.       return "Not implemented";
  66. }
  67.  
  68. /*-------------------------------------------------------------------------
  69.  * gr_save:
  70.  *   Save the current image to a file
  71.  *-------------------------------------------------------------------------*/
  72. char*
  73. gr_save (
  74.     char* filename
  75. ) {
  76.     fprintf(stderr,"Called: gr_save(filename=\"%s\")\n",filename);
  77.       return "Not implemented";
  78. }
  79.  
  80. /*-------------------------------------------------------------------------
  81.  * gr_reset:
  82.  *   Return Gr state to its initial configuration.
  83.  *-------------------------------------------------------------------------*/
  84. char*
  85. gr_reset (
  86.     void
  87. ) {
  88.     fprintf(stderr,"Called: gr_reset()\n");
  89.       return (char *)0;
  90. }
  91.  
  92. /*-------------------------------------------------------------------------
  93.  * gr_rotate:
  94.  *   Rotate object about x,y, or z axis by a particular angle in degrees.
  95.  *-------------------------------------------------------------------------*/
  96. char*
  97. gr_rotate (
  98.     char axis, 
  99.     double angle
  100. ) {
  101.     fprintf(stderr,"Called: gr_rotate(axis='%c',angle=%f)\n",axis,angle);
  102.       return "Not implemented";
  103. }
  104.  
  105. /*-------------------------------------------------------------------------
  106.  * gr_scale:
  107.  *   Scale object by a given factor.
  108.  *-------------------------------------------------------------------------*/
  109. char*
  110. gr_scale (
  111.     double factor
  112. ) {
  113.     fprintf(stderr,"Called: gr_scale(factor=%f)\n",factor);
  114.       return "Not implemented";
  115. }
  116.  
  117. /*========================================================================= 
  118.  * Togl callbacks:
  119.  *    These functions are called by Togl when certain events (such as
  120.  *    <Expose>) occur, or when requested using a togl subcommand (such as
  121.  *    render).
  122.  *=========================================================================*/
  123.  
  124. /*-------------------------------------------------------------------------
  125.  * gr_reshape:
  126.  *    When the display area changes size, recompute the perspective matrix
  127.  *    and reload, maintaining the aspect ratio.
  128.  *-------------------------------------------------------------------------*/
  129. void
  130. gr_reshape(
  131.     struct Togl* togl
  132. ) {
  133.     int width, height;
  134.  
  135.     /* Get new size of display 
  136.      */
  137.     width = Togl_Width(togl);
  138.     height = Togl_Height(togl);
  139.  
  140.     /* Set up perspective projection, using current size and aspect
  141.      * ratio of display
  142.      */
  143.     glMatrixMode(GL_PROJECTION);
  144.     glLoadIdentity();
  145.     glViewport(0, 0, width, height);
  146.     gluPerspective(40.0, (GLfloat)width/(GLfloat)height, 0.1, 1000.0);
  147.  
  148.     /* Reset to modelview matrix mode
  149.      */
  150.     glMatrixMode(GL_MODELVIEW);
  151. }
  152.  
  153. /*-------------------------------------------------------------------------
  154.  * gr_render:
  155.  *    Actually draw the scene using OpenGL, using the state previously set
  156.  *-------------------------------------------------------------------------*/
  157. void
  158. gr_render(
  159.     struct Togl* togl
  160. ) {
  161.     /* Reset modelview matrix
  162.      */
  163.     glLoadIdentity();
  164.  
  165.     /* Clear framebuffer (both colour and depth buffers)
  166.      */
  167.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  168.  
  169. #ifdef __EMX__
  170.     /* Helps illustarate the Double Buffer vs Single Buffer in OS/2. */
  171.     glFlush();
  172. #endif
  173.  
  174.     /* Translate local coordinate system away from camera 
  175.      */
  176.     glTranslatef(0, 0, -6);
  177.  
  178.     /* Draw cube in new local coordinate system
  179.      */
  180.     DrawUnitCube();
  181.  
  182.     /* Force OpenGL drawing to completion and swap buffers
  183.      */
  184.     Togl_SwapBuffers(togl);
  185. }
  186.  
  187. /*-------------------------------------------------------------------------
  188.  * gr_initialize:
  189.  *    Once togl widget created, intialize OpenGL context and gr state to
  190.  *    values we want
  191.  *-------------------------------------------------------------------------*/
  192. void 
  193. gr_initialize (
  194.     struct Togl* togl
  195. ) {
  196.     /* Turn on Gouraud shading
  197.      */
  198.     glShadeModel(GL_SMOOTH);
  199.  
  200.     /* Create a bluish background when clear colour buffers
  201.      */
  202.     glClearColor(0.3,0.3,0.8,0.0);
  203.  
  204.     /* Enable depth test
  205.      */
  206.     glEnable(GL_DEPTH_TEST);
  207.  
  208.     /* Initialize the cube display list
  209.      */
  210.     MakeUnitCube();
  211. }
  212.  
  213.