Skip to content

Instantly share code, notes, and snippets.

View XProger's full-sized avatar

Timur Gagiev XProger

View GitHub Profile
@XProger
XProger / png.pas
Created August 26, 2014 06:35
Load static PNG image (gray, 8- 16- 24- 32-bit, no animation, no interlace)
function LoadPNG(const Stream: TStream; out Data: PByteArray; out Width, Height: LongInt): Boolean;
const
IHDR = $52444849;
IDAT = $54414449;
IEND = $444E4549;
PLTE = $45544C50;
tRNS = $534E5274;
var
i, j : LongInt;
Bits : Byte;
@XProger
XProger / jpeg.pas
Created August 26, 2014 06:44
Load baseline JPEG image (gray & rgb)
function LoadJPG(const Stream: TStream; out Data: PByteArray; out Width, Height: LongInt): Boolean;
type
THuffmanTable = record
Bits : array [0..15] of Byte;
HVal : array [Byte] of Byte;
Size : array [Byte] of Byte;
Code : array [Byte] of Word;
end;
TQTable = array [0..63] of Single;
@XProger
XProger / dds.pas
Last active August 29, 2015 14:05
DDS texture loader (2d, cubemap, OpenGL)
constructor TTexture.Create(const FileName: string);
type
TLoadFormat = (lfNULL, lfDXT1c, lfDXT1a, lfDXT3, lfDXT5, lfA8, lfL8, lfAL8,
lfBGRA8, lfBGR8, lfBGR5A1, lfBGR565, lfBGRA4, lfR16F, lfR32F,
lfGR16F, lfGR32F, lfBGRA16F, lfBGRA32F);
TDDS = record
dwMagic : LongWord;
dwSize : LongInt;
dwFlags : LongWord;
dwHeight : LongWord;
@XProger
XProger / vec3.pas
Created August 26, 2014 07:08
Simple float3 vector operations
TVec3f = {$IFDEF FPC} object {$ELSE} record {$ENDIF}
x, y, z : Single;
{$IFNDEF FPC}
class operator Equal(const a, b: TVec3f): Boolean;
class operator Add(const a, b: TVec3f): TVec3f;
class operator Subtract(const a, b: TVec3f): TVec3f;
class operator Multiply(const a, b: TVec3f): TVec3f;
class operator Multiply(const v: TVec3f; x: Single): TVec3f;
{$ENDIF}
function Dot(const v: TVec3f): Single;
@XProger
XProger / quat.pas
Created August 26, 2014 07:13
Quaternion & Dual Quaternion math
TQuat = {$IFDEF FPC} object {$ELSE} record {$ENDIF}
x, y, z, w : Single;
{$IFNDEF FPC}
class operator Equal(const q1, q2: TQuat): Boolean;
class operator Add(const q1, q2: TQuat): TQuat;
class operator Subtract(const q1, q2: TQuat): TQuat;
class operator Multiply(const q: TQuat; x: Single): TQuat;
class operator Multiply(const q1, q2: TQuat): TQuat;
class operator Multiply(const q: TQuat; const v: TVec3f): TVec3f;
{$ENDIF}
@XProger
XProger / mat4.pas
Created August 26, 2014 07:18
Simple float4x4 matrix operations
TMat4f = {$IFDEF FPC} object {$ELSE} record {$ENDIF}
private
function GetPos: TVec3f; // https://gist.github.com/XProger/da9a74ae8b37905b421a
procedure SetPos(const v: TVec3f);
function GetRot: TQuat; // https://gist.github.com/XProger/def254d40a237cc0f0b2
procedure SetRot(const q: TQuat);
public
e00, e10, e20, e30,
e01, e11, e21, e31,
e02, e12, e22, e32,
@XProger
XProger / blend.cpp
Created October 14, 2014 20:33
Fast integer alpha blending
uint32 blend(uint32 color1, uint32 color2, uint8 alpha) {
uint32 rb = color1 & 0xff00ff;
uint32 g = color1 & 0x00ff00;
rb += ((color2 & 0xff00ff) - rb) * alpha >> 8;
g += ((color2 & 0x00ff00) - g) * alpha >> 8;
return (rb & 0xff00ff) | (g & 0xff00);
}
@XProger
XProger / lines_count_regexp.txt
Last active September 15, 2015 20:34
lines count regexp
\S+\s*\r\n
mode out color out alpha
-------------- -------------- -----------
layer/over: ( sc+(1-sa)*dc , sa+da-sa*da )
multiply: ( sc*dc , sa+da-sa*da )
screen: ( sa*da - (da-dc)*(sa-sc) , sa+da-sa*da )
lighten: ( max(sa*dc,sc*da) , sa+da-sa*da )
darken: ( min(sa*dc,sc*da) , sa+da-sa*da )
add: ( min(dc+sc,1) , min(sa+da,1) )
subtract: ( max(dc-sc,0) , min(sa+da,1) )
difference: ( abs(sa*dc-sc*da) , sa+da-sa*da )
@XProger
XProger / powf_fast.c
Last active January 10, 2024 19:33
fast powf function (IEEE-754 hack)
float powf_fast(float a, float b) {
union { float d; int x; } u = { a };
u.x = (int)(b * (u.x - 1064866805) + 1064866805);
return u.d;
}