fix vertex colors support

main
Dominik Madarász 2024-08-23 14:15:56 +02:00
parent de5f778843
commit 097c1f286b
11 changed files with 86 additions and 27 deletions

View File

@ -419,6 +419,8 @@ void main() {
diffuse = u_diffuse; // * v_color;
}
diffuse *= v_color;
if (u_texlit) {
vec4 litsample = texture(u_lightmap, v_texcoord);

View File

@ -384758,7 +384758,7 @@ typedef struct iqm_vertex {
GLubyte blendindexes[4];
GLubyte blendweights[4];
GLfloat blendvertexindex;
GLubyte color[4];
GLfloat color[4];
GLfloat texcoord2[2];
} iqm_vertex;
@ -385018,7 +385018,7 @@ void model_set_state(model_t m) {
glEnableVertexAttribArray(3);
// vertex color
glVertexAttribPointer(11, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(iqm_vertex), (GLvoid*)offsetof(iqm_vertex,color) );
glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, sizeof(iqm_vertex), (GLvoid*)offsetof(iqm_vertex,color) );
glEnableVertexAttribArray(11);
// lmap data
@ -385084,7 +385084,7 @@ bool model_load_meshes(iqm_t *q, const struct iqmheader *hdr, model_t *m) {
float *inposition = NULL, *innormal = NULL, *intangent = NULL, *intexcoord = NULL, *invertexindex = NULL;
uint8_t *inblendindex8 = NULL, *inblendweight8 = NULL;
int *inblendindexi = NULL; float *inblendweightf = NULL;
uint8_t *invertexcolor8 = NULL;
float *invertexcolor = NULL;
struct iqmvertexarray *vas = (struct iqmvertexarray *)&q->buf[hdr->ofs_vertexarrays];
for(int i = 0; i < (int)hdr->num_vertexarrays; i++) {
struct iqmvertexarray *va = &vas[i];
@ -385094,7 +385094,7 @@ bool model_load_meshes(iqm_t *q, const struct iqmheader *hdr, model_t *m) {
break; case IQM_NORMAL: ASSERT(va->format == IQM_FLOAT && va->size == 3); innormal = (float *)&q->buf[va->offset]; lil32pf(innormal, 3*hdr->num_vertexes);
break; case IQM_TANGENT: ASSERT(va->format == IQM_FLOAT && va->size == 4); intangent = (float *)&q->buf[va->offset]; lil32pf(intangent, 4*hdr->num_vertexes);
break; case IQM_TEXCOORD: ASSERT(va->format == IQM_FLOAT && va->size == 2); intexcoord = (float *)&q->buf[va->offset]; lil32pf(intexcoord, 2*hdr->num_vertexes);
break; case IQM_COLOR: ASSERT(va->size == 4); ASSERT(va->format == IQM_UBYTE); invertexcolor8 = (uint8_t *)&q->buf[va->offset];
break; case IQM_COLOR: ASSERT(va->size == 4); ASSERT(va->format == IQM_FLOAT); invertexcolor = (float *)&q->buf[va->offset];
break; case IQM_BLENDINDEXES: ASSERT(va->size == 4); ASSERT(va->format == IQM_UBYTE || va->format == IQM_INT);
if(va->format == IQM_UBYTE) inblendindex8 = (uint8_t *)&q->buf[va->offset];
else inblendindexi = (int *)&q->buf[va->offset];
@ -385159,7 +385159,26 @@ bool model_load_meshes(iqm_t *q, const struct iqmheader *hdr, model_t *m) {
float conv = i;
memcpy(&v->blendvertexindex, &conv, 4);
}
if(invertexcolor8) memcpy(v->color, &invertexcolor8[i*4], sizeof(v->color));
if(invertexcolor) {
v->color[0] = invertexcolor[i*4+0];
v->color[1] = invertexcolor[i*4+1];
v->color[2] = invertexcolor[i*4+2];
v->color[3] = invertexcolor[i*4+3];
}
else {
v->color[0] = 1.0f;
v->color[1] = 1.0f;
v->color[2] = 1.0f;
v->color[3] = 1.0f;
}
/* handle vertex colors for parts of mesh that don't utilise it. */
if (v->color[0] + v->color[1] + v->color[2] + v->color[3] < 0.001f) {
v->color[0] = 1.0f;
v->color[1] = 1.0f;
v->color[2] = 1.0f;
v->color[3] = 1.0f;
}
}
if(!q->vbo) glGenBuffers(1, &q->vbo);

View File

@ -3145,7 +3145,7 @@ typedef struct iqm_vertex {
GLubyte blendindexes[4];
GLubyte blendweights[4];
GLfloat blendvertexindex;
GLubyte color[4];
GLfloat color[4];
GLfloat texcoord2[2];
} iqm_vertex;
@ -3405,7 +3405,7 @@ void model_set_state(model_t m) {
glEnableVertexAttribArray(3);
// vertex color
glVertexAttribPointer(11, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(iqm_vertex), (GLvoid*)offsetof(iqm_vertex,color) );
glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, sizeof(iqm_vertex), (GLvoid*)offsetof(iqm_vertex,color) );
glEnableVertexAttribArray(11);
// lmap data
@ -3471,7 +3471,7 @@ bool model_load_meshes(iqm_t *q, const struct iqmheader *hdr, model_t *m) {
float *inposition = NULL, *innormal = NULL, *intangent = NULL, *intexcoord = NULL, *invertexindex = NULL;
uint8_t *inblendindex8 = NULL, *inblendweight8 = NULL;
int *inblendindexi = NULL; float *inblendweightf = NULL;
uint8_t *invertexcolor8 = NULL;
float *invertexcolor = NULL;
struct iqmvertexarray *vas = (struct iqmvertexarray *)&q->buf[hdr->ofs_vertexarrays];
for(int i = 0; i < (int)hdr->num_vertexarrays; i++) {
struct iqmvertexarray *va = &vas[i];
@ -3481,7 +3481,7 @@ bool model_load_meshes(iqm_t *q, const struct iqmheader *hdr, model_t *m) {
break; case IQM_NORMAL: ASSERT(va->format == IQM_FLOAT && va->size == 3); innormal = (float *)&q->buf[va->offset]; lil32pf(innormal, 3*hdr->num_vertexes);
break; case IQM_TANGENT: ASSERT(va->format == IQM_FLOAT && va->size == 4); intangent = (float *)&q->buf[va->offset]; lil32pf(intangent, 4*hdr->num_vertexes);
break; case IQM_TEXCOORD: ASSERT(va->format == IQM_FLOAT && va->size == 2); intexcoord = (float *)&q->buf[va->offset]; lil32pf(intexcoord, 2*hdr->num_vertexes);
break; case IQM_COLOR: ASSERT(va->size == 4); ASSERT(va->format == IQM_UBYTE); invertexcolor8 = (uint8_t *)&q->buf[va->offset];
break; case IQM_COLOR: ASSERT(va->size == 4); ASSERT(va->format == IQM_FLOAT); invertexcolor = (float *)&q->buf[va->offset];
break; case IQM_BLENDINDEXES: ASSERT(va->size == 4); ASSERT(va->format == IQM_UBYTE || va->format == IQM_INT);
if(va->format == IQM_UBYTE) inblendindex8 = (uint8_t *)&q->buf[va->offset];
else inblendindexi = (int *)&q->buf[va->offset];
@ -3546,7 +3546,26 @@ bool model_load_meshes(iqm_t *q, const struct iqmheader *hdr, model_t *m) {
float conv = i;
memcpy(&v->blendvertexindex, &conv, 4);
}
if(invertexcolor8) memcpy(v->color, &invertexcolor8[i*4], sizeof(v->color));
if(invertexcolor) {
v->color[0] = invertexcolor[i*4+0];
v->color[1] = invertexcolor[i*4+1];
v->color[2] = invertexcolor[i*4+2];
v->color[3] = invertexcolor[i*4+3];
}
else {
v->color[0] = 1.0f;
v->color[1] = 1.0f;
v->color[2] = 1.0f;
v->color[3] = 1.0f;
}
/* handle vertex colors for parts of mesh that don't utilise it. */
if (v->color[0] + v->color[1] + v->color[2] + v->color[3] < 0.001f) {
v->color[0] = 1.0f;
v->color[1] = 1.0f;
v->color[2] = 1.0f;
v->color[3] = 1.0f;
}
}
if(!q->vbo) glGenBuffers(1, &q->vbo);

View File

@ -19930,7 +19930,7 @@ typedef struct iqm_vertex {
GLubyte blendindexes[4];
GLubyte blendweights[4];
GLfloat blendvertexindex;
GLubyte color[4];
GLfloat color[4];
GLfloat texcoord2[2];
} iqm_vertex;
@ -20190,7 +20190,7 @@ void model_set_state(model_t m) {
glEnableVertexAttribArray(3);
// vertex color
glVertexAttribPointer(11, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(iqm_vertex), (GLvoid*)offsetof(iqm_vertex,color) );
glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, sizeof(iqm_vertex), (GLvoid*)offsetof(iqm_vertex,color) );
glEnableVertexAttribArray(11);
// lmap data
@ -20256,7 +20256,7 @@ bool model_load_meshes(iqm_t *q, const struct iqmheader *hdr, model_t *m) {
float *inposition = NULL, *innormal = NULL, *intangent = NULL, *intexcoord = NULL, *invertexindex = NULL;
uint8_t *inblendindex8 = NULL, *inblendweight8 = NULL;
int *inblendindexi = NULL; float *inblendweightf = NULL;
uint8_t *invertexcolor8 = NULL;
float *invertexcolor = NULL;
struct iqmvertexarray *vas = (struct iqmvertexarray *)&q->buf[hdr->ofs_vertexarrays];
for(int i = 0; i < (int)hdr->num_vertexarrays; i++) {
struct iqmvertexarray *va = &vas[i];
@ -20266,7 +20266,7 @@ bool model_load_meshes(iqm_t *q, const struct iqmheader *hdr, model_t *m) {
break; case IQM_NORMAL: ASSERT(va->format == IQM_FLOAT && va->size == 3); innormal = (float *)&q->buf[va->offset]; lil32pf(innormal, 3*hdr->num_vertexes);
break; case IQM_TANGENT: ASSERT(va->format == IQM_FLOAT && va->size == 4); intangent = (float *)&q->buf[va->offset]; lil32pf(intangent, 4*hdr->num_vertexes);
break; case IQM_TEXCOORD: ASSERT(va->format == IQM_FLOAT && va->size == 2); intexcoord = (float *)&q->buf[va->offset]; lil32pf(intexcoord, 2*hdr->num_vertexes);
break; case IQM_COLOR: ASSERT(va->size == 4); ASSERT(va->format == IQM_UBYTE); invertexcolor8 = (uint8_t *)&q->buf[va->offset];
break; case IQM_COLOR: ASSERT(va->size == 4); ASSERT(va->format == IQM_FLOAT); invertexcolor = (float *)&q->buf[va->offset];
break; case IQM_BLENDINDEXES: ASSERT(va->size == 4); ASSERT(va->format == IQM_UBYTE || va->format == IQM_INT);
if(va->format == IQM_UBYTE) inblendindex8 = (uint8_t *)&q->buf[va->offset];
else inblendindexi = (int *)&q->buf[va->offset];
@ -20331,7 +20331,26 @@ bool model_load_meshes(iqm_t *q, const struct iqmheader *hdr, model_t *m) {
float conv = i;
memcpy(&v->blendvertexindex, &conv, 4);
}
if(invertexcolor8) memcpy(v->color, &invertexcolor8[i*4], sizeof(v->color));
if(invertexcolor) {
v->color[0] = invertexcolor[i*4+0];
v->color[1] = invertexcolor[i*4+1];
v->color[2] = invertexcolor[i*4+2];
v->color[3] = invertexcolor[i*4+3];
}
else {
v->color[0] = 1.0f;
v->color[1] = 1.0f;
v->color[2] = 1.0f;
v->color[3] = 1.0f;
}
/* handle vertex colors for parts of mesh that don't utilise it. */
if (v->color[0] + v->color[1] + v->color[2] + v->color[3] < 0.001f) {
v->color[0] = 1.0f;
v->color[1] = 1.0f;
v->color[2] = 1.0f;
v->color[3] = 1.0f;
}
}
if(!q->vbo) glGenBuffers(1, &q->vbo);

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -974,7 +974,7 @@ void export_custom_vertexarrays(FILE *out, const struct aiScene *scene)
if (mesh->mColors[t]) {
if (!seen[custom]) {
if (first) { fprintf(out, "\n"); first = 0; }
fprintf(out, "vertexarray custom%d ubyte 4 \"color.%d\"\n", custom, t);
fprintf(out, "vertexarray custom%d float 4 \"color.%d\"\n", custom, t);
seen[custom] = 1;
}
}
@ -1192,19 +1192,19 @@ void export_node(FILE *out, const struct aiScene *scene, const struct aiNode *no
}
if (mesh->mColors[0]) {
float r = mesh->mColors[0][k].r; r = floorf(r * 255) / 255;
float g = mesh->mColors[0][k].g; g = floorf(g * 255) / 255;
float b = mesh->mColors[0][k].b; b = floorf(b * 255) / 255;
float a = mesh->mColors[0][k].a; a = floorf(a * 255) / 255;
fprintf(out, "vc %.9g %.9g %.9g %.9g\n", r, g, b, a);
float r = mesh->mColors[0][k].r;
float g = mesh->mColors[0][k].g;
float b = mesh->mColors[0][k].b;
float a = mesh->mColors[0][k].a;
fprintf(out, "vc %.02f %.02f %.02f %.02f\n", r, g, b, a);
}
for (t = 1; t <= MAX_COL; t++) {
if (mesh->mColors[t]) {
float r = mesh->mColors[t][k].r; r = floorf(r * 255) / 255;
float g = mesh->mColors[t][k].g; g = floorf(g * 255) / 255;
float b = mesh->mColors[t][k].b; b = floorf(b * 255) / 255;
float a = mesh->mColors[t][k].a; a = floorf(a * 255) / 255;
fprintf(out, "v%d %.9g %.9g %.9g %.9g\n", FIRST_COL+t-1, r, g, b, a);
float r = mesh->mColors[t][k].r;
float g = mesh->mColors[t][k].g;
float b = mesh->mColors[t][k].b;
float a = mesh->mColors[t][k].a;
fprintf(out, "v%d %.02f %.02f %.02f %.02f\n", FIRST_COL+t-1, r, g, b, a);
}
}

Binary file not shown.

View File

@ -2478,7 +2478,7 @@ void makemeshes()
setupvertexarray<IQM_BLENDINDEXES>(eblends, IQM_BLENDINDEXES, IQM_UBYTE, 4);
setupvertexarray<IQM_BLENDWEIGHTS>(eblends, IQM_BLENDWEIGHTS, IQM_UBYTE, 4);
}
if(ecolors.length()) setupvertexarray<IQM_COLOR>(ecolors, IQM_COLOR, IQM_UBYTE, 4);
if(ecolors.length()) setupvertexarray<IQM_COLOR>(ecolors, IQM_COLOR, IQM_FLOAT, 4);
loopi(10) if(ecustom[i].length()) setupvertexarray<IQM_CUSTOM>(ecustom[i], IQM_CUSTOM + i, IQM_FLOAT, 4);
if(epositions.length())

Binary file not shown.