1 module ecs_utils.gfx.mesh;
2 
3 import bindbc.sdl;
4 
5 import bubel.ecs.std;
6 
7 import ecs_utils.gfx.buffer;
8 
9 version(WebAssembly)import glad.gl.gles2;
10 else version(Android)import glad.gl.gles2;
11 else import glad.gl.gl;
12 //import mutils.serializer.json;
13 
14 extern(C):
15 
16 struct Mesh
17 {
18     bool load(const char[] path) nothrow
19     {
20         struct LoadData
21         {
22             /*struct Vertex
23             {
24                 struct Binding
25                 {
26                     @("malloc") string type;
27                     uint stride;
28                 }
29                 @("malloc") Binding[] bindings;
30             }
31             Vertex vertex;*/
32             @("malloc") ushort[] indices;
33             @("malloc") float[] vertices;
34             //int i;
35 
36             void dispose() nothrow
37             {
38                 if(indices)Mallocator.dispose(indices);
39                 if(vertices)Mallocator.dispose(vertices);
40 
41                 /*foreach(binding; vertex.bindings)Mallocator.instance.dispose(cast(char[])binding.type);
42 
43                 if(vertex.bindings)Mallocator.instance.dispose(vertex.bindings);*/
44             }
45         }
46 
47         char[] cpath = (cast(char*)alloca(path.length+1))[0..path.length+1];
48         cpath[0..$-1] = path[0..$];
49         cpath[$-1] = 0;
50 
51         SDL_RWops* file = SDL_RWFromFile(cpath.ptr,"r");//SDL_LoadFile(cpath.ptr,);
52         if(file)
53         {
54             size_t size = cast(size_t)SDL_RWsize(file);
55             //data.code = Mallocator.instance.makeArray!char(size);
56             //data.code[$-1] = 0;
57             char[] buffer = Mallocator.makeArray!char(size);
58             SDL_RWread(file,buffer.ptr,size,1);
59 
60             LoadData load_data;
61             scope(exit)load_data.dispose();
62 
63             /*JSONSerializer serializer = Mallocator.make!JSONSerializer;
64             scope(exit)Mallocator.dispose(serializer);
65             serializer.serialize!(Load.yes, true)(load_data,buffer);*/
66 
67             indices = Mallocator.makeArray(load_data.indices);
68             /*vertex.create();
69             Vertex.Binding[] bindings = (cast(Vertex.Binding*)alloca(Vertex.Binding.sizeof*load_data.vertex.bindings.length))[0..load_data.vertex.bindings.length];
70             uint vertex_size = 0;
71             uint alignment = 4;
72             foreach(i, binding;load_data.vertex.bindings)
73             {
74                 uint new_size = binding.stride;
75                 bindings[i].stride = binding.stride;
76                 if(binding.type == "float_rg")
77                 {
78                     bindings[i].type = Vertex.Type.float_rg;
79                     new_size += 8;
80                 }
81                 if(new_size > vertex_size)vertex_size = new_size;
82                 //new_size = new_size + (3 - (new_size)%alignment)
83             }
84             vertex.data.size = vertex_size;
85             vertex.attachBindings(bindings);*/
86 
87             vertices = Mallocator.makeArray(load_data.vertices);
88             /*vertices = Mallocator.instance.makeArray!ubyte(vertex_size * load_data.vertices.length);
89             {
90                 foreach()
91             }*/
92 
93             SDL_RWclose(file);
94             load_data.dispose();
95             return true;
96         }
97         else return false;
98     }
99 
100     void uploadData() nothrow
101     {
102         vbo.create();
103         vbo.bufferData(Buffer.BindTarget.array,16,cast(uint)vertices.length,GL_STATIC_DRAW,vertices.ptr);
104 
105         ibo.create();
106         ibo.bufferData(Buffer.BindTarget.element_array,2,cast(uint)indices.length,GL_STATIC_DRAW,indices.ptr);
107     }
108 
109     void bind() nothrow
110     {
111         vbo.bind(Buffer.BindTarget.array);
112         ibo.bind(Buffer.BindTarget.element_array);
113 
114         glVertexAttribPointer(0,2,GL_FLOAT,false,16,null);
115         glVertexAttribPointer(1,2,GL_FLOAT,false,16,cast(void*)8);
116     }
117 
118     float[] vertices;
119     ushort[] indices;
120     Buffer vbo;
121     Buffer ibo;
122     //Vertex vertex;
123 }