zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Light.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <zeni_graphics.h>
19 
20 #ifndef DISABLE_DX9
21 #include <d3dx9.h>
22 #endif
23 
24 #ifndef DISABLE_GL
25 #if defined(REQUIRE_GL_ES)
26 #include <GLES/gl.h>
27 #else
28 #include <GL/glew.h>
29 #endif
30 #endif
31 
32 namespace Zeni {
33 
34  Light::Light(const Color &ambient_, const Color &diffuse_, const Color &specular_, const Point3f &position_, const Vector3f &spot_direction_)
35  : light_type(LIGHT_POINT),
36  diffuse(diffuse_),
37  specular(specular_),
38  ambient(ambient_),
39  position(position_),
40  spot_direction(spot_direction_),
41  range(float(pow(2.0f, 100.0f))),
42  spot_exponent(1.0f),
43  constant_attenuation(1.0f),
44  linear_attenuation(0.0f),
45  quadratic_attenuation(0.0f),
46  spot_theta(2*Global::pi),
47  spot_phi(2*Global::pi)
48  {
49  }
50 
51 #ifndef DISABLE_GL_FIXED
52  void Light::set(const GLenum &number, Video_GL_Fixed &) const {
53  GLfloat pos[] = {position.x, position.y, position.z, 1.0f};
54 
56  pos[0] = -spot_direction.x;
57  pos[1] = -spot_direction.y;
58  pos[2] = -spot_direction.z;
59  pos[3] = 0.0f;
60  }
61 
62  glEnable(number);
63  glLightfv(number, GL_AMBIENT, reinterpret_cast<const GLfloat *>(&ambient));
64  glLightfv(number, GL_DIFFUSE, reinterpret_cast<const GLfloat *>(&diffuse));
65  glLightfv(number, GL_SPECULAR, reinterpret_cast<const GLfloat *>(&specular));
66  glLightfv(number, GL_POSITION, pos);
67  glLightfv(number, GL_SPOT_DIRECTION, reinterpret_cast<const GLfloat*>(&spot_direction));
69  glLightf(number, GL_SPOT_CUTOFF, spot_phi*90.0f/Global::pi); // halve
73  }
74 #endif
75 
76 #ifndef DISABLE_GL_SHADER
77  void Light::set(const GLenum &number, Video_GL_Shader &vgl) const {
79  set(number, reinterpret_cast<Video_GL_Fixed &>(vgl));
80  }
81 #endif
82 
83 #ifndef DISABLE_DX9
84  void Light::set(const DWORD &number, Video_DX9 &screen) const {
85  screen.get_d3d_device()->SetLight(number, reinterpret_cast<const D3DLIGHT9 *>(this));
86  screen.get_d3d_device()->LightEnable(number, TRUE);
87  }
88 #endif
89 
90 }
static const double pi
Definition: e_atan2.c:47
Point3f position
The position of the Light (irrelevant for directional lights or purely ambient lights) ...
Definition: Light.h:86
Color diffuse
The diffuse Color.
Definition: Light.h:83
The Direct3D9 Rendering System.
Definition: Video_DX9.h:62
LPDIRECT3DDEVICE9 & get_d3d_device()
See DirectX Documentation for details.
Definition: Video_DX9.hxx:48
unsigned int GLenum
Definition: gl2.h:23
#define GL_SPOT_CUTOFF
Definition: glew_head.h:544
#define GL_SPOT_EXPONENT
Definition: glew_head.h:543
GLclampf f
Definition: glew.h:3390
#define GL_DIFFUSE
Definition: glew_head.h:539
LIGHT_TYPE light_type
The type of the Light.
Definition: Light.h:82
#define glLightf
Definition: gl_mangle.h:985
Color specular
The specular Color.
Definition: Light.h:84
void set(const GLenum &number, Video_GL_Fixed &screen) const
Definition: Light.cpp:52
float spot_exponent
Similar to Falloff in OpenGL, describes the intensity of the Light from the inner cone to the outer c...
Definition: Light.h:89
A 3D Point represented with floats.
Definition: Coordinate.h:133
float constant_attenuation
Part of the equation describing the weakening of Light over distance. Read online for more informatio...
Definition: Light.h:90
GLenum GLint * range
Definition: glew.h:3391
#define GL_CONSTANT_ATTENUATION
Definition: glew_head.h:545
#define GL_SPOT_DIRECTION
Definition: glew_head.h:542
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
The OpenGL Rendering System.
const float pi
pi == 3.1415926...
Definition: Vector3f.cpp:27
#define glEnable
Definition: gl_mangle.h:441
#define GL_LINEAR_ATTENUATION
Definition: glew_head.h:546
The OpenGL Rendering System.
khronos_float_t GLfloat
Definition: gl2.h:33
#define GL_AMBIENT
Definition: glew_head.h:538
#define glLightfv
Definition: gl_mangle.h:986
#define GL_SPECULAR
Definition: glew_head.h:540
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
float linear_attenuation
Part of the equation describing the weakening of Light over distance. Read online for more informatio...
Definition: Light.h:91
float spot_phi
The angle, in radians, describing the size of the outer cone (automatically decreases theta if necess...
Definition: Light.h:94
Light(const Color &ambient=Color(1.0f, 0.0f, 0.0f, 0.0f), const Color &diffuse=Color(1.0f, 1.0f, 1.0f, 1.0f), const Color &specular=Color(1.0f, 1.0f, 1.0f, 1.0f), const Point3f &position=Point3f(0.0f, 0.0f, 0.0f), const Vector3f &spot_direction=Vector3f(0.0f, 0.0f,-1.0f))
An Alternative to the numerous setter functions.
Definition: Light.cpp:34
float quadratic_attenuation
Part of the equation describing the weakening of Light over distance. Read online for more informatio...
Definition: Light.h:92
Point3f spot_direction
The direction of the Light (relevent only to spotlights)
Definition: Light.h:87
#define GL_QUADRATIC_ATTENUATION
Definition: glew_head.h:547
#define TRUE
Definition: ftobjs.h:53
Color ambient
The ambient Color.
Definition: Light.h:85
#define GL_POSITION
Definition: glew_head.h:541
Color.
Definition: Color.h:41