| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- //========================================================================
- // 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 <SDL3_image/SDL_image.h>
- ////////////////////////////////////////////////////
- // 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<IResourceLoader> CreateDDSResourceLoader()
- {
- return std::shared_ptr<IResourceLoader>(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<IResourceLoader> CreateJPGResourceLoader()
- {
- return std::shared_ptr<IResourceLoader>(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<ResHandle> handle)
- {
- std::shared_ptr<OpenGLTextureResourceExtraData> extra = std::shared_ptr<OpenGLTextureResourceExtraData>(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;
- }
|