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.

43 lines
1.4 KiB

5 years ago
  1. #include "particleSystem.h"
  2. #include "utils.h"
  3. void ParticleSystem::Init(float x, float y, float z)
  4. {
  5. mModelMatrix = glm::translate(x, y, z);
  6. mVertexBuffer = new VertexBuffer;
  7. int particleCount = 180;
  8. mVertexBuffer->SetSize(particleCount);
  9. for (int i = 0; i < particleCount; ++i) {
  10. mVertexBuffer->SetPosition(i, 2.0f*cosf(float(i) * 8.0f*3.14f / 180.0f), 0.0f, 2.0f*sinf(float(i) * 8.0f*3.14f / 180.0f));
  11. mVertexBuffer->SetColor(i, 0.1f, 0.4f, 0.6f);
  12. }
  13. mShader = new Shader;
  14. mShader->Init("Res/particle.vs", "Res/particle.fs");
  15. mShader->SetTexture("U_Texture", CreateProcedureTexture(128));
  16. }
  17. void ParticleSystem::Draw(glm::mat4 & viewMatrix, glm::mat4 & projectionMatrix) {
  18. glEnable(GL_POINT_SPRITE);
  19. glEnable(GL_PROGRAM_POINT_SIZE);
  20. glDisable(GL_DEPTH_TEST);
  21. glEnable(GL_BLEND);
  22. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  23. mVertexBuffer->Bind();
  24. mShader->Bind(glm::value_ptr(mModelMatrix), glm::value_ptr(viewMatrix), glm::value_ptr(projectionMatrix));
  25. glDrawArrays(GL_POINTS, 0, mVertexBuffer->mVertexCount);
  26. mVertexBuffer->Unbind();
  27. glDisable(GL_BLEND);
  28. glDisable(GL_POINT_SPRITE);
  29. glDisable(GL_PROGRAM_POINT_SIZE);
  30. }
  31. void ParticleSystem::Update(float deltaTime) {
  32. static float angle = 0.0f;
  33. angle += deltaTime*10.0f;
  34. mModelMatrix = glm::rotate(angle, 0.0f, 1.0f, 0.0f);
  35. for (int i = 0; i < mVertexBuffer->mVertexCount; ++i) {
  36. Vertex &vertex = mVertexBuffer->Get(i);
  37. vertex.Normal[1] = 0.1f*i;
  38. if (i > 90) {
  39. break;
  40. }
  41. }
  42. }