Draw vertices / triangles

FossilOrigin-Name: 4f4baa85c1fdb1d605305a137e9beb1aff7d09304d565d73f1ae5bdab3f49c34
This commit is contained in:
Jonathan Schleifer 2023-01-09 22:39:01 +00:00
parent 40756ef329
commit c322610fe4
4 changed files with 119 additions and 1 deletions

View file

@ -28,6 +28,8 @@ OF_ASSUME_NONNULL_BEGIN
O3DWinAPIWindow *_winAPIWindow; O3DWinAPIWindow *_winAPIWindow;
#endif #endif
GrContext_t _context; GrContext_t _context;
OFSize _clippingWindow;
bool _syncsToVerticalBlank;
} }
@end @end

View file

@ -13,9 +13,15 @@
* this file. * this file.
*/ */
#include <assert.h>
#import "O3DGlide3Renderer.h" #import "O3DGlide3Renderer.h"
#import "O3DWinAPIWindow.h" #import "O3DWinAPIWindow.h"
@interface O3DGlide3Renderer ()
- (void)setState;
@end
static OFSize static OFSize
screenResolutionToSize(GrScreenResolution_t resolution) screenResolutionToSize(GrScreenResolution_t resolution)
{ {
@ -180,7 +186,27 @@ floatToScreenRefresh(float refresh)
return GR_REFRESH_NONE; return GR_REFRESH_NONE;
} }
static OF_INLINE void
translateVertices(O3DVertex *vertices, size_t count, OFSize size)
{
/*
* Range is -1 to 1, we add 1, so we get 0 to 2 - so we need to
* multiply with half the width / height.
*/
size.width /= 2;
size.height /= 2;
for (size_t i = 0; i < count; i++) {
vertices[i].x += 1;
vertices[i].y += 1;
vertices[i].x *= size.width;
vertices[i].y *= size.height;
}
}
@implementation O3DGlide3Renderer @implementation O3DGlide3Renderer
@synthesize syncsToVerticalBlank = _syncsToVerticalBlank;
- (instancetype)initWithOptions: - (instancetype)initWithOptions:
(OFDictionary OF_GENERIC(OFString *, id) *)options (OFDictionary OF_GENERIC(OFString *, id) *)options
{ {
@ -297,5 +323,57 @@ floatToScreenRefresh(float refresh)
GR_COLORFORMAT_RGBA, GR_ORIGIN_LOWER_LEFT, 2, 1)) == 0) GR_COLORFORMAT_RGBA, GR_ORIGIN_LOWER_LEFT, 2, 1)) == 0)
@throw [OFInitializationFailedException @throw [OFInitializationFailedException
exceptionWithClass: self.class]; exceptionWithClass: self.class];
_clippingWindow = [resolution.firstObject sizeValue];
[self setState];
}
- (void)setState
{
grVertexLayout(GR_PARAM_XY, offsetof(O3DVertex, x), GR_PARAM_ENABLE);
grSstOrigin(GR_ORIGIN_LOWER_LEFT);
}
- (void)beginFrame
{
assert(_context != 0);
if (!grSelectContext(_context))
[self setState];
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)drawVertices: (OFData *)vertices_
{
assert(vertices_.itemSize == sizeof(O3DVertex));
OFMutableData *vertices = [[vertices_ mutableCopy] autorelease];
translateVertices(vertices.mutableItems, vertices.count,
_clippingWindow);
grDrawVertexArrayContiguous(GR_TRIANGLE_STRIP,
vertices.count, vertices.mutableItems, sizeof(O3DVertex));
}
- (void)endFrame
{
grBufferSwap(_syncsToVerticalBlank ? 1 : 0);
} }
@end @end

View file

@ -19,13 +19,22 @@ OF_ASSUME_NONNULL_BEGIN
typedef OFPair OF_GENERIC(OFValue *, OFNumber *) *O3DResolution; typedef OFPair OF_GENERIC(OFValue *, OFNumber *) *O3DResolution;
typedef struct OF_BOXABLE {
float x, y;
} O3DVertex;
@protocol O3DRenderer <OFObject> @protocol O3DRenderer <OFObject>
@property (readonly, nonatomic) OFArray OF_GENERIC(OFPair OF_GENERIC(OFValue *, @property (readonly, nonatomic) OFArray OF_GENERIC(OFPair OF_GENERIC(OFValue *,
OFNumber *) *) *availableResolutions; OFNumber *) *) *availableResolutions;
@property (nonatomic) bool syncsToVerticalBlank;
- (instancetype)initWithOptions: - (instancetype)initWithOptions:
(nullable OFDictionary OF_GENERIC(OFString *, id) *)options; (nullable OFDictionary OF_GENERIC(OFString *, id) *)options;
- (void)createWithResolution: (O3DResolution)resolution; - (void)createWithResolution: (O3DResolution)resolution;
- (void)beginFrame;
- (void)setColor: (OFColor *)color;
- (void)drawVertices: (OFData *)vertices;
- (void)endFrame;
@end @end
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -39,7 +39,36 @@ OF_APPLICATION_DELEGATE(TestsAppDelegate)
secondObject: [OFNumber numberWithFloat: 60]]; secondObject: [OFNumber numberWithFloat: 60]];
[engine.renderer createWithResolution: resolution]; [engine.renderer createWithResolution: resolution];
[OFThread sleepForTimeInterval: 5]; O3DVertex values1[] = {
{ -1, -1 },
{ 0, 1 },
{ 1, -1 }
};
O3DVertex values2[] = {
{ -0.5, 0.5 },
{ 0, -0.5 },
{ 0.5, 0.5 }
};
OFData *vertices1 = [OFData
dataWithItemsNoCopy: values1
count: sizeof(values1) / sizeof(*values2)
itemSize: sizeof(O3DVertex)
freeWhenDone: false];
OFData *vertices2 = [OFData
dataWithItemsNoCopy: values2
count: sizeof(values2) / sizeof(*values2)
itemSize: sizeof(O3DVertex)
freeWhenDone: false];
OFDate *startDate = [OFDate date];
while (-startDate.timeIntervalSinceNow < 5) {
[engine.renderer beginFrame];
[engine.renderer setColor: [OFColor yellow]];
[engine.renderer drawVertices: vertices1];
[engine.renderer setColor: [OFColor red]];
[engine.renderer drawVertices: vertices2];
[engine.renderer endFrame];
}
[OFApplication terminate]; [OFApplication terminate];
} }