~matttbe/ubuntu/quantal/rss-glx/lp1017780

« back to all changes in this revision

Viewing changes to src/PixelCity/Light.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2010-07-22 06:50:37 UTC
  • mfrom: (1.1.6 upstream) (2.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100722065037-094eshi3ljwbtfa1
Tags: 0.9.1-3ubuntu1
* Merge from Debian unstable (LP: #604945). Remaining changes:
  - Added an apport package hook
  - Changed libgl1-mesa-swx11-dev to libgl1-mesa-dev in Build-Depends
* Dropped changes:
  - Drop dependency on openal. As the package is in universe now, it's not 
    mandatory anymore
  - src/skyrocket.{cpp,xml}: Disable sound by default. Same reason.
  - Add --disable-sound configure flag to debian/rules. Same reason.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-----------------------------------------------------------------------------
 
2
 
 
3
  Light.cpp
 
4
 
 
5
  2006 Shamus Young
 
6
 
 
7
-------------------------------------------------------------------------------
 
8
 
 
9
  This tracks and renders the light sources. (Note that they do not really 
 
10
  CAST light in the OpenGL sense of the world, these are just simple panels.) 
 
11
  These are NOT subclassed to entities because these are dynamic.  Some lights 
 
12
  blink, and thus they can't go into the fixed render lists managed by 
 
13
  Entity.cpp.  
 
14
 
 
15
-----------------------------------------------------------------------------*/
 
16
 
 
17
#define MAX_SIZE            5
 
18
 
 
19
#include <math.h>
 
20
#include <GL/gl.h>
 
21
#include <GL/glu.h>
 
22
 
 
23
#include "glTypes.h"
 
24
#include "Camera.h"
 
25
#include "Entity.h"
 
26
#include "GetTickCount.h"
 
27
#include "Light.h"
 
28
#include "Macro.h"
 
29
#include "Math.h"
 
30
#include "Random.h"
 
31
#include "PixelCity.h"
 
32
#include "Texture.h"
 
33
#include "Visible.h"
 
34
 
 
35
static GLvector2      angles[5][360];
 
36
static CLight*        head;
 
37
static bool           angles_done;
 
38
static int            count;
 
39
 
 
40
/*-----------------------------------------------------------------------------
 
41
 
 
42
-----------------------------------------------------------------------------*/
 
43
 
 
44
void LightClear ()
 
45
{
 
46
 
 
47
  CLight*   l;
 
48
 
 
49
  while (head) {
 
50
    l = head;
 
51
    head = l->_next;
 
52
    delete l;
 
53
  }
 
54
  count = 0;
 
55
 
 
56
}
 
57
 
 
58
/*-----------------------------------------------------------------------------
 
59
 
 
60
-----------------------------------------------------------------------------*/
 
61
 
 
62
int LightCount ()
 
63
{
 
64
 
 
65
  return count;
 
66
 
 
67
}
 
68
 
 
69
 
 
70
/*-----------------------------------------------------------------------------
 
71
 
 
72
-----------------------------------------------------------------------------*/
 
73
 
 
74
void LightRender ()
 
75
{
 
76
 
 
77
  CLight*     l;
 
78
 
 
79
  if (!EntityReady ())
 
80
    return;
 
81
  if (!angles_done) {
 
82
    for (int size = 0; size < MAX_SIZE; size++) {
 
83
      for (int i = 0 ;i < 360; i++) {
 
84
        angles[size][i].x = cosf ((float)i * DEGREES_TO_RADIANS) * ((float)size + 0.5f);
 
85
        angles[size][i].y = sinf ((float)i * DEGREES_TO_RADIANS) * ((float)size + 0.5f);
 
86
      }
 
87
    }
 
88
  }
 
89
  glDepthMask (false);
 
90
  glEnable (GL_BLEND);
 
91
  glDisable (GL_CULL_FACE);
 
92
  glBlendFunc (GL_ONE, GL_ONE);
 
93
  glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_LIGHT));
 
94
  glDisable (GL_CULL_FACE);
 
95
  glBegin (GL_QUADS);
 
96
  for (l = head; l; l = l->_next) 
 
97
    l->Render ();
 
98
  glEnd ();
 
99
  glDepthMask (true);
 
100
 
 
101
}
 
102
 
 
103
/*-----------------------------------------------------------------------------
 
104
 
 
105
-----------------------------------------------------------------------------*/
 
106
 
 
107
CLight::CLight (GLvector pos, GLrgba color, int size)
 
108
{
 
109
 
 
110
  _position = pos;
 
111
  _color = color;
 
112
  _size = CLAMP (size, 0, (MAX_SIZE - 1));
 
113
  _vert_size = (float)_size + 0.5f;
 
114
  _flat_size = _vert_size + 0.5f;
 
115
  _blink = false;
 
116
  _cell_x = WORLD_TO_GRID(pos.x);
 
117
  _cell_z = WORLD_TO_GRID(pos.z);
 
118
  _next = head;
 
119
  head = this;
 
120
  count++;
 
121
 
 
122
 
 
123
}
 
124
 
 
125
/*-----------------------------------------------------------------------------
 
126
 
 
127
-----------------------------------------------------------------------------*/
 
128
 
 
129
void CLight::Blink ()
 
130
{
 
131
 
 
132
  _blink = true;
 
133
  //we don't want blinkers to be in sync, so have them blink at 
 
134
  //slightly different rates. (Milliseconds)
 
135
  _blink_interval = 1500 + RandomVal (500);
 
136
 
 
137
}
 
138
 
 
139
/*-----------------------------------------------------------------------------
 
140
 
 
141
-----------------------------------------------------------------------------*/
 
142
 
 
143
void CLight::Render ()
 
144
{
 
145
 
 
146
  int       angle;
 
147
  GLvector  pos;
 
148
  GLvector  camera;
 
149
  GLvector  camera_position;
 
150
  GLvector2 offset;
 
151
 
 
152
  if (!Visible (_cell_x, _cell_z))
 
153
    return;
 
154
  camera = CameraAngle ();
 
155
  camera_position = CameraPosition ();
 
156
  if (fabs (camera_position.x - _position.x) > RenderFogDistance ())
 
157
    return;
 
158
  if (fabs (camera_position.z - _position.z) > RenderFogDistance ())
 
159
    return;
 
160
  if (_blink && (GetTickCount () % _blink_interval) > 200)
 
161
    return;
 
162
  angle = (int)MathAngle (camera.y);
 
163
  offset = angles[_size][angle];
 
164
  pos = _position;
 
165
  glColor4fv (&_color.red);
 
166
  glTexCoord2f (0, 0);   
 
167
  glVertex3f (pos.x + offset.x, pos.y - _vert_size, pos.z + offset.y);
 
168
  glTexCoord2f (0, 1);   
 
169
  glVertex3f (pos.x - offset.x, pos.y - _vert_size, pos.z - offset.y);
 
170
  glTexCoord2f (1, 1);   
 
171
  glVertex3f (pos.x - offset.x, pos.y + _vert_size, pos.z - offset.y);
 
172
  glTexCoord2f (1, 0);   
 
173
  glVertex3f (pos.x + offset.x, pos.y + _vert_size, pos.z + offset.y);
 
174
 
 
175
}