Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7165 was 7108, checked in by bensch, 19 years ago

lighting war

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