Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save attic-stuff/0f32a7c97e20920f3eb848413e000921 to your computer and use it in GitHub Desktop.

Select an option

Save attic-stuff/0f32a7c97e20920f3eb848413e000921 to your computer and use it in GitHub Desktop.
convert a vertex buffer into an obj file specifically for crocotile3d
function threedee_write_buffer_to_crocotile_obj(raw_vbo_buffer, name) {
static read_vertex = function(raw_buffer) {
var x_position = buffer_read(raw_buffer, buffer_f32);
var y_position = buffer_read(raw_buffer, buffer_f32);
var z_position = buffer_read(raw_buffer, buffer_f32);
var x_normal = buffer_read(raw_buffer, buffer_f32);
var y_normal = buffer_read(raw_buffer, buffer_f32);
var z_normal = buffer_read(raw_buffer, buffer_f32);
var r_color = buffer_read(raw_buffer, buffer_u8) / 255;
var g_color = buffer_read(raw_buffer, buffer_u8) / 255;
var b_color = buffer_read(raw_buffer, buffer_u8) / 255;
var a_color = ((buffer_read(raw_buffer, buffer_u8) / 255) * 100) / 100;
var u_texture = buffer_read(raw_buffer, buffer_f32);
var v_texture = buffer_read(raw_buffer, buffer_f32);
var v = $"v {string_format_trimmed(x_position, 20, 20)} {string_format_trimmed(y_position, 20, 20)} {string_format_trimmed(z_position, 20, 20)} {string_format(r_color, 1, 20)} {string_format(g_color, 1, 20)} {string_format(b_color, 1, 20)} {string_format(a_color, 1, 20)}";
var vt = $"vt {string_format(u_texture, 1, 20)} {string_format(v_texture, 1, 20)}";
var vn = $"vn {string_format(x_normal, 1, 20)} {string_format(y_normal, 1, 20)} {string_format(z_normal, 1, 20)}";
return { v, vt, vn };
}
var vertex_positions = [];
var vertex_texture_coordinates = [];
var vertex_normals = [];
var triangle_faces = [];
var raw_vbo_buffer_size = buffer_get_size(raw_vbo_buffer);
buffer_seek(raw_vbo_buffer, buffer_seek_start, 0);
while (buffer_tell(raw_vbo_buffer) < raw_vbo_buffer_size) {
var point_a = read_vertex(raw_vbo_buffer);
var point_b = read_vertex(raw_vbo_buffer);
var point_c = read_vertex(raw_vbo_buffer);
var point_a_vertex_positions_index = int64(array_push_if(vertex_positions, point_a.v) + 1);
var point_b_vertex_positions_index = int64(array_push_if(vertex_positions, point_b.v) + 1);
var point_c_vertex_positions_index = int64(array_push_if(vertex_positions, point_c.v) + 1);
var point_a_vertex_texture_coordinates_index = int64(array_push_if(vertex_texture_coordinates, point_a.vt) + 1);
var point_b_vertex_texture_coordinates_index = int64(array_push_if(vertex_texture_coordinates, point_b.vt) + 1);
var point_c_vertex_texture_coordinates_index = int64(array_push_if(vertex_texture_coordinates, point_c.vt) + 1);
var point_a_vertex_normals_index = int64(array_push_if(vertex_normals, point_a.vn) + 1);
var point_b_vertex_normals_index = int64(array_push_if(vertex_normals, point_b.vn) + 1);
var point_c_vertex_normals_index = int64(array_push_if(vertex_normals, point_c.vn) + 1);
var f = $"f {point_a_vertex_positions_index}/{point_a_vertex_texture_coordinates_index}/{point_a_vertex_normals_index} {point_b_vertex_positions_index}/{point_b_vertex_texture_coordinates_index}/{point_b_vertex_normals_index} {point_c_vertex_positions_index}/{point_c_vertex_texture_coordinates_index}/{point_c_vertex_normals_index}";
array_push(triangle_faces, f);
}
var file = file_text_open_write($"{name}.obj");
file_text_write_string(file, "o Default");
file_text_writeln(file);
repeat (array_length(vertex_positions)) {
file_text_write_string(file, array_shift(vertex_positions));
file_text_writeln(file);
}
repeat (array_length(vertex_texture_coordinates)) {
file_text_write_string(file, array_shift(vertex_texture_coordinates));
file_text_writeln(file);
}
repeat (array_length(vertex_normals)) {
file_text_write_string(file, array_shift(vertex_normals));
file_text_writeln(file);
}
file_text_write_string(file, $"g {name}");
file_text_writeln(file);
file_text_write_string(file, "usemtl 0");
file_text_writeln(file);
repeat (array_length(triangle_faces)) {
file_text_write_string(file, array_shift(triangle_faces));
file_text_writeln(file);
}
file_text_close(file);
return true;
}
function string_format_trimmed(value, total, decimal_places) {
return string_trim(string_format(value, total, decimal_places));
}
function array_push_if(array, value) {
var index = array_get_index(array, value);
if (index == -1) {
array_push(array, value);
return array_length(array) - 1;
}
return index;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment