[1853] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
[1855] | 12 | |
---|
| 13 | ### File Specific: |
---|
[3436] | 14 | main-programmer: Benjamin Grauer |
---|
[1855] | 15 | co-programmer: ... |
---|
[1853] | 16 | */ |
---|
| 17 | |
---|
| 18 | |
---|
[3436] | 19 | #include "light.h" |
---|
[1853] | 20 | |
---|
[3436] | 21 | #include "glincl.h" |
---|
[1853] | 22 | |
---|
[1856] | 23 | using namespace std; |
---|
[1853] | 24 | |
---|
[3437] | 25 | //! Definition of the Lights |
---|
| 26 | int lightsV[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7}; |
---|
[1856] | 27 | |
---|
[3437] | 28 | |
---|
[3245] | 29 | /** |
---|
[3436] | 30 | \brief standard constructor for a Light |
---|
[3245] | 31 | */ |
---|
[3436] | 32 | Light::Light () |
---|
[3365] | 33 | { |
---|
[3437] | 34 | this->setClassName ("Light"); |
---|
[1853] | 35 | |
---|
[3437] | 36 | glEnable (GL_LIGHTING); |
---|
| 37 | this->lights = new LightValue*[NUMBEROFLIGHTS]; |
---|
| 38 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
| 39 | lights[i] = NULL; |
---|
| 40 | this->currentLight = new LightValue; |
---|
[3436] | 41 | } |
---|
[1853] | 42 | |
---|
[3245] | 43 | /** |
---|
| 44 | \brief standard deconstructor |
---|
[3329] | 45 | \todo this deconstructor is not jet implemented - do it |
---|
[1853] | 46 | |
---|
[3245] | 47 | */ |
---|
[3436] | 48 | Light::~Light () |
---|
| 49 | { |
---|
| 50 | glDisable(GL_LIGHTING); |
---|
[3437] | 51 | |
---|
| 52 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
| 53 | this->deleteLight(i); |
---|
| 54 | delete lights; |
---|
| 55 | Light::singletonRef = NULL; |
---|
[3436] | 56 | } |
---|
[1853] | 57 | |
---|
[3437] | 58 | Light* Light::singletonRef = NULL; |
---|
| 59 | |
---|
[3436] | 60 | /** |
---|
[3437] | 61 | \returns The Instance of the Lights |
---|
| 62 | */ |
---|
| 63 | Light* Light::getInstance(void) |
---|
| 64 | { |
---|
| 65 | if (singletonRef) |
---|
| 66 | return singletonRef; |
---|
| 67 | else |
---|
| 68 | return Light::singletonRef = new Light(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
[3436] | 72 | \brief initializes a new Light with default values, and enables GL_LIGHTING |
---|
| 73 | */ |
---|
[3437] | 74 | void Light::init(int lightNumber) |
---|
[3436] | 75 | { |
---|
[3437] | 76 | PRINTF(3)("initializing Light number %d.\n", lightNumber); |
---|
| 77 | // enable The light |
---|
| 78 | glEnable(lightsV[lightNumber]); |
---|
| 79 | this->currentLight = lights[lightNumber] = new LightValue; |
---|
[3436] | 80 | |
---|
[3437] | 81 | // set default values |
---|
| 82 | this->currentLight->lightNumber = lightNumber; |
---|
[3436] | 83 | this->setPosition(Vector(0.0, 0.0, 0.0)); |
---|
| 84 | this->setDiffuseColor(1.0, 1.0, 1.0); |
---|
| 85 | this->setSpecularColor(1.0, 1.0, 1.0); |
---|
| 86 | // lmodelAmbient[] = {.1, .1, .1, 1.0}; |
---|
| 87 | } |
---|
[1853] | 88 | |
---|
[3437] | 89 | /** |
---|
| 90 | \brief Adds a new Light, by selecting the First free slot. |
---|
| 91 | |
---|
| 92 | if no slot is free error |
---|
| 93 | */ |
---|
| 94 | int Light::addLight(void) |
---|
| 95 | { |
---|
| 96 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
| 97 | if (!this->lights[i]) |
---|
| 98 | return addLight(i); |
---|
| 99 | PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS); |
---|
| 100 | return -1; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | \brief Adds a new Light |
---|
| 105 | \param lightNumber The number of the light to add. |
---|
| 106 | |
---|
| 107 | if the Number is not free: warn, automatically choose another slot. |
---|
| 108 | */ |
---|
| 109 | int Light::addLight(int lightNumber) |
---|
| 110 | { |
---|
| 111 | if (this->lights[lightNumber]) |
---|
| 112 | { |
---|
| 113 | PRINTF(2)("Lightslot %d is allready taken, trying another one\n", lightNumber); |
---|
| 114 | return this->addLight(); |
---|
| 115 | } |
---|
| 116 | this->init(lightNumber); |
---|
| 117 | this->currentLight = this->lights[lightNumber]; |
---|
| 118 | return lightNumber; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | \brief select the light to work with |
---|
| 123 | \param lightNumber the light to work with |
---|
| 124 | */ |
---|
| 125 | void Light::editLightNumber(int lightNumber) |
---|
| 126 | { |
---|
| 127 | this->currentLight = lights[lightNumber]; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | \brief Delete the current Light |
---|
| 132 | */ |
---|
| 133 | void Light::deleteLight(void) |
---|
| 134 | { |
---|
| 135 | this->deleteLight(this->currentLight->lightNumber); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | \brief delete light. |
---|
| 140 | \param lightnumber the number of the light to delete |
---|
| 141 | */ |
---|
| 142 | void Light::deleteLight(int lightNumber) |
---|
| 143 | { |
---|
| 144 | if (this->lights[lightNumber]) |
---|
| 145 | { |
---|
| 146 | PRINTF(3)("deleting Light number %d\n", lightNumber); |
---|
| 147 | delete this->lights[lightNumber]; |
---|
| 148 | glDisable(lightsV[lightNumber]); |
---|
| 149 | this->lights[lightNumber] = NULL; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | |
---|
[3436] | 155 | // set Attributes |
---|
[3245] | 156 | /** |
---|
[3436] | 157 | \brief Sets a Position for the Light. |
---|
| 158 | \param position The new position of the Light. |
---|
| 159 | \todo patrick: is it ok to set a Light Position even if it is derived from p_node?? |
---|
| 160 | */ |
---|
| 161 | void Light::setPosition(Vector position) |
---|
| 162 | { |
---|
[3437] | 163 | this->currentLight->lightPosition[0] = position.x; |
---|
| 164 | this->currentLight->lightPosition[1] = position.y; |
---|
| 165 | this->currentLight->lightPosition[2] = position.z; |
---|
| 166 | this->currentLight->lightPosition[3] = 0.0; |
---|
[3245] | 167 | |
---|
[3437] | 168 | glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition); |
---|
| 169 | } |
---|
[3436] | 170 | |
---|
[3437] | 171 | void Light::setPosition(GLfloat x, GLfloat y, GLfloat z) |
---|
| 172 | { |
---|
| 173 | this->currentLight->lightPosition[0] = x; |
---|
| 174 | this->currentLight->lightPosition[1] = y; |
---|
| 175 | this->currentLight->lightPosition[2] = z; |
---|
| 176 | this->currentLight->lightPosition[3] = 0.0; |
---|
| 177 | |
---|
| 178 | glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition); |
---|
[3436] | 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | \brief sets an emitting Diffuse color for the Light |
---|
| 183 | \param r red |
---|
| 184 | \param g green |
---|
| 185 | \param b blue |
---|
[3245] | 186 | */ |
---|
[3436] | 187 | void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b) |
---|
| 188 | { |
---|
[3437] | 189 | this->currentLight->diffuseColor[0] = r; |
---|
| 190 | this->currentLight->diffuseColor[1] = g; |
---|
| 191 | this->currentLight->diffuseColor[2] = b; |
---|
| 192 | this->currentLight->diffuseColor[3] = 1.0; |
---|
[3436] | 193 | |
---|
[3437] | 194 | glLightfv (GL_LIGHT0, GL_DIFFUSE, this->currentLight->diffuseColor); |
---|
[3436] | 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | \brief sets an emitting Ambient color for the Light |
---|
| 200 | \param r red |
---|
| 201 | \param g green |
---|
| 202 | \param b blue |
---|
| 203 | */ |
---|
| 204 | void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b) |
---|
| 205 | { |
---|
[3437] | 206 | this->currentLight->specularColor[0] = r; |
---|
| 207 | this->currentLight->specularColor[1] = g; |
---|
| 208 | this->currentLight->specularColor[2] = b; |
---|
| 209 | this->currentLight->specularColor[3] = 1.0; |
---|
[3436] | 210 | |
---|
[3437] | 211 | glLightfv (GL_LIGHT0, GL_SPECULAR, this->currentLight->specularColor); |
---|
[3436] | 212 | } |
---|
| 213 | |
---|
| 214 | // get Attributes |
---|
| 215 | |
---|
| 216 | /** |
---|
| 217 | \returns the Position of the Light |
---|
| 218 | */ |
---|
| 219 | Vector Light::getPosition(void) |
---|
| 220 | { |
---|
[3437] | 221 | Vector tmpPosition(this->currentLight->lightPosition[0], this->currentLight->lightPosition[1], this->currentLight->lightPosition[2]); |
---|
[3436] | 222 | return tmpPosition; |
---|
| 223 | } |
---|