Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/graphics_engine.cc @ 8445

Last change on this file since 8445 was 8267, checked in by amaechler, 18 years ago

raise the number:)

File size: 17.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
17
18#include "graphics_engine.h"
19#include "util/loading/resource_manager.h"
20#include "state.h"
21
22#include "world_entity.h"
23
24#include "render_2d.h"
25#include "text_engine.h"
26#include "light.h"
27#include "shader.h"
28#include "debug.h"
29
30#include "util/preferences.h"
31#include "substring.h"
32#include "text.h"
33
34#include "globals.h"
35#include "texture.h"
36
37#include "effects/graphics_effect.h"
38#include "effects/fog_effect.h"
39#include "effects/lense_flare.h"
40
41#include "shell_command.h"
42
43
44#include "parser/tinyxml/tinyxml.h"
45#include "util/loading/load_param.h"
46#include "util/loading/factory.h"
47#include "class_list.h"
48
49#ifdef __WIN32__
50 #include "static_model.h"
51#endif
52using namespace std;
53
54SHELL_COMMAND(wireframe, GraphicsEngine, wireframe);
55
56
57/**
58 * @brief standard constructor
59 */
60GraphicsEngine::GraphicsEngine ()
61{
62  this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine");
63  this->setName("GraphicsEngine");
64
65  this->isInit = false;
66
67  this->bDisplayFPS = false;
68  this->minFPS = 9999;
69  this->maxFPS = 0;
70
71  this->geTextCFPS = NULL;
72  this->geTextMaxFPS = NULL;
73  this->geTextMinFPS = NULL;
74
75  this->fullscreenFlag = 0;
76  this->videoFlags = 0;
77  this->screen = NULL;
78
79  // initialize the Modules
80  TextEngine::getInstance();
81  this->graphicsEffects = NULL;
82
83}
84
85/**
86 * @brief The Pointer to this GraphicsEngine
87 */
88GraphicsEngine* GraphicsEngine::singletonRef = NULL;
89
90/**
91 * @brief destructs the graphicsEngine.
92*/
93GraphicsEngine::~GraphicsEngine ()
94{
95  // delete what has to be deleted here
96  this->displayFPS( false );
97
98  //TextEngine
99  delete TextEngine::getInstance();
100  // render 2D
101  delete Render2D::getInstance();
102
103  SDL_QuitSubSystem(SDL_INIT_VIDEO);
104  //   if (this->screen != NULL)
105  //     SDL_FreeSurface(this->screen);
106
107  GraphicsEngine::singletonRef = NULL;
108}
109
110
111/**
112 * @brief loads the GraphicsEngine Specific Parameters.
113 * @param root: the XML-Element to load the Data From
114 */
115void GraphicsEngine::loadParams(const TiXmlElement* root)
116{
117  LoadParamXML(root, "GraphicsEffect", this, GraphicsEngine, loadGraphicsEffects)
118  .describe("loads a graphics effect");
119}
120
121
122
123
124/**
125 * @param root The XML-element to load GraphicsEffects from
126 */
127void GraphicsEngine::loadGraphicsEffects(const TiXmlElement* root)
128{
129  LOAD_PARAM_START_CYCLE(root, element);
130  {
131    PRINTF(4)("element is: %s\n", element->Value());
132    Factory::fabricate(element);
133  }
134  LOAD_PARAM_END_CYCLE(element);
135}
136
137
138
139/**
140 * @brief initializes the GraphicsEngine with default settings.
141 */
142int GraphicsEngine::init()
143{
144  if (this->isInit)
145    return -1;
146  this->initVideo(640, 480, 16);
147
148  return 0;
149}
150
151/**
152 * @brief loads the GraphicsEngine's settings from a given ini-file and section
153 * @returns nothing usefull
154 */
155int GraphicsEngine::initFromPreferences()
156{
157  // looking if we are in fullscreen-mode
158  MultiType fullscreen = Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO, CONFIG_NAME_FULLSCREEN, "0");
159
160  if (fullscreen.getBool())
161    this->fullscreenFlag = SDL_FULLSCREEN;
162
163  // looking if we are in fullscreen-mode
164  MultiType textures = Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO_ADVANCED, CONFIG_NAME_TEXTURES, "1");
165    Texture::setTextureEnableState(textures.getBool());
166
167  // searching for a usefull resolution
168  SubString resolution(Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO, CONFIG_NAME_RESOLUTION, "640x480").c_str(), 'x'); ///FIXME
169  //resolution.debug();
170  MultiType x = resolution.getString(0), y = resolution.getString(1);
171  this->initVideo(x.getInt(), y.getInt(), 16);
172
173  //   GraphicsEffect* fe = new FogEffect(NULL);
174  //   this->loadGraphicsEffect(fe);
175  //   fe->activate();
176  //   PRINTF(0)("--------------------------------------------------------------\n");
177
178  //LenseFlare* ge = new LenseFlare();
179  //this->loadGraphicsEffect(ge);
180
181  //ge->addFlare("pictures/lense_flare/sun.png"); //sun
182  //ge->addFlare("pictures/lense_flare/lens2.png"); //first halo
183  //ge->addFlare("pictures/lense_flare/lens1.png"); //small birst
184  //ge->addFlare("pictures/lense_flare/lens3.png"); //second halo
185  //ge->addFlare("pictures/lense_flare/lens4.png");
186  //ge->addFlare("pictures/lense_flare/lens1.png");
187  //ge->addFlare("pictures/lense_flare/lens3.png");
188
189  //ge->activate();
190
191  return 0;
192}
193
194
195
196/**
197 * @brief initializes the Video for openGL.
198 *
199 * This has to be done only once when starting orxonox.
200 */
201int GraphicsEngine::initVideo(unsigned int resX, unsigned int resY, unsigned int bbp)
202{
203  if (this->isInit)
204    return -1;
205  //   initialize SDL_VIDEO
206  if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
207  {
208    PRINTF(1)("could not initialize SDL Video\n");
209    //      return -1;
210  }
211  // initialize SDL_GL-settings
212  this->setGLattribs();
213
214  // setting the Video Flags.
215  this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE ;
216
217  /* query SDL for information about our video hardware */
218  const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo ();
219  if( videoInfo == NULL)
220  {
221    PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError());
222    SDL_Quit ();
223  }
224  if( videoInfo->hw_available)
225    this->videoFlags |= SDL_HWSURFACE;
226  else
227    this->videoFlags |= SDL_SWSURFACE;
228  /*
229  if(VideoInfo -> blit_hw)
230    VideoFlags |= SDL_HWACCEL;
231  */
232  // setting up the Resolution
233  this->setResolution(resX, resY, bbp);
234
235  // GRABBING ALL GL-extensions
236  this->grabHardwareSettings();
237
238  // Enable default GL stuff
239  glEnable(GL_DEPTH_TEST);
240
241  Render2D::getInstance();
242
243  this->isInit = true;
244
245  return 0;
246}
247
248/**
249 * @brief sets the Window Captions and the Name of the icon.
250 * @param windowName The name of the Window
251 * @param icon The name of the Icon on the Disc
252 */
253void GraphicsEngine::setWindowName(const std::string& windowName, const std::string& icon)
254{
255  SDL_Surface* iconSurf = SDL_LoadBMP(icon.c_str());
256  if (iconSurf != NULL)
257  {
258    Uint32 colorkey = SDL_MapRGB(iconSurf->format, 0, 0, 0);
259    SDL_SetColorKey(iconSurf, SDL_SRCCOLORKEY, colorkey);
260    SDL_WM_SetIcon(iconSurf, NULL);
261    SDL_FreeSurface(iconSurf);
262  }
263
264  SDL_WM_SetCaption (windowName.c_str(), icon.c_str());
265}
266
267
268/**
269 * @brief Sets the GL-attributes
270 */
271int GraphicsEngine::setGLattribs()
272{
273  // Set video mode
274  // TO DO: parse arguments for settings
275  //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
276  //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
277  //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
278  //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
279
280
281  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
282  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
283  SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0);
284  SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0);
285  SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0);
286  SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
287  SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);
288
289  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);      //Use at least 5 bits of Red
290  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);    //Use at least 5 bits of Green
291  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);     //Use at least 5 bits of Blue
292  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);   //Use at least 16 bits for the depth buffer
293  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);  //Enable double buffering
294
295  glEnable(GL_CULL_FACE);
296  glCullFace(GL_FRONT);
297
298  return 0;
299}
300
301/**
302 * @brief grabs the Hardware Specifics
303 *
304 * checks for all the different HW-types
305 */
306void GraphicsEngine::grabHardwareSettings()
307{
308  const char* renderer = (const char*) glGetString(GL_RENDERER);
309  const char* vendor   = (const char*) glGetString(GL_VENDOR);
310  const char* version  = (const char*) glGetString(GL_VERSION);
311  const char* extensions = (const char*) glGetString(GL_EXTENSIONS);
312
313  //  printf("%s %s %s\n %s", renderer, vendor, version, extensions);
314
315  if (renderer != NULL)
316  {
317    this->hwRenderer == renderer;
318  }
319  if (vendor != NULL)
320  {
321    this->hwVendor == vendor;
322  }
323  if (version != NULL)
324  {
325    this->hwVersion == version;
326  }
327
328  if (extensions != NULL)
329    this->hwExtensions.split(extensions, " \n\t,");
330
331  PRINT(4)("Running on : vendor: %s,  renderer: %s,  version:%s\n", vendor, renderer, version);
332  PRINT(4)("Extensions:\n");
333  for (unsigned int i = 0; i < this->hwExtensions.size(); i++)
334    PRINT(4)("%d: %s\n", i, this->hwExtensions[i].c_str());
335
336
337  // inizializing GLEW
338  GLenum err = glewInit();
339  if (GLEW_OK != err)
340  {
341    /* Problem: glewInit failed, something is seriously wrong. */
342    PRINTF(1)("%s\n", glewGetErrorString(err));
343  }
344  PRINTF(4)("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
345}
346
347
348/**
349 * @brief sets the Resolution of the Screen to display the Graphics to.
350 * @param width The width of the window
351 * @param height The height of the window
352 * @param bpp bits per pixel
353 */
354int GraphicsEngine::setResolution(int width, int height, int bpp)
355{
356  this->resolutionX = width;
357  this->resolutionY = height;
358  this->bitsPerPixel = bpp;
359  State::setResolution( width, height);
360
361  if (this->screen != NULL)
362    SDL_FreeSurface(screen);
363  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL)
364  {
365    PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
366    //    SDL_Quit();
367    //    return -1;
368  }
369  glViewport(0, 0, width, height);                     // Reset The Current Viewport
370
371#ifdef __WIN32__
372  // REBUILDING TEXTURES (ON WINDOWS CONTEXT SWITCH)
373  const std::list<BaseObject*>* texList = ClassList::getList(CL_TEXTURE);
374  if (texList != NULL)
375  {
376    std::list<BaseObject*>::const_iterator reTex;
377    for (reTex = texList->begin(); reTex != texList->end(); reTex++)
378      dynamic_cast<Texture*>(*reTex)->rebuild();
379  }
380  // REBUILDING MODELS
381  const std::list<BaseObject*>* modelList = ClassList::getList(CL_STATIC_MODEL);
382  if (texList != NULL)
383  {
384    std::list<BaseObject*>::const_iterator reModel;
385    for (reModel = modelList->begin(); reModel != modelList->end(); reModel++)
386      dynamic_cast<StaticModel*>(*reModel)->rebuild();
387  }
388#endif /* __WIN32__ */
389
390  return 0;
391}
392
393/**
394 * @brief sets Fullscreen mode
395 * @param fullscreen true if fullscreen, false if windowed
396*/
397void GraphicsEngine::setFullscreen(bool fullscreen)
398{
399  if (fullscreen)
400    this->fullscreenFlag = SDL_FULLSCREEN;
401  else
402    this->fullscreenFlag = 0;
403  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
404}
405
406/**
407 * @brief sets the background color
408 * @param red the red part of the background
409 * @param blue the blue part of the background
410 * @param green the green part of the background
411 * @param alpha the alpha part of the background
412 */
413void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
414{
415  glClearColor(red, green, blue, alpha);
416}
417
418/**
419 * @brief Signalhandler, for when the resolution has changed
420 * @param resizeInfo SDL information about the size of the new screen size
421 */
422int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
423{
424  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
425
426  return 0;
427}
428
429/**
430 * @brief entering 2D Mode
431 * this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
432 */
433void GraphicsEngine::enter2DMode()
434{
435  //GraphicsEngine::storeMatrices();
436  SDL_Surface *screen = SDL_GetVideoSurface();
437
438  /* Note, there may be other things you need to change,
439     depending on how you have your OpenGL state set up.
440  */
441  glPushAttrib(GL_ENABLE_BIT);
442  glDisable(GL_DEPTH_TEST);
443  glDisable(GL_CULL_FACE);
444  glDisable(GL_LIGHTING);  // will be set back when leaving 2D-mode
445
446  glMatrixMode(GL_PROJECTION);
447  glPushMatrix();
448  glLoadIdentity();
449  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
450
451  glMatrixMode(GL_MODELVIEW);
452  glPushMatrix();
453  glLoadIdentity();
454}
455
456/**
457 * @brief leaves the 2DMode again also @see Font::enter2DMode()
458 */
459void GraphicsEngine::leave2DMode()
460{
461
462  glMatrixMode(GL_MODELVIEW);
463  glPopMatrix();
464
465  glMatrixMode(GL_PROJECTION);
466  glPopMatrix();
467
468  glPopAttrib();
469}
470
471/**
472 * @brief changes to wireframe-mode.
473 */
474void GraphicsEngine::wireframe()
475{
476  glPolygonMode(GL_FRONT, GL_LINE);
477}
478
479/**
480 * @brief stores the GL_matrices
481 */
482void GraphicsEngine::storeMatrices()
483{
484  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
485  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
486  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
487}
488
489//! the stored ModelView Matrix.
490GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
491//! the stored Projection Matrix
492GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
493//! The ViewPort
494GLint GraphicsEngine::viewPort[4] = {0,0,0,0};
495
496
497
498/**
499 * @brief outputs all the Fullscreen modes.
500 */
501void GraphicsEngine::listModes()
502{
503  /* Get available fullscreen/hardware modes */
504  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
505
506  /* Check is there are any modes available */
507  if(this->videoModes == (SDL_Rect **)0)
508  {
509    PRINTF(1)("No modes available!\n");
510    exit(-1);
511  }
512
513  /* Check if our resolution is restricted */
514  if(this->videoModes == (SDL_Rect **)-1)
515  {
516    PRINTF(2)("All resolutions available.\n");
517  }
518  else
519  {
520    /* Print valid modes */
521    PRINT(0)("Available Resoulution Modes are\n");
522    for(int i = 0; this->videoModes[i]; ++i)
523      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
524  }
525}
526
527/**
528 * @brief checks wether a certain extension is availiable
529 * @param extension the Extension to check for (ex. GL_ARB_texture_env_dot3)
530 * @return true if it is, false otherwise
531 */
532bool GraphicsEngine::hwSupportsEXT(const std::string& extension)
533{
534  for (unsigned int i = 0; i < this->hwExtensions.size(); i++)
535    if ( this->hwExtensions.getString(i) == extension)
536      return true;
537  return false;
538}
539
540/**
541 * @brief updates everything that is to be updated in the GraphicsEngine
542 */
543void GraphicsEngine::update(float dt)
544{
545  Render2D::getInstance()->update(dt);
546}
547
548
549/**
550 * @brief ticks the Text
551 * @param dt the time passed
552 */
553void GraphicsEngine::tick(float dt)
554{
555  if( unlikely(this->bDisplayFPS))
556  {
557    this->currentFPS = 1.0/dt;
558    if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
559    if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
560
561#ifndef NO_TEXT
562    char tmpChar1[20];
563    sprintf(tmpChar1, "Current:  %4.0f", this->currentFPS);
564    this->geTextCFPS->setText(tmpChar1);
565    char tmpChar2[20];
566    sprintf(tmpChar2, "Max:    %4.0f", this->maxFPS);
567    this->geTextMaxFPS->setText(tmpChar2);
568    char tmpChar3[20];
569    sprintf(tmpChar3, "Min:    %4.0f", this->minFPS);
570    this->geTextMinFPS->setText(tmpChar3);
571#endif /* NO_TEXT */
572
573  }
574
575  Render2D::getInstance()->tick(dt);
576
577  // tick the graphics effects
578  if (this->graphicsEffects != NULL || (this->graphicsEffects = ClassList::getList(CL_GRAPHICS_EFFECT)) != NULL)
579  {
580    std::list<BaseObject*>::const_iterator it;
581    for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++)
582      dynamic_cast<GraphicsEffect*>(*it)->tick(dt);
583  }
584}
585
586/**
587 * @brief draws all Elements that should be displayed on the Background.
588 */
589void GraphicsEngine::drawBackgroundElements() const
590{
591  GraphicsEngine::storeMatrices();
592
593  Render2D::getInstance()->draw(E2D_LAYER_BELOW_ALL, E2D_LAYER_BELOW_ALL);
594}
595
596void GraphicsEngine::draw() const
597{
598  //  LightManager::getInstance()->draw();
599
600  if (this->graphicsEffects != NULL)
601  {
602    //draw the graphics effects
603    list<BaseObject*>::const_iterator it;
604    for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++)
605      dynamic_cast<GraphicsEffect*>(*it)->draw();
606  }
607  GraphicsEngine::storeMatrices();
608  Shader::suspendShader();
609  Render2D::getInstance()->draw(E2D_LAYER_BOTTOM, E2D_LAYER_ABOVE_ALL);
610  Shader::restoreShader();
611}
612
613
614/**
615 * @brief displays the Frames per second
616 * @param display if the text should be displayed
617*/
618void GraphicsEngine::displayFPS(bool display)
619{
620#ifndef NO_TEXT
621  if( display )
622  {
623    if (this->geTextCFPS == NULL)
624    {
625      this->geTextCFPS = new Text("fonts/arial_black.ttf", 15);
626      this->geTextCFPS->setName("curFPS");
627      this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
628      this->geTextCFPS->setAbsCoor2D(5, 0);
629    }
630    if (this->geTextMaxFPS == NULL)
631    {
632      this->geTextMaxFPS = new Text("fonts/arial_black.ttf", 15);
633      this->geTextMaxFPS->setName("MaxFPS");
634      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
635      this->geTextMaxFPS->setAbsCoor2D(5, 20);
636    }
637    if (this->geTextMinFPS == NULL)
638    {
639      this->geTextMinFPS = new Text("fonts/arial_black.ttf", 15);
640      this->geTextMinFPS->setName("MinFPS");
641      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
642      this->geTextMinFPS->setAbsCoor2D(5, 40);
643    }
644  }
645  else
646  {
647    delete this->geTextCFPS;
648    this->geTextCFPS = NULL;
649    delete this->geTextMaxFPS;
650    this->geTextMaxFPS = NULL;
651    delete this->geTextMinFPS;
652    this->geTextMinFPS = NULL;
653  }
654  this->bDisplayFPS = display;
655#else
656  this->bDisplayFPS = false;
657#endif /* NO_TEXT */
658}
659
660
661/**
662 * @brief processes the events for the GraphicsEngine class
663 * @param the event to handle
664 */
665void GraphicsEngine::process(const Event &event)
666{
667  switch (event.type)
668  {
669  case EV_VIDEO_RESIZE:
670    this->resolutionChanged(event.resize);
671    break;
672  }
673}
Note: See TracBrowser for help on using the repository browser.