skybox.cpp 3.0 KB

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