Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/qt_gui/src/lib/graphics/graphics_engine.cc @ 7640

Last change on this file since 7640 was 7632, checked in by bensch, 19 years ago

Saving and defaultValues introduced

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