/***********************************************************************/ /***********************************************************************/ /* FreeVR OpenGL tutorial example 1: ex1_bare.c */ /* FreeVR OpenGL tutorial example 2: ex2_static.c */ /* Last tested with: FreeVR version 0.6a */ /* Last tested with: FreeVR version 0.6a */ /* Last Modified: March 23, 2004 */ /* Last Modified: March 23, 2004 */ /* URL: http://freevr.org */ /* URL: http://freevr.org */ /***********************************************************************/ /***********************************************************************/ /* This example code demonstrates a VR application program that */ /* This example code demonstrates a VR application program that */ /* initializes the VR library routines, and renders a blue cube and */ /* initializes the VR library routines, and renders a blue cube and */ /* a red pyramid on the floor near the front wall. */ /* a red pyramid on the floor near the front wall, but in a more */ /* civilized fashion than before. */ /* */ /* */ /* */ /* */ /* The features of this example include: */ /* The new features of this example include: */ /* - FreeVR Library functions, variables, and macros: */ /* - FreeVR Library functions, variables, and macros: */ /* * vrStart() */ /* * vrConfigure() */ /* * vrFunctionSetCallback(VRFUNC_DISPLAY, ...) */ /* * vrFunctionSetCallback(VRFUNC_DISPLAY_INIT, ...) */ /* * vrExit() */ /* * vrFrame() */ /* - A procedure that renders a simple static world */ /* * vrGet2switchValue() */ /* * vrSystemSetName() */ /* * vrSystemSetAuthors() */ /* * vrSystemSetExtraInfo() */ /* * vrSystemSetStatusDescription() */ /* * vrInputSet2switchDescription() */ /* - Internally specifies the basic user interface */ /* - Creates a separate "World simulation" routine/file */ /* - Creates a separate graphics initialization routine (init_gfx) */ /* - VR housekeeping is done in vrFrame() */ /* - Exits the application when the 'Escape' key is pressed */ /* - Cleans up even when an interrupt signal is received */ /* */ /* */ /* */ /* */ /* Considerations: */ /* Considerations: */ /* - No interaction at all */ /* - Doesn't do anything more interesting, other than wait for user */ /* - Program self-terminates after 10 seconds */ /* to terminate. */ /* - Does not handle everything in a civilized manner */ /* - update_world() routine highlights the fact that the simulation */ /* should be separate from the rendering. */ /* - NOTE: the Keypad-9 key toggles the help display on the screen */ /* with the input focus. */ /* */ /* */ /* This application uses the files: */ /* This application uses the files: */ /* - shapes.c (Bill Sherman's simple OpenGL shapes) */ /* - shapes.c (Bill Sherman's simple OpenGL shapes) */ /* */ /* */ /* Copyright 2010, William R. Sherman, All Rights Reserved. */ /* Copyright 2010, William R. Sherman, All Rights Reserved. */ /* In particular, the right to republish this file in whole or in */ /* In particular, the right to republish this file in whole or in */ /* part, in any form (including electronic forms) is prohibited */ /* part, in any form (including electronic forms) is prohibited */ /* without written consent of the copyright holder. */ /* without written consent of the copyright holder. */ /***********************************************************************/ /***********************************************************************/ #include <stdio.h> /* needed for printf(), etc. */ #include <stdio.h> /* needed for printf(), etc. */ #include <stdlib.h> /* needed for exit() */ #include <stdlib.h> /* needed for exit() */ #include <signal.h> /* needed for signal() and its arguments */ #include <GL/gl.h> /* needed for all OpenGL functions & values */ #include <GL/gl.h> /* needed for all OpenGL functions & values */ #include "freevr.h" /* needed for FreeVR functions & values */ #include "freevr.h" /* needed for FreeVR functions & values */ /**************************************************************************** /**************************************************************************** /* These function declarations are very important -- they will be used as arg /* These function declarations are very important -- they will be used as arg void exit_application(int signal); /* cleanly close down operati void init_gfx(void); /* initialize the graphics void draw_world(void); /* graphically render the wor void draw_world(void); /* graphically render the wor void update_world(void); /* perform the world simulati /***********************************************************************/ /***********************************************************************/ main() main(int argc, char* argv[]) { { /**********************************/ /**********************************/ /*** initialize FreeVR routines ***/ /*** initialize FreeVR routines ***/ /**********************************/ /**********************************/ signal(SIGINT, SIG_IGN); vrConfigure(&argc, argv, NULL); /* allow CLAs to affect confi vrStart(); vrStart(); signal(SIGINT, exit_application); /***************************************/ /***************************************/ /* set up the FreeVR library functions */ /* set up the FreeVR library functions */ vrFunctionSetCallback(VRFUNC_DISPLAY_INIT, vrCallbackCreate(init_gfx, vrFunctionSetCallback(VRFUNC_DISPLAY, vrCallbackCreate(draw_world, 0) vrFunctionSetCallback(VRFUNC_DISPLAY, vrCallbackCreate(draw_world, 0) /********************************************************/ /* define the application name, authors, controls, etc. */ vrSystemSetName("FreeVR tutorial: example 2 -- static"); vrSystemSetAuthors("Bill Sherman"); vrSystemSetExtraInfo("An example to show a basic static world"); vrSystemSetStatusDescription("Application running fine"); vrInputSet2switchDescription(0, "Terminate the application"); /***************************/ /***************************/ /*** do world simulation ***/ /*** do world simulation ***/ /***************************/ /***************************/ /* do stuff here -- or not */ /* run until terminate key is pressed (Escape by default) */ vrSleep(10000000); /* 10 seconds */ while(!vrGet2switchValue(0)) { vrFrame(); update_world(); } /*********************/ /*********************/ /*** close up shop ***/ /*** close up shop ***/ /*********************/ /*********************/ exit_application(0); } /********************************************************************/ /* exit_application(): clean up anything started by the application */ /* (forked processes, open files, open sockets, etc.) */ /********************************************************************/ void exit_application(int signal) { vrExit(); vrExit(); exit(0); exit(0); } } /* ----------------8<-----------------8<-----------------8<----------------*/ /* ----------------8<-----------------8<-----------------8<----------------*/ /* In a non-example applicatation, the following would be a separate file. */ /* In a non-example applicatation, the following would be a separate file. */ /**********************************/ /**********************************/ /** The World Simulation section **/ /**********************************/ /**********************************/ /********************************************************************/ /* update_world(): Do absolutely nothing. The short sleep allows */ /* multiprocessing systems a good place to switch to other */ /* processing threads/forks -- something that can greatly improve */ /* the rendering frame rate. */ /* Conceptually, this is the place where the "world physics" or the */ /* world simulation should be, along with user interface */ /* processing, as will be exemplified in forthcoming examples. */ /* NOTE: it is very important that the simulation calculations be */ /* separate from the rendering of the virtual world. */ /********************************************************************/ void update_world(void) { /* Nothing happens -- this is a static world. */ /****************************************************/ /* yield to allow other processes access to the CPU */ vrSleep(10); } /* ----------------8<-----------------8<-----------------8<----------------*/ /* In a non-example applicatation, the following would be a separate file. */ /**************************/ /**************************/ /**************************/ /**************************/ /** The Graphics section **/ /** The Graphics section **/ /**************************/ /**************************/ /**************************/ /**************************/ /* functions from shapes.c */ /* functions from shapes.c */ void draw_cube(); void draw_cube(); void draw_pyramid(); void draw_pyramid(); /*********************************************************************/ /* init_gfx(): initialize general graphics properties (eg. lighting) */ /*********************************************************************/ void init_gfx(void) { /****************************************/ /* set the render initialization values */ glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0); /**************************************/ /* set the polygon shading parameters */ glShadeModel(GL_SMOOTH); } /*************************************************************/ /*************************************************************/ /* draw_world(): Render the two objects in the world -- the */ /* draw_world(): Render the two objects in the world -- the */ /* blue cube, and red pyramid -- such that they are on the */ /* blue cube, and red pyramid -- such that they are on the */ /* XZ plane (typically the floor), and 6 feet forward */ /* XZ plane (typically the floor), and 6 feet forward */ /* (typically on the other side of a CAVE's front screen). */ /* (typically on the other side of a CAVE's front screen). */ /*************************************************************/ /*************************************************************/ void draw_world(void) void draw_world(void) { { /**************************************/ /**************************************/ /* set the polygon shading parameters */ glShadeModel(GL_SMOOTH); /**************************************/ /* clear the screen -- very important */ /* clear the screen -- very important */ glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /************************/ /************************/ /* draw all the objects */ /* draw all the objects */ /* a blue cube */ /* a blue cube */ glColor3ub(100, 100, 255); glColor3ub(100, 100, 255); glTranslatef(-2.0, 1.0, -6.0); glTranslatef(-2.0, 1.0, -6.0); draw_cube(); draw_cube(); /* a red pyramid */ /* a red pyramid */ glColor3ub(255, 100, 100); glColor3ub(255, 100, 100); glTranslatef(4.0, 0.0, 0.0); glTranslatef(4.0, 0.0, 0.0); draw_pyramid(); draw_pyramid(); } }