/
pico.h
579 lines (492 loc) · 18.7 KB
/
pico.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
* pico.h
*
* This file is part of the "PicoRenderer" (Copyright (c) 2014 by Lukas Hermanns)
* See "LICENSE.txt" for license information.
*/
#ifndef __PR_PICO_H__
#define __PR_PICO_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "types.h"
#include "error_ids.h"
#include "platform.h"
#include "enums.h"
#include "structs.h"
#include "consts.h"
#include <stdio.h>
// --- common --- //
//! Initializes the pico renderer.
PRboolean prInit();
//! Releases the pico renderer.
PRboolean prRelease();
//! Returns the last error. By default PR_ERROR_NONE.
PRenum prGetError();
//! Sets the error event handler.
void prErrorHandler(PR_ERROR_HANDLER_PROC errorHandler);
/**
Returns the specified string or null if 'str' is invalid.
\param[in] str Specifies the string which is to be returned. Must begin with 'PR_STRING_...'.
*/
const char* prGetString(PRenum str);
/**
Returns the specified parameter value.
\param[in] param Specifies the parameter which is to be queried.
- PR_MAX_TEXTURE_SIZE: Returns the maximum texture size.
*/
PRint prGetIntegerv(PRenum param);
// --- context --- //
/**
Generates a new render context. At least one render context is required to render anything.
\param[in] desc Specifies the OS dependent render context description.
\param[in] width Specifies the context width.
\param[in] height Specifies the context height.
\remarks The render context must be deleted with 'prDeleteContext'.
\see prDeleteContext
\see prMakeCurrent
*/
PRobject prCreateContext(const PRcontextdesc* desc, PRuint width, PRuint height);
/**
Deletes the specified render context.
\param[in] context Specifies the render context which is to be deleted.
This must be generated by 'prCreateContext'.
\see prCreateContext
*/
void prDeleteContext(PRobject context);
/**
Makes the specified context to the current context.
By default each new created context will be the new current context.
\param[in] context Specifies the new current context, which was created with a call to prCreateContext.
\see prCreateContext
*/
void prMakeCurrent(PRobject context);
//! Presents the currently bound frame buffer in the specified render context.
void prPresent(PRobject context);
// --- framebuffer --- //
/**
Generates a new frame buffer. At least one frame buffer is required to render anything.
\param[in] width Specifies the frame buffer width.
\param[in] height Specifies the frame buffer height.
\return Frame buffer object.
\remarks The frame buffer must be deleted with 'prDeleteFrameBuffer'.
\see prDeleteFrameBuffer
*/
PRobject prCreateFrameBuffer(PRuint width, PRuint height);
/**
Deletes the specified frame buffer.
\param[in] frameBuffer Specifies the frame buffer which is to be deleted.
This must be generated by 'prCreateFrameBuffer'.
\see prCreateFrameBuffer
*/
void prDeleteFrameBuffer(PRobject frameBuffer);
/**
Binds the specified frame buffer.
\param[in] frameBuffer Specifies the frame buffer which is to be bound.
If this is zero, no frame buffer is bound.
\note After a new frame buffer has bound, the viewport and scissor must be set again.
\see prViewport
\see prScissor
*/
void prBindFrameBuffer(PRobject frameBuffer);
/**
Clears the specified frame buffer.
\param[in] frameBuffer Specifies the frame buffer which is to be cleared.
\param[in] clearDepth Specifies the depth value to clear the depth buffer.
\param[in] clearFlags Specifies the clear flags. This can be a bitwise OR combination of the following bit masks:
- PR_COLOR_BUFFER_BIT: Clears the color buffer.
- PR_DEPTH_BUFFER_BIT: Clears the depth buffer.
*/
void prClearFrameBuffer(PRobject frameBuffer, PRfloat clearDepth, PRbitfield clearFlags);
// --- texture --- //
/**
Generates a new texture.
\return Texture object.
\remarks The texture must be deleted with 'prDeleteTexture'.
\see prDeleteTexture
*/
PRobject prCreateTexture();
/**
Deletes the specified texture.
\param[in] texture Specifies the texture which is to be deleted.
This must be generated by 'prCreateTexture'.
\see prCreateTexture
*/
void prDeleteTexture(PRobject texture);
/**
Binds the specified texture.
\param[in] texture Specifies the texture which is to be bound.
If this is zero, no texture is bound.
*/
void prBindTexture(PRobject texture);
/**
Sets the 2D image data to the specified texture.
\param[in] texture Specifies the texture whose image data is to be set.
\param[in] width Specifies the image width. This will be the final texture width.
\param[in] height Specifies the image height. This will be the final texture height.
\param[in] format Specifies the image data format. This must be PR_UBYTE_RGB.
\param[in] data Raw pointer to the image data. This must be in the format: PRubyte[width*height*3].
\param[in] dither Specifies whether dithering is to be applied to the image (to compensate 8-bit colors).
\param[in] generateMips Specifies whether MIP maps are to be generated for this texture.
*/
void prTexImage2D(
PRobject texture, PRtexsize width, PRtexsize height, PRenum format,
const PRvoid* data, PRboolean dither, PRboolean generateMips
);
/**
Sets the 2D image data from file to the specified texture.
\param[in] texture Specifies the texture whose image data is to be set.
\param[in] filename Specifies the image filename. Valid image file formats are: BMP, PNG, TGA, JPEG (base line only).
\param[in] dither Specifies whether dithering is to be applied to the image (to compensate 8-bit colors).
\param[in] generateMips Specifies whether MIP maps are to be generated for this texture.
\see prTexImage2D
*/
void prTexImage2DFromFile(PRobject texture, const char* filename, PRboolean dither, PRboolean generateMips);
/**
Sets the texture environment parameters.
\param[in] param Specifies the paramer whose value is to be set. Valid values are:
- PR_TEXTURE_LOD_BIAS: Specifies the level-of-detail bias for MIP-mapping. Must be in the range [0, 255]. By default 0.
\param[in] value Specifies the new integer value.
*/
void prTexEnvi(PRenum param, PRint value);
/**
Returns a parameter of the specified texture MIP-map.
\param[in] texture Specifies the texture whose parameter is to be determined.
\param[in] mipLevel Specifiefs the MIP-map level.
\param[in] param Specifies the parameter which is to be determined.
- PR_TEXTURE_WIDTH: Returns the MIP-map texture width.
- PR_TEXTURE_HEIGHT: Returns the MIP-map texture height.
*/
PRint prGetTexLevelParameteri(PRobject texture, PRubyte mipLevel, PRenum param);
// --- vertexbuffer --- //
/**
Generates a new vertex buffer with the specified number of vertices.
\return Vertex buffer object.
\remarks The vertex buffer must be deleted with 'prDeleteVertexBuffer'.
\see prDeleteVertexBuffer
*/
PRobject prCreateVertexBuffer();
/**
Deletes the specified vertex buffer.
\param[in] vertexBuffer Specifies the vertex buffer which is to be deleted.
This must be generated by 'prCreateVertexBuffer'.
\see prCreateVertexBuffer
*/
void prDeleteVertexBuffer(PRobject vertexBuffer);
/**
Sets the vertex buffer data.
\param[in] vertexBuffer Specifies the vertex buffer whose vertex data is to be set.
\param[in] numVertices Specifies the number of vertices.
\param[in] coords Raw pointer to the vertex coordinates data.
If this is null, the coordinate of all vertices will be initialized with { 0, 0, 0 }.
\param[in] texCoords Raw pointer to the vertex texture coordinates data.
If this is null, the texture coordinate of all vertices will be initialized with { 0, 0 }.
\param[in] vertexStride Specifies the vertex stride size (in bytes).
\code
struct MyVertex
{
PRfloat x, y, z; // Vertex coordinates X, Y, Z
PRfloat u, v; // Texture coordinates U, V
};
struct MyVertex[32] = ...
prVertexBufferData(vertexBuffer, 32, &(MyVertex[0].x), &(MyVertex[0].u), sizeof(MyVertex));
\endcode
*/
void prVertexBufferData(PRobject vertexBuffer, PRsizei numVertices, const PRvoid* coords, const PRvoid* texCoords, PRsizei vertexStride);
/**
Reads and sets the vertex buffer data from the specified file.
\param[in] vertexBuffer Specifies the vertex buffer whose vertex data is to be set.
\param[out] numVertices Specifies the resulting number of vertices.
\param[in] file Pointer to the file object. This file must be opened in binary read mode: fopen(filename, "rb").
\code
// File format:
numVertices: 16-bit unsigned integer
vertices[numVertices]: 'numVertices' * (five 32-bit floating point values for: x, y, z, u, v) (see 'PRvertex').
\endcode
\see PRvertex
*/
void prVertexBufferDataFromFile(PRobject vertexBuffer, PRsizei* numVertices, FILE* file);
/**
Binds the specified vertex buffer.
\param[in] vertexBuffer Specifies the vertex buffer which is to be bound.
If this is zero, no vertex buffer is bound.
*/
void prBindVertexBuffer(PRobject vertexBuffer);
// --- indexbuffer --- //
/**
Generates a new index buffer with the specified number of indices.
\return Index buffer object.
\remarks The index buffer must be deleted with 'prDeleteIndexBuffer'.
\see prDeleteIndexBuffer
*/
PRobject prCreateIndexBuffer();
/**
Deletes the specified index buffer.
\param[in] indexBuffer Specifies the index buffer which is to be deleted.
This must be generated by 'prCreateIndexBuffer'.
\see prCreateIndexBuffer
*/
void prDeleteIndexBuffer(PRobject indexBuffer);
/**
Sets the index buffer data.
\param[in] indexBuffer Specifies the index buffer whose vertex data is to be set.
\param[in] indices Pointer to the index data. Only 16-bit unsigned integers are allowed as vertex indices.
\param[in] numIndices Specifies the number of indices. The array 'indices' must be large enough!
*/
void prIndexBufferData(PRobject indexBuffer, const PRushort* indices, PRsizei numIndices);
/**
Reads and sets the index buffer data from the specified file.
\param[in] indexBuffer Specifies the index buffer whose index data is to be set.
\param[out] numIndices Specifies the resulting number of indices.
\param[in] file Pointer to the file object. This file must be opened in binary read mode: fopen(filename, "rb").
\code
// File format:
numIndices: 16-bit unsigned integer
indices[numVertices]: 'numIndices' * (16-bit unsigned integer).
\endcode
*/
void prIndexBufferDataFromFile(PRobject indexBuffer, PRsizei* numIndices, FILE* file);
/**
Binds the specified index buffer.
\param[in] indexBuffer Specifies the index buffer which is to be bound.
If this is zero, no index buffer is bound.
*/
void prBindIndexBuffer(PRobject indexBuffer);
// --- matrices --- //
/**
Sets the projection matrix.
\param[in] matrix4x4 Raw pointer to a 4x4 left-handed projection matrix (in projection space).
\see prBuildPerspectiveProjection
\see prBuildOrthogonalProjection
*/
void prProjectionMatrix(const PRfloat* matrix4x4);
/**
Sets the view matrix.
\param[in] matrix4x4 Raw pointer to a 4x4 left-handed view matrix (in view space).
*/
void prViewMatrix(const PRfloat* matrix4x4);
/**
Sets the world matrix.
\param[in] matrix4x4 Raw pointer to a 4x4 left-handed world matrix (in world space).
*/
void prWorldMatrix(const PRfloat* matrix4x4);
/**
Builds a 4x4 left-handed perspective projection matrix.
\param[out] matrix4x4 Raw pointer to the resulting 4x4 matrix.
\param[in] aspectRatio Specifies the perspective aspect ratio.
\param[in] nearPlane Specifies the near plane.
\param[in] farPlane Specifies the far plane.
\param[in] fov Specifies the field-of-view angle (in radians).
*/
void prBuildPerspectiveProjection(PRfloat* matrix4x4, PRfloat aspectRatio, PRfloat nearPlane, PRfloat farPlane, PRfloat fov);
/**
Builds a 4x4 left-handed orthogonal projection matrix.
\param[out] matrix4x4 Raw pointer to the resulting 4x4 matrix.
\param[in] width Specifies the orthogonal width.
\param[in] height Specifies the orthogonal height.
\param[in] nearPlane Specifies the near plane.
\param[in] farPlane Specifies the far plane.
*/
void prBuildOrthogonalProjection(PRfloat* matrix4x4, PRfloat width, PRfloat height, PRfloat nearPlane, PRfloat farPlane);
/**
Translates the specified 4x4 left-handed matrix.
\param[in,out] matrix4x4 Raw pointer to the 4x4 matrix which is to be translated.
\param[in] x Specifies the X direction.
\param[in] y Specifies the Y direction.
\param[in] z Specifies the Z direction.
*/
void prTranslate(PRfloat* matrix4x4, PRfloat x, PRfloat y, PRfloat z);
/**
Rotates the specified 4x4 left-handed matrix in a free angle.
\param[in,out] matrix4x4 Raw pointer to the 4x4 matrix which is to be rotated.
\param[in] x Specifies the X rotation axis.
\param[in] y Specifies the Y rotation axis.
\param[in] z Specifies the Z rotation axis.
\param[in] angle Specifies the rotation angle (in radians).
*/
void prRotate(PRfloat* matrix4x4, PRfloat x, PRfloat y, PRfloat z, PRfloat angle);
/**
Scales the specified 4x4 left-handed matrix.
\param[in,out] matrix4x4 Raw pointer to the 4x4 matrix which is to be scaled.
\param[in] x Specifies the width.
\param[in] y Specifies the height.
\param[in] z Specifies the depth.
*/
void prScale(PRfloat* matrix4x4, PRfloat x, PRfloat y, PRfloat z);
/**
Loads the identity of the specified 4x4 left-handed matrix.
\param[out] matrix4x4 Raw pointer to the 4x4 matrix whose identity is to be loaded.
*/
void prLoadIdentity(PRfloat* matrix4x4);
// --- states --- //
/**
Sets the specified state.
\param[in] cap Specifies the capability whose state is to be changed. Valid values are:
- PR_SCISSOR - Enables/disables the scissor rectangle (see prScissor). By default PR_FALSE.
\param[in] state Specifies the new state.
\see prEnable
\see prDisable
*/
void prSetState(PRenum cap, PRboolean state);
/**
Returns the current value of the specified state.
\param[in] cap Specifies the capability whose state is to be returned.
\see prSetState
*/
PRboolean prGetState(PRenum cap);
/**
Enables the specified capability. This is equivalent to:
\code
prSetState(cap, PR_TRUE);
\endcode
\see prSetState
*/
void prEnable(PRenum cap);
/**
Disables the specified capability. This is equivalent to:
\code
prSetState(cap, PR_FALSE);
\endcode
\see prSetState
*/
void prDisable(PRenum cap);
/**
Sets the viewport for the currently bound framebuffer.
\note A frame buffer must be bound before this function can be used!
\see prBindFrameBuffer
*/
void prViewport(PRint x, PRint y, PRint width, PRint height);
/**
Sets the scissor rectangle for the currently bound framebuffer.
\note A frame buffer must be bound before this function can be used!
\see prBindFrameBuffer
*/
void prScissor(PRint x, PRint y, PRint width, PRint height);
/**
Sets the depth range for the currently bound framebuffer.
\note A frame buffer must be bound before this function can be used!
\see prBindFrameBuffer
*/
void prDepthRange(PRfloat minDepth, PRfloat maxDepth);
/**
Sets the face culling mode.
\param[in] mode Specifies the new culling mode.
This must be PR_CULL_NONE, PR_CULL_FRONT or PR_CULL_BACK.
By default PR_CULL_NONE.
*/
void prCullMode(PRenum mode);
/**
Sets the polygon drawing mode.
\param[in] mode Specifies the new polygon mode.
This must be PR_POLYGON_FILL, PR_POLYGON_LINE or PR_POLYGON_POINT.
By default PR_POLYGON_FILL.
*/
void prPolygonMode(PRenum mode);
// --- drawing --- //
//! Sets the clear color. Default is (0, 0, 0).
void prClearColor(PRubyte r, PRubyte g, PRubyte b);
//! Binds the current color index.
void prColor(PRubyte r, PRubyte g, PRubyte b);
//! Draws a single 2D point onto the screen.
void prDrawScreenPoint(PRint x, PRint y);
//! Draws a single 2D line onto the screen.
void prDrawScreenLine(PRint x1, PRint y1, PRint x2, PRint y2);
//! Draws a single 2D image with the currently bound texture.
void prDrawScreenImage(PRint left, PRint top, PRint right, PRint bottom);
/**
Draws the specified amount of primitives.
\param[in] primitives Specifies the primitive types. Valid values are:
PR_POINTS, PR_LINES, PR_LINE_STRIP, PR_LINE_LOOP, PR_TRIANGLES, PR_TRIANGLE_STRIP, PR_TRIANGLE_FAN.
\param[in] numVertices Specifies the number of vertices to draw.
\param[in] firstVertex Specifies the first vertex to draw.
\remarks A vertex buffer must be bound.
\see prBindVertexBuffer
*/
void prDraw(PRenum primitives, PRushort numVertices, PRushort firstVertex);
/**
Draws the specified amount of primitives.
\param[in] primitives Specifies the primitive types. Valid values are:
PR_POINTS, PR_LINES, PR_LINE_STRIP, PR_LINE_LOOP, PR_TRIANGLES, PR_TRIANGLE_STRIP, PR_TRIANGLE_FAN.
\param[in] numVertices Specifies the number of vertices to draw.
\param[in] firstVertex Specifies the first vertex to draw.
\remarks A vertex buffer and an index buffer must be bound.
\see prBindVertexBuffer
\see prBindIndexBuffer
*/
void prDrawIndexed(PRenum primitives, PRushort numVertices, PRushort firstVertex);
// --- immediate mode --- //
/**
Begins the immediate drawing mode.
\param[in] primitives Specifies the primitive types. Valid values are:
PR_POINTS, PR_LINES, PR_LINE_STRIP, PR_LINE_LOOP, PR_TRIANGLES, PR_TRIANGLE_STRIP, PR_TRIANGLE_FAN.
\remarks This must be finished by calling "prEnd". Here is a usage example:
\code
// Draw a quad
prBegin(PR_TRIANGLE_STRIP);
{
prTexCoord2i(0, 0);
prVertex2i(-1, 1);
prTexCoord2i(1, 0);
prVertex2i(1, 1);
prTexCoord2i(0, 1);
prVertex2i(-1, -1);
prTexCoord2i(1, 1);
prVertex2i(1, -1);
}
prEnd();
\encode
\remarks This is equivalent to drawing the primitives with a vertex buffer (but no index buffer).
\note This is slower than using a vertex buffer. A global immediate vertex buffer is used,
to draw the primitives. This internal global buffer is severely limited (by default 32 vertices).
However, you can drawn unlimited primitives, since the buffer works like a stream,
which will be flushed when it's full (i.e. the current content will be drawn to the frame buffer).
\see prEnd
\see prDraw
*/
void prBegin(PRenum primitives);
/**
Ends the immediate drawing mode.
\remarks This must be started by calling "prBegin".
\see prBegin
*/
void prEnd();
/**
Sets the texture coordinates of the current vertex in the immediate drawing mode.
\remarks This must be called between "prBegin" and "prEnd".
\see prBegin
\see prEnd
*/
void prTexCoord2f(PRfloat u, PRfloat v);
// \see prTexCoord2f
void prTexCoord2i(PRint u, PRint v);
/**
Sets the coordinates of the current vertex in the immediate drawing mode.
This function will also increment the vertex counter,
i.e. the next calls to "prTexCoord..." will configure the next vertex.
Thus call "prVertex..." only when all configurations for the current vertex are done.
\remarks This must be called between "prBegin" and "prEnd".
\see prBegin
\see prEnd
*/
void prVertex4f(PRfloat x, PRfloat y, PRfloat z, PRfloat w);
//! \see prVertex4f
void prVertex4i(PRint x, PRint y, PRint z, PRint w);
/**
Sets the coordinates of the current vertex. The 'w' component will be set to 1.0.
\see prVertex4f
*/
void prVertex3f(PRfloat x, PRfloat y, PRfloat z);
//! \see prVertex3f
void prVertex3i(PRint x, PRint y, PRint z);
/**
Sets the coordinates of the current vertex.
The 'z' component will be set to 0.0 and the 'w' component will be set to 1.0.
\see prVertex4f
*/
void prVertex2f(PRfloat x, PRfloat y);
//! \see prVertex2f
void prVertex2i(PRint x, PRint y);
#ifdef __cplusplus
} // /extern "C"
#endif
#endif