//======================================================================== // File: Material.cpp - stores texture and material information for D3D9 and D3D11 // // Part of the GameCode4 Application // // GameCode4 is the sample application that encapsulates much of the source code // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David // "Rez" Graham, published by Charles River Media. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574 // // If this source code has found it's way to you, and you think it has helped you // in any way, do the authors a favor and buy a new copy of the book - there are // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com // by clicking here: // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller= // // There's a companion web site at http://www.mcshaffry.com/GameCode/ // // The source code is managed and maintained through Google Code: // http://code.google.com/p/gamecode4/ // // (c) Copyright 2012 Michael L. McShaffry and David Graham // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser GPL v3 // as published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // http://www.gnu.org/licenses/lgpl-3.0.txt for more details. // // You should have received a copy of the GNU Lesser GPL v3 // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // //======================================================================== #include "Material.h" #include //////////////////////////////////////////////////// // class Material - Chapter 14, page 486 //////////////////////////////////////////////////// Material::Material() { m_Diffuse = g_White; m_Ambient = Color(0.10f, 0.10f, 0.10f, 1.0f); m_Specular = g_White; m_Emissive = g_Black; m_Power = 0; } void Material::SetAmbient(const Color &color) { m_Ambient = color; } void Material::SetDiffuse(const Color &color) { m_Diffuse = color; } void Material::SetSpecular(const Color &color, const float power) { m_Specular = color; m_Power = power; } void Material::SetEmissive(const Color &color) { m_Emissive = color; } void Material::SetAlpha(const float alpha) { m_Diffuse.a = alpha; } void Material::D3Use9() { // [munnellg] noop on linux // DXUTGetD3D9Device()->SetMaterial( &m_D3DMaterial ); } // // class DdsResourceLoader - creates an interface with the Resource cache to load DDS files // class DdsResourceLoader : public TextureResourceLoader { public: virtual std::string VGetPattern() { return "*.dds"; } }; std::shared_ptr CreateDDSResourceLoader() { return std::shared_ptr(GCC_NEW DdsResourceLoader()); } // // class JpgResourceLoader - creates an interface with the Resource cache to load JPG files // class JpgResourceLoader : public TextureResourceLoader { public: virtual std::string VGetPattern() { return "*.jpg"; } }; std::shared_ptr CreateJPGResourceLoader() { return std::shared_ptr(GCC_NEW JpgResourceLoader()); } OpenGLTextureResourceExtraData::OpenGLTextureResourceExtraData() : m_texture_id(0) { } OpenGLTextureResourceExtraData::~OpenGLTextureResourceExtraData() { glDeleteTextures(1, &m_texture_id); } unsigned int TextureResourceLoader::VGetLoadedResourceSize(char *rawBuffer, unsigned int rawSize) { // This will keep the resource cache from allocating memory for the texture, so DirectX / OpenGL can manage it on it's own. return 0; } bool TextureResourceLoader::VLoadResource(char *rawBuffer, unsigned int rawSize, std::shared_ptr handle) { std::shared_ptr extra = std::shared_ptr(GCC_NEW OpenGLTextureResourceExtraData); SDL_IOStream* rw = SDL_IOFromConstMem(rawBuffer, rawSize); if (!rw) { GCC_ERROR(SDL_GetError()); return false; } SDL_Surface *surface = IMG_Load_IO(rw, 0); if (!surface) { GCC_ERROR(SDL_GetError()); return false; } SDL_Surface *formattedSurface = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32); SDL_DestroySurface(surface); if (!formattedSurface) { GCC_ERROR(SDL_GetError()); return false; } glGenTextures(1, &(extra->m_texture_id)); glBindTexture(GL_TEXTURE_2D, extra->m_texture_id); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, formattedSurface->w, formattedSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, formattedSurface->pixels ); SDL_DestroySurface(formattedSurface); // set texture parameters glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); GLfloat maxAnisotropy = 0.0f; glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &maxAnisotropy); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, maxAnisotropy); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, 0); handle->SetExtra(extra); return true; }