dummy pixelbuffer displaying with raylib
This commit is contained in:
parent
254cb069a3
commit
9c4c59e073
66
src/main.odin
Normal file
66
src/main.odin
Normal file
@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import rl "vendor:raylib"
|
||||
import "core:fmt"
|
||||
|
||||
|
||||
WINDOW_WIDTH :: 1280
|
||||
WINDOW_HEIGHT :: 720
|
||||
|
||||
rl_window_loop :: proc() {
|
||||
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Rayt");
|
||||
defer rl.CloseWindow()
|
||||
|
||||
|
||||
rl_image := rl.Image {
|
||||
data = raw_data(pixelbuffer_rgb),
|
||||
width = cast(i32)image.width,
|
||||
height = cast(i32)image.height,
|
||||
mipmaps = 1,
|
||||
format = .UNCOMPRESSED_R8G8B8
|
||||
}
|
||||
defer rl.UnloadImage(rl_image)
|
||||
fmt.println("Created raylib image from rgb data")
|
||||
|
||||
|
||||
texture := rl.LoadTextureFromImage(rl_image)
|
||||
defer rl.UnloadTexture(texture)
|
||||
fmt.println("Loaded texture from image")
|
||||
//rl_window_loop(texture)
|
||||
|
||||
|
||||
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
|
||||
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.BLUE)
|
||||
|
||||
rl.DrawTexture(texture, 0, 0, rl.WHITE)
|
||||
|
||||
rl.DrawCircle(400, 300, 50, rl.GREEN)
|
||||
rl.DrawLine(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, rl.BLUE)
|
||||
|
||||
|
||||
rl.DrawCircle(100, 100, 120, rl.RED)
|
||||
|
||||
|
||||
rl.EndDrawing()
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
|
||||
// Fill pixelbuffer with raytraced image.
|
||||
rayt_cpu_main()
|
||||
|
||||
fmt.println("Finished raytracing, launching Raylib window")
|
||||
|
||||
rl_window_loop()
|
||||
}
|
||||
166
src/rayt_base.odin
Normal file
166
src/rayt_base.odin
Normal file
@ -0,0 +1,166 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:math/rand"
|
||||
|
||||
RAND_SEED :: 1984
|
||||
|
||||
// Global program parameters
|
||||
IMAGE_WIDTH :: 1280
|
||||
ASPECT_RATIO :: 1.7778 // 16:9
|
||||
|
||||
MAX_NUM_ENTITIES :: 64
|
||||
|
||||
Vec3 :: distinct [3]f32
|
||||
|
||||
Image :: struct {
|
||||
width : u32,
|
||||
height : u32,
|
||||
aspect_ratio : f32
|
||||
}
|
||||
image: Image
|
||||
|
||||
|
||||
Camera :: struct {
|
||||
center : Vec3,
|
||||
up : Vec3,
|
||||
focal_length : f32,
|
||||
}
|
||||
camera: Camera
|
||||
|
||||
Viewport :: struct {
|
||||
width : f32,
|
||||
height : f32,
|
||||
aspect_ratio : f32,
|
||||
u : Vec3,
|
||||
v : Vec3,
|
||||
upper_left : Vec3,
|
||||
pixel_origin : Vec3,
|
||||
pixel_delta_u : Vec3,
|
||||
pixel_delta_v : Vec3,
|
||||
}
|
||||
viewport: Viewport
|
||||
|
||||
HitRecord :: struct {
|
||||
point : Vec3,
|
||||
normal : Vec3,
|
||||
t : f32,
|
||||
hit : b32,
|
||||
front_face : b32,
|
||||
}
|
||||
|
||||
EntityKind :: enum {
|
||||
Tri
|
||||
}
|
||||
|
||||
Entity :: struct {
|
||||
kind: EntityKind,
|
||||
center: Vec3,
|
||||
v0: Vec3,
|
||||
v1: Vec3,
|
||||
v2: Vec3,
|
||||
}
|
||||
entities: [MAX_NUM_ENTITIES]Entity
|
||||
|
||||
pixelbuffer: []Vec3
|
||||
pixelbuffer_rgb: []u8
|
||||
|
||||
vec3_rand_uniform :: proc() -> Vec3 {
|
||||
return Vec3{rand.float32(), rand.float32(), rand.float32()}
|
||||
}
|
||||
|
||||
rayt_cpu_main :: proc() {
|
||||
rand.reset(RAND_SEED)
|
||||
|
||||
// Random triangles
|
||||
{
|
||||
center_shift := Vec3{5.0, 5.0, 5.0}
|
||||
// Generate triangles inside a box
|
||||
for i in 0..<MAX_NUM_ENTITIES {
|
||||
r0 := vec3_rand_uniform();
|
||||
r1 := vec3_rand_uniform();
|
||||
r2 := vec3_rand_uniform();
|
||||
|
||||
// Put the first vertex within a 10x10x10 cube centered at the origin
|
||||
v0 := 9.0*r0
|
||||
v0 = v0 - center_shift
|
||||
|
||||
entities[i].kind = EntityKind.Tri
|
||||
entities[i].v0 = v0
|
||||
entities[i].v1 = v0 + r1
|
||||
entities[i].v2 = v0 + r2
|
||||
entities[i].center = 0.3333*(entities[i].v0 + entities[i].v1 + entities[i].v2)
|
||||
}
|
||||
}
|
||||
|
||||
// Set up scene globals
|
||||
{
|
||||
image.width = IMAGE_WIDTH
|
||||
image.aspect_ratio = ASPECT_RATIO
|
||||
image.height = cast(u32)(cast(f32)image.width/image.aspect_ratio) + 1
|
||||
fmt.printf("Preparing image with (w,h,ratio): (%i, %i, %.4f) \n",
|
||||
image.width, image.height, image.aspect_ratio)
|
||||
|
||||
camera.focal_length = 3.0
|
||||
camera.center = Vec3{0.0, 0.0, 18.0}
|
||||
|
||||
viewport.height = 2.0
|
||||
viewport.width = viewport.height * cast(f32)(image.width)/cast(f32)(image.height)
|
||||
viewport.aspect_ratio = viewport.width/viewport.height
|
||||
viewport.u = Vec3{viewport.width, 0.0, 0.0}
|
||||
viewport.v = Vec3{0.0, -viewport.height, 0.0}
|
||||
|
||||
width_inverse := 1.0/cast(f32)image.width
|
||||
height_inverse := 1.0/cast(f32)image.height
|
||||
viewport.pixel_delta_u = width_inverse * viewport.u
|
||||
viewport.pixel_delta_v = height_inverse * viewport.v
|
||||
|
||||
upper_left := camera.center - Vec3{0.0, 0.0, camera.focal_length}
|
||||
upper_left = upper_left - 0.5*(viewport.u) - 0.5*(viewport.v)
|
||||
viewport.upper_left = upper_left
|
||||
|
||||
viewport.pixel_origin = upper_left + 0.5 * (viewport.pixel_delta_u + viewport.pixel_delta_v)
|
||||
fmt.printf("Viewport size %.2f x %.2f, aspect ratio: %.4f \n",
|
||||
viewport.width, viewport.height, viewport.aspect_ratio)
|
||||
}
|
||||
|
||||
// Allocate pixelbuffer array
|
||||
num_pixels := image.width * image.height
|
||||
pixelbuffer = make([]Vec3, num_pixels);
|
||||
pixelbuffer_rgb = make([]u8, num_pixels * 3) // rgb values for each pixel
|
||||
|
||||
// Temp fill pixels
|
||||
{
|
||||
for x in 0..<image.width {
|
||||
for y in 0..<image.height {
|
||||
pixel_idx := y * image.width + x
|
||||
|
||||
x_val := cast(f32)x/cast(f32)image.width
|
||||
y_val := cast(f32)y/cast(f32)image.height
|
||||
pixelbuffer[pixel_idx] = Vec3{x_val, y_val, 0.0}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Translate pixelbuffer with colors from 0 to 1, to rgb 0..255
|
||||
{
|
||||
for x in 0..<image.width {
|
||||
for y in 0..<image.height {
|
||||
pixel_idx := y * image.width + x
|
||||
rgb_idx := pixel_idx * 3
|
||||
r := pixelbuffer[pixel_idx][0]
|
||||
g := pixelbuffer[pixel_idx][1]
|
||||
b := pixelbuffer[pixel_idx][2]
|
||||
|
||||
pixelbuffer_rgb[rgb_idx + 0] = u8(255.999 * r)
|
||||
pixelbuffer_rgb[rgb_idx + 1] = u8(255.999 * g)
|
||||
pixelbuffer_rgb[rgb_idx + 2] = u8(255.999 * b)
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
timeBuild.ctm
BIN
timeBuild.ctm
Binary file not shown.
Loading…
Reference in New Issue
Block a user