Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/graphics_engine.cc @ 7028

Last change on this file since 7028 was 6990, checked in by bensch, 19 years ago

orxonox/trunk: better mouse capture now

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