3D Underwater World (using OpenGL and SDL)

Copyright Tristan Aubrey-Jones January 2008.

This is an example OpenGL SDL application which creates an animated 3D underwater world (screenshot), with "Thunderbird 4" with headlights moving on a spline path, a randomly generated sand terrain, sunken ship/submarine/treasure chest models, and swaying fish and seaweed. It is designed to demonstrate basic OpenGL features, and the structure is as follows:

main.cpp

home Home   up Up   ( Download )


/* COMP3004: COURSEWORK 3 * Tristan Aubrey-Jones (taj105) * ------------------------------- */ #include #include #include #include "global.h" #include "game.h" // method declarations void initgl(int, char**); // CONTROL // program entry point int main(int argc, char *argv[]) { // setup graphics library initgl(argc, argv); // set window manager title bar SDL_WM_SetCaption( "COMP3004 Coursework 3", "cwk3" ); // sets up a parallel projection on the SDL window glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // run Game* game = new Game(); game->run(); // quit delete game; SDL_Quit(); return 0; } // set up opengl and sdl // -------------------------------------------------------------- // TAKEN FROM COMP3004 COLOURED CUBE NOTES EXAMPLE // https://secure.ecs.soton.ac.uk/notes/comp3004/lectures/code.html // VERBATIM BEGINS HERE void initgl(int argc, char* argv[]) { int rgb_size[3]; int bpp; int i; Uint32 video_flags = SDL_OPENGL; if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError()); exit( 1 ); } /* See if we should detect the display depth */ if ( bpp == 0 ) { if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) { bpp = 8; } else { bpp = 16; /* More doesn't seem to work */ } } for ( i=1; argv[i]; ++i ) { if ( strcmp(argv[i], "-fullscreen") == 0 ) { video_flags |= SDL_FULLSCREEN; } } bpp = 32; /* Initialize the display */ switch (bpp) { case 8: rgb_size[0] = 3; rgb_size[1] = 3; rgb_size[2] = 2; break; case 15: case 16: rgb_size[0] = 5; rgb_size[1] = 5; rgb_size[2] = 5; break; default: rgb_size[0] = 8; rgb_size[1] = 8; rgb_size[2] = 8; break; } SDL_GL_SetAttribute( SDL_GL_RED_SIZE, rgb_size[0] ); // Sets bits per channel SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, rgb_size[1] ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, rgb_size[2] ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 ); // 3 channels per pixel (R, G and B) SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 ); // ? SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 1 ); SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 ); SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, 1 ); if ( SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, bpp, video_flags) == NULL ) { fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError()); SDL_Quit(); exit(1); } } // -------------------------------------------------------------------- // VERBATIM ENDS HERE