Working on math libraries

This commit is contained in:
Krzosa Karol
2022-10-13 12:33:16 +02:00
parent 6e8acf7dc8
commit 022f874c32
8 changed files with 58 additions and 42 deletions

View File

@@ -5,6 +5,6 @@ rem cl main.cpp -I.. user32.lib
clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib
rem ubuntu run clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o core.out
main examples/arms_race/arms_race.core
rem main examples/arms_race/arms_race.core
popd

View File

@@ -5,6 +5,9 @@ Current:
- [ ] Maybe wait to implement WASM / bytecode emitter before doing this?
Probably need some UInt Int types and those should be default, target
decides the size of these
- [ ] Fix invalid error message: AlmostLinearToSRGB :: (a: Color);; return {sqrtf(a.r), sqrtf(a.g), sqrtf(a.b), a.a}
- [ ] Fix untyped literal going to codegen stage, example in arms_race
- [ ] Fix and decide what to do when initializing global variable using not constants
- [ ] Fix adressing void is possible, maybe make it possible to address void
using bytes
* a.void + 10
@@ -25,6 +28,7 @@ In the future
- [ ] Add ability to do i: int = 0 inside for loops for i: int = 0, i < 10, i+=1
- [ ] Complicated c declaration generation
- [ ] Other kinds of casts, a cast from structs of same layout, a cast without conversion
- [ ] Expand macros
- [ ] Defer

View File

@@ -1,6 +1,6 @@
#import "Math.core"
V3 :: #import "MathVec3.core"
Vec3 :: V3.Vec3
F :: #import "MathF32.core"
V3 :: #import "MathVec3.core"; Vec3 :: V3.Vec3
V2 :: #import "MathVec2.core"; Vec2 :: V2.Vec2
Epsilon :: 0.00001
Screen : *U32
@@ -18,8 +18,8 @@ Raymarcher_Update :: ()
forward := Vec3{0, 0, -1}
side := V3.Normalize(V3.Cross(forward, up))
LightPos.x = cosf(TotalTime->F32)*4
LightPos.y = sinf(TotalTime->F32)*4
LightPos.x = F.Cos(TotalTime->F32)*4
LightPos.y = F.Sin(TotalTime->F32)*4
ambient_color := Vec3{0.2,0.2,0.2}
diffuse_color := Vec3{0.7,0.2,0.2}
@@ -77,9 +77,9 @@ Raymarcher_Update :: ()
color = color * light_intensity
// Gamma correction
color.x = sqrtf(color.x)
color.y = sqrtf(color.y)
color.z = sqrtf(color.z)
color.x = F.SquareRoot(color.x)
color.y = F.SquareRoot(color.y)
color.z = F.SquareRoot(color.z)
Screen[x + y*X] = V3.ConvertToARGB(color)
else;; Screen[x + y*X] = 0
@@ -126,7 +126,7 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS
}
Assert(RegisterClassW(&w) != 0)
screen_size: Vec2I = {1280, 720}
screen_size: V2.Vec2I = {1280, 720}
window := CreateWindowExW(
dwExStyle = 0, hWndParent = 0, hMenu = 0, lpParam = 0,
X = CW_USEDEFAULT, Y = CW_USEDEFAULT, nWidth = screen_size.x->int, nHeight = screen_size.y->int,
@@ -179,12 +179,12 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS
Windows_Bitmap :: struct
size: Vec2I
size: V2.Vec2I
data: *U32
hdc: HDC
dib: HBITMAP
CreateBitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap
CreateBitmap :: (size: V2.Vec2I, bottom_up: Bool = true): Windows_Bitmap
result: Windows_Bitmap = {size = size}
if bottom_up == false
result.size.y = -result.size.y

34
modules/MathF32.core Normal file
View File

@@ -0,0 +1,34 @@
sqrtf :: #foreign (value: F32): F32
cosf :: #foreign (value: F32): F32
sinf :: #foreign (value: F32): F32
floorf :: #foreign (value: F32): F32
roundf :: #foreign (value: F32): F32
ceilf :: #foreign (value: F32): F32
Floor :: floorf
Round :: roundf
Ceil :: ceilf
SquareRoot :: sqrtf
Cos :: cosf
Sin :: sinf
Clamp :: (min: F32, value: F32, max: F32): F32
if value > max;; return max
if value < min;; return min
return value
ClampBottom :: (min: F32, value: F32): F32
if value < min;; return min
return value
Absolute :: (val: F32): F32
if val < 0;; return -val
return val
Min :: (a: F32, b: F32): F32
if a > b ;; return b
return a
Max :: (a: F32, b: F32): F32
if a > b ;; return a
return b

View File

@@ -1,10 +1,3 @@
sqrtf :: #foreign (value: F32): F32
cosf :: #foreign (value: F32): F32
sinf :: #foreign (value: F32): F32
floorf :: #foreign (value: F32): F32
roundf :: #foreign (value: F32): F32
ceilf :: #foreign (value: F32): F32
Vec2I :: struct;; x: S64; y: S64
Vec2 :: struct;; x: F32; y: F32
@@ -37,21 +30,3 @@ Vec2 :: struct;; x: F32; y: F32
FloorVec2ToVec2I :: (a: Vec2): Vec2I ;; return {floorf(a.x)->S64, floorf(a.y)->S64}
CastVec2ToVec2I :: (a: Vec2): Vec2I ;; return {a.x->S64, a.y->S64}
F32_Clamp :: (min: F32, value: F32, max: F32): F32
if value > max;; return max
if value < min;; return min
return value
F32_ClampBottom :: (min: F32, value: F32): F32
if value < min;; return min
return value
F32_Absolute :: (val: F32): F32
if val < 0;; return -val
return val
F32_Min :: (a: F32, b: F32): F32
if a > b ;; return b ; return a
F32_Max :: (a: F32, b: F32): F32
if a > b ;; return a ; return b

View File

@@ -1,4 +1,4 @@
#import "Math.core"
#import "MathF32.core"
Vec3 :: struct ;; x: F32; y: F32; z: F32
Length :: (a: Vec3): F32 ;; return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z)
@@ -36,9 +36,9 @@ Reflect :: (a: Vec3, normal: Vec3): Vec3
return result
ConvertToARGB :: (a: Vec3): U32
a.x = F32_Clamp(0, a.x, 1)
a.y = F32_Clamp(0, a.y, 1)
a.z = F32_Clamp(0, a.z, 1)
a.x = Clamp(0, a.x, 1)
a.y = Clamp(0, a.y, 1)
a.z = Clamp(0, a.z, 1)
r := (a.x * 255)->U32 << 16
g := (a.y * 255)->U32 << 8
b := (a.z * 255)->U32 << 0

View File

@@ -58,6 +58,7 @@ Mouse :: struct
wheel: S64
#import "Base.core"
#import "Math.core"
#import "MathF32.core"
#import "MathVec2.core"
#import "Arena.core"
#load "$os_multimedia.core"

View File

@@ -163,6 +163,7 @@ UpdateMultimedia :: (): Bool
Mu.window.y = size.y
Mu.window.sizef.x = Mu.window.x->F32
Mu.window.sizef.y = Mu.window.y->F32
Mu.window.size = size
Mu.frame_count += 1
@@ -186,6 +187,7 @@ UpdateMultimedia :: (): Bool
WindowProc :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT
result: LRESULT
if msg == WM_DESTROY
// @todo: Add destroy window
PostQuitMessage(0)
return 0
elif msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN