Materializer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <kapusha/core/Core.h>
  2. #include <kapusha/io/Stream.h>
  3. #include <kapusha/render/Render.h>
  4. #include "ResRes.h"
  5. #include "VTF.h"
  6. #include "Materializer.h"
  7. static const char* shader_vertex =
  8. #if KAPUSHA_GLES
  9. "precision mediump float;\n"
  10. #endif
  11. "uniform mat4 um4_view, um4_proj;\n"
  12. "uniform vec4 uv4_trans;\n"
  13. "uniform vec2 uv2_texscale;\n"
  14. "attribute vec4 av4_vtx, av4_tex;\n"
  15. "varying vec2 vv2_tex;\n"
  16. "void main(){\n"
  17. "gl_Position = um4_proj * um4_view * (av4_vtx + uv4_trans);\n"
  18. "vv2_tex = av4_tex.xy * uv2_texscale;\n"
  19. "}"
  20. ;
  21. static const char* shader_fragment =
  22. #if KAPUSHA_GLES
  23. "precision mediump float;\n"
  24. #endif
  25. "uniform sampler2D us2_texture;\n"
  26. "varying vec2 vv2_tex;\n"
  27. "void main(){\n"
  28. "gl_FragColor = texture2D(us2_texture, vv2_tex);\n"
  29. "}"
  30. ;
  31. static const char* shader_vertex_lightmap =
  32. #if KAPUSHA_GLES
  33. "precision mediump float;\n"
  34. #endif
  35. "uniform mat4 um4_view, um4_proj;\n"
  36. "uniform vec4 uv4_trans;\n"
  37. "attribute vec4 av4_vertex;\n"
  38. "attribute vec2 av2_lightmap;\n"
  39. "varying vec2 vv2_lightmap;\n"
  40. "void main(){\n"
  41. "gl_Position = um4_proj * um4_view * (av4_vertex + uv4_trans);\n"
  42. "vv2_lightmap = av2_lightmap;\n"
  43. "}"
  44. ;
  45. static const char* shader_fragment_lightmap =
  46. #if KAPUSHA_GLES
  47. "precision mediump float;\n"
  48. #endif
  49. "uniform sampler2D us2_lightmap;\n"
  50. "varying vec2 vv2_lightmap;\n"
  51. "void main(){\n"
  52. "gl_FragColor = texture2D(us2_lightmap, vv2_lightmap) + vec4(.02);\n"
  53. "}"
  54. ;
  55. static const char* shader_vertex_white =
  56. #if KAPUSHA_GLES
  57. "precision mediump float;\n"
  58. #endif
  59. "uniform mat4 um4_view, um4_proj;\n"
  60. "uniform vec4 uv4_trans;\n"
  61. "attribute vec4 av4_vertex;\n"
  62. "void main(){\n"
  63. "gl_Position = um4_proj * um4_view * (av4_vertex + uv4_trans);\n"
  64. "}"
  65. ;
  66. static const char* shader_fragment_white =
  67. #if KAPUSHA_GLES
  68. "precision mediump float;\n"
  69. #endif
  70. "void main(){\n"
  71. "gl_FragColor = vec4(1.);\n"
  72. "}"
  73. ;
  74. Materializer::Materializer(const ResRes& resources)
  75. : resources_(resources)
  76. {
  77. UBER_SHADER1111_ = new kapusha::Program(shader_vertex,
  78. shader_fragment);
  79. }
  80. Materializer::~Materializer(void)
  81. {
  82. }
  83. kapusha::Material* Materializer::loadMaterial(const char *name_raw)
  84. {
  85. std::string name(name_raw);
  86. //! \fixme broken atm
  87. KP_ASSERT(name != "__BSP_edge");
  88. auto fm = cached_materials_.find(name);
  89. if (fm != cached_materials_.end())
  90. return fm->second;
  91. if (name == "__lightmap_only")
  92. {
  93. GL_ASSERT
  94. kapusha::Program *prog = new kapusha::Program(shader_vertex_lightmap,
  95. shader_fragment_lightmap);
  96. GL_ASSERT
  97. kapusha::Material* mat = new kapusha::Material(prog);
  98. GL_ASSERT
  99. cached_materials_[name] = mat;
  100. return mat;
  101. } else if (name == "__white")
  102. {
  103. kapusha::Program *prog = new kapusha::Program(shader_vertex_white,
  104. shader_fragment_white);
  105. kapusha::Material* mat = new kapusha::Material(prog);
  106. cached_materials_[name] = mat;
  107. return mat;
  108. }
  109. #if 0 // texture support is no more
  110. kapusha::StreamSeekable* restream = resources_.open(name.c_str(), ResRes::ResourceTexture);
  111. kapusha::Material* mat = new kapusha::Material(UBER_SHADER1111_);
  112. if (restream)
  113. {
  114. VTF tex;
  115. kapusha::Texture *texture = tex.load(*restream);
  116. if (texture)
  117. {
  118. mat->setUniform("uv2_texscale",
  119. kapusha::vec2f(1.f / tex.size().x, 1.f / tex.size().y));
  120. mat->setTexture("us2_texture", texture);
  121. }
  122. delete restream;
  123. }
  124. cached_materials_[name] = mat;
  125. #endif
  126. KP_ASSERT(!"true materials are not supported");
  127. return 0;
  128. }
  129. void Materializer::print() const
  130. {
  131. L("Cached materials:");
  132. int i = 0;
  133. for (auto it = cached_materials_.begin();
  134. it != cached_materials_.end(); ++it, ++i)
  135. L("\t%d: %s", i, it->first.c_str());
  136. L("TOTAL: %d", cached_materials_.size());
  137. }