Line data Source code
1 0 : /*
2 :
3 : OOBreakPatternEntity.m
4 :
5 : Entity implementing tunnel effect for hyperspace and stations.
6 :
7 :
8 : Oolite
9 : Copyright (C) 2004-2013 Giles C Williams and contributors
10 :
11 : This program is free software; you can redistribute it and/or
12 : modify it under the terms of the GNU General Public License
13 : as published by the Free Software Foundation; either version 2
14 : of the License, or (at your option) any later version.
15 :
16 : This program is distributed in the hope that it will be useful,
17 : but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : GNU General Public License for more details.
20 :
21 : You should have received a copy of the GNU General Public License
22 : along with this program; if not, write to the Free Software
23 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 : MA 02110-1301, USA.
25 :
26 : */
27 :
28 : #import "OOBreakPatternEntity.h"
29 : #import "OOColor.h"
30 : #import "Universe.h"
31 : #import "OOMacroOpenGL.h"
32 :
33 :
34 : @interface OOBreakPatternEntity (Private)
35 :
36 0 : - (void) setInnerColorComponents:(GLfloat[4])color1 outerColorComponents:(GLfloat[4])color2;
37 :
38 : @end
39 :
40 :
41 : @implementation OOBreakPatternEntity
42 :
43 0 : - (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 0 : - (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 0 : - (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 0 : - (void) drawImmediate:(bool)immediate translucent:(bool)translucent
129 : {
130 : // check if has been hidden.
131 : if (!isImmuneToBreakPatternHide) return;
132 :
133 : if (translucent || immediate)
134 : {
135 : OO_ENTER_OPENGL();
136 : OOSetOpenGLState(OPENGL_STATE_OPAQUE);
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 :
157 : OOVerifyOpenGLState();
158 : OOCheckOpenGLErrors(@"OOBreakPatternEntity after drawing %@", self);
159 : }
160 : }
161 :
162 :
163 0 : - (BOOL) canCollide
164 : {
165 : return NO;
166 : }
167 :
168 :
169 0 : - (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
|