Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOBreakPatternEntity.m
Go to the documentation of this file.
1/*
2
3OOBreakPatternEntity.m
4
5Entity implementing tunnel effect for hyperspace and stations.
6
7
8Oolite
9Copyright (C) 2004-2013 Giles C Williams and contributors
10
11This program is free software; you can redistribute it and/or
12modify it under the terms of the GNU General Public License
13as published by the Free Software Foundation; either version 2
14of the License, or (at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24MA 02110-1301, USA.
25
26*/
27
29#import "OOColor.h"
30#import "Universe.h"
31#import "OOMacroOpenGL.h"
32
33
34@interface OOBreakPatternEntity (Private)
35
36- (void) setInnerColorComponents:(GLfloat[4])color1 outerColorComponents:(GLfloat[4])color2;
37
38@end
39
40
41@implementation OOBreakPatternEntity
42
43- (id) initWithPolygonSides:(NSUInteger)sides startAngle:(float)startAngleDegrees aspectRatio:(float)aspectRatio
44{
45 sides = MIN(MAX((NSUInteger)3, sides), (NSUInteger)kOOBreakPatternMaxSides);
46
47 if ((self = [super init]))
48 {
49 _vertexCount = (sides + 1) * 2;
50 float angle = startAngleDegrees * M_PI / 180.0f;
51 float deltaAngle = M_PI * 2.0f / sides;
52 float xAspect = fmin(1.0f, aspectRatio);
53 float yAspect = fmin(1.0f, 1.0f / aspectRatio);
54
55 NSUInteger vi = 0;
56 for (NSUInteger i = 0; i < sides; i++)
57 {
58 float s = sin(angle) * xAspect;
59 float c = cos(angle) * yAspect;
60
61 _vertexPosition[vi++] = (Vector) { s * 50, c * 50, -40 };
62 _vertexPosition[vi++] = (Vector) { s * 40, c * 40, 0 };
63
64 angle += deltaAngle;
65 }
66
67 _vertexPosition[vi++] = _vertexPosition[0];
68 _vertexPosition[vi++] = _vertexPosition[1];
69
70 [self setInnerColorComponents:(GLfloat[]){ 1.0f, 0.0f, 0.0f, 0.5f }
71 outerColorComponents:(GLfloat[]){ 0.0f, 0.0f, 1.0f, 0.25f }];
72
73 [self setStatus:STATUS_EFFECT];
74 [self setScanClass:CLASS_NO_DRAW];
75
76 isImmuneToBreakPatternHide = YES;
77 }
78
79 return self;
80}
81
82
83+ (instancetype) breakPatternWithPolygonSides:(NSUInteger)sides startAngle:(float)startAngleDegrees aspectRatio:(float)aspectRatio
84{
85 return [[[self alloc] initWithPolygonSides:sides startAngle:startAngleDegrees aspectRatio:aspectRatio] autorelease];
86}
87
88
89- (void) setInnerColor:(OOColor *)color1 outerColor:(OOColor *)color2
90{
91 GLfloat inner[4], outer[4];
92 [color1 getRed:&inner[0] green:&inner[1] blue:&inner[2] alpha:&inner[3]];
93 [color2 getRed:&outer[0] green:&outer[1] blue:&outer[2] alpha:&outer[3]];
94 [self setInnerColorComponents:inner outerColorComponents:outer];
95}
96
97
98- (void) setInnerColorComponents:(GLfloat[4])color1 outerColorComponents:(GLfloat[4])color2
99{
100 GLfloat *colors[2] = { color1, color2 };
101
102 for (NSUInteger i = 0; i < _vertexCount; i++)
103 {
104 GLfloat *color = colors[i & 1];
105 memcpy(&_vertexColor[i], color, sizeof (GLfloat) * 4);
106 }
107}
108
109
110- (void) setLifetime:(double)lifetime
111{
112 _lifetime = lifetime;
113}
114
115
116- (void) update:(OOTimeDelta) delta_t
117{
118 [super update:delta_t];
119
120 _lifetime -= BREAK_PATTERN_RING_SPEED * delta_t;
121 if (_lifetime < 0.0)
122 {
123 [UNIVERSE removeEntity:self];
124 }
125}
126
127
128- (void) drawImmediate:(bool)immediate translucent:(bool)translucent
129{
130 // check if has been hidden.
131 if (!isImmuneToBreakPatternHide) return;
132
133 if (translucent || immediate)
134 {
137
138 OOGL(glDisable(GL_LIGHTING));
139 OOGL(glDisable(GL_TEXTURE_2D));
140 OOGL(glEnable(GL_BLEND));
141 OOGL(glDepthMask(GL_FALSE));
142 OOGL(glDisableClientState(GL_NORMAL_ARRAY));
143
144 OOGL(glVertexPointer(3, GL_FLOAT, 0, _vertexPosition));
145 OOGL(glEnableClientState(GL_COLOR_ARRAY));
146 OOGL(glColorPointer(4, GL_FLOAT, 0, _vertexColor));
147
148 OOGL(glDrawArrays(GL_TRIANGLE_STRIP, 0, _vertexCount));
149
150 OOGL(glEnable(GL_LIGHTING));
151 OOGL(glEnable(GL_TEXTURE_2D));
152 OOGL(glDisable(GL_BLEND));
153 OOGL(glDepthMask(GL_TRUE));
154 OOGL(glEnableClientState(GL_NORMAL_ARRAY));
155 OOGL(glDisableClientState(GL_COLOR_ARRAY));
156
158 OOCheckOpenGLErrors(@"OOBreakPatternEntity after drawing %@", self);
159 }
160}
161
162
163- (BOOL) canCollide
164{
165 return NO;
166}
167
168
169- (BOOL) isBreakPattern
170{
171 return YES;
172}
173
174@end
175
176
177@implementation Entity (OOBreakPatternEntity)
178
179- (BOOL) isBreakPattern
180{
181 return NO;
182}
183
184@end
@ kOOBreakPatternMaxSides
#define BREAK_PATTERN_RING_SPEED
#define OO_ENTER_OPENGL()
#define MAX(A, B)
Definition OOMaths.h:114
#define MIN(A, B)
Definition OOMaths.h:111
#define M_PI
Definition OOMaths.h:73
@ OPENGL_STATE_OPAQUE
Definition OOOpenGL.h:123
#define OOVerifyOpenGLState()
Definition OOOpenGL.h:136
BOOL OOCheckOpenGLErrors(NSString *format,...)
Definition OOOpenGL.m:39
#define OOSetOpenGLState(STATE)
Definition OOOpenGL.h:135
#define OOGL(statement)
Definition OOOpenGL.h:251
double OOTimeDelta
Definition OOTypes.h:224
void setScanClass:(OOScanClass sClass)
Definition Entity.m:799
void setStatus:(OOEntityStatus stat)
Definition Entity.m:787
void setInnerColorComponents:outerColorComponents:(GLfloat[4] color1, [outerColorComponents] GLfloat[4] color2)
void getRed:green:blue:alpha:(float *red,[green] float *green,[blue] float *blue,[alpha] float *alpha)
Definition OOColor.m:368