Oolite
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 "OOMacroOpenGL.h"
31#import "Universe.h"
32
33@interface OOBreakPatternEntity (Private)
34
35- (void)setInnerColorComponents:(GLfloat[4])color1 outerColorComponents:(GLfloat[4])color2;
36
37@end
38
39@implementation OOBreakPatternEntity
40
41- (id)initWithPolygonSides:(NSUInteger)sides startAngle:(float)startAngleDegrees aspectRatio:(float)aspectRatio
42{
43 sides = MIN(MAX((NSUInteger)3, sides), (NSUInteger)kOOBreakPatternMaxSides);
44
45 if ((self = [super init])) {
46 _vertexCount = (sides + 1) * 2;
47 float angle = startAngleDegrees * M_PI / 180.0f;
48 float deltaAngle = M_PI * 2.0f / sides;
49 float xAspect = fmin(1.0f, aspectRatio);
50 float yAspect = fmin(1.0f, 1.0f / aspectRatio);
51
52 NSUInteger vi = 0;
53 for (NSUInteger i = 0; i < sides; i++) {
54 float s = sin(angle) * xAspect;
55 float c = cos(angle) * yAspect;
56
57 _vertexPosition[vi++] = (Vector) { s * 50, c * 50, -40 };
58 _vertexPosition[vi++] = (Vector) { s * 40, c * 40, 0 };
59
60 angle += deltaAngle;
61 }
62
65
66 [self setInnerColorComponents:(GLfloat[]) { 1.0f, 0.0f, 0.0f, 0.5f }
67 outerColorComponents:(GLfloat[]) { 0.0f, 0.0f, 1.0f, 0.25f }];
68
69 [self setStatus:STATUS_EFFECT];
70 [self setScanClass:CLASS_NO_DRAW];
71
73 }
74
75 return self;
76}
77
78+ (instancetype)breakPatternWithPolygonSides:(NSUInteger)sides startAngle:(float)startAngleDegrees aspectRatio:(float)aspectRatio
79{
80 return [[[self alloc] initWithPolygonSides:sides startAngle:startAngleDegrees aspectRatio:aspectRatio] autorelease];
81}
82
83- (void)setInnerColor:(OOColor*)color1 outerColor:(OOColor*)color2
84{
85 GLfloat inner[4], outer[4];
86 [color1 getRed:&inner[0] green:&inner[1] blue:&inner[2] alpha:&inner[3]];
87 [color2 getRed:&outer[0] green:&outer[1] blue:&outer[2] alpha:&outer[3]];
89}
90
91- (void)setInnerColorComponents:(GLfloat[4])color1 outerColorComponents:(GLfloat[4])color2
92{
93 GLfloat* colors[2] = { color1, color2 };
94
95 for (NSUInteger i = 0; i < _vertexCount; i++) {
96 GLfloat* color = colors[i & 1];
97 memcpy(&_vertexColor[i], color, sizeof(GLfloat) * 4);
98 }
99}
100
101- (void)setLifetime:(double)lifetime
102{
103 _lifetime = lifetime;
104}
105
106- (void)update:(OOTimeDelta)delta_t
107{
108 [super update:delta_t];
109
111 if (_lifetime < 0.0) {
112 [UNIVERSE removeEntity:self];
113 }
114}
115
116- (void)drawImmediate:(bool)immediate translucent:(bool)translucent
117{
118 // check if has been hidden.
120 return;
121
122 if (translucent || immediate) {
125
126 OOGL(glDisable(GL_LIGHTING));
127 OOGL(glDisable(GL_TEXTURE_2D));
128 OOGL(glEnable(GL_BLEND));
129 OOGL(glDepthMask(GL_FALSE));
130 OOGL(glDisableClientState(GL_NORMAL_ARRAY));
131
132 OOGL(glVertexPointer(3, GL_FLOAT, 0, _vertexPosition));
133 OOGL(glEnableClientState(GL_COLOR_ARRAY));
134 OOGL(glColorPointer(4, GL_FLOAT, 0, _vertexColor));
135
136 OOGL(glDrawArrays(GL_TRIANGLE_STRIP, 0, _vertexCount));
137
138 OOGL(glEnable(GL_LIGHTING));
139 OOGL(glEnable(GL_TEXTURE_2D));
140 OOGL(glDisable(GL_BLEND));
141 OOGL(glDepthMask(GL_TRUE));
142 OOGL(glEnableClientState(GL_NORMAL_ARRAY));
143 OOGL(glDisableClientState(GL_COLOR_ARRAY));
144
146 OOCheckOpenGLErrors(@"OOBreakPatternEntity after drawing %@", self);
147 }
148}
149
151{
152 return NO;
153}
154
156{
157 return YES;
158}
159
160@end
161
162@implementation Entity (OOBreakPatternEntity)
163
165{
166 return NO;
167}
168
169@end
#define BREAK_PATTERN_RING_SPEED
@ kOOBreakPatternMaxSides
#define OO_ENTER_OPENGL()
#define MAX(A, B)
Definition OOMaths.h:94
#define MIN(A, B)
Definition OOMaths.h:91
#define M_PI
Definition OOMaths.h:54
@ OPENGL_STATE_OPAQUE
Definition OOOpenGL.h:121
#define OOVerifyOpenGLState()
Definition OOOpenGL.h:133
BOOL OOCheckOpenGLErrors(NSString *format,...)
Definition OOOpenGL.m:38
#define OOSetOpenGLState(STATE)
Definition OOOpenGL.h:132
#define OOGL(statement)
Definition OOOpenGL.h:262
double OOTimeDelta
Definition OOTypes.h:193
unsigned isImmuneToBreakPatternHide
Definition Entity.m:1050
void update:(OOTimeDelta delta_t)
Definition Entity.m:850
void setScanClass:(OOScanClass sClass)
Definition Entity.m:738
id init()
Definition Entity.m:64
void setStatus:(OOEntityStatus stat)
Definition Entity.m:728
void setInnerColorComponents:outerColorComponents:(GLfloat[4] color1, [outerColorComponents] GLfloat[4] color2)
GLfloat _vertexColor[kOOBreakPatternMaxVertices][4]
Vector _vertexPosition[kOOBreakPatternMaxVertices]
void getRed:green:blue:alpha:(float *red,[green] float *green,[blue] float *blue,[alpha] float *alpha)
Definition OOColor.m:344