skybox.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "skybox.h"
  2. #include "Resource.h"
  3. #include "Renderer.h"
  4. #include "Math.h"
  5. #include "Camera.h"
  6. #include "Scene.h"
  7. #include "App.h"
  8. #include "MainRenderer.h"
  9. static float coords [][4][3] =
  10. {
  11. // front
  12. { { 1, 1, -1}, {-1, 1, -1}, {-1, -1, -1}, { 1, -1, -1} },
  13. // back
  14. { {-1, 1, 1}, { 1, 1, 1}, { 1, -1, 1}, {-1, -1, 1} },
  15. // left
  16. { {-1, 1, -1}, {-1, 1, 1}, {-1, -1, 1}, {-1, -1, -1} },
  17. // right
  18. { { 1, 1, 1}, { 1, 1, -1}, { 1, -1, -1}, { 1, -1, 1} },
  19. // up
  20. { { 1, 1, 1}, {-1, 1, 1}, {-1, 1, -1}, { 1, 1, -1} },
  21. //
  22. { { 1, -1, -1}, {-1, -1, -1}, {-1, -1, 1}, { 1, -1, 1} }
  23. };
  24. /*
  25. =======================================================================================================================================
  26. load =
  27. =======================================================================================================================================
  28. */
  29. bool Skybox::load(const char* filenames[6])
  30. {
  31. for(int i=0; i<6; i++)
  32. {
  33. textures[i] = Resource::textures.load(filenames[i]);
  34. }
  35. noise = Resource::textures.load("gfx/noise2.tga");
  36. noise->setTexParameter(GL_TEXTURE_WRAP_S, GL_REPEAT);
  37. noise->setTexParameter(GL_TEXTURE_WRAP_T, GL_REPEAT);
  38. shader = Resource::shaders.load("shaders/ms_mp_skybox.glsl");
  39. return true;
  40. }
  41. /*
  42. =======================================================================================================================================
  43. render =
  44. =======================================================================================================================================
  45. */
  46. void Skybox::Render(const Mat3& rotation)
  47. {
  48. glDisable(GL_DEPTH_TEST);
  49. glDisable(GL_BLEND);
  50. glPushMatrix();
  51. shader->bind();
  52. glUniform1i(shader->findUniVar("colormap")->getLoc(), 0);
  53. shader->findUniVar("noisemap")->setTexture(*noise, 1);
  54. glUniform1f(shader->findUniVar("timer")->getLoc(), (rotation_ang/(2*PI))*100);
  55. glUniform3fv(shader->findUniVar("sceneAmbientCol")->getLoc(), 1, &(Vec3(1.0, 1.0, 1.0) / app->getScene()->getAmbientCol())[0]);
  56. // set the rotation matrix
  57. Mat3 tmp(rotation);
  58. tmp.rotateYAxis(rotation_ang);
  59. app->getMainRenderer()->loadMatrix(Mat4(tmp));
  60. rotation_ang += 0.0001;
  61. if(rotation_ang >= 2*PI) rotation_ang = 0.0;
  62. const float ffac = 0.001; // fault factor. To eliminate the artefacts in the edge of the quads caused by texture filtering
  63. float uvs [][2] = { {1.0-ffac, 1.0-ffac}, {0.0+ffac, 1.0-ffac}, {0.0+ffac, 0.0+ffac}, {1.0-ffac, 0.0+ffac} };
  64. for(int i=0; i<6; i++)
  65. {
  66. textures[i]->bind(0);
  67. glBegin(GL_QUADS);
  68. glTexCoord2fv(uvs[0]);
  69. glVertex3fv(coords[i][0]);
  70. glTexCoord2fv(uvs[1]);
  71. glVertex3fv(coords[i][1]);
  72. glTexCoord2fv(uvs[2]);
  73. glVertex3fv(coords[i][2]);
  74. glTexCoord2fv(uvs[3]);
  75. glVertex3fv(coords[i][3]);
  76. glEnd();
  77. }
  78. glPopMatrix();
  79. }