g3d.jamo (built in)

g3d: 3D drawing with a camera, lights, fog, textures and meshes, on top of gfx. use "g3d.jamo"

g3d draws into gfx's picture, which has a depth buffer, so 2D drawing like text can go on top afterward. It switches OpenGL to its own settings when it draws, and gfx switches them back. A mesh is a list of triangles, each corner 8 floats: place x, y and z, the normal's x, y and z, and the texture spot's u and v. Matrices are 16 32-bit floats, a column at a time, as OpenGL takes them.

structVec3

struct Vec3
    x: float
    y: float
    z: float
end

structMesh

struct Mesh
    vertex_array: int
    buffer: int
    count: int
    open: bool
end

structTexture

struct Texture
    id: int
    width: int
    height: int
    open: bool
end

structG3d

struct G3d
    gfx: Gfx
    shader: int
    places: [int]
    eye: Vec3
    target: Vec3
    field: float
    near: float
    far: float
    sun_direction: Vec3
    sun_color: Color
    ambient_color: Color
    fog_color: Color
    fog_start: float
    fog_end: float
    fog_on: bool
    lamp_places: [Vec3]
    lamp_colors: [Color]
    lamp_reaches: [float]
    changed: bool
    seen_width: int
    seen_height: int
    matrices: int
    white: Texture
    meshes: [Mesh]
    textures: [Texture]
    open: bool
    closed_by_program: bool
end

funcvec3

func vec3(x: float, y: float, z: float): Vec3

funcstart_3d

func start_3d(g: Gfx): G3d

funcG3d.must_be_open

func G3d.must_be_open()

funcG3d.enter

func G3d.enter()

Switches OpenGL to g3d's settings, after drawing what gfx has waiting

funcG3d.camera

func G3d.camera(eye: Vec3, target: Vec3)

Puts the camera at eye, looking at target

funcG3d.lens

func G3d.lens(field: float, near: float, far: float)

How wide the camera sees, in degrees, and the nearest and farthest it draws

funcG3d.sun

func G3d.sun(direction: Vec3, color: Color)

Sunlight shining in a direction, like vec3(0.0, -1.0, 0.0) for straight down

funcG3d.ambient

func G3d.ambient(color: Color)

The light that reaches everything, even in shade

funcG3d.lamp

func G3d.lamp(number: int, place: Vec3, color: Color, reach: float)

One of 4 lamps, 0 to 3, lighting things nearer than reach

funcG3d.lamp_off

func G3d.lamp_off(number: int)

funcG3d.fog

func G3d.fog(color: Color, start: float, end_at: float)

Things fade into the fog color from start to end, counting from the camera

funcG3d.no_fog

func G3d.no_fog()

funcG3d.send_scene

func G3d.send_scene()

Sends the camera, lights and fog to the graphics card, when they've changed

funcG3d.draw

func G3d.draw(mesh: Mesh, place: Vec3, turn: Vec3, size: Vec3, color: Color)

Draws a mesh at a place, turned by degrees around x, y and z, and stretched by size

funcG3d.draw_textured

func G3d.draw_textured(mesh: Mesh, texture: Texture, place: Vec3, turn: Vec3, size: Vec3)

funcG3d.draw_with

func G3d.draw_with(mesh: Mesh, texture: Texture, place: Vec3, turn: Vec3, size: Vec3, color: Color)

funcg3d_set

func g3d_set(matrix: int, row: int, column: int, value: float)

funcg3d_get

func g3d_get(matrix: int, row: int, column: int): float

funcg3d_clear

func g3d_clear(matrix: int)

funcg3d_multiply

func g3d_multiply(result: int, a: int, b: int)

funcg3d_normalize

func g3d_normalize(v: Vec3): Vec3

funcg3d_cross

func g3d_cross(a: Vec3, b: Vec3): Vec3

funcg3d_look

func g3d_look(matrix: int, eye: Vec3, target: Vec3)

The camera's matrix. Looking straight up or down, the top of the screen points along -z or +z

funcg3d_perspective

func g3d_perspective(matrix: int, field: float, aspect: float, near: float, far: float)

funcg3d_model

func g3d_model(model: int, normals: int, place: Vec3, turn: Vec3, size: Vec3)

Moves, turns (around y, then x, then z) and stretches. The normals turn the same way but stretch the other way, so lighting stays right on stretched meshes

funcg3d_column

func g3d_column(model: int, normals: int, column: int, x: float, y: float, z: float, stretch: float)

One turned column of both matrices, stretched one way for the model and the other for normals

funcG3d.mesh

func G3d.mesh(points: [float]): Mesh

A mesh from triangles: every 3 corners make one, and each corner is 8 floats (see the top)

funcG3d.make_mesh

func G3d.make_mesh(data: int, count: int): Mesh

funcMesh.close

func Mesh.close()

funcg3d_corner

func g3d_corner(points: [float], x: float, y: float, z: float, nx: float, ny: float, nz: float, u: float, v: float)

funcg3d_side

func g3d_side(points: [float], c: Vec3, a: Vec3, b: Vec3, n: Vec3)

One side of a cube, as two triangles: a corner, then the two directions across the side

funcG3d.cube

func G3d.cube(): Mesh

A cube 1 wide, centered on its place

funcG3d.plane

func G3d.plane(tiles: float): Mesh

A flat square 1 wide on the ground, facing up, with its texture repeated tiles times each way

funcG3d.sphere

func G3d.sphere(detail: int): Mesh

A ball 1 wide. More detail makes it rounder, from 3 to 64

funcG3d.load_texture

func G3d.load_texture(path: string): Texture

Loads a PNG picture to wrap onto meshes. It repeats past the edges

funcG3d.make_texture

func G3d.make_texture(pixels: int, width: int, height: int): Texture

funcTexture.close

func Texture.close()

funcg3d_bad_obj

func g3d_bad_obj(path: string, line: int, why: string)

funcg3d_number

func g3d_number(data: string, place: int, finish: int, path: string, line: int): float

Reads a number written from place up to finish, like 12, -0.5 or 1.5e-3

funcg3d_part

func g3d_part(words: string, from: int, to: int): string

funcg3d_index

func g3d_index(data: string, place: int, finish: int, count: int, kind: string, path: string, line: int): int

One face corner's number for a list of this many, counting from 1, or from the end when below 0

funcG3d.load_obj

func G3d.load_obj(path: string): Mesh

Reads an OBJ model: its points (v), texture spots (vt), normals (vn) and faces (f). Faces with more than 3 corners are cut into triangles, and faces without normals get flat ones

funcG3d.close

func G3d.close()