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.

skybox.cpp 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <glm/gtc/matrix_transform.hpp>
  2. #include <glm/gtc/type_ptr.hpp>
  3. #include <glm/gtx/transform.hpp>
  4. #include "loadShader.h"
  5. #include "graphics/skybox.h"
  6. #include "graphics/glstate.h"
  7. namespace vtk {
  8. Skybox::Skybox() {
  9. //xyzrgb
  10. float mVerts[] = {
  11. // front
  12. -1.0f, 1.0f, -1.0f,
  13. 1.0f, 1.0f, -1.0f,
  14. 1.0f, -1.0f, -1.0f,
  15. -1.0f, 1.0f, -1.0f,
  16. 1.0f, -1.0f, -1.0f,
  17. -1.0f, -1.0f, -1.0f,
  18. // back
  19. -1.0f, 1.0f, 1.0f,
  20. 1.0f, -1.0f, 1.0f,
  21. 1.0f, 1.0f, 1.0f,
  22. -1.0f, 1.0f, 1.0f,
  23. -1.0f, -1.0f, 1.0f,
  24. 1.0f, -1.0f, 1.0f,
  25. // left
  26. -1.0f, 1.0f, 1.0f,
  27. -1.0f, 1.0f, -1.0f,
  28. -1.0f, -1.0f, -1.0f,
  29. -1.0f, 1.0f, 1.0f,
  30. -1.0f, -1.0f, -1.0f,
  31. -1.0f, -1.0f, 1.0f,
  32. // right
  33. 1.0f, 1.0f, 1.0f,
  34. 1.0f, -1.0f, -1.0f,
  35. 1.0f, 1.0f, -1.0f,
  36. 1.0f, 1.0f, 1.0f,
  37. 1.0f, -1.0f, 1.0f,
  38. 1.0f, -1.0f, -1.0f,
  39. // top
  40. -1.0f, 1.0f, 1.0f,
  41. 1.0f, 1.0f, 1.0f,
  42. 1.0f, 1.0f, -1.0f,
  43. -1.0f, 1.0f, 1.0f,
  44. 1.0f, 1.0f, -1.0f,
  45. -1.0f, 1.0f, -1.0f,
  46. // bottom
  47. -1.0f, -1.0f, 1.0f,
  48. 1.0f, -1.0f, -1.0f,
  49. 1.0f, -1.0f, 1.0f,
  50. -1.0f, -1.0f, 1.0f,
  51. -1.0f, -1.0f, -1.0f,
  52. 1.0f, -1.0f, -1.0f
  53. };
  54. mShader = LoadShaders("res/shaders/skybox.vert", "res/shaders/skybox.frag");
  55. mViewMat = glGetUniformLocation(mShader, "view");
  56. mProjMat = glGetUniformLocation(mShader, "proj");
  57. glGenVertexArrays(1, &mVao);
  58. glBindVertexArray(mVao);
  59. glGenBuffers(1, &mVbo);
  60. glBindBuffer(GL_ARRAY_BUFFER, mVbo);
  61. glBufferData(GL_ARRAY_BUFFER, sizeof(mVerts), mVerts, GL_STATIC_DRAW);
  62. GLint posAttrib = glGetAttribLocation(mShader, "position");
  63. glEnableVertexAttribArray(posAttrib);
  64. glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), 0);
  65. }
  66. void Skybox::draw(RenderTask& task) {
  67. auto camera = task.getCamera();
  68. auto viewMat = camera.getAngleMatrix();
  69. auto proj = camera.getProjectionMatrix();
  70. glUseProgram(mShader);
  71. glUniformMatrix4fv(mViewMat, 1, GL_FALSE, glm::value_ptr(viewMat));
  72. glUniformMatrix4fv(mProjMat, 1, GL_FALSE, glm::value_ptr(proj));
  73. glDisable(GL_DEPTH_TEST);
  74. glBindVertexArray(mVao);
  75. glDrawArrays(GL_TRIANGLES, 0, 36);
  76. glEnable(GL_DEPTH_TEST);
  77. }
  78. }