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.
 
 
 

93 rivejä
2.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #include <lol/engine-internal.h>
  11. #include <cstring>
  12. #include <cstdlib>
  13. namespace lol
  14. {
  15. /*
  16. * Primitive class
  17. */
  18. PrimitiveMesh::PrimitiveMesh(SubMesh *submesh, mat4 const &matrix)
  19. : m_submesh(submesh),
  20. m_matrix(matrix)
  21. {
  22. }
  23. PrimitiveMesh::~PrimitiveMesh()
  24. {
  25. }
  26. void PrimitiveMesh::Render() const
  27. {
  28. /* TODO: this should be the main entry for rendering of all
  29. * primitives found in the scene graph. When we have one. */
  30. Shader *shader = nullptr;
  31. ShaderUniform u_model, u_modelview, u_normalmat, uni_tex, uni_texsize;
  32. ShaderAttrib a_pos, a_tex;
  33. {
  34. /* If this primitive uses a new shader, update attributes */
  35. if (m_submesh->GetShader() != shader)
  36. {
  37. shader = m_submesh->GetShader();
  38. a_pos = shader->GetAttribLocation(VertexUsage::Position, 0);
  39. a_tex = shader->GetAttribLocation(VertexUsage::TexCoord, 0);
  40. shader->Bind();
  41. /* Per-scene matrices */
  42. ShaderUniform u_mat;
  43. u_mat = shader->GetUniformLocation("u_projection");
  44. shader->SetUniform(u_mat, Scene::GetCamera()->GetProjection());
  45. u_mat = shader->GetUniformLocation("u_view");
  46. shader->SetUniform(u_mat, Scene::GetCamera()->GetView());
  47. u_mat = shader->GetUniformLocation("u_inv_view");
  48. shader->SetUniform(u_mat, inverse(Scene::GetCamera()->GetView()));
  49. /* Per-object matrices, will be set later */
  50. u_model = shader->GetUniformLocation("u_model");
  51. u_modelview = shader->GetUniformLocation("u_modelview");
  52. u_normalmat = shader->GetUniformLocation("u_normalmat");
  53. /* Per-scene environment */
  54. array<Light *> const &lights = Scene::GetLights();
  55. array<vec4> light_data;
  56. /* FIXME: the 4th component of the position can be used for other things */
  57. /* FIXME: GetUniform("blabla") is costly */
  58. for (auto l : lights)
  59. light_data << vec4(l->GetPosition(), (float)l->GetType()) << l->GetColor();
  60. while (light_data.Count() < LOL_MAX_LIGHT_COUNT)
  61. light_data << vec4::zero << vec4::zero;
  62. ShaderUniform u_lights = shader->GetUniformLocation("u_lights");
  63. shader->SetUniform(u_lights, light_data);
  64. }
  65. shader->SetUniform(u_model, m_matrix);
  66. mat4 modelview = Scene::GetCamera()->GetView() * m_matrix;
  67. shader->SetUniform(u_modelview, modelview);
  68. shader->SetUniform(u_normalmat, transpose(inverse(mat3(modelview))));
  69. m_submesh->Render();
  70. }
  71. }
  72. } /* namespace lol */