Last active
December 27, 2021 10:39
-
-
Save bartteunis/c0090af9c3252b78c9b4920b6ab4f8ee to your computer and use it in GitHub Desktop.
Very unfinished idea for a vertex format wrapper struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
#macro POSITION_2D vertex_format_add_position | |
#macro POSITION_3D vertex_format_add_position_3d | |
#macro COLOR vertex_format_add_color | |
#macro UV vertex_format_add_texcoord | |
#macro NORMAL vertex_format_add_normal | |
/* | |
vertex_format_add_position_3d(); | |
vertex_position_3d(); | |
buffer_write(buffer, buffer_f32, 4); | |
*/ | |
function VertexFormat(attributes) constructor { | |
vertex_format_begin(); | |
var i = 0; | |
repeat(array_length(attributes)) { | |
attributes[i++](); | |
} | |
format = vertex_format_end(); | |
descriptor = attributes; | |
static _add = { | |
POSITION_2D: vertex_position, | |
POSITION_3D: vertex_position_3d, | |
COLOR: vertex_color, | |
UV: vertex_texcoord, | |
NORMAL: vertex_normal, | |
} | |
attribute_list = function() { | |
var index = ds_map_create(); | |
index[?vertex_format_add_position] = 0; | |
index[?vertex_format_add_position_3d] = 0; | |
index[?vertex_format_add_color] = 0; | |
index[?vertex_format_add_texcoord] = 0; | |
index[?vertex_format_add_normal] = 0; | |
var str_list = ""; | |
for(var i = 0;i < array_length(descriptor);i++) { | |
var desc = descriptor[i]; | |
switch(desc) { | |
case vertex_format_add_position: | |
str_list += "attribute vec2 in_Position"; | |
break; | |
case vertex_format_add_position_3d: | |
str_list += "attribute vec3 in_Position"; | |
break; | |
case vertex_format_add_color: | |
str_list += "attribute vec4 in_Colour"; | |
break; | |
case vertex_format_add_texcoord: | |
str_list += "attribute vec2 in_TextureCoord"; | |
break; | |
case vertex_format_add_normal: | |
str_list += "attribute vec3 in_Normal"; | |
break; | |
} | |
str_list += (index[?desc] == 0) ? "" : string(index[?desc]+1); | |
str_list += ";\n"; | |
index[?desc]++; | |
} | |
ds_map_destroy(index); | |
return str_list; | |
} | |
_begin = function() { | |
vb = vertex_create_buffer(); | |
vertex_begin(vb, format); | |
} | |
vertex = function(params) { | |
for(var i = 0;i < array_length(params);i++) { | |
array_insert(params, 0, vb); | |
_add[$ descriptor[i]](params); | |
} | |
} | |
_end = function(freeze) { | |
vertex_end(vb); | |
vertex_freeze(vb); | |
} | |
vb = undefined; | |
bytesize = function() { | |
static buff = buffer_create(256, buffer_fixed, 1); | |
var vb = vertex_create_buffer_from_buffer_ext( | |
buff, format, 0, 1 | |
); | |
var size = vertex_get_buffer_size(vb); | |
vertex_delete_buffer(vb); | |
return size; | |
} | |
toString = function() { | |
var str = "{"; | |
var names = variable_struct_get_names(self); | |
for(var i = 0;i < array_length(names);i++) { | |
var name = names[i]; | |
if is_method(self[$name]) { continue; } | |
str += " " + name + " : " + string(self[$name]) + ","; | |
} | |
str += "}"; | |
return str; | |
} | |
size = bytesize(format); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment