From 4255767f9f7cd592f74169a44d9fdc90e263e08b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 5 Feb 2025 20:34:04 +1100 Subject: [PATCH 1/5] Refactor dtex parser --- samples/depth_funcs_alpha_testing/gl_png.c | 315 +++++++++++---------- samples/depth_funcs_alpha_testing/gl_png.h | 2 +- 2 files changed, 159 insertions(+), 158 deletions(-) diff --git a/samples/depth_funcs_alpha_testing/gl_png.c b/samples/depth_funcs_alpha_testing/gl_png.c index 5415638..01e8ad5 100644 --- a/samples/depth_funcs_alpha_testing/gl_png.c +++ b/samples/depth_funcs_alpha_testing/gl_png.c @@ -14,178 +14,179 @@ GLfloat global_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat global_ambient[] = {1.0, 1.0, 1.0, 1.0}; +static int decode_dtex(Image* image, FILE* file) { + struct { + char id[4]; // 'DTEX' + GLushort width; + GLushort height; + GLuint type; + GLuint size; + } header; + + fread(&header, sizeof(header), 1, file); + + image->twiddled = (header.type & (1 << 26)) < 1; + image->compressed = (header.type & (1 << 30)) > 0; + image->mipmapped = (header.type & (1 << 31)) > 0; + GLuint format = (header.type >> 27) & 0b111; + + image->data = (char *) malloc (header.size); + image->sizeX = header.width; + image->sizeY = header.height; + image->dataSize = header.size; + + fread(image->data, image->dataSize, 1, file); + fclose(file); + + if(image->compressed) { + printf("Compressed - "); + if(image->twiddled) { + printf("Twiddled - "); + switch(format) { + case 0: { + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_TWID_KOS; + } else { + image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_TWID_KOS; + } + } break; + case 1: { + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_RGB_565_VQ_MIPMAP_TWID_KOS; + } else { + image->internalFormat = GL_COMPRESSED_RGB_565_VQ_TWID_KOS; + } + } break; + case 2: { + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_TWID_KOS; + } else { + image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_TWID_KOS; + } + } + break; + default: + fprintf(stderr, "Invalid texture format"); + return 0; + } + } else { + switch(format) { + case 0: { + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_KOS; + } else { + image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_KOS; + } + } break; + case 1: { + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_RGB_565_VQ_MIPMAP_KOS; + } else { + image->internalFormat = GL_COMPRESSED_RGB_565_VQ_KOS; + } + } break; + case 2: { + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_KOS; + } else { + image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_KOS; + } + } + break; + default: + fprintf(stderr, "Invalid texture format"); + return 0; + } + } + } else { + printf("Uncompressed - "); + //printf("Color:%u -", format); + switch(format) { + case 0: + image->internalFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS; + //image->internalFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV; + break; + case 1: + image->internalFormat = GL_UNSIGNED_SHORT_5_6_5_REV; + break; + case 2: + image->internalFormat = GL_UNSIGNED_SHORT_4_4_4_4_REV; + break; + } + } + printf("\n"); +} int dtex_to_gl_texture(texture *tex, char* filename) { // Load Texture - Image *image; + Image image = { 0 }; - // allocate space for texture - image = (Image *) malloc(sizeof(Image)); - if (image == NULL) { - printf("No memory for .DTEX file\n"); - return(0); - } + FILE* file = NULL; - FILE* file = NULL; + // make sure the file is there. + if ((file = fopen(filename, "rb")) == NULL) + { + printf("File not found"); + return 0; + } - // make sure the file is there. - if ((file = fopen(filename, "rb")) == NULL) - { - printf("File not found"); - return 0; - } + decode_dtex(&image, file); - struct { - char id[4]; // 'DTEX' - GLushort width; - GLushort height; - GLuint type; - GLuint size; - } header; + // Create Texture + GLuint texture_id; + glGenTextures(1, &texture_id); + glBindTexture(GL_TEXTURE_2D, texture_id); // 2d texture (x and y size) - fread(&header, sizeof(header), 1, file); + GLint newFormat = GL_RGB; + GLint colorType = GL_RGB; - GLboolean twiddled = (header.type & (1 << 26)) < 1; - GLboolean compressed = (header.type & (1 << 30)) > 0; - GLboolean mipmapped = (header.type & (1 << 31)) > 0; - GLuint format = (header.type >> 27) & 0b111; + if (image.internalFormat == GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS || + image.internalFormat == GL_UNSIGNED_SHORT_4_4_4_4_REV){ + newFormat = GL_BGRA; + colorType = GL_RGBA; + printf("Reversing RGBA\n"); + } - image->data = (char *) malloc (header.size); - image->sizeX = header.width; - image->sizeY = header.height; - image->dataSize = header.size; - - GLuint expected = 2 * header.width * header.height; - GLuint ratio = (GLuint) (((GLfloat) expected) / ((GLfloat) header.size)); - - fread(image->data, image->dataSize, 1, file); - fclose(file); - - if(compressed) { - printf("Compressed - "); - if(twiddled) { - printf("Twiddled - "); - switch(format) { - case 0: { - if(mipmapped) { - image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_TWID_KOS; - } else { - image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_TWID_KOS; - } - } break; - case 1: { - if(mipmapped) { - image->internalFormat = GL_COMPRESSED_RGB_565_VQ_MIPMAP_TWID_KOS; - } else { - image->internalFormat = GL_COMPRESSED_RGB_565_VQ_TWID_KOS; - } - } break; - case 2: { - if(mipmapped) { - image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_TWID_KOS; - } else { - image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_TWID_KOS; - } - } - break; - default: - fprintf(stderr, "Invalid texture format"); - return 0; - } - } else { - switch(format) { - case 0: { - if(mipmapped) { - image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_KOS; - } else { - image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_KOS; - } - } break; - case 1: { - if(mipmapped) { - image->internalFormat = GL_COMPRESSED_RGB_565_VQ_MIPMAP_KOS; - } else { - image->internalFormat = GL_COMPRESSED_RGB_565_VQ_KOS; - } - } break; - case 2: { - if(mipmapped) { - image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_KOS; - } else { - image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_KOS; - } - } - break; - default: - fprintf(stderr, "Invalid texture format"); - return 0; - } - } - } else { - printf("Uncompressed - "); - //printf("Color:%u -", format); - switch(format) { - - case 0: - image->internalFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS; - //image->internalFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV; - break; - case 1: - image->internalFormat = GL_UNSIGNED_SHORT_5_6_5_REV; - break; - case 2: - image->internalFormat = GL_UNSIGNED_SHORT_4_4_4_4_REV; - break; - } - } - printf("\n"); - - // Create Texture - GLuint texture_id; - glGenTextures(1, &texture_id); - glBindTexture(GL_TEXTURE_2D, texture_id); // 2d texture (x and y size) - - GLint newFormat = format; - GLint colorType = GL_RGB; - - if (image->internalFormat == GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS || - image->internalFormat == GL_UNSIGNED_SHORT_4_4_4_4_REV){ - newFormat = GL_BGRA; - colorType = GL_RGBA; - printf("Reversing RGBA\n"); - } - - if (image->internalFormat == GL_UNSIGNED_SHORT_5_6_5_REV){ - newFormat = GL_RGB; - colorType = GL_RGB; - printf("Reversing RGB\n"); - } + if (image.internalFormat == GL_UNSIGNED_SHORT_5_6_5_REV){ + newFormat = GL_RGB; + colorType = GL_RGB; + printf("Reversing RGB\n"); + } + if (image.compressed) { + glCompressedTexImage2D(GL_TEXTURE_2D, 0, + image.internalFormat, image.sizeX, image.sizeY, + 0, image.dataSize, image.data); + } else { glTexImage2D(GL_TEXTURE_2D, 0, - colorType, image->sizeX, image->sizeY, 0, - newFormat, image->internalFormat, image->data); + colorType, image.sizeX, image.sizeY, 0, + newFormat, image.internalFormat, image.data); + } - tex->id = texture_id; - tex->w = image->sizeX; - tex->h = image->sizeY; - tex->u = 0.f; - tex->v = 0.f; - tex->a = tex->light = 1; - tex->color[0] = tex->color[1] = tex->color[2] = 1.0f; - tex->uSize = tex->vSize = 1.0f; - tex->xScale = tex->yScale = 1.0f; - tex->format = image->internalFormat; - tex->min_filter = tex->mag_filter = GL_NEAREST; - tex->blend_source = GL_SRC_ALPHA; - tex->blend_dest = GL_ONE_MINUS_SRC_ALPHA; - strcpy(tex->path, filename); + tex->id = texture_id; + tex->w = image.sizeX; + tex->h = image.sizeY; + tex->u = 0.f; + tex->v = 0.f; + tex->a = tex->light = 1; + tex->color[0] = tex->color[1] = tex->color[2] = 1.0f; + tex->uSize = tex->vSize = 1.0f; + tex->xScale = tex->yScale = 1.0f; + tex->format = image.internalFormat; + tex->min_filter = tex->mag_filter = GL_NEAREST; + tex->blend_source = GL_SRC_ALPHA; + tex->blend_dest = GL_ONE_MINUS_SRC_ALPHA; + strcpy(tex->path, filename); + + GLuint expected = 2 * image.sizeX * image.sizeY; + GLuint ratio = (GLuint) (((GLfloat) expected) / ((GLfloat) image.dataSize)); - printf("Texture size: %lu x %lu\n", image->sizeX, image->sizeY); - printf("Texture ratio: %d\n", ratio); - printf("Texture size: %lu x %lu\n", image->sizeX, image->sizeY); - printf("Texture %s loaded\n", tex->path); + printf("Texture size: %lu x %lu\n", image.sizeX, image.sizeY); + printf("Texture ratio: %d\n", ratio); + printf("Texture size: %lu x %lu\n", image.sizeX, image.sizeY); + printf("Texture %s loaded\n", tex->path); - return(1); + return(1); } void draw_textured_quad(texture *tex) { diff --git a/samples/depth_funcs_alpha_testing/gl_png.h b/samples/depth_funcs_alpha_testing/gl_png.h index 29b312f..0639e36 100644 --- a/samples/depth_funcs_alpha_testing/gl_png.h +++ b/samples/depth_funcs_alpha_testing/gl_png.h @@ -30,7 +30,7 @@ typedef struct Image { unsigned long sizeY; char *data; GLenum internalFormat; - GLboolean mipmapped; + unsigned int mipmapped, compressed, twiddled; unsigned int dataSize; } Image; From 72baba7a5ec563cc6d30496f5c15925302b77046 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 5 Feb 2025 21:36:28 +1100 Subject: [PATCH 2/5] Refactor dtex parser again --- samples/depth_funcs_alpha_testing/gl_png.c | 138 +++++++++++---------- 1 file changed, 71 insertions(+), 67 deletions(-) diff --git a/samples/depth_funcs_alpha_testing/gl_png.c b/samples/depth_funcs_alpha_testing/gl_png.c index 01e8ad5..2d16032 100644 --- a/samples/depth_funcs_alpha_testing/gl_png.c +++ b/samples/depth_funcs_alpha_testing/gl_png.c @@ -38,83 +38,87 @@ static int decode_dtex(Image* image, FILE* file) { fread(image->data, image->dataSize, 1, file); fclose(file); - if(image->compressed) { - printf("Compressed - "); - if(image->twiddled) { - printf("Twiddled - "); - switch(format) { - case 0: { - if(image->mipmapped) { - image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_TWID_KOS; - } else { - image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_TWID_KOS; - } - } break; - case 1: { - if(image->mipmapped) { - image->internalFormat = GL_COMPRESSED_RGB_565_VQ_MIPMAP_TWID_KOS; - } else { - image->internalFormat = GL_COMPRESSED_RGB_565_VQ_TWID_KOS; - } - } break; - case 2: { - if(image->mipmapped) { - image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_TWID_KOS; - } else { - image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_TWID_KOS; - } - } - break; - default: - fprintf(stderr, "Invalid texture format"); - return 0; - } - } else { - switch(format) { - case 0: { - if(image->mipmapped) { - image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_KOS; - } else { - image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_KOS; - } - } break; - case 1: { - if(image->mipmapped) { - image->internalFormat = GL_COMPRESSED_RGB_565_VQ_MIPMAP_KOS; - } else { - image->internalFormat = GL_COMPRESSED_RGB_565_VQ_KOS; - } - } break; - case 2: { - if(image->mipmapped) { - image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_KOS; - } else { - image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_KOS; - } - } - break; - default: - fprintf(stderr, "Invalid texture format"); - return 0; - } - } - } else { - printf("Uncompressed - "); - //printf("Color:%u -", format); + if(image->compressed && image->twiddled) { switch(format) { case 0: + puts("Compressed & Twiddled - ARGB 1555"); + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_TWID_KOS; + } else { + image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_TWID_KOS; + } + return 1; + + case 1: + puts("Compressed & Twiddled - RGB 565"); + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_RGB_565_VQ_MIPMAP_TWID_KOS; + } else { + image->internalFormat = GL_COMPRESSED_RGB_565_VQ_TWID_KOS; + } + return 1; + + case 2: + puts("Compressed & Twiddled - ARGB 4444"); + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_TWID_KOS; + } else { + image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_TWID_KOS; + } + return 1; + + } + } else if (image->compressed) { + switch(format) { + case 0: + puts("Compressed - ARGB 1555"); + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_KOS; + } else { + image->internalFormat = GL_COMPRESSED_ARGB_1555_VQ_KOS; + } + return 1; + + case 1: + puts("Compressed - RGB 565"); + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_RGB_565_VQ_MIPMAP_KOS; + } else { + image->internalFormat = GL_COMPRESSED_RGB_565_VQ_KOS; + } + return 1; + + case 2: + puts("Compressed - ARGB 4444"); + if(image->mipmapped) { + image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_KOS; + } else { + image->internalFormat = GL_COMPRESSED_ARGB_4444_VQ_KOS; + } + return 1; + } + } else { + switch (format) { + case 0: + puts("Uncompressed - ARGB 1555"); image->internalFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS; //image->internalFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV; - break; + return 1; + case 1: + puts("Uncompressed - RGB 565"); image->internalFormat = GL_UNSIGNED_SHORT_5_6_5_REV; - break; + return 1; + case 2: + puts("Uncompressed - ARGB 4444"); image->internalFormat = GL_UNSIGNED_SHORT_4_4_4_4_REV; - break; + return 1; } } - printf("\n"); + + fprintf(stderr, "Invalid texture format %u\n", header.type); + return 0; } int dtex_to_gl_texture(texture *tex, char* filename) { From 455ad29e4c05c1f81a4d60efaa08d43b5df776ba Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 5 Feb 2025 21:42:50 +1100 Subject: [PATCH 3/5] At least the provided .dtex files load now --- samples/depth_funcs_alpha_testing/gl_png.c | 33 ++++++++-------------- samples/depth_funcs_alpha_testing/gl_png.h | 2 +- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/samples/depth_funcs_alpha_testing/gl_png.c b/samples/depth_funcs_alpha_testing/gl_png.c index 2d16032..29becd4 100644 --- a/samples/depth_funcs_alpha_testing/gl_png.c +++ b/samples/depth_funcs_alpha_testing/gl_png.c @@ -101,18 +101,23 @@ static int decode_dtex(Image* image, FILE* file) { switch (format) { case 0: puts("Uncompressed - ARGB 1555"); - image->internalFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS; - //image->internalFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV; + image->internalFormat = GL_ARGB1555_TWID_KOS; + image->transferFormat = GL_BGRA; + image->transferType = GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS; return 1; case 1: puts("Uncompressed - RGB 565"); - image->internalFormat = GL_UNSIGNED_SHORT_5_6_5_REV; + image->internalFormat = GL_RGB565_TWID_KOS; + image->transferFormat = GL_RGB; + image->transferType = GL_UNSIGNED_SHORT_5_6_5_TWID_KOS; return 1; case 2: puts("Uncompressed - ARGB 4444"); - image->internalFormat = GL_UNSIGNED_SHORT_4_4_4_4_REV; + image->internalFormat = GL_ARGB4444_TWID_KOS; + image->transferFormat = GL_BGRA; + image->transferType = GL_UNSIGNED_SHORT_4_4_4_4_REV_TWID_KOS; return 1; } } @@ -141,30 +146,14 @@ int dtex_to_gl_texture(texture *tex, char* filename) { glGenTextures(1, &texture_id); glBindTexture(GL_TEXTURE_2D, texture_id); // 2d texture (x and y size) - GLint newFormat = GL_RGB; - GLint colorType = GL_RGB; - - if (image.internalFormat == GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS || - image.internalFormat == GL_UNSIGNED_SHORT_4_4_4_4_REV){ - newFormat = GL_BGRA; - colorType = GL_RGBA; - printf("Reversing RGBA\n"); - } - - if (image.internalFormat == GL_UNSIGNED_SHORT_5_6_5_REV){ - newFormat = GL_RGB; - colorType = GL_RGB; - printf("Reversing RGB\n"); - } - if (image.compressed) { glCompressedTexImage2D(GL_TEXTURE_2D, 0, image.internalFormat, image.sizeX, image.sizeY, 0, image.dataSize, image.data); } else { glTexImage2D(GL_TEXTURE_2D, 0, - colorType, image.sizeX, image.sizeY, 0, - newFormat, image.internalFormat, image.data); + image.internalFormat, image.sizeX, image.sizeY, 0, + image.transferFormat, image.transferType, image.data); } tex->id = texture_id; diff --git a/samples/depth_funcs_alpha_testing/gl_png.h b/samples/depth_funcs_alpha_testing/gl_png.h index 0639e36..4b16eec 100644 --- a/samples/depth_funcs_alpha_testing/gl_png.h +++ b/samples/depth_funcs_alpha_testing/gl_png.h @@ -29,7 +29,7 @@ typedef struct Image { unsigned long sizeX; unsigned long sizeY; char *data; - GLenum internalFormat; + GLenum internalFormat, transferFormat, transferType; unsigned int mipmapped, compressed, twiddled; unsigned int dataSize; } Image; From 62cb72091d8da93a3ab2e1ccff5517250741f82e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 5 Feb 2025 21:47:35 +1100 Subject: [PATCH 4/5] Fix wrong glEnable call --- samples/depth_funcs_alpha_testing/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/depth_funcs_alpha_testing/main.c b/samples/depth_funcs_alpha_testing/main.c index 517d6a0..257b093 100644 --- a/samples/depth_funcs_alpha_testing/main.c +++ b/samples/depth_funcs_alpha_testing/main.c @@ -88,7 +88,7 @@ void DrawGLScene() glLoadIdentity(); glTranslated(-1 , -1, -5); glDepthFunc(GL_LESS); - glEnable(GL_DEPTH_FUNC); + glEnable(GL_DEPTH_TEST); for (int i = 0; i < 5; i++) { glTranslated(0.5, 0, -0.2); From 41e30028df8ffa68b36a9426dfb3229171afc475 Mon Sep 17 00:00:00 2001 From: GPF Date: Wed, 5 Feb 2025 10:42:05 -0700 Subject: [PATCH 5/5] fixed gluPerspective to use GLdouble instead of GLfloat for m4-single compilation. --- GL/glu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GL/glu.c b/GL/glu.c index 2ed4444..9b2f48d 100644 --- a/GL/glu.c +++ b/GL/glu.c @@ -4,9 +4,9 @@ /* Set the Perspective */ void APIENTRY gluPerspective(GLdouble angle, GLdouble aspect, GLdouble znear, GLdouble zfar) { - GLfloat fW, fH; + GLdouble fW, fH; - fH = tanf(angle * (M_PI / 360.0f)) * znear; + fH = tan(angle * (M_PI / 360.0)) * znear; fW = fH * aspect; glFrustum(-fW, fW, -fH, fH, znear, zfar);