Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOPolygonSprite Class Reference

#include <OOPolygonSprite.h>

+ Inheritance diagram for OOPolygonSprite:
+ Collaboration diagram for OOPolygonSprite:

Instance Methods

(id) - initWithDataArray:outlineWidth:name:
 
(void) - drawFilled
 
(void) - drawOutline
 
(void) - dealloc [implementation]
 
(NSString *) - descriptionComponents [implementation]
 
(void) - drawWithData:count:VBO: [implementation]
 
(void) - resetGraphicsState [implementation]
 
(BOOL) - loadPolygons:outlineWidth: [implementation]
 
(void) - oo_drawHUDBeaconIconAt:size:alpha:z: [implementation]
 

Private Attributes

GLfloat * _solidData
 
size_t _solidCount
 
GLfloat * _outlineData
 
size_t _outlineCount
 
NSString * _name
 

Detailed Description

Definition at line 37 of file OOPolygonSprite.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Definition at line 78 of file OOPolygonSprite.m.

157{
159
160#ifndef NDEBUG
161 DESTROY(_name);
162#endif
163 free(_solidData);
164 free(_outlineData);
165
166 [super dealloc];
167}
#define DESTROY(x)
Definition OOCocoa.h:77
void unregisterClient:(id< OOGraphicsResetClient > client)
OOGraphicsResetManager * sharedManager()
GLfloat * _outlineData

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Definition at line 78 of file OOPolygonSprite.m.

172{
173 return _name;
174}

◆ drawFilled

- (void) drawFilled

Definition at line 78 of file OOPolygonSprite.m.

223{
224#if !OO_USE_VBO
225 GLuint _solidVBO; // Unusued
226#endif
227
228 [self drawWithData:_solidData count:_solidCount VBO:&_solidVBO];
229}

◆ drawOutline

- (void) drawOutline

Definition at line 78 of file OOPolygonSprite.m.

233{
234#if !OO_USE_VBO
235 GLuint _outlineVBO; // Unusued
236#endif
237
238 [self drawWithData:_outlineData count:_outlineCount VBO:&_outlineVBO];
239}

◆ drawWithData:count:VBO:

- (void) drawWithData: (GLfloat *) data
count: (size_t) count
VBO: (GLuint *) vbo 
implementation

Definition at line 78 of file OOPolygonSprite.m.

178 :(GLfloat *)data count:(size_t)count VBO:(GLuint *)vbo
179{
180 if (count == 0) return;
181 NSParameterAssert(vbo != NULL && data != NULL);
182
185
186#if OO_USE_VBO
188
189 if (useVBO)
190 {
191 if (*vbo == 0)
192 {
193 OOGL(glGenBuffersARB(1, vbo));
194 if (*vbo != 0)
195 {
196 OOGL(glBindBufferARB(GL_ARRAY_BUFFER, *vbo));
197 OOGL(glBufferDataARB(GL_ARRAY_BUFFER, sizeof (GLfloat) * count * 2, data, GL_STATIC_DRAW));
198 }
199 }
200 else
201 {
202 OOGL(glBindBufferARB(GL_ARRAY_BUFFER, *vbo));
203 }
204 if (*vbo != 0) data = NULL; // Must pass NULL pointer to glVertexPointer to use VBO.
205 }
206#endif
207
208 OOGL(glEnableClientState(GL_VERTEX_ARRAY));
209 OOGL(glVertexPointer(2, GL_FLOAT, 0, data));
210 OOGL(glDrawArrays(GL_TRIANGLES, 0, count));
211 OOGL(glDisableClientState(GL_VERTEX_ARRAY));
212
213#if OO_USE_VBO
214 if (useVBO) OOGL(glBindBufferARB(GL_ARRAY_BUFFER, 0));
215#endif
216
218 OOCheckOpenGLErrors(@"OOPolygonSprite after rendering %@", self);
219}
#define OO_ENTER_OPENGL()
@ OPENGL_STATE_OVERLAY
Definition OOOpenGL.h:126
#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
unsigned count
OOOpenGLExtensionManager * sharedManager()

◆ initWithDataArray:outlineWidth:name:

- (id) initWithDataArray: (NSArray *) dataArray
outlineWidth: (GLfloat) outlineWidth
name: (NSString *) name 

Definition at line 78 of file OOPolygonSprite.m.

124 :(NSArray *)dataArray outlineWidth:(GLfloat)outlineWidth name:(NSString *)name
125{
126 if ((self = [super init]))
127 {
128#ifndef NDEBUG
129 _name = [name copy];
130#endif
131
132 if ([dataArray count] == 0)
133 {
134 [self release];
135 return nil;
136 }
137
138 // Normalize data to array-of-arrays form.
139 if (![[dataArray objectAtIndex:0] isKindOfClass:[NSArray class]])
140 {
141 dataArray = [NSArray arrayWithObject:dataArray];
142 }
143 if (![self loadPolygons:dataArray outlineWidth:outlineWidth])
144 {
145 [self release];
146 return nil;
147 }
148
150 }
151
152 return self;
153}
return nil
void registerClient:(id< OOGraphicsResetClient > client)

◆ loadPolygons:outlineWidth:

- (BOOL) loadPolygons: (NSArray *) dataArray
outlineWidth: (float) outlineWidth 
implementation

Provided by category OOPolygonSprite(Private).

Definition at line 78 of file OOPolygonSprite.m.

257 :(NSArray *)dataArray outlineWidth:(float)outlineWidth
258{
259 NSParameterAssert(dataArray != nil);
260
261 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
262 GLUtesselator *tesselator = NULL;
263
264 TessPolygonData polygonData;
265 memset(&polygonData, 0, sizeof polygonData);
266 polygonData.OK = YES;
267#ifndef NDEBUG
268 polygonData.name = _name;
269 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"polygon-sprite-dump-svg"]) SVGDumpBegin(&polygonData);
270#endif
271
272 // For efficiency, grow to more than big enough for most cases to avoid regrowing.
273 if (!GrowTessPolygonData(&polygonData, 100))
274 {
275 polygonData.OK = NO;
276 goto END;
277 }
278
279 tesselator = gluNewTess();
280 if (tesselator == NULL)
281 {
282 polygonData.OK = NO;
283 goto END;
284 }
285
286 dataArray = DataArrayToPoints(&polygonData, dataArray);
287
288 /*** Tesselate polygon fill ***/
289 gluTessCallback(tesselator, GLU_TESS_BEGIN_DATA, (TessFuncPtr)TessBeginCallback);
290 gluTessCallback(tesselator, GLU_TESS_VERTEX_DATA, (TessFuncPtr)TessVertexCallback);
291 gluTessCallback(tesselator, GLU_TESS_END_DATA, (TessFuncPtr)TessEndCallback);
292 gluTessCallback(tesselator, GLU_TESS_ERROR_DATA, (TessFuncPtr)ErrorCallback);
293 gluTessCallback(tesselator, GLU_TESS_COMBINE_DATA, (TessFuncPtr)TessCombineCallback);
294
295 gluTessBeginPolygon(tesselator, &polygonData);
296 SVGDumpBeginGroup(&polygonData, @"Fill");
297
298 NSUInteger contourCount = [dataArray count], contourIndex;
299 for (contourIndex = 0; contourIndex < contourCount && polygonData.OK; contourIndex++)
300 {
301 NSArray *contour = [dataArray oo_arrayAtIndex:contourIndex];
302 if (contour == nil)
303 {
304 polygonData.OK = NO;
305 break;
306 }
307
308 SubmitVertices(tesselator, &polygonData, contour);
309 }
310
311 gluTessEndPolygon(tesselator);
312 SVGDumpEndGroup(&polygonData);
313
314 if (polygonData.OK)
315 {
316 _solidCount = polygonData.count;
317
318 if (_solidCount != 0)
319 {
320 _solidData = realloc(polygonData.data, polygonData.count * sizeof (GLfloat) * 2);
321 if (_solidData != NULL) polygonData.data = NULL; // realloc succeded.
322 else
323 {
324 // Unlikely, but legal: realloc failed to shrink buffer.
325 _solidData = polygonData.data;
326 if (_solidData == NULL) polygonData.OK = NO;
327 }
328 }
329 else
330 {
331 // Empty polygon.
332 _solidData = NULL;
333 }
334 }
335 if (!polygonData.OK) goto END;
336
337 /*** Tesselate polygon outline ***/
338 gluDeleteTess(tesselator);
339 tesselator = gluNewTess();
340 if (tesselator == NULL)
341 {
342 polygonData.OK = NO;
343 goto END;
344 }
345
346 polygonData.count = 0;
347 polygonData.capacity = 0;
348 if (!GrowTessPolygonData(&polygonData, 100))
349 {
350 polygonData.OK = NO;
351 goto END;
352 }
353#ifndef NDEBUG
354 polygonData.generatingOutline = YES;
355#endif
356
357 gluTessCallback(tesselator, GLU_TESS_BEGIN_DATA, (TessFuncPtr)TessBeginCallback);
358 gluTessCallback(tesselator, GLU_TESS_VERTEX_DATA, (TessFuncPtr)TessVertexCallback);
359 gluTessCallback(tesselator, GLU_TESS_END_DATA, (TessFuncPtr)TessEndCallback);
360 gluTessCallback(tesselator, GLU_TESS_ERROR_DATA, (TessFuncPtr)ErrorCallback);
361 gluTessCallback(tesselator, GLU_TESS_COMBINE_DATA, (TessFuncPtr)TessCombineCallback);
362 gluTessProperty(tesselator, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);
363
364 gluTessBeginPolygon(tesselator, &polygonData);
365 SVGDumpBeginGroup(&polygonData, @"Outline");
366
367 outlineWidth *= 0.5f; // Half the width in, half the width out.
368 contourCount = [dataArray count];
369 for (contourIndex = 0; contourIndex < contourCount && polygonData.OK; contourIndex++)
370 {
371 NSArray *contour = [dataArray oo_arrayAtIndex:contourIndex];
372 if (contour == nil)
373 {
374 polygonData.OK = NO;
375 break;
376 }
377
378 SubmitVertices(tesselator, &polygonData, BuildOutlineContour(contour, outlineWidth, NO));
379 SubmitVertices(tesselator, &polygonData, BuildOutlineContour(contour, outlineWidth, YES));
380 }
381
382 gluTessEndPolygon(tesselator);
383 SVGDumpEndGroup(&polygonData);
384
385 if (polygonData.OK)
386 {
387 if (polygonData.count != 0)
388 {
389 _outlineCount = polygonData.count;
390 _outlineData = realloc(polygonData.data, polygonData.count * sizeof (GLfloat) * 2);
391 if (_outlineData != NULL) polygonData.data = NULL; // realloc succeded.
392 else
393 {
394 // Unlikely, but legal: realloc failed to shrink buffer.
395 _outlineData = polygonData.data;
396 if (_outlineData == NULL) polygonData.OK = NO;
397 }
398 }
399 else
400 {
401 // Empty polygon.
402 _outlineCount = 0;
403 _outlineData = NULL;
404 }
405 }
406
407END:
408 SVGDumpEnd(&polygonData);
409 free(polygonData.data);
410 gluDeleteTess(tesselator);
411 [pool release];
412#ifndef NDEBUG
413 DESTROY(polygonData.debugSVG);
414#endif
415 return polygonData.OK;
416}
static void APIENTRY TessVertexCallback(void *vertexData, void *polygonData)
static void APIENTRY TessEndCallback(void *polygonData)
static void APIENTRY TessBeginCallback(GLenum type, void *polygonData)
static void SVGDumpBeginGroup(TessPolygonData *data, NSString *name)
static NSArray * DataArrayToPoints(TessPolygonData *data, NSArray *dataArray)
GLvoid(* TessFuncPtr)()
static void SubmitVertices(GLUtesselator *tesselator, TessPolygonData *polygonData, NSArray *contour)
static void APIENTRY ErrorCallback(GLenum error, void *polygonData)
static void APIENTRY TessCombineCallback(GLdouble coords[3], void *vertexData[4], GLfloat weight[4], void **outData, void *polygonData)
static NSArray * BuildOutlineContour(NSArray *dataArray, GLfloat width, BOOL inner)
static void SVGDumpBegin(TessPolygonData *data)
static void SVGDumpEndGroup(TessPolygonData *data)
static void SVGDumpEnd(TessPolygonData *data)
static BOOL GrowTessPolygonData(TessPolygonData *data, size_t capacityHint)
NSMutableString * debugSVG

◆ oo_drawHUDBeaconIconAt:size:alpha:z:

- (void) oo_drawHUDBeaconIconAt: (NSPoint) where
size: (NSSize) size
alpha: (GLfloat) alpha
z: (GLfloat) z 
implementation

Provided by category OOPolygonSprite(OOHUDBeaconIcon).

Definition at line 4384 of file HeadUpDisplay.m.

4443 :(NSPoint)where size:(NSSize)size alpha:(GLfloat)alpha z:(GLfloat)z
4444{
4445 GLfloat x = where.x - size.width;
4446 GLfloat y = where.y - 1.5 * size.height;
4447
4448 GLfloat ox = x - size.width * 0.5;
4449 GLfloat oy = y - size.height * 0.5;
4450 GLfloat width = size.width * (1.0f / 6.0f);
4451 GLfloat height = size.height * (1.0f / 6.0f);
4452
4454 OOGLTranslateModelView(make_vector(ox, oy, z));
4455 OOGLScaleModelView(make_vector(width, height, 1.0f));
4456 [self drawFilled];
4457 glColor4f(0.0, 0.0, 0.0, 0.5 * alpha);
4458 [self drawOutline];
4460}
void OOGLScaleModelView(Vector scale)
void OOGLPushModelView(void)
void OOGLTranslateModelView(Vector vector)
OOMatrix OOGLPopModelView(void)
float y
float x
voidpf void uLong size
Definition ioapi.h:134

◆ resetGraphicsState

- (void) resetGraphicsState
implementation

Definition at line 78 of file OOPolygonSprite.m.

243{
244#if OO_USE_VBO
246
247 if (_solidVBO != 0) glDeleteBuffersARB(1, &_solidVBO);
248 if (_outlineVBO != 0) glDeleteBuffersARB(1, &_outlineVBO);
249
250 _solidVBO = 0;
251 _outlineVBO = 0;
252#endif
253}

Member Data Documentation

◆ _name

- (NSString*) _name
private

Definition at line 51 of file OOPolygonSprite.h.

◆ _outlineCount

- (size_t) _outlineCount
private

Definition at line 43 of file OOPolygonSprite.h.

◆ _outlineData

- (GLfloat*) _outlineData
private

Definition at line 42 of file OOPolygonSprite.h.

◆ _solidCount

- (size_t) _solidCount
private

Definition at line 41 of file OOPolygonSprite.h.

◆ _solidData

- (GLfloat*) _solidData
private

Definition at line 40 of file OOPolygonSprite.h.


The documentation for this class was generated from the following files: