Material.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //========================================================================
  2. // File: Material.cpp - stores texture and material information for D3D9 and D3D11
  3. //
  4. // Part of the GameCode4 Application
  5. //
  6. // GameCode4 is the sample application that encapsulates much of the source code
  7. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. // "Rez" Graham, published by Charles River Media.
  9. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. //
  11. // If this source code has found it's way to you, and you think it has helped you
  12. // in any way, do the authors a favor and buy a new copy of the book - there are
  13. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. // by clicking here:
  15. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. //
  17. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. //
  19. // The source code is managed and maintained through Google Code:
  20. // http://code.google.com/p/gamecode4/
  21. //
  22. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. //
  24. // This program is free software; you can redistribute it and/or
  25. // modify it under the terms of the GNU Lesser GPL v3
  26. // as published by the Free Software Foundation.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. //
  33. // You should have received a copy of the GNU Lesser GPL v3
  34. // along with this program; if not, write to the Free Software
  35. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. //
  37. //========================================================================
  38. #include "Material.h"
  39. #include <SDL3_image/SDL_image.h>
  40. ////////////////////////////////////////////////////
  41. // class Material - Chapter 14, page 486
  42. ////////////////////////////////////////////////////
  43. Material::Material()
  44. {
  45. m_Diffuse = g_White;
  46. m_Ambient = Color(0.10f, 0.10f, 0.10f, 1.0f);
  47. m_Specular = g_White;
  48. m_Emissive = g_Black;
  49. m_Power = 0;
  50. }
  51. void Material::SetAmbient(const Color &color)
  52. {
  53. m_Ambient = color;
  54. }
  55. void Material::SetDiffuse(const Color &color)
  56. {
  57. m_Diffuse = color;
  58. }
  59. void Material::SetSpecular(const Color &color, const float power)
  60. {
  61. m_Specular = color;
  62. m_Power = power;
  63. }
  64. void Material::SetEmissive(const Color &color)
  65. {
  66. m_Emissive = color;
  67. }
  68. void Material::SetAlpha(const float alpha)
  69. {
  70. m_Diffuse.a = alpha;
  71. }
  72. void Material::D3Use9()
  73. {
  74. // [munnellg] noop on linux
  75. // DXUTGetD3D9Device()->SetMaterial( &m_D3DMaterial );
  76. }
  77. //
  78. // class DdsResourceLoader - creates an interface with the Resource cache to load DDS files
  79. //
  80. class DdsResourceLoader : public TextureResourceLoader
  81. {
  82. public:
  83. virtual std::string VGetPattern() { return "*.dds"; }
  84. };
  85. std::shared_ptr<IResourceLoader> CreateDDSResourceLoader()
  86. {
  87. return std::shared_ptr<IResourceLoader>(GCC_NEW DdsResourceLoader());
  88. }
  89. //
  90. // class JpgResourceLoader - creates an interface with the Resource cache to load JPG files
  91. //
  92. class JpgResourceLoader : public TextureResourceLoader
  93. {
  94. public:
  95. virtual std::string VGetPattern() { return "*.jpg"; }
  96. };
  97. std::shared_ptr<IResourceLoader> CreateJPGResourceLoader()
  98. {
  99. return std::shared_ptr<IResourceLoader>(GCC_NEW JpgResourceLoader());
  100. }
  101. OpenGLTextureResourceExtraData::OpenGLTextureResourceExtraData()
  102. : m_texture_id(0)
  103. {
  104. }
  105. OpenGLTextureResourceExtraData::~OpenGLTextureResourceExtraData()
  106. {
  107. glDeleteTextures(1, &m_texture_id);
  108. }
  109. unsigned int TextureResourceLoader::VGetLoadedResourceSize(char *rawBuffer, unsigned int rawSize)
  110. {
  111. // This will keep the resource cache from allocating memory for the texture, so DirectX / OpenGL can manage it on it's own.
  112. return 0;
  113. }
  114. bool TextureResourceLoader::VLoadResource(char *rawBuffer, unsigned int rawSize, std::shared_ptr<ResHandle> handle)
  115. {
  116. std::shared_ptr<OpenGLTextureResourceExtraData> extra = std::shared_ptr<OpenGLTextureResourceExtraData>(GCC_NEW OpenGLTextureResourceExtraData);
  117. SDL_IOStream* rw = SDL_IOFromConstMem(rawBuffer, rawSize);
  118. if (!rw)
  119. {
  120. GCC_ERROR(SDL_GetError());
  121. return false;
  122. }
  123. SDL_Surface *surface = IMG_Load_IO(rw, 0);
  124. if (!surface) {
  125. GCC_ERROR(SDL_GetError());
  126. return false;
  127. }
  128. SDL_Surface *formattedSurface = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
  129. SDL_DestroySurface(surface);
  130. if (!formattedSurface)
  131. {
  132. GCC_ERROR(SDL_GetError());
  133. return false;
  134. }
  135. glGenTextures(1, &(extra->m_texture_id));
  136. glBindTexture(GL_TEXTURE_2D, extra->m_texture_id);
  137. glTexImage2D(
  138. GL_TEXTURE_2D,
  139. 0,
  140. GL_RGBA,
  141. formattedSurface->w,
  142. formattedSurface->h,
  143. 0,
  144. GL_RGBA,
  145. GL_UNSIGNED_BYTE,
  146. formattedSurface->pixels
  147. );
  148. SDL_DestroySurface(formattedSurface);
  149. // set texture parameters
  150. glGenerateMipmap(GL_TEXTURE_2D);
  151. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  152. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  153. // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  154. // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  155. GLfloat maxAnisotropy = 0.0f;
  156. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &maxAnisotropy);
  157. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, maxAnisotropy);
  158. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  159. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  160. glBindTexture(GL_TEXTURE_2D, 0);
  161. handle->SetExtra(extra);
  162. return true;
  163. }