Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The main tools used were Ghidra, Ida and windbg. The process of creating the ini
## Current status / TODO:

- Lighting is working, including bounce and phong.
- Supports JPG, TGA, PCX and WAL. Not supported yet: M32 and M8 formats.
- Supports JPG, TGA, PNG, PCX and WAL. Not supported yet: M32 and M8 formats.
- The -update flag is not implemented yet.
- Performance test.
- Test sunlight, all command args and entity options.
Expand Down
87 changes: 70 additions & 17 deletions src/blarghrad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ extern "C" {
#include "jpeglib.h"
}

// [slipyx] add png support
#define CUTE_PNG_IMPLEMENTATION
#include "cute_png.h"

// GLOBALS
vec3_t ambient = { 0, 0, 0 };
int game = 0;
Expand Down Expand Up @@ -770,24 +774,28 @@ void LoadTGA(char *name, byte **pixels, int *width, int *height)
fclose(fin);
}

int TryLoadTGA(int txnum)
// [slipyx] load rgba PNG data using cute_png
void LoadPNG(char *name, byte **pixels, int *width, int *height)
{
char filename[1024];

byte* pic = nullptr;
const char* texturename = texinfo[txnum].texture;
sprintf(filename, "textures/%s.tga", texturename);
if (!RelativeFileExists(filename)) {
return 0;
}

int width, height;
LoadTGA(filename, &pic, &width, &height);
if (!pic) {
return 0;
}
int pnglen = 0;
FILE* pngfile = OpenFileFromDiskOrPak(name, &pnglen);
if (!pngfile)
pngfile = fopen(name, "rb");
if (!pngfile)
Error("Couldn't read %s", name);
byte* pngbuf = (byte*)malloc(pnglen);
fread(pngbuf, pnglen, 1, pngfile);
fclose(pngfile);
cp_image_t pngimg = cp_load_png_mem(pngbuf, pnglen);
*width = pngimg.w;
*height = pngimg.h;
*pixels = (byte*)(pngimg.pix);
}

projtexture_t *projtex = CreateProjTexture(texturename, width, height);
// [slipyx] calculates average of rgba data and sets texture reflectivity
void SetTextureReflectivityFromRGBA(int txnum, const char *name, byte *pic, int width, int height)
{
projtexture_t *projtex = CreateProjTexture(name, width, height);
int pixelcount = width * height;

vec3_t sum_for_avg;
Expand Down Expand Up @@ -815,7 +823,7 @@ int TryLoadTGA(int txnum)
}
}
if (projtex) {
StoreTextureForProjection(projtex, texturename);
StoreTextureForProjection(projtex, name);
}
if (num_transparent == pixelcount) {
VectorClear(texture_reflectivity[txnum]);
Expand All @@ -827,11 +835,55 @@ int TryLoadTGA(int txnum)
texture_reflectivity[txnum].z = sum_for_avg.z * fVar2 / 255;
}
SetTextureReflectivity(txnum);
}

int TryLoadTGA(int txnum)
{
char filename[1024];

byte* pic = nullptr;
const char* texturename = texinfo[txnum].texture;
sprintf(filename, "textures/%s.tga", texturename);
if (!RelativeFileExists(filename)) {
return 0;
}

int width, height;
LoadTGA(filename, &pic, &width, &height);
if (!pic) {
return 0;
}

SetTextureReflectivityFromRGBA(txnum, texturename, pic, width, height);

free(pic);
return 1;
}


int TryLoadPNG(int txnum)
{
char filename[1024];

byte* pic = nullptr;
const char* texturename = texinfo[txnum].texture;
sprintf(filename, "textures/%s.png", texturename);
if (!RelativeFileExists(filename)) {
return 0;
}

int width, height;
LoadPNG(filename, &pic, &width, &height);
if (!pic) {
return 0;
}

SetTextureReflectivityFromRGBA(txnum, texturename, pic, width, height);

free(pic);
return 1;
}

void maybe_LoadJPG(const char* filename, byte** bytes, int* width, int* height)
{
jpeg_decompress_struct cinfo;
Expand Down Expand Up @@ -1178,6 +1230,7 @@ void CalcTextureReflectivityMain(void)
// if first time encountering, calculate ref
if (iVar4 == txnum) {
if (!TryLoadTGA(txnum) &&
!TryLoadPNG(txnum) &&
!TryLoadJPG(txnum) &&
!TryLoadM32(txnum) &&
!TryLoadM8(txnum) &&
Expand Down
Loading