4D3 Computer Graphics Opengl Project Report
Total Page:16
File Type:pdf, Size:1020Kb
4D3 Computer Graphics OpenGL Project Report Mikhail Volkov <volkovm at tcd dot ie> 04400593 January 18, 2010 Department of Electronic & Electrical Engineering Trinity College Dublin Contents I User Documentation 3 1 Introduction 3 2 Getting Started 4 2.1 Operating System Requirements . .4 2.2 Setup . .4 2.3 Controls . .4 II Developer Documentation 5 1 Technical Overview 5 1.1 Why C#? . .5 1.2 Why Tao? . .5 1.3 Why FreeGlut? . .6 2 Implementation Details 6 2.1 Navigation . .7 2.1.1 Mouse Look . .7 2.1.2 WASD Movement . .8 2.2 Representation . 10 2.3 Physics and Collision Detection . 13 2.3.1 Collisions between Entities and Bullets . 13 2.3.2 Collision with Walls . 14 2.3.3 Bullet Management . 16 2.4 Models and Animation . 17 2.4.1 Hierarchical Spider Models . 17 2.4.2 Animation . 21 2.4.3 Animated Sky . 23 2.5 Gameplay and AI . 25 2.5.1 Spider Bite Handling . 25 2.5.2 Spider AI . 25 2.5.3 Spider Eggs . 26 2.6 Stereo Display Mode . 26 3 Sources 31 4 Final Remarks 31 Part I User Documentation 1 Introduction Beware of the Spider was meant to be an introspective exploration of my irrational fear of spiders. Armed with a flak cannon, the protagonist navigates through a maze, slaughtering giant arachnids and leaving a trail of blood and carnage in his wake. The objective of the game is to find and destroy the (obviously evil) Gargantuan Spider Queen. But it is no easy feat! The labyrinth is infested with her progeny and the floors are littered with spider eggs. You start with 100 life and 200 shells of ammunition. If a spider sees you he will chase until he catches you, or until you are out of sight. If a spider catches you he will commence feasting on your blood and you will continuously lose life. Should your life reach zero, you will be dead. Finally, you should tread carefully around spider eggs or you may be in for a nasty surprise. You have been warned! The game features a first-person view, WASD navigation with mouse look, collision detection with the environment and between entities, animated hierarchical spiders, enemy AI, and last but not least, in the true spirit of cheesy horror movies from the sixties, the game features an old school red-cyan anaglyph mode. Figure 1: Startled people watching a 3D movie 3 2 GETTING STARTED 2 Getting Started 2.1 Operating System Requirements The game uses the resolution of your primary monitor and runs in fullscreen. It was developed and tested on Windows Vista (32-bit), and it runs very smoothly at 1280×800 and 1280×1024 resolutions. It does not run well on Windows XP! The mouse and keyboard sensitivities do not agree between the two operating systems, and it is generally slow for some unknown reason. Avoid running it on XP, as it will ruin the game. 2.2 Setup Beware of the Spider requires that you have the latest FreeGlut binaries (available at http://freeglut.sourceforge.net) and the latest version of the Tao Frame- work (available at http://sourceforge.net/projects/taoframework) installed on your computer. Once everything is set up, you can run the .exe or open the C# solution and run using Visual Studio. You must run the .exe from its original directory as it will look for resources in specific locations. 2.3 Controls The basic controls allow you to navigate through the maze and to shoot. This is all you need to play the game. With the advanced controls you can enable stereo mode and change the stereo settings. The default stereo settings are optimized for most people on most display monitors and you are advised not to change them. However, not everybody perceives stereo in the same way, and you can fine-tune the stereo settings using the arrow keys if you really want. The focal length determines the point of the scene which is on the projection plane. In other words, everything in front of that point will be perceived as coming out of the screen and everything behind that point will be perceived as going into the screen. The interocular distance is the distance between the two cameras of the stereo view, and is meant to correspond to the distance between the eyes. W Move forward V Toggle stereo mode S Move backward Up arrow Increase focal length A Strafe left Down arrow Decrease focal length D Strage right Left Arrow Increase interocular distance Mouse Look Right arrow Decrease interocular distance Left click Shoot P Toggle wireframe mode (cheating!) Table 1: Basic controls Table 2: Advanced controls 4D3 Computer Graphics 4 Mikhail Volkov OpenGL Project Report Part II Developer Documentation 1 Technical Overview Beware of the Spider is written in C# using the Tao Framework. The Tao Framework is a collection of bindings to facilitate cross-platform media application development utilizing the .NET and Mono platforms. For this game I used the Tao.OpenGl 2.1.0.12 and Tao.FreeGlut 2.4.0.2 assemblies. 1.1 Why C#? To a large extent it comes down to personal preference. I am a big fan of .NET and I was curious to see how a computationally demanding application such as a 3D game would cope compared with a native C++ solution, for example. Furthermore, C# is a powerful, expressive and elegant language. Throughout this report I will allude to this claim, giving specific examples of things I did with C# that you cannot do with C++, and how it improved the design of my game. 1.2 Why Tao? There are several possibilities for using OpenGL with C#. • SharpGL: I started off with SharpGL. It is very easy to use, but I found it to be somewhat buggy and unstable. SharpGL requires you to instantiate an OpenGL object, rather than making static calls from a namespace, which felt somewhat unintuitive. It also features a Scene Graph interface which makes it much easier to work with certain OpenGL structures like cameras and textures. However, it obfuscates the underlying principles and was not good from a learning point of view. • CsGL: This was the second API I tried. CsGL felt much more robust than SharpGL, but it is not supported any more and most of its user base seems to have migrated to Tao. • Tao: Tao has been around for a number of years and it is the most established OpenGl framework for .NET, with a large developer community. Tao features bindings for the entire OpenGL API, as well as FreeGlut and many other third party libraries. • OpenTK: OpenTK is cross-platform, offers a number of helper libraries, and is growing in popularity. But Tao is more mature, and OpenTK is actually based on Tao so in the end I decided to use Tao and did not get a chance to try OpenTK. 5 2 IMPLEMENTATION DETAILS 1.3 Why FreeGlut? Finally, it is worth a mention that a .NET framework can be used with any .NET language, so C++/CLI was another tempting possibility. We have the benefits of a managed environment and the ability to drop down to native C++ for performance bot- tlenecks. This would also have been possible in combination with FreeGlut. However, I believe C# offered more advantages as a language. 1.3 Why FreeGlut? Tao is simply a collection of bindings for the native OpenGL binaries. In order for OpenGL to work it requires a device and a rendering context, and we have exactly the same options to choose from under Tao. These are some of the options that I considered: • Glut/FreeGlut: This is the course standard. It is fast, cross-platform and very easy to use. It is also very readable and a lot of examples on the internet use FreeGlut. Given that I am already deviating from the course in my choice of programming language, I was reluctant to also use a different window manager, unless I saw very a compelling reason to do so. • SimpleOpenGlControl: This is a widget that creates an OpenGl context as a Windows Forms control. It means that we can select it from the toolbox in Visual Studio and drag and drop it into our Windows Forms application. In- fact we can have multiple controls in one application. This is very powerful and I used it in the initial stages of the game development as a rapid prototyping environment. But ultimately it is not suitable for this sort of game as it slow, and in most cases users would prefer to play a first-person shooter in fullscreen so they probably would not need or want buttons and menus. • Win32: This is the context used in the NeHe tutorials. It sets up a Win32 environment and compares quite favorably with FreeGlut in terms of performance. Although it offers more control than FreeGlut at a low level, it also poses more potential problems. I saw no reason to use this, other than for the sake of it. • SDL: Another good alternative and we get the extra benefits of sound and True- Type fonts. Tao also has a wrapper for SDL so this would have been easy to do. I have no idea about performance though, and I did not see a really convincing reason to choose this over FreeGlut. 2 Implementation Details This section will detail all the notable features in Beware of the Spider. They are listed roughly in the order I worked on them and this reflects what I believe to be the most appropriate order in the development cycle of this kind of game.