Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# KeosShadersLoader.cpp

Go to the documentation of this file.

    00001 /*
    00002  * This source file is part of KEOS (Free 3D Engine)
    00003  * For the latest info, see http://www.keosengine.org/
    00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it under
    00007  * the terms of the GNU Lesser General Public License as published by the Free Software
    00008  * Foundation; either version 2 of the License, or (at your option) any later
    00009  * version.
    00010  *
    00011  * This program is distributed in the hope that it will be useful, but WITHOUT
    00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU Lesser General Public License along with
    00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00018  * http://www.gnu.org/copyleft/lesser.txt.
    00019  *
    00020  */
    00021 #include "KeosShadersLoader.h"
    00022 #include "KeosRenderSystem.h"
    00023 #include "KeosException.h"
    00024
    00025 namespace Keos
    00026 {
    00027         //=======================================================================
    00028     // CShadersLoader implementation
    00029     //=======================================================================
    00030
    00031     //-----------------------------------------------------------------------
    00032         CShadersLoader::CShadersLoader(TShaderType Type) :
    00033         m_Type(Type)
    00034         {
    00035                 // Callback error
    00036                 cgSetErrorCallback(&OnError);
    00037
    00038                 // Context creation
    00039                 m_Context = cgCreateContext();
    00040         }
    00041
    00042         //-----------------------------------------------------------------------
    00043         CShadersLoader::~CShadersLoader()
    00044         {
    00045                 // @todo : cette destruction bug sous OpenGL, impossible de savoir pourquoi
    00046
    00047                 // Context destruction
    00048                 if (m_Context)
    00049                         cgDestroyContext(m_Context);
    00050         }
    00051
    00052         //-----------------------------------------------------------------------
    00053         IShaderBase* CShadersLoader::LoadFromFile(const String& strFilename)
    00054         {
    00055                 if (m_Type == SHADER_VERTEX && !Renderer->GetCapabilities()->HasCapability(CAP_VERTEX_PROGRAM))
    00056                         LOADINGFAILED_EXCEPT(strFilename, "Vertex programs (shaders) not supported !","CShadersLoader::LoadFromFile");
    00057
    00058                 if (m_Type == SHADER_PIXEL && !Renderer->GetCapabilities()->HasCapability(CAP_FRAGMENT_PROGRAM))
    00059                         LOADINGFAILED_EXCEPT(strFilename, "Fragment programs (pixel shaders) not supported !","CShadersLoader::LoadFromFile");
    00060
    00061                 // Load and compilation of the shader
    00062                 CGprogram Program;
    00063                 try
    00064                 {
    00065                         Program = cgCreateProgramFromFile(m_Context,
    00066                                                                                         CG_SOURCE,
    00067                                                                                         strFilename.c_str(),
    00068                                                                                         Renderer->GetShaderProfile(m_Type),
    00069                                                                                         "main",
    00070                                                                                         const_cast<const char**>(Renderer->GetShaderOptions(m_Type)));
    00071                 }
    00072                 catch (CException& E)
    00073                 {
    00074             LOADINGFAILED_EXCEPT(strFilename, E.what() + std::string("\n") + cgGetLastListing(m_Context),"CShadersLoader::LoadFromFile");
    00075                 }
    00076
    00077                 return Renderer->CreateShader(Program, m_Type);
    00078         }
    00079
    00080         //-----------------------------------------------------------------------
    00081         void CShadersLoader::OnError()
    00082         {
    00083                 throw CException(cgGetErrorString(cgGetError()));
    00084         }
    00085
    00086
    00087 } // namespace Keos