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.

73 lines
1.8 KiB

  1. #include "frustum.h"
  2. #include "misc.h"
  3. Frustum::Frustum()
  4. {
  5. }
  6. void Frustum::Init()
  7. {
  8. /*����program*/
  9. mProgram = CreateGPUProgram("res/shader/WhiteColor.vs", "res/shader/WhiteColor.fs");
  10. mPosLocation = glGetAttribLocation(mProgram, "pos");
  11. mMLocation = glGetUniformLocation(mProgram, "M");
  12. mVLocation = glGetUniformLocation(mProgram, "V");
  13. mPLocation = glGetUniformLocation(mProgram, "P");
  14. }
  15. void Frustum::InitPerspective(float fov, float aspect, float zNear, float zFar)
  16. {
  17. //������������
  18. float halfFOV = 22.5f;
  19. float randianHalfFOV = 3.14*halfFOV / 180.0f;
  20. float tanHalfFOV = sinf(randianHalfFOV) / cosf(randianHalfFOV);
  21. float yNear = tanHalfFOV * zNear;
  22. float xNear = yNear*aspect;
  23. //����Զ������
  24. float yFar = tanHalfFOV * zFar;
  25. float xFar = yFar*aspect;
  26. float vertexes[24] = {
  27. -xNear,-yNear,-zNear,
  28. xNear,-yNear,-zNear,
  29. xNear,yNear,-zNear,
  30. -xNear,yNear,-zNear,
  31. -xFar,-yFar,-zFar,
  32. xFar,-yFar,-zFar,
  33. xFar,yFar,-zFar,
  34. -xFar,yFar,-zFar,
  35. };
  36. mVBO = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(float)*24, GL_STATIC_DRAW, vertexes);
  37. unsigned int indexes[] = {
  38. 0,1,1,2,2,3,3,0, //��������
  39. 4,5,5,6,6,7,7,4, //Զ������
  40. 0,4,3,7,2,6,1,5
  41. };
  42. mIBO = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*24, GL_STATIC_DRAW, indexes);
  43. }
  44. void Frustum::Draw(float* M, float* V, float* P)
  45. {
  46. glUseProgram(mProgram);
  47. glUniformMatrix4fv(mMLocation, 1, GL_FALSE, M);
  48. glUniformMatrix4fv(mVLocation, 1, GL_FALSE, V);
  49. glUniformMatrix4fv(mPLocation, 1, GL_FALSE, P);
  50. glBindBuffer(GL_ARRAY_BUFFER, mVBO);
  51. glEnableVertexAttribArray(mPosLocation);
  52. glVertexAttribPointer(mPosLocation, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);
  53. glBindBuffer(GL_ARRAY_BUFFER, 0);
  54. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO);
  55. glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);
  56. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  57. glUseProgram(0);
  58. }