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.
 
 
 

155 lines
3.2 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. * Mesh class
  17. */
  18. Mesh::Mesh()
  19. {
  20. }
  21. Mesh::~Mesh()
  22. {
  23. }
  24. void Mesh::Render(Scene& scene, mat4 const &matrix)
  25. {
  26. //if (scene.HasPrimitiveRenderer(this) < m_submeshes.Count())
  27. {
  28. for (int i = 0; i < m_submeshes.Count(); ++i)
  29. scene.AddPrimitiveRenderer(this, new PrimitiveMesh(m_submeshes[i], matrix));
  30. }
  31. }
  32. void Mesh::Render()
  33. {
  34. for (int i = 0; i < m_submeshes.Count(); ++i)
  35. m_submeshes[i]->Render();
  36. }
  37. void Mesh::SetMaterial(Shader *shader)
  38. {
  39. for (int i = 0; i < m_submeshes.Count(); ++i)
  40. m_submeshes[i]->SetShader(shader);
  41. }
  42. /*
  43. * SubMesh class
  44. */
  45. SubMesh::SubMesh(Shader *shader, VertexDeclaration *vdecl)
  46. : m_mesh_prim(MeshPrimitive::Triangles),
  47. m_shader(shader),
  48. m_vdecl(vdecl)
  49. {
  50. Ticker::Ref(m_shader);
  51. }
  52. SubMesh::~SubMesh()
  53. {
  54. Ticker::Unref(m_shader);
  55. // TODO: cleanup
  56. }
  57. void SubMesh::SetMeshPrimitive(MeshPrimitive mesh_primitive)
  58. {
  59. m_mesh_prim = mesh_primitive;
  60. }
  61. void SubMesh::SetShader(Shader *shader)
  62. {
  63. Ticker::Unref(m_shader);
  64. m_shader = shader;
  65. Ticker::Ref(m_shader);
  66. }
  67. Shader *SubMesh::GetShader()
  68. {
  69. return m_shader;
  70. }
  71. void SubMesh::SetVertexDeclaration(VertexDeclaration *vdecl)
  72. {
  73. m_vdecl = vdecl;
  74. }
  75. void SubMesh::SetVertexBuffer(int index, VertexBuffer* vbo)
  76. {
  77. while (index >= m_vbos.Count())
  78. m_vbos.Push(nullptr);
  79. m_vbos[index] = vbo;
  80. }
  81. void SubMesh::SetIndexBuffer(IndexBuffer* ibo)
  82. {
  83. m_ibo = ibo;
  84. }
  85. void SubMesh::AddTexture(const char* name, Texture* texture)
  86. {
  87. m_textures.Push(String(name), texture);
  88. }
  89. void SubMesh::Render()
  90. {
  91. int vertex_count = 0;
  92. for (int i = 0; i < m_vbos.Count(); ++i)
  93. {
  94. ShaderAttrib attribs[12];
  95. if (m_vbos[i] == nullptr)
  96. {
  97. Log::Error("trying to render a mesh with a null VBO\n");
  98. continue;
  99. }
  100. int usages[VertexUsage::MAX] = { 0 };
  101. VertexStreamBase stream = m_vdecl->GetStream(i);
  102. for (int j = 0; j < stream.GetStreamCount(); ++j)
  103. {
  104. VertexUsage usage = stream.GetUsage(j);
  105. int usage_index = usage.ToScalar();
  106. attribs[j] = m_shader->GetAttribLocation(usage, usages[usage_index]++);
  107. }
  108. vertex_count = (int)m_vbos[i]->GetSize() / m_vdecl->GetStream(i).GetSize();
  109. m_vdecl->SetStream(m_vbos[i], attribs);
  110. }
  111. for (int i = 0; i < m_textures.Count(); ++i)
  112. {
  113. // TODO: might be good to cache this
  114. ShaderUniform u_tex = m_shader->GetUniformLocation(m_textures[i].m1.C());
  115. m_shader->SetUniform(u_tex, m_textures[i].m2->GetTextureUniform(), i);
  116. }
  117. m_ibo->Bind();
  118. m_vdecl->Bind();
  119. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, vertex_count,
  120. 0, (int)m_ibo->GetSize() / sizeof(uint16_t));
  121. m_vdecl->Unbind();
  122. m_ibo->Unbind();
  123. }
  124. } /* namespace lol */