Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/graphics_engine.cc @ 8432

Last change on this file since 8432 was 8322, checked in by patrick, 18 years ago

bsp_model: added antialiasing to the orxonox grpahics engine, activated per default

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 "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->bAntialiasing = true;
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 * @brief The Pointer to this GraphicsEngine
88 */
89GraphicsEngine* GraphicsEngine::singletonRef = NULL;
90
91/**
92 * @brief 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 * @brief 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 * @brief 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 * @brief 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 * @brief 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 ;
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 * @brief 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 * @brief 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  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);      //Use at least 5 bits of Red
285  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);    //Use at least 5 bits of Green
286  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);     //Use at least 5 bits of Blue
287  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);   //Use at least 16 bits for the depth buffer
288  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);  //Enable double buffering
289
290  // enable antialiasing?
291  if( this->bAntialiasing)
292  {
293    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,4);
294    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1);
295  }
296
297  glEnable(GL_CULL_FACE);
298  glCullFace(GL_FRONT);
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
391/**
392 * @brief sets Fullscreen mode
393 * @param fullscreen true if fullscreen, false if windowed
394*/
395void GraphicsEngine::setFullscreen(bool fullscreen)
396{
397  if (fullscreen)
398    this->fullscreenFlag = SDL_FULLSCREEN;
399  else
400    this->fullscreenFlag = 0;
401  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
402}
403
404/**
405 * @brief sets the background color
406 * @param red the red part of the background
407 * @param blue the blue part of the background
408 * @param green the green part of the background
409 * @param alpha the alpha part of the background
410 */
411void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
412{
413  glClearColor(red, green, blue, alpha);
414}
415
416/**
417 * @brief Signalhandler, for when the resolution has changed
418 * @param resizeInfo SDL information about the size of the new screen size
419 */
420int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
421{
422  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
423}
424
425/**
426 * @brief entering 2D Mode
427 * this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
428 */
429void GraphicsEngine::enter2DMode()
430{
431  //GraphicsEngine::storeMatrices();
432  SDL_Surface *screen = SDL_GetVideoSurface();
433
434  /* Note, there may be other things you need to change,
435     depending on how you have your OpenGL state set up.
436  */
437  glPushAttrib(GL_ENABLE_BIT);
438  glDisable(GL_DEPTH_TEST);
439  glDisable(GL_CULL_FACE);
440  glDisable(GL_LIGHTING);  // will be set back when leaving 2D-mode
441
442  glMatrixMode(GL_PROJECTION);
443  glPushMatrix();
444  glLoadIdentity();
445  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
446
447  glMatrixMode(GL_MODELVIEW);
448  glPushMatrix();
449  glLoadIdentity();
450}
451
452/**
453 * @brief leaves the 2DMode again also @see Font::enter2DMode()
454 */
455void GraphicsEngine::leave2DMode()
456{
457
458  glMatrixMode(GL_MODELVIEW);
459  glPopMatrix();
460
461  glMatrixMode(GL_PROJECTION);
462  glPopMatrix();
463
464  glPopAttrib();
465}
466
467/**
468 * @brief changes to wireframe-mode.
469 */
470void GraphicsEngine::wireframe()
471{
472  glPolygonMode(GL_FRONT, GL_LINE);
473}
474
475/**
476 * @brief stores the GL_matrices
477 */
478void GraphicsEngine::storeMatrices()
479{
480  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
481  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
482  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
483}
484
485//! the stored ModelView Matrix.
486GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
487//! the stored Projection Matrix
488GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
489//! The ViewPort
490GLint GraphicsEngine::viewPort[4] = {0,0,0,0};
491
492
493
494/**
495 * @brief outputs all the Fullscreen modes.
496 */
497void GraphicsEngine::listModes()
498{
499  /* Get available fullscreen/hardware modes */
500  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
501
502  /* Check is there are any modes available */
503  if(this->videoModes == (SDL_Rect **)0)
504  {
505    PRINTF(1)("No modes available!\n");
506    exit(-1);
507  }
508
509  /* Check if our resolution is restricted */
510  if(this->videoModes == (SDL_Rect **)-1)
511  {
512    PRINTF(2)("All resolutions available.\n");
513  }
514  else
515  {
516    /* Print valid modes */
517    PRINT(0)("Available Resoulution Modes are\n");
518    for(int i = 0; this->videoModes[i]; ++i)
519      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
520  }
521}
522
523/**
524 * @brief checks wether a certain extension is availiable
525 * @param extension the Extension to check for (ex. GL_ARB_texture_env_dot3)
526 * @return true if it is, false otherwise
527 */
528bool GraphicsEngine::hwSupportsEXT(const std::string& extension)
529{
530  for (unsigned int i = 0; i < this->hwExtensions.size(); i++)
531    if ( this->hwExtensions.getString(i) == extension)
532      return true;
533  return false;
534}
535
536/**
537 * @brief updates everything that is to be updated in the GraphicsEngine
538 */
539void GraphicsEngine::update(float dt)
540{
541  Render2D::getInstance()->update(dt);
542}
543
544
545/**
546 * @brief ticks the Text
547 * @param dt the time passed
548 */
549void GraphicsEngine::tick(float dt)
550{
551  if( unlikely(this->bDisplayFPS))
552  {
553    this->currentFPS = 1.0/dt;
554    if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
555    if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
556
557#ifndef NO_TEXT
558    char tmpChar1[20];
559    sprintf(tmpChar1, "Current:  %4.0f", this->currentFPS);
560    this->geTextCFPS->setText(tmpChar1);
561    char tmpChar2[20];
562    sprintf(tmpChar2, "Max:    %4.0f", this->maxFPS);
563    this->geTextMaxFPS->setText(tmpChar2);
564    char tmpChar3[20];
565    sprintf(tmpChar3, "Min:    %4.0f", this->minFPS);
566    this->geTextMinFPS->setText(tmpChar3);
567#endif /* NO_TEXT */
568
569  }
570
571  Render2D::getInstance()->tick(dt);
572
573  // tick the graphics effects
574  if (this->graphicsEffects != NULL || (this->graphicsEffects = ClassList::getList(CL_GRAPHICS_EFFECT)) != NULL)
575  {
576    std::list<BaseObject*>::const_iterator it;
577    for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++)
578      dynamic_cast<GraphicsEffect*>(*it)->tick(dt);
579  }
580}
581
582/**
583 * @brief draws all Elements that should be displayed on the Background.
584 */
585void GraphicsEngine::drawBackgroundElements() const
586{
587  GraphicsEngine::storeMatrices();
588
589  Render2D::getInstance()->draw(E2D_LAYER_BELOW_ALL, E2D_LAYER_BELOW_ALL);
590}
591
592void GraphicsEngine::draw() const
593{
594  //  LightManager::getInstance()->draw();
595
596  if (this->graphicsEffects != NULL)
597  {
598    //draw the graphics effects
599    list<BaseObject*>::const_iterator it;
600    for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++)
601      dynamic_cast<GraphicsEffect*>(*it)->draw();
602  }
603  GraphicsEngine::storeMatrices();
604  Shader::suspendShader();
605  Render2D::getInstance()->draw(E2D_LAYER_BOTTOM, E2D_LAYER_ABOVE_ALL);
606  Shader::restoreShader();
607}
608
609
610/**
611 * @brief displays the Frames per second
612 * @param display if the text should be displayed
613*/
614void GraphicsEngine::displayFPS(bool display)
615{
616#ifndef NO_TEXT
617  if( display )
618  {
619    if (this->geTextCFPS == NULL)
620    {
621      this->geTextCFPS = new Text("fonts/arial_black.ttf", 15);
622      this->geTextCFPS->setName("curFPS");
623      this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
624      this->geTextCFPS->setAbsCoor2D(5, 0);
625    }
626    if (this->geTextMaxFPS == NULL)
627    {
628      this->geTextMaxFPS = new Text("fonts/arial_black.ttf", 15);
629      this->geTextMaxFPS->setName("MaxFPS");
630      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
631      this->geTextMaxFPS->setAbsCoor2D(5, 20);
632    }
633    if (this->geTextMinFPS == NULL)
634    {
635      this->geTextMinFPS = new Text("fonts/arial_black.ttf", 15);
636      this->geTextMinFPS->setName("MinFPS");
637      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
638      this->geTextMinFPS->setAbsCoor2D(5, 40);
639    }
640  }
641  else
642  {
643    delete this->geTextCFPS;
644    this->geTextCFPS = NULL;
645    delete this->geTextMaxFPS;
646    this->geTextMaxFPS = NULL;
647    delete this->geTextMinFPS;
648    this->geTextMinFPS = NULL;
649  }
650  this->bDisplayFPS = display;
651#else
652  this->bDisplayFPS = false;
653#endif /* NO_TEXT */
654}
655
656
657/**
658 * @brief processes the events for the GraphicsEngine class
659 * @param the event to handle
660 */
661void GraphicsEngine::process(const Event &event)
662{
663  switch (event.type)
664  {
665  case EV_VIDEO_RESIZE:
666    this->resolutionChanged(event.resize);
667    break;
668  }
669}
Note: See TracBrowser for help on using the repository browser.