#Weird rendering issues with OpenGL

1 messages · Page 1 of 1 (latest)

noble plank
#

main.zig

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();

    try window.init();
    defer window.deinit();

    try renderer.init();
    defer renderer.deinit(allocator);

    for (0..10) |i| {
        const delta: f32 = @floatFromInt(i);
        _ = try renderer.newObject(
            allocator,
            &[_]renderer.Vertex{
                .{ .position = .{ -0.5, 0, 0.5 }, .color = .{ 1, 0, 0 }, .tex_coord = .{ 0, 0 } },
                .{ .position = .{ -0.5, 0, -0.5 }, .color = .{ 0, 1, 0 }, .tex_coord = .{ 0, 0 } },
                .{ .position = .{  0.5, 0, -0.5 }, .color = .{ 0, 0, 1 }, .tex_coord = .{ 0, 0 } },
                .{ .position = .{  0.5, 0, 0.5 }, .color = .{ 1, 1, 0 }, .tex_coord = .{ 0, 0 } },
                .{ .position = .{  0.0, 0.8, 0 }, .color = .{ 1, 1, 1 }, .tex_coord = .{ 0, 0 } },
            },
            &[_]usize{ 
                0, 1, 2,
                0, 2, 3,
                0, 1, 4,
                1, 2, 4,
                2, 3, 4,
                3, 0, 4
            },
            .{ 0, 0 + delta, 0 },
            .{ 0 + (4*delta), 0, 0 },
        );
    }

    _ = try renderer.newObject(
        allocator,
        &[_]renderer.Vertex{
            .{ .position = .{ -0.5, 0, 0.5 }, .color = .{ 0, 0, 1 }, .tex_coord = .{ 0, 0 } },
            .{ .position = .{ -0.5, 0, -0.5 }, .color = .{ 0, 0, 1 }, .tex_coord = .{ 0, 0 } },
            .{ .position = .{  0.5, 0, -0.5 }, .color = .{ 0, 0, 1 }, .tex_coord = .{ 0, 0 } },
            .{ .position = .{  0.5, 0, 0.5 }, .color = .{ 0, 0, 1 }, .tex_coord = .{ 0, 0 } },
            .{ .position = .{  0.0, 0.8, 0 }, .color = .{ 0, 0, 1 }, .tex_coord = .{ 0, 0 } },
        },
        &[_]usize{ 
            0, 1, 2,
            0, 2, 3,
            0, 1, 4,
            1, 2, 4,
            2, 3, 4,
            3, 0, 4
        },
        .{ 0, 0, 0 },
        .{ 0, 0, 0 },
    );
...
#

main.zig

    
    // temp player camera
    var cam = renderer.Camera{
        .position = .{ 0, -0.5, -5 },
        .rotation = .{ 0, 0, 0, 1 },
        .fov = 90,
        .near_plane = 0.001, // temp
        .far_plane = 10000, // temp
        .owner = null,
    };

    // update buffers
    try renderer.updateBuffers();

    // main loop
    try window.update(&cam);

    // cleanup
    try network.deinit(allocator);
}
#

updateBuffers()

pub fn updateBuffers() !void {
    const vertex_count: isize = @intCast(renderer.vertices.len);
    const index_count: isize = @intCast(renderer.indices.len);

    // not sure how performant this approach is, might change later

    // update the vertex buffer with the new vertices
    gl.BufferData(gl.ARRAY_BUFFER, @sizeOf(renderer.Vertex) * vertex_count, null, gl.DYNAMIC_DRAW);
    for (renderer.vertices.getOrder()) |i| {
        const vertex: renderer.Vertex = renderer.vertices.getAssertExists(i);
        gl.BufferSubData(gl.ARRAY_BUFFER, @sizeOf(renderer.Vertex) * @as(isize, @intCast(i)), @sizeOf(renderer.Vertex), &vertex);
    }

    // update the index buffer with the new indices
    gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, @sizeOf(u32) * index_count, null, gl.DYNAMIC_DRAW);
    for (renderer.indices.getOrder()) |i| {
        const index: usize = renderer.indices.getAssertExists(i);
        gl.BufferSubData(gl.ELEMENT_ARRAY_BUFFER, @sizeOf(u32) * @as(isize, @intCast(i)), @sizeOf(u32), &index);
    }
}
#

update() code

// setup model matrix for each mesh and draw the vertices
    for (renderer.objects.getOrder()) |i| {
        const object = renderer.objects.getAssertExists(i);

        var model: zmath.Mat = zmath.identity();
        
        // rot
        model = zmath.matFromRollPitchYaw(object.rotation[0] * (std.math.pi / 180.0), object.rotation[1] * (std.math.pi / 180.0), object.rotation[2] * (std.math.pi / 180.0)); // converts to quaternion under the hood

        // test local rotation
        model = zmath.mul(model, zmath.rotationY(delta_rotation * (std.math.pi / 180.0)));
        
        // pos
        model = zmath.mul(model, zmath.translation(object.position[0], object.position[1], object.position[2]));

        gl.UniformMatrix4fv(model_location, 1, gl.FALSE, zmath.arrNPtr(&model));
        gl.DrawElements(gl.TRIANGLES, @intCast(object.index_count), gl.UNSIGNED_INT, object.index_start * @sizeOf(usize)); // whyd the last argument change from *usize to usize????
    }
#

newObject()

pub fn newObject(allocator: std.mem.Allocator, new_vertices: []const Vertex, new_indices: []const usize, position: [3]f32, rotation: [3]f32) !Object {
    const object = Object {
        .position = position,
        .rotation = rotation,

        .vertex_start = @enumFromInt(vertices.len),
        .vertex_count = @intCast(new_vertices.len),

        .index_start = @intCast(indices.len),
        .index_count = @intCast(new_indices.len),

        .material = @enumFromInt(0), // not implemented yet
    };

    for (new_vertices) |vertex| {
        try vertices.append(allocator, vertex);
    }

    for (new_indices) |index| {
        try indices.append(allocator, index + @intFromEnum(object.vertex_start));
    }

    // add the object to the object list
    try objects.append(allocator, object);

    return object;
}
#

removeObject()

pub fn removeObject(index: usize) !void {
    const object: Object = try objects.get(index);

    for (@intFromEnum(object.vertex_start)..(@intFromEnum(object.vertex_start) + object.vertex_count)) |i| {
        try vertices.remove(i);
    }

    for (object.index_start..(object.index_start + object.index_count)) |i| {
        try indices.remove(i);
    }

    try objects.remove(index);
}
#

let me know if anything else would be helpful

#

I suspect it is most likely something to do with updateBuffers()

orchid oasis
#

gl.DrawElements(gl.TRIANGLES, @intCast(object.index_count), gl.UNSIGNED_INT, object.index_start * @sizeOf(usize));
just a guess but maybe the last part should be @sizeOf(u32) and not @sizeOf(usize)? on 64 bit it would check out with only half of everything rendering

noble plank
#

I'll try it but index_count is u32 while index_start is usize

#

same behavior

#

also another thing, I'm noticing weird behavior with how it treats the position data I input

#

for example the first 10 objects I add will gradually increase vertically by 1 unit (again, only 5 render)

#

and the 11th object (outside of the for loop, colored blue this time)'s position just doesn't matter at all

#

and will always be at the top of however many rendered (pasted image #2)