glc_light.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Version 2.0.0 Beta 1, packaged on April 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU General Public License as published by
00011  the Free Software Foundation; either version 2 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU General Public License for more details.
00018 
00019  You should have received a copy of the GNU General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 *****************************************************************************/
00024 
00026 
00027 #include "glc_light.h"
00028 #include "../glc_openglexception.h"
00029 
00030 #include <QtDebug>
00031 
00033 // Constructor Destructor
00035 GLC_Light::GLC_Light()
00036 :GLC_Object("Light")
00037 , m_LightID(GL_LIGHT1)  // Default Light ID
00038 , m_ListID(0)                   // By default display list ID= 0
00039 , m_ListIsValid(false)  // By default display list is not valid
00040 , m_AmbientColor(Qt::black)             // By default diffuse color is set to black
00041 , m_DiffuseColor(Qt::white)             // By default diffuse color is set to white
00042 , m_SpecularColor(Qt::white)    // By default specular color is set to white
00043 , m_Position()
00044 , m_TwoSided(false)
00045 {
00047 }
00048 
00049 GLC_Light::GLC_Light(const QColor& color)
00050 :GLC_Object("Light")
00051 , m_LightID(GL_LIGHT1)  // Default Light ID
00052 , m_ListID(0)                   // By default display list ID= 0
00053 , m_ListIsValid(false)  // By default display list is not valid
00054 , m_AmbientColor(Qt::black)             // By default diffuse color is set to black
00055 , m_DiffuseColor(color)                 // Diffuse color is set to color
00056 , m_SpecularColor(Qt::white)    // By default specular color is set to white
00057 , m_Position()
00058 , m_TwoSided(false)
00059 {
00061 }
00062 
00063 GLC_Light::~GLC_Light(void)
00064 {
00065         deleteList();
00066 }
00067 
00069 // Set Functions
00071 
00072 // Set the light position by a 4D point
00073 void GLC_Light::setPosition(const GLC_Point3d &pos)
00074 {
00075         m_Position= pos;
00076         m_ListIsValid = false;
00077 }
00078 
00079 // Set the light position by a 3 GLfloat
00080 void GLC_Light::setPosition(GLfloat x, GLfloat y, GLfloat z)
00081 {
00082         m_Position.setVect(static_cast<double>(x), static_cast<double>(y), static_cast<double>(z));
00083         m_ListIsValid = false;
00084 }
00085 
00086 // Set light's ambient color by a QColor
00087 void GLC_Light::setAmbientColor(const QColor& color)
00088 {
00089         m_AmbientColor= color;
00090         m_ListIsValid = false;
00091 }
00092 
00093 // Set light's diffuse color by a QColor
00094 void GLC_Light::setDiffuseColor(const QColor& color)
00095 {
00096         m_DiffuseColor= color;
00097         m_ListIsValid = false;
00098 }
00099 
00100 // Set light's specular color by a QColor
00101 void GLC_Light::setSpecularColor(const QColor& color)
00102 {
00103         m_SpecularColor= color;
00104         m_ListIsValid = false;
00105 }
00106 
00108 void setMode(const GLenum);
00109 
00110 
00112 // OpenGL Functions
00114 
00115 // Create light's OpenGL list
00116 void GLC_Light::creationList(GLenum Mode)
00117 {
00118         if(!m_ListID)           // OpenGL list not created
00119         {
00120                 m_ListID= glGenLists(1);
00121 
00122                 if (!m_ListID)  // OpenGL List Id not get
00123                 {
00124                         glDraw();
00125                         qDebug("GLC_Lumiere::CreationListe Display list not create");
00126                 }
00127         }
00128         // OpenGL list creation and execution
00129         glNewList(m_ListID, Mode);
00130 
00131         // Light execution
00132         glDraw();
00133 
00134         glEndList();
00135 
00136         m_ListIsValid= true;
00137 
00138         // OpenGL error handler
00139         GLenum error= glGetError();
00140         if (error != GL_NO_ERROR)
00141         {
00142                 GLC_OpenGlException OpenGlException("GLC_Light::CreationList ", error);
00143                 throw(OpenGlException);
00144         }
00145 }
00146 
00147 // Execute OpenGL light
00148 void GLC_Light::glExecute(GLenum Mode)
00149 {
00150         // Object Name
00151         glLoadName(id());
00152 
00153 
00154         if (!m_ListIsValid)
00155         {
00156                 // OpenGL list is not valid
00157 
00158                 creationList(Mode);
00159         }
00160         else
00161         {
00162                 glCallList(m_ListID);
00163         }
00164 
00165         // OpenGL error handler
00166         GLenum error= glGetError();
00167         if (error != GL_NO_ERROR)
00168         {
00169                 GLC_OpenGlException OpenGlException("GLC_Light::GlExecute ", error);
00170                 throw(OpenGlException);
00171         }
00172 
00173 }
00174 
00175 // OpenGL light set up
00176 void GLC_Light::glDraw(void)
00177 {
00178         // Set the lighting model
00179         if (m_TwoSided)
00180         {
00181                 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
00182         }
00183         else
00184         {
00185                 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
00186         }
00187 
00188         // Color
00189         GLfloat setArray[4]= {static_cast<GLfloat>(m_AmbientColor.redF()),
00190                                                                         static_cast<GLfloat>(m_AmbientColor.greenF()),
00191                                                                         static_cast<GLfloat>(m_AmbientColor.blueF()),
00192                                                                         static_cast<GLfloat>(m_AmbientColor.alphaF())};
00193         glLightfv(m_LightID, GL_AMBIENT, setArray);             // Setup The Ambient Light
00194 
00195         setArray[0]= static_cast<GLfloat>(m_DiffuseColor.redF());
00196         setArray[1]= static_cast<GLfloat>(m_DiffuseColor.greenF());
00197         setArray[2]= static_cast<GLfloat>(m_DiffuseColor.blueF());
00198         setArray[3]= static_cast<GLfloat>(m_DiffuseColor.alphaF());
00199         glLightfv(m_LightID, GL_DIFFUSE, setArray);             // Setup The Diffuse Light
00200 
00201 
00202         setArray[0]= static_cast<GLfloat>(m_SpecularColor.redF());
00203         setArray[1]= static_cast<GLfloat>(m_SpecularColor.greenF());
00204         setArray[2]= static_cast<GLfloat>(m_SpecularColor.blueF());
00205         setArray[3]= static_cast<GLfloat>(m_SpecularColor.alphaF());
00206         glLightfv(m_LightID, GL_SPECULAR, setArray);    // Setup The specular Light
00207 
00208         // Position
00209         setArray[0]= static_cast<GLfloat>(m_Position.x());
00210         setArray[1]= static_cast<GLfloat>(m_Position.y());
00211         setArray[2]= static_cast<GLfloat>(m_Position.z());
00212         glLightfv(m_LightID, GL_POSITION, setArray);    // Position The Light
00213 
00214         // OpenGL error handler
00215         GLenum error= glGetError();
00216         if (error != GL_NO_ERROR)
00217         {
00218                 GLC_OpenGlException OpenGlException("GLC_Light::GlDraw ", error);
00219                 throw(OpenGlException);
00220         }
00221 
00222 }
00223 
00225 // Private services Functions
00227 
00228 // Delete OpenGL Display list
00229 void GLC_Light::deleteList(void)
00230 {
00231         // if the list is valid, the list is deleted
00232         if (glIsList(m_ListID))
00233         {
00234                 glDeleteLists(m_ListID, 1);
00235         }
00236 }

SourceForge.net Logo

©2005 Laurent Ribon