You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.5 KiB

5 years ago
  1. #include "skybox.h"
  2. #include "utils.h"
  3. void SkyBox::Init(const char *imageDir) {
  4. mShader = new Shader[6];
  5. mVertexBuffer = new VertexBuffer[6];
  6. InitFront(imageDir);
  7. InitBack(imageDir);
  8. InitLeft(imageDir);
  9. InitRight(imageDir);
  10. InitTop(imageDir);
  11. InitBottom(imageDir);
  12. }
  13. void SkyBox::InitFront(const char *imageDir) {
  14. mShader[0].Init("Res/skybox.vs", "Res/skybox.fs");
  15. char temp[256];
  16. memset(temp, 0, 256);
  17. strcpy(temp, imageDir);
  18. strcat(temp, "front.bmp");
  19. mShader[0].SetTexture("U_Texture", temp);
  20. mVertexBuffer[0].SetSize(4);
  21. mVertexBuffer[0].SetPosition(0, -0.5f, -0.5f, -0.5f);
  22. mVertexBuffer[0].SetTexcoord(0, 0.0f, 0.0f);
  23. mVertexBuffer[0].SetPosition(1, 0.5f, -0.5f, -0.5f);
  24. mVertexBuffer[0].SetTexcoord(1, 1.0f, 0.0f);
  25. mVertexBuffer[0].SetPosition(2, -0.5f, 0.5f, -0.5f);
  26. mVertexBuffer[0].SetTexcoord(2, 0.0f, 1.0f);
  27. mVertexBuffer[0].SetPosition(3, 0.5f, 0.5f, -0.5f);
  28. mVertexBuffer[0].SetTexcoord(3, 1.0f, 1.0f);
  29. }
  30. void SkyBox::InitBack(const char *imageDir) {
  31. mShader[1].Init("Res/skybox.vs", "Res/skybox.fs");
  32. char temp[256];
  33. memset(temp, 0, 256);
  34. strcpy(temp, imageDir);
  35. strcat(temp, "back.bmp");
  36. mShader[1].SetTexture("U_Texture", temp);
  37. mVertexBuffer[1].SetSize(4);
  38. mVertexBuffer[1].SetPosition(0, 0.5f, -0.5f, 0.5f);
  39. mVertexBuffer[1].SetTexcoord(0, 0.0f, 0.0f);
  40. mVertexBuffer[1].SetPosition(1, -0.5f, -0.5f, 0.5f);
  41. mVertexBuffer[1].SetTexcoord(1, 1.0f, 0.0f);
  42. mVertexBuffer[1].SetPosition(2, 0.5f, 0.5f, 0.5f);
  43. mVertexBuffer[1].SetTexcoord(2, 0.0f, 1.0f);
  44. mVertexBuffer[1].SetPosition(3, -0.5f, 0.5f, 0.5f);
  45. mVertexBuffer[1].SetTexcoord(3, 1.0f, 1.0f);
  46. }
  47. void SkyBox::InitLeft(const char *imageDir) {
  48. mShader[2].Init("Res/skybox.vs", "Res/skybox.fs");
  49. char temp[256];
  50. memset(temp, 0, 256);
  51. strcpy(temp, imageDir);
  52. strcat(temp, "left.bmp");
  53. mShader[2].SetTexture("U_Texture", temp);
  54. mVertexBuffer[2].SetSize(4);
  55. mVertexBuffer[2].SetPosition(0, -0.5f, -0.5f, 0.5f);
  56. mVertexBuffer[2].SetTexcoord(0, 0.0f, 0.0f);
  57. mVertexBuffer[2].SetPosition(1, -0.5f, -0.5f, -0.5f);
  58. mVertexBuffer[2].SetTexcoord(1, 1.0f, 0.0f);
  59. mVertexBuffer[2].SetPosition(2, -0.5f, 0.5f, 0.5f);
  60. mVertexBuffer[2].SetTexcoord(2, 0.0f, 1.0f);
  61. mVertexBuffer[2].SetPosition(3, -0.5f, 0.5f, -0.5f);
  62. mVertexBuffer[2].SetTexcoord(3, 1.0f, 1.0f);
  63. }
  64. void SkyBox::InitRight(const char *imageDir) {
  65. mShader[3].Init("Res/skybox.vs", "Res/skybox.fs");
  66. char temp[256];
  67. memset(temp, 0, 256);
  68. strcpy(temp, imageDir);
  69. strcat(temp, "right.bmp");
  70. mShader[3].SetTexture("U_Texture", temp);
  71. mVertexBuffer[3].SetSize(4);
  72. mVertexBuffer[3].SetPosition(0, 0.5f, -0.5f, -0.5f);
  73. mVertexBuffer[3].SetTexcoord(0, 0.0f, 0.0f);
  74. mVertexBuffer[3].SetPosition(1, 0.5f, -0.5f, 0.5f);
  75. mVertexBuffer[3].SetTexcoord(1, 1.0f, 0.0f);
  76. mVertexBuffer[3].SetPosition(2, 0.5f, 0.5f, -0.5f);
  77. mVertexBuffer[3].SetTexcoord(2, 0.0f, 1.0f);
  78. mVertexBuffer[3].SetPosition(3, 0.5f, 0.5f, 0.5f);
  79. mVertexBuffer[3].SetTexcoord(3, 1.0f, 1.0f);
  80. }
  81. void SkyBox::InitTop(const char *imageDir) {
  82. mShader[4].Init("Res/skybox.vs", "Res/skybox.fs");
  83. char temp[256];
  84. memset(temp, 0, 256);
  85. strcpy(temp, imageDir);
  86. strcat(temp, "top.bmp");
  87. mShader[4].SetTexture("U_Texture", temp);
  88. mVertexBuffer[4].SetSize(4);
  89. mVertexBuffer[4].SetPosition(0, -0.5f, 0.5f, -0.5f);
  90. mVertexBuffer[4].SetTexcoord(0, 0.0f, 0.0f);
  91. mVertexBuffer[4].SetPosition(1, 0.5f, 0.5f, -0.5f);
  92. mVertexBuffer[4].SetTexcoord(1, 1.0f, 0.0f);
  93. mVertexBuffer[4].SetPosition(2, -0.5f, 0.5f, 0.5f);
  94. mVertexBuffer[4].SetTexcoord(2, 0.0f, 1.0f);
  95. mVertexBuffer[4].SetPosition(3, 0.5f, 0.5f, 0.5f);
  96. mVertexBuffer[4].SetTexcoord(3, 1.0f, 1.0f);
  97. }
  98. void SkyBox::InitBottom(const char *imageDir) {
  99. mShader[5].Init("Res/skybox.vs", "Res/skybox.fs");
  100. char temp[256];
  101. memset(temp, 0, 256);
  102. strcpy(temp, imageDir);
  103. strcat(temp, "bottom.bmp");
  104. mShader[5].SetTexture("U_Texture", temp);
  105. mVertexBuffer[5].SetSize(4);
  106. mVertexBuffer[5].SetPosition(0, -0.5f, -0.5f, 0.5f);
  107. mVertexBuffer[5].SetTexcoord(0, 0.0f, 0.0f);
  108. mVertexBuffer[5].SetPosition(1, 0.5f, -0.5f, 0.5f);
  109. mVertexBuffer[5].SetTexcoord(1, 1.0f, 0.0f);
  110. mVertexBuffer[5].SetPosition(2, -0.5f, -0.5f, -0.5f);
  111. mVertexBuffer[5].SetTexcoord(2, 0.0f, 1.0f);
  112. mVertexBuffer[5].SetPosition(3, 0.5f, -0.5f, -0.5f);
  113. mVertexBuffer[5].SetTexcoord(3, 1.0f, 1.0f);
  114. }
  115. void SkyBox::Draw(glm::mat4 &V, glm::mat4&P, float x, float y, float z) {
  116. glDisable(GL_DEPTH_TEST);
  117. mModelMatrix = glm::translate(x, y, z);
  118. for (int i = 0; i < 6; ++i) {
  119. mVertexBuffer[i].Bind();
  120. mShader[i].Bind(glm::value_ptr(mModelMatrix), glm::value_ptr(V), glm::value_ptr(P));
  121. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  122. mVertexBuffer[i].Unbind();
  123. }
  124. }