Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/volumetric_fog/src/lib/graphics/graphics_engine.cc @ 10569

Last change on this file since 10569 was 9935, checked in by hdavid, 18 years ago

branches/volumetric_gof: empty base files

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