A Haxe Implementation of Essential Opengl Examples Using Duell Tool
Total Page:16
File Type:pdf, Size:1020Kb
A Haxe Implementation of Essential OpenGL Examples Using Duell Tool Bachelor Arbeit Alexis Iakovenko-Marinitch Fachbereich Mathematik und Informatik Institut f¨urInformatik Freie Universit¨atBerlin, Deutschland July 29, 2016 Gutachter: Prof. Dr. Tim Landgraf und Dipl. Inf. Till Zoppke Betreuer: Dipl. Inf. Till Zoppke Externer Betreuer: Sven Otto, GameDuell GmbH Selbstst¨andigkeitserkl¨arung Hiermit best¨atigeich, dass ich die vorliegende Ausarbeitung mit dem Titel A Haxe Implementation of Essential OpenGL Examples Using Duell Tool selbstst¨andigund ohne unerlaubte Hilfe angefertigt habe. Ich versichere, dass ich ausschließlich die angegebenen Quellen in Anspruch genom- men habe. Statement of Authorship I declare that this thesis entitled A Haxe Implementation of Essential OpenGL Ex- amples Using Duell Tool is the result of my own research except as cited in the references. The thesis has not been accepted for any degree and is not concurrently submitted in candidature of any other degree. Except where acknowledged in the customary manner, the material presented in this thesis is, to the best of my knowl- edge, original and has not been submitted in whole or part for a degree in any university. Alexis Iakovenko-Marinitch Berlin, July 29, 2016 Abstract With the augmenting variety of platforms on the market, supporting as many of them became an inevitable obstacle to confront in order to reach as many users as possible. Platforms support different native languages, such as Java for Android or Objective-C for iOS. The most straight-forward way to support both of these platforms is to produce native codes for each of them, which is greatly cost and time consuming. There are several tools to avoid programming in the native languages of targeted platforms and to achieve cross-platform programming, but using them generally results in a performance loss. For video games, such a loss can be critical, which is why native programming is favored in order to get the best performance possible from the targeted platforms. In this paper we investigate the productivity and efficiency of cross-platform pro- gramming with Haxe by reimplementing essential OpenGL features while targeting two platforms at the same time, Android and Windows. The result of our research showed that, while the consumed time in programming cost trivially less effort than native coding on each of the different platforms, Haxe did not engender any significant performance loss. To deepen this investigation, expanding the scenes' complexity or making an ac- tual game would put the devices to test in more extreme conditions. Contents 1 Introduction 1 1.1 Motivation . .1 1.2 Problem . .2 1.3 Plan . .3 2 Haxe and Duell Tool 5 2.1 Haxe . .5 2.2 Duell Tool . .6 2.2.1 Running a project with Duell . .6 3 OpenGL 7 3.1 Introduction . .7 3.2 OpenGL ES . .7 3.3 The flow of an OpenGL program . .8 3.3.1 Initialization . .8 3.3.2 Updating and rendering cycle . .9 3.3.3 Destroying . .9 3.4 Shading . .9 3.4.1 Vertex Shader . .9 3.4.2 Fragment Shader . 10 3.5 OpenGL in Haxe with DuellKit . 10 4 The Examples 13 4.1 Hello World! . 13 4.1.1 The triangle . 14 4.1.2 The colors . 14 4.2 3D scene . 15 4.2.1 The 3D Objects . 15 4.2.2 The Lighting . 16 4.2.3 The Texturing . 16 4.3 Text . 16 4.3.1 Bitmap Font Rendering . 18 5 Implementation 19 5.1 Global structure . 19 5.2 Triangle . 19 5.2.1 Vertex and Index buffers . 19 7 5.2.2 Rendering . 20 5.3 3D model and lighting . 22 5.3.1 .Obj Format . 22 5.3.2 Model, view and projection matrices . 26 5.3.3 Texturing . 29 5.3.4 Lighting . 30 5.3.5 Final output . 34 5.4 Text . 34 5.4.1 Texture map . 35 5.4.2 Texture coordinates . 35 5.4.3 Background . 37 5.4.4 Final output . 38 6 Testing and evaluation 39 6.1 Triangle . 39 6.1.1 Features . 39 6.1.2 Performances . 40 6.1.3 Conclusion . 40 6.2 3D model and lighting . 40 6.2.1 Features . 41 6.2.2 Performances . 41 6.2.3 Conclusion . 41 6.3 Text . 41 6.3.1 Features . 41 6.3.2 Performances . 42 6.3.3 Conclusion . 42 7 Summing up and perspectives 43 Bibliography 45 List of Figures 3.1 In green the Index Buffer’s content. In blue the vertices coordinates.9 4.1 The first PowerVR example . 13 4.2 The second PowerVR example . 15 4.3 The third PowerVR example . 16 4.4 Parameters of a single character . 17 4.5 Texture map for a geometry of quads . 18 5.1 Result of the first example . 22 5.2 The UV mapping of a cube in Blender . 24 5.3 The UV map for a human . 24 5.4 The looks of the texture and the final model . 25 5.5 Three teapots set in World Space . 26 5.6 On the Left two teapots and a camera in World Space; On the right ev- erything is transformed into View Space (World Space is represented only to help visualize the transformation) . 27 5.7 Perspective Projection . 28 5.8 A lit and an unlit sphere . 30 5.9 .obj 3D object textured and lit . 34 5.10 Texture atlas . 35 5.11 Tile, Tile width, letter width . 36 5.12 Texture for the background . 37 5.13 Bitmap Text output . 38 6.1 Comparison between example 1 and its implementation . 39 6.2 Comparison between example 2 and its implementation . 40 6.3 Comparison between example 3 and its implementation . 42 Chapter 1 Introduction A graphic engine is a part of a program responsible for the rendering of 2D or 3D computer graphics of an application[1]. It can achieve several goals. Most of the time, a graphic engine facilitates the work of a developer, so that he can focus on the actual application's functionalities. For example, a game developer should not burden himself with the rendering technology behind his game, but concentrate on the game mechanics, logics and implementation instead[2]. But rendering something on the screen is not enough. A graphic engine must fulfill specific requirements, depending on the application a developer wants to make. To cover the majority of their users' needs and allow them to be used for different projects and contexts, graphic engines should be easy to use and offer a large variety of functionalities and tools. Some noteworthy technologies are: texturing, loading 3D models, shaders, lighting, shadows, transparency, particle system, fog, reflection, text/UI rendering. [3, 4, 5] However offering these tools is not the only challenge a graphic engine must overcome. In addition to producing pretty images on the screen, the rendering process must happen efficiently. For our use case, games, the rendering happens in real-time and must be achieved in fractions of seconds so that the animations and game mechanics appear fluid to the player, giving him the best experience possible. To that effect, constructing a graphic engine requires an enormous amount of work and versatile expertises in the computer science sector. 1.1 Motivation Because of the never ending improvements of graphics processing units, 3D is not only a standard for PCs but also for mobiles[6, 7]. There are several different popular platforms[8, 9] on the market which make it profitable to create multi-platform code and develop softwares which will work on as many devices as possible. More and more 2D and 3D cross-platform games and graphic engines are being created and are arousing important interest around them[10]. However adding support for an additional platform can require a lot more programming hours and severe additional costs. There are many reasons for this. Different platforms require programming in different languages: Java for Android, Objective-C/Swift for iOS and so on[11, 12]. This means programming the same features multiple times and the mastering of different programming languages is necessary. To resolve this problem, there exist lots of tools which help the developer with one code to create software working 1 Chapter 1 Introduction on multiple devices[13, 14]. Generally those tools reduce the programming time drastically, but show significant drops in performance. The main goal of this thesis is to show that using multi-platform tools is a viable option for developing a cross- platform graphic engine. 1.2 Problem In order to investigate the viability of programming a cross-platform graphic engine with multi-platform tools, we decided to approach a company that is using such tools everyday. GameDuell GmbH is a company developing video games accross multiple plat- forms, such as iOS, Android, Facebook and web [15]. With the aim of solving the problem of cross-platform developing, GameDuell is using a cross-platform pro- gramming language called Haxe [16]. The company published succesful 2D cross- platform games [15, 17] coded with Haxe, using a tool they made to facilitate the cross-platform development, the Duell Tool, which provides build plugins for iOS, Android, Flash and HTML5. They are very confident with the resulting productiv- ity when using Haxe and the Duell Tool for 2D games, yet for the third dimension, they are still in the research phase. With the purpose of contributing to their research, we will use the Haxe multi- platform programming language combined with the Duell Tool and show that the gain in required development time of a graphic engine's fundamental 3D features significantly outweighs the loss of performance resulting from the multi-platform programming.