Add GL_ELEMENT_ARRAY_BUFFER to index buffers

Fixes more or less all WebGL rendering issues when building Principia for the web
This commit is contained in:
ROllerozxa 2025-12-27 21:29:38 +01:00
commit 848dce5262
13 changed files with 25 additions and 26 deletions

View file

@ -69,6 +69,7 @@ cable::_init(void)
buf->usage = GL_STREAM_DRAW;
ibuf->usage = GL_STATIC_DRAW;
ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
va = new tms::varray(2);
va->map_attribute("position", 3, GL_FLOAT, buf);
@ -122,6 +123,7 @@ cable::_init(void)
plug_ibuf = new tms::gbuffer(MAX_PLUGS * indices_per_plug * sizeof(uint16_t));
plug_ibuf->usage = GL_STATIC_DRAW;
plug_ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
for (int x=0; x<3; x++) {
plug_buf[x] = new tms::gbuffer(MAX_PLUGS * vertices_per_plug * sizeof(struct vertex));

View file

@ -51,6 +51,7 @@ _init()
{
ibuf = new tms::gbuffer((3 * 16 * 16 * indices_per_tpixel * sizeof(uint16_t))/* / 4 * 3*/);
ibuf->usage = GL_STATIC_DRAW;
ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
uint16_t *i = (uint16_t*)tms_gbuffer_get_buffer(ibuf);
uint16_t *ri = (uint16_t*)((char*)tms_gbuffer_get_buffer(mm->indices)+mm->i_start*2);
@ -67,6 +68,7 @@ _init()
{
grass_ibuf = new tms::gbuffer(MAX_GRASS_PER_CHUNK*6*sizeof(short));
grass_ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
uint16_t *i = (uint16_t*)tms_gbuffer_get_buffer(grass_ibuf);
for (int x=0; x<MAX_GRASS_PER_CHUNK; x++) {
i[x*6+0] = x*4;
@ -137,7 +139,7 @@ chunk_window::reset()
this->isset = false;
}
/**
/**
* Sets a pixel at global gx and gy
* returns the affected chunk
**/
@ -156,7 +158,7 @@ chunk_window::set_pixel(int gx, int gy, int z, int material)
return c;
}
/**
/**
* Gets the pixel at global gx and gy
* returns the material of the given pixel
**/
@ -533,7 +535,7 @@ void flood_fill(chunk_window *win, int x, int y)
}
}
/**
/**
* Create the texture that is used for terrain visibility
*
* This function is NOT thread safe

View file

@ -35,6 +35,7 @@ static void init()
vbuf = tms_gbuffer_alloc(4096*sizeof(struct vertex2));
ibuf = tms_gbuffer_alloc(32*4096*sizeof(uint16_t));
ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
va = tms_varray_alloc(2);
tms_varray_map_attribute(va, "position", 3, GL_FLOAT, vbuf);

View file

@ -73,6 +73,7 @@ group::group()
this->va = tms_varray_alloc(2);
this->vbuf = tms_gbuffer_alloc(1); /* XXX */
this->ibuf = tms_gbuffer_alloc(1);
this->ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
tms_varray_map_attribute(this->va, "position", 3, GL_FLOAT, this->vbuf);
tms_varray_map_attribute(this->va, "normal", 3, GL_FLOAT, this->vbuf);
@ -1099,11 +1100,13 @@ group::finalize()
}
if (num_wood > 0) {
this->wooden_ibuf.target = GL_ELEMENT_ARRAY_BUFFER;
tms_gbuffer_upload(&this->wooden_ibuf);
tms_gbuffer_upload(&this->wooden_vbuf);
}
if (num_plastic > 0) {
this->plastic_ibuf.target = GL_ELEMENT_ARRAY_BUFFER;
tms_gbuffer_upload(&this->plastic_ibuf);
tms_gbuffer_upload(&this->plastic_vbuf);
}

View file

@ -424,6 +424,7 @@ read_cache(lvlbuf *lb)
data = (char*)malloc(indices_size);
lb->r_buf(data, indices_size);
m->indices = tms_gbuffer_alloc_fill(data, indices_size);
m->indices->target = GL_ELEMENT_ARRAY_BUFFER;
free(data);
m->va = tms_varray_alloc(3);

View file

@ -48,6 +48,7 @@ pixel::initialize()
_ibuf = new tms::gbuffer(MAX_PIXELS * indices_per_pixel * sizeof(uint16_t));
_ibuf->usage = GL_STATIC_DRAW;
_ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
for (int x=0; x<3; x++) {

View file

@ -130,6 +130,7 @@ static void _init()
vbuf->usage = GL_STREAM_DRAW;
ibuf->usage = GL_STATIC_DRAW;
ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
va = tms_varray_alloc(4);
tms_varray_map_attribute(va, "position", 3, GL_FLOAT, vbuf);

View file

@ -22,6 +22,7 @@ polygon::_init()
vbuf->usage = GL_STATIC_DRAW;
ibuf = tms_gbuffer_alloc(90 * MAX_POLYGONS * sizeof(uint16_t));
ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
va = tms_varray_alloc(3);
tms_varray_map_attribute(va, "position", 3, GL_FLOAT, vbuf);

View file

@ -40,6 +40,7 @@ rope::_init()
buf->usage = GL_STREAM_DRAW;
ibuf->usage = GL_STATIC_DRAW;
ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
va = new tms::varray(3);
va->map_attribute("position", 3, GL_FLOAT, buf);

View file

@ -235,6 +235,7 @@ init_buffers(void)
tms_ddraw_circle_mesh = circle;
rsquare_ibuf = tms_gbuffer_alloc(18 * 3 * sizeof(uint16_t));
rsquare_ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
uint16_t *i = (uint16_t*)tms_gbuffer_get_buffer(rsquare_ibuf);
for (int iy=0; iy<3; ++iy) {
@ -247,28 +248,6 @@ init_buffers(void)
i[iy*3*6+ix*6+5] = ix + 5 + iy * 4;
}
}
/*
for (int x=0; x<9; ++x) {
i[x*6+0] = (x*4)+1;
i[x*6+1] = (x*4)+0;
i[x*6+2] = (x*4)+4;
i[x*6+3] = (x*4)+1;
i[x*6+4] = (x*4)+4;
i[x*6+5] = (x*4)+5;
tms_debugf("[%d], %d - %d - %d",
x,
i[x*6+0],
i[x*6+1],
i[x*6+2]);
tms_debugf("[%d], %d - %d - %d",
x,
i[x*6+3],
i[x*6+4],
i[x*6+5]);
}
*/
//tms_fatalf("x");
tms_gbuffer_upload(rsquare_ibuf);

View file

@ -158,6 +158,7 @@ tms_meshfactory_get_cylinder(void)
struct tms_gbuffer *vbuf = tms_gbuffer_alloc(sizeof(struct cvert) * CQ*4);
//struct tms_gbuffer *ibuf = tms_gbuffer_alloc(sizeof(short) * (CQ*4 + 12));
struct tms_gbuffer *ibuf = tms_gbuffer_alloc(sizeof(short) * (CQ*6+CQ*3+CQ*3));
ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
struct cvert *vertices = tms_gbuffer_get_buffer(vbuf);;
short *indices = tms_gbuffer_get_buffer(ibuf);
@ -224,6 +225,7 @@ const struct tms_mesh *tms_meshfactory_get_cube(void)
{
if (cube == 0) {
struct tms_gbuffer *indices = tms_gbuffer_alloc_fill(cube_indices, sizeof(cube_indices));
indices->target = GL_ELEMENT_ARRAY_BUFFER;
struct tms_gbuffer *vertices = tms_gbuffer_alloc_fill(cube_verts, sizeof(cube_verts));
struct tms_varray *va = tms_varray_alloc(3);
tms_varray_map_attribute(va, "position", 3, GL_FLOAT, vertices);

View file

@ -14,6 +14,7 @@ struct tms_model *tms_model_alloc(void)
m->vertices = tms_gbuffer_alloc(0);
m->indices = tms_gbuffer_alloc(0);
m->indices->target = GL_ELEMENT_ARRAY_BUFFER;
tms_varray_map_attribute(m->va, "position", 3, GL_FLOAT, m->vertices);
tms_varray_map_attribute(m->va, "normal", 3, GL_FLOAT, m->vertices);
@ -45,7 +46,10 @@ tms_model_upload(struct tms_model *m)
{
if (m->meshes) {
tms_gbuffer_upload(m->vertices);
tms_gbuffer_upload(m->indices);
if (m->indices) {
m->indices->target = GL_ELEMENT_ARRAY_BUFFER;
tms_gbuffer_upload(m->indices);
}
}
}

View file

@ -221,6 +221,7 @@ tpixel::initialize()
_ibuf = new tms::gbuffer(MAX_TPIXELS * indices_per_tpixel * sizeof(uint16_t));
_ibuf->usage = GL_STATIC_DRAW;
_ibuf->target = GL_ELEMENT_ARRAY_BUFFER;
for (int x=0; x<3; x++) {