2017-05-16 12:53:28 +00:00
|
|
|
#ifndef DTRENDERER_RENDER_H
|
|
|
|
#define DTRENDERER_RENDER_H
|
|
|
|
|
|
|
|
#include "dqn.h"
|
2017-06-18 10:25:57 +00:00
|
|
|
#include "DTRendererPlatform.h"
|
2017-05-16 12:53:28 +00:00
|
|
|
|
2017-05-19 10:45:18 +00:00
|
|
|
#define DTRRENDER_INV_255 1.0f/255.0f
|
2017-05-18 16:21:44 +00:00
|
|
|
|
2017-05-16 16:49:33 +00:00
|
|
|
typedef struct DTRBitmap DTRBitmap;
|
|
|
|
|
2017-05-18 16:21:44 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utility
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2017-05-23 12:42:54 +00:00
|
|
|
typedef struct DTRRenderBuffer
|
|
|
|
{
|
|
|
|
i32 width;
|
|
|
|
i32 height;
|
|
|
|
i32 bytesPerPixel;
|
2017-06-19 04:14:13 +00:00
|
|
|
PlatformLock *renderLock;
|
|
|
|
volatile u8 *memory; // Format: XX RR GG BB, and has (width * height * bytesPerPixels) elements
|
|
|
|
volatile f32 *zBuffer; // zBuffer has (width * height) elements
|
2017-05-23 12:42:54 +00:00
|
|
|
|
2017-06-19 08:22:02 +00:00
|
|
|
volatile bool *pixelLockTable; // has (width * height) elements
|
|
|
|
|
2017-05-23 12:42:54 +00:00
|
|
|
} DTRRenderBuffer;
|
|
|
|
|
2017-06-09 05:55:58 +00:00
|
|
|
// Using transforms for 2D ignores the 'z' element.
|
2017-05-16 16:49:33 +00:00
|
|
|
typedef struct DTRRenderTransform
|
|
|
|
{
|
2017-05-18 11:33:39 +00:00
|
|
|
f32 rotation = 0; // Rotation in degrees
|
2017-06-09 05:55:58 +00:00
|
|
|
DqnV3 anchor = DqnV3_1f(0.5f); // Anchor has expected range of [0->1]
|
|
|
|
DqnV3 scale = DqnV3_1f(1.0f);
|
2017-05-16 16:49:33 +00:00
|
|
|
} DTRRenderTransform;
|
|
|
|
|
|
|
|
inline DTRRenderTransform DTRRender_DefaultTransform()
|
|
|
|
{
|
|
|
|
DTRRenderTransform result = {};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: 0.33f is the midpoint of a triangle
|
|
|
|
inline DTRRenderTransform DTRRender_DefaultTriangleTransform()
|
|
|
|
{
|
|
|
|
DTRRenderTransform result = {};
|
2017-06-09 05:55:58 +00:00
|
|
|
result.anchor = DqnV3_1f(0.33f);
|
2017-05-16 16:49:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
2017-05-16 12:53:28 +00:00
|
|
|
|
2017-05-18 16:21:44 +00:00
|
|
|
// NOTE(doyle): 1 suffix represents the range the color is being sent in which
|
|
|
|
// means all the colors should be in the range of [0-1]
|
|
|
|
inline f32 DTRRender_SRGB1ToLinearSpacef (f32 val);
|
|
|
|
inline DqnV4 DTRRender_SRGB1ToLinearSpaceV4(DqnV4 color);
|
|
|
|
inline f32 DTRRender_LinearToSRGB1Spacef (f32 val);
|
|
|
|
inline DqnV4 DTRRender_LinearToSRGB1SpaceV4(DqnV4 color);
|
|
|
|
|
|
|
|
// Takes SRGB in [0->1], converts to linear space, premultiplies alpha and returns
|
|
|
|
// color back in SRGB space.
|
|
|
|
inline DqnV4 DTRRender_PreMultiplyAlphaSRGB1WithLinearConversion(DqnV4 color);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Rendering
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2017-06-08 11:46:03 +00:00
|
|
|
enum DTRRenderShadingMode
|
|
|
|
{
|
|
|
|
DTRRenderShadingMode_FullBright,
|
|
|
|
DTRRenderShadingMode_Flat,
|
|
|
|
DTRRenderShadingMode_Gouraud,
|
|
|
|
};
|
|
|
|
|
2017-06-09 05:55:58 +00:00
|
|
|
// DTRRenderLight is used for specifying an individual light for illuminating meshes.
|
|
|
|
// vector: The light direction, it does NOT need to be normalised. Will be normalise for you.
|
2017-06-08 11:46:03 +00:00
|
|
|
typedef struct DTRRenderLight
|
|
|
|
{
|
|
|
|
enum DTRRenderShadingMode mode;
|
2017-06-09 05:55:58 +00:00
|
|
|
DqnV3 vector;
|
|
|
|
DqnV4 color;
|
2017-06-08 11:46:03 +00:00
|
|
|
} DTRRenderLight;
|
|
|
|
|
2017-06-18 13:07:49 +00:00
|
|
|
typedef struct DTRRenderContext
|
|
|
|
{
|
2017-06-19 08:22:02 +00:00
|
|
|
DTRRenderBuffer *renderBuffer;
|
|
|
|
DqnMemStack *tempStack;
|
|
|
|
PlatformAPI *api;
|
|
|
|
PlatformJobQueue *jobQueue;
|
2017-06-19 16:13:03 +00:00
|
|
|
|
|
|
|
bool multithread;
|
2017-06-18 13:07:49 +00:00
|
|
|
} DTRRenderContext;
|
|
|
|
|
2017-06-09 05:55:58 +00:00
|
|
|
// NOTE: All colors should be in the range of [0->1] where DqnV4 is a struct with 4 floats, rgba
|
|
|
|
// Leaving len = -1 for text will make the system use strlen to determine len.
|
2017-06-18 13:07:49 +00:00
|
|
|
void DTRRender_Text (DTRRenderContext context, const DTRFont font, DqnV2 pos, const char *const text, DqnV4 color = DqnV4_1f(1), i32 len = -1);
|
|
|
|
void DTRRender_Line (DTRRenderContext context, DqnV2i a, DqnV2i b, DqnV4 color);
|
|
|
|
void DTRRender_Rectangle (DTRRenderContext context, DqnV2 min, DqnV2 max, DqnV4 color, const DTRRenderTransform transform = DTRRender_DefaultTransform());
|
|
|
|
void DTRRender_Mesh (DTRRenderContext context, PlatformJobQueue *const jobQueue, DTRMesh *const mesh, DTRRenderLight lighting, const DqnV3 pos, const DTRRenderTransform transform);
|
|
|
|
void DTRRender_Triangle (DTRRenderContext context, DqnV3 p1, DqnV3 p2, DqnV3 p3, DqnV4 color, const DTRRenderTransform transform = DTRRender_DefaultTriangleTransform());
|
|
|
|
void DTRRender_TexturedTriangle(DTRRenderContext context, DqnV3 p1, DqnV3 p2, DqnV3 p3, DqnV2 uv1, DqnV2 uv2, DqnV2 uv3, DTRBitmap *const texture, DqnV4 color, const DTRRenderTransform transform = DTRRender_DefaultTriangleTransform());
|
|
|
|
void DTRRender_Bitmap (DTRRenderContext context, DTRBitmap *const bitmap, DqnV2 pos, const DTRRenderTransform transform = DTRRender_DefaultTransform(), DqnV4 color = DqnV4_4f(1, 1, 1, 1));
|
|
|
|
void DTRRender_Clear (DTRRenderContext context, DqnV3 color);
|
2017-05-16 12:53:28 +00:00
|
|
|
|
|
|
|
#endif
|