glc_light.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00026
00027 #include "glc_light.h"
00028 #include "../glc_openglexception.h"
00029
00030 #include <QtDebug>
00031
00033
00035 GLC_Light::GLC_Light()
00036 :GLC_Object("Light")
00037 , m_LightID(GL_LIGHT1)
00038 , m_ListID(0)
00039 , m_ListIsValid(false)
00040 , m_AmbientColor(Qt::black)
00041 , m_DiffuseColor(Qt::white)
00042 , m_SpecularColor(Qt::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)
00052 , m_ListID(0)
00053 , m_ListIsValid(false)
00054 , m_AmbientColor(Qt::black)
00055 , m_DiffuseColor(color)
00056 , m_SpecularColor(Qt::white)
00057 , m_Position()
00058 , m_TwoSided(false)
00059 {
00061 }
00062
00063 GLC_Light::~GLC_Light(void)
00064 {
00065 deleteList();
00066 }
00067
00069
00071
00072
00073 void GLC_Light::setPosition(const GLC_Point3d &pos)
00074 {
00075 m_Position= pos;
00076 m_ListIsValid = false;
00077 }
00078
00079
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
00087 void GLC_Light::setAmbientColor(const QColor& color)
00088 {
00089 m_AmbientColor= color;
00090 m_ListIsValid = false;
00091 }
00092
00093
00094 void GLC_Light::setDiffuseColor(const QColor& color)
00095 {
00096 m_DiffuseColor= color;
00097 m_ListIsValid = false;
00098 }
00099
00100
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
00114
00115
00116 void GLC_Light::creationList(GLenum Mode)
00117 {
00118 if(!m_ListID)
00119 {
00120 m_ListID= glGenLists(1);
00121
00122 if (!m_ListID)
00123 {
00124 glDraw();
00125 qDebug("GLC_Lumiere::CreationListe Display list not create");
00126 }
00127 }
00128
00129 glNewList(m_ListID, Mode);
00130
00131
00132 glDraw();
00133
00134 glEndList();
00135
00136 m_ListIsValid= true;
00137
00138
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
00148 void GLC_Light::glExecute(GLenum Mode)
00149 {
00150
00151 glLoadName(id());
00152
00153
00154 if (!m_ListIsValid)
00155 {
00156
00157
00158 creationList(Mode);
00159 }
00160 else
00161 {
00162 glCallList(m_ListID);
00163 }
00164
00165
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
00176 void GLC_Light::glDraw(void)
00177 {
00178
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
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);
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);
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);
00207
00208
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);
00213
00214
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
00227
00228
00229 void GLC_Light::deleteList(void)
00230 {
00231
00232 if (glIsList(m_ListID))
00233 {
00234 glDeleteLists(m_ListID, 1);
00235 }
00236 }