Oolite
Loading...
Searching...
No Matches
OOCrosshairs.m
Go to the documentation of this file.
1/*
2
3OOCrosshairs.m
4Oolite
5
6
7Copyright (C) 2008 Jens Ayton
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25SOFTWARE.
26
27*/
28
29#import "OOCrosshairs.h"
30#import "MyOpenGLView.h"
32#import "OOColor.h"
33#import "OOMacroOpenGL.h"
34#import "Universe.h"
35
36@interface OOCrosshairs (Private)
37
38- (void)setUpDataWithPoints:(NSArray*)points
39 scale:(GLfloat)scale
40 color:(OOColor*)color
41 overallAlpha:(GLfloat)alpha;
42
43- (void)setUpDataForOnePoint:(NSArray*)pointInfo
44 scale:(GLfloat)scale
45 colorComps:(float[4])colorComps
46 overallAlpha:(GLfloat)alpha
47 data:(GLfloat*)ioBuffer;
48
49@end
50
51@implementation OOCrosshairs
52
53- (id)initWithPoints:(NSArray*)points
54 scale:(GLfloat)scale
55 color:(OOColor*)color
56 overallAlpha:(GLfloat)alpha
57{
58 if ((self = [super init])) {
59 if (alpha > 0.0f && (color == nil || [color alphaComponent] != 0.0f)) {
60 [self setUpDataWithPoints:points scale:scale color:color overallAlpha:alpha];
61 }
62 }
63
64 return self;
65}
66
67- (void)dealloc
68{
69 free(_data);
70
71 [super dealloc];
72}
73
74- (void)render
75{
76 if (_data != NULL) {
79
80 OOGL(glPushAttrib(GL_ENABLE_BIT));
81 OOGL(glDisable(GL_LIGHTING));
82 OOGL(glDisable(GL_TEXTURE_2D));
84 OOGLTranslateModelView(make_vector(0.0, 0.0, [[UNIVERSE gameView] display_z]));
85
86 OOGL(glVertexPointer(2, GL_FLOAT, sizeof(GLfloat) * 6, _data));
87 OOGL(glColorPointer(4, GL_FLOAT, sizeof(GLfloat) * 6, _data + 2));
88
89 OOGL(glEnableClientState(GL_VERTEX_ARRAY));
90 OOGL(glEnableClientState(GL_COLOR_ARRAY));
91
92 OOGL(glDrawArrays(GL_LINES, 0, _count * 2));
93
94 OOGL(glDisableClientState(GL_VERTEX_ARRAY));
95 OOGL(glDisableClientState(GL_COLOR_ARRAY));
96
98 OOGL(glPopAttrib());
99
101 OOCheckOpenGLErrors(@"OOCrosshairs after rendering");
102 }
103}
104
105- (void)setUpDataWithPoints:(NSArray*)points
106 scale:(GLfloat)scale
107 color:(OOColor*)color
108 overallAlpha:(GLfloat)alpha
109{
110 NSUInteger i;
111 float colorComps[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
112 GLfloat* data = NULL;
113
114 _count = [points count];
115 if (_count == 0)
116 return;
117
118 _data = malloc(sizeof(GLfloat) * 12 * _count); // 2 coordinates, 4 colour components for each endpoint of each line segment
119 [color getRed:&colorComps[0] green:&colorComps[1] blue:&colorComps[2] alpha:&colorComps[3]];
120
121 // Turn NSArray into GL-friendly element array
122 data = _data;
123 for (i = 0; i < _count; i++) {
124 [self setUpDataForOnePoint:[points oo_arrayAtIndex:i]
125 scale:scale
126 colorComps:colorComps
127 overallAlpha:alpha
128 data:data];
129 data += 12;
130 }
131}
132
133- (void)setUpDataForOnePoint:(NSArray*)pointInfo
134 scale:(GLfloat)scale
135 colorComps:(float[4])colorComps
136 overallAlpha:(GLfloat)alpha
137 data:(GLfloat*)ioBuffer
138{
139 GLfloat x1, y1, a1, x2, y2, a2;
140 GLfloat r, g, b, a;
141
142 if ([pointInfo count] >= 6) {
143 a1 = [pointInfo oo_floatAtIndex:0];
144 x1 = [pointInfo oo_floatAtIndex:1] * scale;
145 y1 = [pointInfo oo_floatAtIndex:2] * scale;
146 a2 = [pointInfo oo_floatAtIndex:3];
147 x2 = [pointInfo oo_floatAtIndex:4] * scale;
148 y2 = [pointInfo oo_floatAtIndex:5] * scale;
149 r = colorComps[0];
150 g = colorComps[1];
151 b = colorComps[2];
152 a = colorComps[3];
153
154 /* a1/a2 * a is hud.plist and crosshairs.plist - specified alpha,
155 which must be clamped to 0..1 so the plist-specified alpha can't
156 "escape" the overall alpha range. The result of scaling this by
157 overall HUD alpha is then clamped again for robustness.
158 */
159 a1 = OOClamp_0_1_f(OOClamp_0_1_f(a1 * a) * alpha);
160 a2 = OOClamp_0_1_f(OOClamp_0_1_f(a2 * a) * alpha);
161 } else {
162 // Bad entry, write red point in middle.
163 x1 = -0.01f;
164 x2 = 0.01f;
165 y1 = y2 = 0.0f;
166 r = 1.0f;
167 g = b = 0.0f;
168 a1 = a2 = 1.0;
169 }
170
171 *ioBuffer++ = x1;
172 *ioBuffer++ = y1;
173 *ioBuffer++ = r;
174 *ioBuffer++ = g;
175 *ioBuffer++ = b;
176 *ioBuffer++ = a1;
177
178 *ioBuffer++ = x2;
179 *ioBuffer++ = y2;
180 *ioBuffer++ = r;
181 *ioBuffer++ = g;
182 *ioBuffer++ = b;
183 *ioBuffer++ = a2;
184
185 (void)ioBuffer;
186}
187
188@end
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define OO_ENTER_OPENGL()
void OOGLPushModelView(void)
void OOGLTranslateModelView(Vector vector)
OOMatrix OOGLPopModelView(void)
@ OPENGL_STATE_OVERLAY
Definition OOOpenGL.h:124
#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
unsigned count
return nil
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
#define UNIVERSE
Definition Universe.h:827
void getRed:green:blue:alpha:(float *red,[green] float *green,[blue] float *blue,[alpha] float *alpha)
Definition OOColor.m:344
NSUInteger _count
void setUpDataWithPoints:scale:color:overallAlpha:(NSArray *points, [scale] GLfloat scale, [color] OOColor *color, [overallAlpha] GLfloat alpha)
void setUpDataForOnePoint:scale:colorComps:overallAlpha:data:(NSArray *pointInfo, [scale] GLfloat scale, [colorComps] float[4] colorComps, [overallAlpha] GLfloat alpha, [data] GLfloat *ioBuffer)
GLfloat * _data