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.

45 lines
1.7 KiB

4 years ago
  1. #include "ground.h"
  2. void Ground::Init() {
  3. mVertexBuffer = new VertexBuffer;//�����������ݺ�vbo
  4. mVertexBuffer->SetSize(1600);
  5. for (int z = 0; z < 20; ++z) {
  6. float zStart = 100.0f - z*10.0f;
  7. for (int x = 0; x < 20; ++x) {
  8. int offset = (x + z * 20) * 4;
  9. float xStart = x*10.0f - 100.0f;
  10. mVertexBuffer->SetPosition(offset, xStart, -1.0f, zStart);
  11. mVertexBuffer->SetPosition(offset + 1, xStart + 10.0f, -1.0f, zStart);
  12. mVertexBuffer->SetPosition(offset + 2, xStart, -1.0f, zStart - 10.0f);
  13. mVertexBuffer->SetPosition(offset + 3, xStart + 10.0f, -1.0f, zStart - 10.0f);
  14. mVertexBuffer->SetNormal(offset, 0.0f, 1.0f, 0.0f);
  15. mVertexBuffer->SetNormal(offset + 1, 0.0f, 1.0f, 0.0f);
  16. mVertexBuffer->SetNormal(offset + 2, 0.0f, 1.0f, 0.0f);
  17. mVertexBuffer->SetNormal(offset + 3, 0.0f, 1.0f, 0.0f);
  18. if ((z % 2) ^ (x % 2)) {
  19. mVertexBuffer->SetColor(offset, 0.1f, 0.1f, 0.1f);
  20. mVertexBuffer->SetColor(offset + 1, 0.1f, 0.1f, 0.1f);
  21. mVertexBuffer->SetColor(offset + 2, 0.1f, 0.1f, 0.1f);
  22. mVertexBuffer->SetColor(offset + 3, 0.1f, 0.1f, 0.1f);
  23. }
  24. else {
  25. mVertexBuffer->SetColor(offset, 0.8f, 0.8f, 0.8f);
  26. mVertexBuffer->SetColor(offset + 1, 0.8f, 0.8f, 0.8f);
  27. mVertexBuffer->SetColor(offset + 2, 0.8f, 0.8f, 0.8f);
  28. mVertexBuffer->SetColor(offset + 3, 0.8f, 0.8f, 0.8f);
  29. }
  30. }
  31. }
  32. /*����������shader*/
  33. mShader = new Shader;
  34. mShader->Init("Res/ground.vs", "Res/ground.fs");
  35. }
  36. void Ground::Draw(glm::mat4 & viewMatrix, glm::mat4 & projectionMatrix) {
  37. glEnable(GL_DEPTH_TEST);
  38. mVertexBuffer->Bind();
  39. mShader->Bind(glm::value_ptr(mModelMatrix), glm::value_ptr(viewMatrix), glm::value_ptr(projectionMatrix));
  40. for (int i = 0; i < 400; i++) {
  41. glDrawArrays(GL_TRIANGLE_STRIP, i * 4, 4);
  42. }
  43. mVertexBuffer->Unbind();
  44. }