117 wiersze
2.3 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. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstring>
  14. #include <cstdlib>
  15. #include "core.h"
  16. namespace lol
  17. {
  18. /*
  19. * Mesh class
  20. */
  21. Mesh::Mesh()
  22. {
  23. }
  24. Mesh::~Mesh()
  25. {
  26. }
  27. void Mesh::Render(mat4 const &model)
  28. {
  29. //for (int i = 0; i < m_submeshes.Count(); ++i)
  30. // m_submeshes[i]->Render(model);
  31. }
  32. /*
  33. * SubMesh class
  34. */
  35. SubMesh::SubMesh(lol::VertexDeclaration* vdecl)
  36. : m_mesh_prim(MeshPrimitive::Triangles)
  37. {
  38. m_vdecl = vdecl;
  39. }
  40. SubMesh::~SubMesh()
  41. {
  42. // TODO: cleanup
  43. }
  44. void SubMesh::SetMeshPrimitive(MeshPrimitive mesh_primitive)
  45. {
  46. m_mesh_prim = mesh_primitive;
  47. }
  48. void SubMesh::SetVertexBuffer(int index, VertexBuffer* vbo)
  49. {
  50. while (index >= m_vbos.Count())
  51. m_vbos.Push(nullptr);
  52. m_vbos[index] = vbo;
  53. }
  54. void SubMesh::AddTexture(const char* name, Texture* texture)
  55. {
  56. m_textures.Push(String(name), texture);
  57. }
  58. void SubMesh::Render(Shader* shader)
  59. {
  60. int vertex_count = 0;
  61. for (int i = 0; i < m_vbos.Count(); ++i)
  62. {
  63. ShaderAttrib attribs[12];
  64. if (m_vbos[i] == nullptr)
  65. {
  66. Log::Error("trying to render a mesh with a null vbo");
  67. return;
  68. }
  69. int indices[VertexUsage::Max];
  70. memset(indices, 0, sizeof(int) * VertexUsage::Max);
  71. VertexStreamBase stream = m_vdecl->GetStream(i);
  72. for (int j = 0; j < stream.GetStreamCount(); ++j)
  73. {
  74. VertexUsage usage = stream.GetUsage(j);
  75. attribs[j] = shader->GetAttribLocation(usage, indices[usage]++);
  76. }
  77. vertex_count = m_vbos[i]->GetSize() / m_vdecl->GetStream(i).GetSize();
  78. m_vdecl->SetStream(m_vbos[i], attribs);
  79. }
  80. for (int i = 0; i < m_textures.Count(); ++i)
  81. {
  82. // TODO: might be good to cache this
  83. ShaderUniform u_tex = shader->GetUniformLocation(m_textures[i].m1.C());
  84. shader->SetUniform(u_tex, m_textures[i].m2->GetTextureUniform(), i);
  85. }
  86. m_vdecl->Bind();
  87. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, vertex_count);
  88. m_vdecl->Unbind();
  89. }
  90. } /* namespace lol */