Oolite
Loading...
Searching...
No Matches
OOLightParticleEntity.m
Go to the documentation of this file.
1/*
2
3OOLightParticleEntity.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
27#import "MyOpenGLView.h"
29#import "OOColor.h"
32#import "OOMacroOpenGL.h"
33#import "OOTexture.h"
34#import "PlayerEntity.h"
35#import "Universe.h"
36
37#define PARTICLE_DISTANCE_SCALE_LOW 12.0
38#define PARTICLE_DISTANCE_SCALE_HIGH 36.0
39
41
42@interface OOLightParticleEntity (Private)
43
45
46@end
47
48@implementation OOLightParticleEntity
49
50- (id)initWithDiameter:(float)diameter
51{
52 if ((self = [super init])) {
56
57 _colorComponents[0] = 1.0f;
58 _colorComponents[1] = 1.0f;
59 _colorComponents[2] = 1.0f;
60 _colorComponents[3] = 1.0f;
61
62 [self setScanClass:CLASS_NO_DRAW];
63 [self setStatus:STATUS_EFFECT];
64 }
65
66 return self;
67}
68
69- (float)diameter
70{
71 return _diameter;
72}
73
74- (void)setDiameter:(float)diameter
75{
77}
78
79- (void)setColor:(OOColor*)color
80{
81 [color getRed:&_colorComponents[0] green:&_colorComponents[1] blue:&_colorComponents[2] alpha:&_colorComponents[3]];
82}
83
84- (void)setColor:(OOColor*)color alpha:(GLfloat)alpha
85{
86 [self setColor:color];
87 _colorComponents[3] = alpha;
88}
89
90- (void)drawSubEntityImmediate:(bool)immediate translucent:(bool)translucent
91{
92 if (!translucent)
93 return;
94
95 /* TODO: someone will inevitably build a ship so big that individual
96 zero_distances are necessary for flashers, if they haven't already.
97 -- Ahruman 2009-09-20
98 */
101 return;
102
103 Entity* father = [self owner];
104 Entity* last = nil;
105 HPVector abspos = position;
106
107 while (father != nil && father != last && father != NO_TARGET) {
109 abspos = HPvector_add(OOHPVectorMultiplyMatrix(abspos, rM), [father position]);
110 last = father;
111
112 if (![father isSubEntity])
113 break;
114 father = [father owner];
115 }
116
117 OOMatrix temp_matrix = OOGLPopModelView();
119
120 /* Flashers are drawn using the absolute view matrix */
121 OOGLLoadModelView([UNIVERSE viewMatrix]);
122 /* ...modified by the aggregate translation calculated above */
123 OOGLTranslateModelView(HPVectorToVector(HPvector_subtract(abspos, [PLAYER viewpointPosition]))); // move to camera-relative position
124 [self drawImmediate:immediate translucent:translucent];
125
126 OOGLLoadModelView(temp_matrix);
127}
128
129- (void)drawImmediate:(bool)immediate translucent:(bool)translucent
130{
131 if (!translucent)
132 return;
133 if ([UNIVERSE breakPatternHide] && ![self isImmuneToBreakPatternHide]) {
134 Entity* father = [self owner];
135 while (father != nil && father != NO_TARGET) {
136 if (![father isSubEntity])
137 break;
138 father = [father owner];
139 }
140 if (![father isImmuneToBreakPatternHide]) {
141 return;
142 }
143 }
145 return;
146
149
150 OOGL(glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT));
151 OOGL(glEnable(GL_BLEND));
152 OOGL(glBlendFunc(GL_SRC_ALPHA, GL_ONE));
153
154 OOGL(glEnable(GL_TEXTURE_2D));
155 OOGL(glDepthMask(GL_FALSE));
156
157 GLfloat distanceAttenuation = cam_zero_distance / no_draw_distance;
158 distanceAttenuation = 1.0 - distanceAttenuation;
159 GLfloat components[4] = { _colorComponents[0], _colorComponents[1], _colorComponents[2], _colorComponents[3] * distanceAttenuation };
160 OOGL(glColor4fv(components));
161
162 OOGL(glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, components));
163 OOGL(glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND));
164
165 OOViewID viewDir = [UNIVERSE viewDirection];
166 if (viewDir != VIEW_GUI_DISPLAY)
168 [[self texture] apply];
169
170 /* NOTE: nominal diameter is actual radius, because of the black border
171 in the texture. However, the offset along the view axis is not
172 affected by the border and needs to be scaled.
173 -- Ahruman 2009-12-20
174 */
175 float viewOffset = _diameter * 0.5f;
176
177 OOGLBEGIN(GL_QUADS);
178 switch (viewDir) {
179 case VIEW_FORWARD:
180 case VIEW_GUI_DISPLAY:
181 glTexCoord2f(0.0, 1.0);
182 glVertex3f(-_diameter, -_diameter, -viewOffset);
183
184 glTexCoord2f(1.0, 1.0);
185 glVertex3f(_diameter, -_diameter, -viewOffset);
186
187 glTexCoord2f(1.0, 0.0);
188 glVertex3f(_diameter, _diameter, -viewOffset);
189
190 glTexCoord2f(0.0, 0.0);
191 glVertex3f(-_diameter, _diameter, -viewOffset);
192 break;
193
194 case VIEW_AFT:
195 glTexCoord2f(0.0, 1.0);
196 glVertex3f(_diameter, -_diameter, viewOffset);
197
198 glTexCoord2f(1.0, 1.0);
199 glVertex3f(-_diameter, -_diameter, viewOffset);
200
201 glTexCoord2f(1.0, 0.0);
202 glVertex3f(-_diameter, _diameter, viewOffset);
203
204 glTexCoord2f(0.0, 0.0);
205 glVertex3f(_diameter, _diameter, viewOffset);
206 break;
207
208 case VIEW_STARBOARD:
209 glTexCoord2f(0.0, 1.0);
210 glVertex3f(-viewOffset, -_diameter, _diameter);
211
212 glTexCoord2f(1.0, 1.0);
213 glVertex3f(-viewOffset, -_diameter, -_diameter);
214
215 glTexCoord2f(1.0, 0.0);
216 glVertex3f(-viewOffset, _diameter, -_diameter);
217
218 glTexCoord2f(0.0, 0.0);
219 glVertex3f(-viewOffset, _diameter, _diameter);
220 break;
221
222 case VIEW_PORT:
223 glTexCoord2f(0.0, 1.0);
224 glVertex3f(viewOffset, -_diameter, -_diameter);
225
226 glTexCoord2f(1.0, 1.0);
227 glVertex3f(viewOffset, -_diameter, _diameter);
228
229 glTexCoord2f(1.0, 0.0);
230 glVertex3f(viewOffset, _diameter, _diameter);
231
232 glTexCoord2f(0.0, 0.0);
233 glVertex3f(viewOffset, _diameter, -_diameter);
234 break;
235
236 case VIEW_CUSTOM: {
237 PlayerEntity* player = PLAYER;
239 vi.x *= _diameter;
240 vi.y *= _diameter;
241 vi.z *= _diameter;
243 vj.x *= _diameter;
244 vj.y *= _diameter;
245 vj.z *= _diameter;
247 vk.x *= viewOffset;
248 vk.y *= viewOffset;
249 vk.z *= viewOffset;
250 glTexCoord2f(0.0, 1.0);
251 glVertex3f(-vi.x - vj.x - vk.x, -vi.y - vj.y - vk.y, -vi.z - vj.z - vk.z);
252 glTexCoord2f(1.0, 1.0);
253 glVertex3f(+vi.x - vj.x - vk.x, +vi.y - vj.y - vk.y, +vi.z - vj.z - vk.z);
254 glTexCoord2f(1.0, 0.0);
255 glVertex3f(+vi.x + vj.x - vk.x, +vi.y + vj.y - vk.y, +vi.z + vj.z - vk.z);
256 glTexCoord2f(0.0, 0.0);
257 glVertex3f(-vi.x + vj.x - vk.x, -vi.y + vj.y - vk.y, -vi.z + vj.z - vk.z);
258 } break;
259
260 default:
261 glTexCoord2f(0.0, 1.0);
262 glVertex3f(-_diameter, -_diameter, -_diameter);
263
264 glTexCoord2f(1.0, 1.0);
265 glVertex3f(_diameter, -_diameter, -_diameter);
266
267 glTexCoord2f(1.0, 0.0);
268 glVertex3f(_diameter, _diameter, -_diameter);
269
270 glTexCoord2f(0.0, 0.0);
271 glVertex3f(-_diameter, _diameter, -_diameter);
272 break;
273 }
274 OOGLEND();
275
276 OOGL(glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE));
277 OOGL(glPopAttrib());
278
280}
281
286
288{
289 if (sBlobTexture == nil) {
290 sBlobTexture = [[OOTexture textureWithName:@"oolite-particle-blur.png"
291 inFolder:@"Textures"
292 options:kOOTextureMinFilterMipMap | kOOTextureMagFilterLinear | kOOTextureAlphaMask
293 anisotropy:kOOTextureDefaultAnisotropy / 2.0
294 lodBias:0.0] retain];
295 [[OOGraphicsResetManager sharedManager] registerClient:(id<OOGraphicsResetClient>)[OOLightParticleEntity class]];
296 }
297}
298
300{
301 if (sBlobTexture == nil)
302 [self setUpTexture];
303 return sBlobTexture;
304}
305
307{
308 [sBlobTexture release];
310}
311
312- (BOOL)isEffect
313{
314 return YES;
315}
316
318{
319 return NO;
320}
321
322#ifdef OO_DEBUG_MONITOR_ENABLED
323- (NSSet*)allTextures
324{
325 return [NSSet setWithObject:[self texture]];
326}
327#endif
328
329@end
#define NO_DRAW_DISTANCE_FACTOR
Definition Entity.h:49
OOINLINE HPVector HPvector_subtract(HPVector a, HPVector b) INLINE_CONST_FUNC
Definition OOHPVector.h:236
OOINLINE Vector HPVectorToVector(HPVector v) INLINE_CONST_FUNC
Definition OOHPVector.h:175
OOINLINE HPVector HPvector_add(HPVector a, HPVector b) INLINE_CONST_FUNC
Definition OOHPVector.h:213
static OOTexture * sBlobTexture
#define PARTICLE_DISTANCE_SCALE_HIGH
#define PARTICLE_DISTANCE_SCALE_LOW
#define OO_ENTER_OPENGL()
#define M_SQRT2
Definition OOMaths.h:72
HPVector OOHPVectorMultiplyMatrix(HPVector v, OOMatrix m) CONST_FUNC
Definition OOMatrix.m:137
void OOGLLoadModelView(OOMatrix matrix)
void OOGLPushModelView(void)
void OOGLTranslateModelView(Vector vector)
void OOGLMultModelView(OOMatrix matrix)
OOMatrix OOGLPopModelView(void)
#define OOGLBEGIN
Definition OOOpenGL.h:269
@ OPENGL_STATE_ADDITIVE_BLENDING
Definition OOOpenGL.h:123
#define OOVerifyOpenGLState()
Definition OOOpenGL.h:133
#define OOSetOpenGLState(STATE)
Definition OOOpenGL.h:132
#define OOGL(statement)
Definition OOOpenGL.h:262
#define OOGLEND
Definition OOOpenGL.h:270
return nil
@ NO_TARGET
Definition OOTypes.h:169
OOViewID
Definition OOTypes.h:39
#define PLAYER
#define UNIVERSE
Definition Universe.h:827
unsigned isImmuneToBreakPatternHide
Definition Entity.m:1050
double camZeroDistance()
Definition Entity.m:653
unsigned isSubEntity
Definition Entity.m:140
void setScanClass:(OOScanClass sClass)
Definition Entity.m:738
GLfloat cam_zero_distance
Definition Entity.h:105
HPVector position
Definition Entity.m:576
GLfloat no_draw_distance
Definition Entity.h:106
id init()
Definition Entity.m:64
void setStatus:(OOEntityStatus stat)
Definition Entity.m:728
id owner()
Definition Entity.m:548
OOMatrix drawRotationMatrix()
Definition Entity.m:808
void getRed:green:blue:alpha:(float *red,[green] float *green,[blue] float *blue,[alpha] float *alpha)
Definition OOColor.m:344
void registerClient:(id< OOGraphicsResetClient > client)
OOGraphicsResetManager * sharedManager()
void setColor:(OOColor *color)
void drawImmediate:translucent:(bool immediate, [translucent] bool translucent)
void apply()
Definition OOTexture.m:258
id textureWithName:inFolder:options:anisotropy:lodBias:(NSString *name,[inFolder] NSString *directory,[options] OOTextureFlags options,[anisotropy] GLfloat anisotropy,[lodBias] GLfloat lodBias)
Definition OOTexture.m:132
Vector customViewRightVector
Vector customViewUpVector
Vector customViewForwardVector
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34