#Implementing models properly

1 messages · Page 1 of 1 (latest)

grizzled yacht
#

Would this be the correct way to implement "models" ```rs
package lvo_models

LVO_Model :: struct {
vertices: [][]f32,
tex_coords: [][]f32,
shading: [][]f32,
is_cube: b32,
transparent: b32,
}

```rs
package lvo_models

LVO_CUBE_MODEL :: LVO_Model {
    is_cube = true,
    transparent = false,
    vertices = {
        {0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5},
                 //....
        }
// ...tex coords, and shading values below this
}

i am retrieving the data as such

create_lvo_block_type :: proc(
    name := "unknown",
    texture_manager: ^LVO_Texture_Manager,
    block_face_textures: map[string]string,
    model: models.LVO_Model = models.LVO_CUBE_MODEL,
) -> LVO_Block_Type {
    block_type := LVO_Block_Type {
        name             = name,
        vertex_positions = slice.clone(model.vertices),
        tex_coords       = slice.clone(model.tex_coords),
        shading_values   = slice.clone(model.shading),
        transparent      = model.transparent,
        is_cube          = model.is_cube,
    }
      // set block faces, etc...
    return block_type
}
#

it seems that they are garbage values

#

if you guys want to take a closer look at the repo i will push it then post the link here

bitter cave
#

Garbage in what way?

grizzled yacht
#

but when i change the lines for setting the vertices, tex_coords, and shading values to the constants it functions properly, like this.

bitter cave
#

Change the lines meaning the order it's defined in the struct?

grizzled yacht
#
block_type := LVO_Block_Type {
        name             = name,
        vertex_positions = slice.clone(CUBE_VERTEX_POSITIONS),
        tex_coords       = slice.clone(CUBE_TEX_COORDS),
        shading_values   = slice.clone(CUBE_SHADING),
        transparent      = model.transparent,
        is_cube          = model.is_cube,
    }
#

these

#

scroll up to see the original code

#

those constants look like this

CUBE_VERTEX_POSITIONS := [][]f32{
    {0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5},
    {-0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5},
    {0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5},
    {-0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5, 0.5},
    {-0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5},
    {0.5, 0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5},
}
bitter cave
#

Hmm, hard to say at first glance. Can you step through the debugger and see when the data is being corrupted?

grizzled yacht
#

sure

bitter cave
#

What if you don't clone and just take the constant slice does it still work?

grizzled yacht
#

does odin not have a built in debugger for vscode?

grizzled yacht
bitter cave
#

It's llvm so

#

You can use the lldb extension

#

For vscode

bitter cave
#

I'm guessing that struct gets mutated later on in the program?

grizzled yacht
#

actually

#

it just crashes

#

index out of range

bitter cave
#

That's probably related to the bug

grizzled yacht
#

this is when it tries to access the vertex positions

bitter cave
#

Okay yeah if you can get the debugger working you'll probably be able to find it very quickly

grizzled yacht
#

im attempting

#

...

bitter cave
#

I suspect something with the order you're doing is corrupting the data

#

And changing the order just results in something else being corrupted

grizzled yacht
#

the block type is not null, but the arrays are null when accessing it

#

when i use slice.clone it is an invalid address

bitter cave
#

Is block types an array or a map of some kind?

grizzled yacht
#
LVO_World :: struct {
    texture_manager: LVO_Texture_Manager,
    chunks:          map[la.Vector3f32]^LVO_Chunk,
    block_types:     [dynamic]LVO_Block_Type,
}
bitter cave
#

Yeah you're probably accessing a block that doesn't exist

#

In the map

grizzled yacht
#

thats chunks

bitter cave
#

You can get a bool from the map

grizzled yacht
#

block_types is a dynamic array of the struct LVO_Block_type

bitter cave
#

Ah block type yes sorry

grizzled yacht
# grizzled yacht

and block_types is being accessed properly as you can see, the name is of length 5

bitter cave
#

Yep

grizzled yacht
#

its just the arrays are null, which leads back to

block_type := LVO_Block_Type {
        name             = name,
        vertex_positions = slice.clone(model.vertices),
        tex_coords       = slice.clone(model.tex_coords),
        shading_values   = slice.clone(model.shading),
        transparent      = model.transparent,
        is_cube          = model.is_cube,
    }
#

but the problem is, nothing is being dropped, no pointers

#

afaik

bitter cave
#

I believe clone will return an empty slice if it fails to allocate

grizzled yacht
#

why would it fail?

bitter cave
#

Many reasons, out of memory, nil allocator, etc

grizzled yacht
naive cradle
# grizzled yacht Would this be the correct way to implement "models" ```rs package lvo_models LV...

That's because those INNER slices get instantiated on the stack, and therefore not valid.

And slice.clone only clones the outer slices.

You can use this procedure to do the double clone BUT why are you trying to make it very very generic? Why not specify the field names?:

double_slice_clone_f32 :: proc(x: [][]f32) -> [][]f32 {
    res := make([][]f32, len(x))
    for _, i in x {
        res[i] = slice.clone(x[i])
    }
    return res
}