From cdf559f8e3fefd28e1e3a8771dcb87ee2432a99c Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Tue, 10 Jan 2023 02:43:32 +0000 Subject: [PATCH] Per vertex colors FossilOrigin-Name: 3632a7239a1926291c9162ba892d2315038a178a31652618df991c0ee549b5b5 --- src/O3DGlideRenderer.m | 25 +++++++------------------ src/O3DRenderer.h | 5 +++-- tests/TestsAppDelegate.m | 18 ++++++++---------- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/O3DGlideRenderer.m b/src/O3DGlideRenderer.m index fd6ecf7..bdb8c95 100644 --- a/src/O3DGlideRenderer.m +++ b/src/O3DGlideRenderer.m @@ -199,23 +199,8 @@ grGlideShutdownCdecl(void) grBufferClear(0, 0, 0); } -- (void)setColor: (OFColor *)color -{ - float values[4]; - [color getRed: &values[0] - green: &values[1] - blue: &values[2] - alpha: &values[3]]; - for (uint_fast8_t i = 0; i < 4; i++) - values[i] *= 255; - grColorCombine(GR_COMBINE_FUNCTION_LOCAL, GR_COMBINE_FACTOR_NONE, - GR_COMBINE_LOCAL_CONSTANT, GR_COMBINE_OTHER_NONE, FXFALSE); - grConstantColorValue((((GrColor_t)values[0]) << 24) | - (((GrColor_t)values[1]) << 16) | (((GrColor_t)values[2]) << 8) | - (GrColor_t)values[2]); -} - -- (void)drawPolygonVertices: (const O3DVertex *)vertices count: (size_t)count +- (void)drawPolygonWithVertices: (const O3DVertex *)vertices + count: (size_t)count { /* * Range is -1 to 1, we add 1, so we get 0 to 2 - so we need to @@ -224,11 +209,15 @@ grGlideShutdownCdecl(void) float halfWidth = grSstScreenWidth() / 2; float halfHeight = grSstScreenHeight() / 2; - GrVertex *glideVertices = OFAllocMemory(count, sizeof(GrVertex)); + GrVertex *glideVertices = OFAllocZeroedMemory(count, sizeof(GrVertex)); @try { for (size_t i = 0; i < count; i++) { glideVertices[i].x = (vertices[i].x + 1) * halfWidth; glideVertices[i].y = (vertices[i].y + 1) * halfHeight; + glideVertices[i].r = vertices[i].r * 255; + glideVertices[i].g = vertices[i].g * 255; + glideVertices[i].b = vertices[i].b * 255; + glideVertices[i].a = vertices[i].a * 255; } grDrawPolygonVertexList((int)count, glideVertices); diff --git a/src/O3DRenderer.h b/src/O3DRenderer.h index 9758cc8..20d3531 100644 --- a/src/O3DRenderer.h +++ b/src/O3DRenderer.h @@ -19,6 +19,7 @@ OF_ASSUME_NONNULL_BEGIN typedef struct OF_BOXABLE { float x, y; + float r, g, b, a; } O3DVertex; @protocol O3DRenderer @@ -31,8 +32,8 @@ typedef struct OF_BOXABLE { refreshRate: (float)refreshRate options: (nullable OFDictionary *)options; - (void)beginFrame; -- (void)setColor: (OFColor *)color; -- (void)drawPolygonVertices: (const O3DVertex *)vertices count: (size_t)count; +- (void)drawPolygonWithVertices: (const O3DVertex *)vertices + count: (size_t)count; - (void)endFrame; @end diff --git a/tests/TestsAppDelegate.m b/tests/TestsAppDelegate.m index a866cc2..4406d78 100644 --- a/tests/TestsAppDelegate.m +++ b/tests/TestsAppDelegate.m @@ -32,23 +32,21 @@ OF_APPLICATION_DELEGATE(TestsAppDelegate) options: nil] autorelease]; const O3DVertex outerTriangle[] = { - { -1, -1 }, - { 0, 1 }, - { 1, -1 } + { -1, -1, 1, 0, 0 }, + { 0, 1, 0, 1, 0 }, + { 1, -1, 0, 0, 1 } }; const O3DVertex innerTriangle[] = { - { -0.5, 0.5 }, - { 0, -0.5 }, - { 0.5, 0.5 } + { -0.5, 0.5, 1, 1, 0 }, + { 0, -0.5, 0, 1, 1 }, + { 0.5, 0.5, 1, 0, 1 } }; OFDate *startDate = [OFDate date]; while (-startDate.timeIntervalSinceNow < 5) { [renderer beginFrame]; - [renderer setColor: [OFColor yellow]]; - [renderer drawPolygonVertices: outerTriangle count: 3]; - [renderer setColor: [OFColor red]]; - [renderer drawPolygonVertices: innerTriangle count: 3]; + [renderer drawPolygonWithVertices: outerTriangle count: 3]; + [renderer drawPolygonWithVertices: innerTriangle count: 3]; [renderer endFrame]; }