Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/lib/graphics/graphics_engine.cc @ 7830

Last change on this file since 7830 was 7727, checked in by bensch, 19 years ago

trunk: some GL-properties for the Textures

File size: 16.8 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 ;
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  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
291/**
292 * grabs the Hardware Specifics
293 * checks for all the different HW-types
294 */
295void GraphicsEngine::grabHardwareSettings()
296{
297  const char* renderer = (const char*) glGetString(GL_RENDERER);
298  const char* vendor   = (const char*) glGetString(GL_VENDOR);
299  const char* version  = (const char*) glGetString(GL_VERSION);
300  const char* extensions = (const char*) glGetString(GL_EXTENSIONS);
301
302  //  printf("%s %s %s\n %s", renderer, vendor, version, extensions);
303
304  if (renderer != NULL)
305  {
306    this->hwRenderer == renderer;
307  }
308  if (vendor != NULL)
309  {
310    this->hwVendor == vendor;
311  }
312  if (version != NULL)
313  {
314    this->hwVersion == version;
315  }
316
317  if (extensions != NULL)
318    this->hwExtensions.split(extensions, " \n\t,");
319
320  PRINT(4)("Running on : vendor: %s,  renderer: %s,  version:%s\n", vendor, renderer, version);
321  PRINT(4)("Extensions:\n");
322  for (unsigned int i = 0; i < this->hwExtensions.size(); i++)
323    PRINT(4)("%d: %s\n", i, this->hwExtensions[i].c_str());
324
325
326  // inizializing GLEW
327  GLenum err = glewInit();
328  if (GLEW_OK != err)
329  {
330    /* Problem: glewInit failed, something is seriously wrong. */
331    PRINTF(1)("%s\n", glewGetErrorString(err));
332  }
333  PRINTF(4)("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
334}
335
336
337/**
338 *  sets the Resolution of the Screen to display the Graphics to.
339 * @param width The width of the window
340 * @param height The height of the window
341 * @param bpp bits per pixel
342 */
343int GraphicsEngine::setResolution(int width, int height, int bpp)
344{
345  this->resolutionX = width;
346  this->resolutionY = height;
347  this->bitsPerPixel = bpp;
348  State::setResolution( width, height);
349
350  if (this->screen != NULL)
351    SDL_FreeSurface(screen);
352  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL)
353  {
354    PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
355    //    SDL_Quit();
356    //    return -1;
357  }
358  glViewport(0, 0, width, height);                     // Reset The Current Viewport
359
360#ifdef __WIN32__
361  // REBUILDING TEXTURES (ON WINDOWS CONTEXT SWITCH)
362  const std::list<BaseObject*>* texList = ClassList::getList(CL_TEXTURE);
363  if (texList != NULL)
364  {
365    std::list<BaseObject*>::const_iterator reTex;
366    for (reTex = texList->begin(); reTex != texList->end(); reTex++)
367      dynamic_cast<Texture*>(*reTex)->rebuild();
368  }
369  // REBUILDING MODELS
370  const std::list<BaseObject*>* modelList = ClassList::getList(CL_STATIC_MODEL);
371  if (texList != NULL)
372  {
373    std::list<BaseObject*>::const_iterator reModel;
374    for (reModel = modelList->begin(); reModel != modelList->end(); reModel++)
375      dynamic_cast<StaticModel*>(*reModel)->rebuild();
376  }
377#endif /* __WIN32__ */
378}
379
380/**
381 *  sets Fullscreen mode
382 * @param fullscreen true if fullscreen, false if windowed
383*/
384void GraphicsEngine::setFullscreen(bool fullscreen)
385{
386  if (fullscreen)
387    this->fullscreenFlag = SDL_FULLSCREEN;
388  else
389    this->fullscreenFlag = 0;
390  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
391}
392
393/**
394 *  sets the background color
395 * @param red the red part of the background
396 * @param blue the blue part of the background
397 * @param green the green part of the background
398 * @param alpha the alpha part of the background
399 */
400void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
401{
402  glClearColor(red, green, blue, alpha);
403}
404
405/**
406 *  Signalhandler, for when the resolution has changed
407 * @param resizeInfo SDL information about the size of the new screen size
408 */
409int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
410{
411  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
412}
413
414/**
415
416/**
417 *  entering 2D Mode
418
419   this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
420*/
421void GraphicsEngine::enter2DMode()
422{
423  //GraphicsEngine::storeMatrices();
424  SDL_Surface *screen = SDL_GetVideoSurface();
425
426  /* Note, there may be other things you need to change,
427     depending on how you have your OpenGL state set up.
428  */
429  glPushAttrib(GL_ENABLE_BIT);
430  glDisable(GL_DEPTH_TEST);
431  glDisable(GL_CULL_FACE);
432  glDisable(GL_LIGHTING);  // will be set back when leaving 2D-mode
433
434  glMatrixMode(GL_PROJECTION);
435  glPushMatrix();
436  glLoadIdentity();
437  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
438
439  glMatrixMode(GL_MODELVIEW);
440  glPushMatrix();
441  glLoadIdentity();
442}
443
444/**
445 *  leaves the 2DMode again also @see Font::enter2DMode()
446 */
447void GraphicsEngine::leave2DMode()
448{
449
450  glMatrixMode(GL_MODELVIEW);
451  glPopMatrix();
452
453  glMatrixMode(GL_PROJECTION);
454  glPopMatrix();
455
456  glPopAttrib();
457}
458
459void GraphicsEngine::wireframe()
460{
461  glPolygonMode(GL_FRONT, GL_LINE);
462}
463
464/**
465 *  stores the GL_matrices
466 */
467void GraphicsEngine::storeMatrices()
468{
469  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
470  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
471  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
472}
473
474//! the stored ModelView Matrix.
475GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
476//! the stored Projection Matrix
477GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
478//! The ViewPort
479GLint GraphicsEngine::viewPort[4] = {0,0,0,0};
480
481
482
483/**
484 *  outputs all the Fullscreen modes.
485 */
486void GraphicsEngine::listModes()
487{
488  /* Get available fullscreen/hardware modes */
489  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
490
491  /* Check is there are any modes available */
492  if(this->videoModes == (SDL_Rect **)0)
493  {
494    PRINTF(1)("No modes available!\n");
495    exit(-1);
496  }
497
498  /* Check if our resolution is restricted */
499  if(this->videoModes == (SDL_Rect **)-1)
500  {
501    PRINTF(2)("All resolutions available.\n");
502  }
503  else
504  {
505    /* Print valid modes */
506    PRINT(0)("Available Resoulution Modes are\n");
507    for(int i = 0; this->videoModes[i]; ++i)
508      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
509  }
510}
511
512/**
513 * checks wether a certain extension is availiable
514 * @param extension the Extension to check for (ex. GL_ARB_texture_env_dot3)
515 * @return true if it is, false otherwise
516 */
517bool GraphicsEngine::hwSupportsEXT(const std::string& extension)
518{
519  for (unsigned int i = 0; i < this->hwExtensions.size(); i++)
520    if ( this->hwExtensions.getString(i) == extension)
521      return true;
522  return false;
523}
524
525/**
526 * updates everything that is to be updated in the GraphicsEngine
527 */
528void GraphicsEngine::update(float dt)
529{
530  Render2D::getInstance()->update(dt);
531}
532
533
534/**
535 *  ticks the Text
536 * @param dt the time passed
537 */
538void GraphicsEngine::tick(float dt)
539{
540  if( unlikely(this->bDisplayFPS))
541  {
542    this->currentFPS = 1.0/dt;
543    if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
544    if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
545
546#ifndef NO_TEXT
547    char tmpChar1[20];
548    sprintf(tmpChar1, "Current:  %4.0f", this->currentFPS);
549    this->geTextCFPS->setText(tmpChar1);
550    char tmpChar2[20];
551    sprintf(tmpChar2, "Max:    %4.0f", this->maxFPS);
552    this->geTextMaxFPS->setText(tmpChar2);
553    char tmpChar3[20];
554    sprintf(tmpChar3, "Min:    %4.0f", this->minFPS);
555    this->geTextMinFPS->setText(tmpChar3);
556#endif /* NO_TEXT */
557
558  }
559
560  Render2D::getInstance()->tick(dt);
561
562  // tick the graphics effects
563  if (this->graphicsEffects != NULL || (this->graphicsEffects = ClassList::getList(CL_GRAPHICS_EFFECT)) != NULL)
564  {
565    std::list<BaseObject*>::const_iterator it;
566    for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++)
567      dynamic_cast<GraphicsEffect*>(*it)->tick(dt);
568  }
569}
570
571
572void GraphicsEngine::draw() const
573{
574  //  LightManager::getInstance()->draw();
575
576  if (this->graphicsEffects != NULL)
577  {
578    //draw the graphics effects
579    list<BaseObject*>::const_iterator it;
580    for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++)
581      dynamic_cast<GraphicsEffect*>(*it)->draw();
582  }
583  GraphicsEngine::storeMatrices();
584  Shader::suspendShader();
585
586  Render2D::getInstance()->draw(E2D_LAYER_ALL);
587  Shader::restoreShader();
588}
589
590
591/**
592 * displays the Frames per second
593 * @param display if the text should be displayed
594*/
595void GraphicsEngine::displayFPS(bool display)
596{
597#ifndef NO_TEXT
598  if( display )
599  {
600    if (this->geTextCFPS == NULL)
601    {
602      this->geTextCFPS = new Text("fonts/arial_black.ttf", 15);
603      this->geTextCFPS->setName("curFPS");
604      this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
605      this->geTextCFPS->setAbsCoor2D(5, 0);
606    }
607    if (this->geTextMaxFPS == NULL)
608    {
609      this->geTextMaxFPS = new Text("fonts/arial_black.ttf", 15);
610      this->geTextMaxFPS->setName("MaxFPS");
611      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
612      this->geTextMaxFPS->setAbsCoor2D(5, 20);
613    }
614    if (this->geTextMinFPS == NULL)
615    {
616      this->geTextMinFPS = new Text("fonts/arial_black.ttf", 15);
617      this->geTextMinFPS->setName("MinFPS");
618      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
619      this->geTextMinFPS->setAbsCoor2D(5, 40);
620    }
621  }
622  else
623  {
624    delete this->geTextCFPS;
625    this->geTextCFPS = NULL;
626    delete this->geTextMaxFPS;
627    this->geTextMaxFPS = NULL;
628    delete this->geTextMinFPS;
629    this->geTextMinFPS = NULL;
630  }
631  this->bDisplayFPS = display;
632#else
633  this->bDisplayFPS = false;
634#endif /* NO_TEXT */
635}
636
637
638/**
639  processes the events for the GraphicsEngine class
640* @param the event to handle
641 */
642void GraphicsEngine::process(const Event &event)
643{
644  switch (event.type)
645  {
646  case EV_VIDEO_RESIZE:
647    this->resolutionChanged(event.resize);
648    break;
649  }
650}
Note: See TracBrowser for help on using the repository browser.