diff --git a/img2array.py b/img2array.py index 7bab2a0..11396a2 100644 --- a/img2array.py +++ b/img2array.py @@ -20,6 +20,8 @@ if len(sys.argv) < 2: quit() FILENAME = "" +PALETTE = "" +USE_PALETTE = False NAME = "texture" GUARDS = False OUT_WIDTH = 64 @@ -34,17 +36,34 @@ for s in sys.argv: GUARDS = True elif s[:2] == "-n": NAME = s[2:] + elif s[:2] == "-p": + PALETTE = s[2:] + USE_PALETTE = True else: FILENAME = s +imageArray = [] +paletteColors = [] +paletteArray = [] + image = Image.open(FILENAME).convert("RGB") pixels = image.load() +if len(PALETTE) > 0: + palette = Image.open(PALETTE).convert("RGB") + pixelsPal = palette.load() + + for y in range(palette.size[1]): + for x in range(palette.size[0]): + c = pixelsPal[x,y] + paletteColors.append(c) + paletteArray.append(c[0]) + paletteArray.append(c[1]) + paletteArray.append(c[2]) + image2 = Image.new("RGB",(OUT_WIDTH,OUT_HEIGHT),color="white") pixels2 = image2.load() -imageArray = [] - for y in range(OUT_HEIGHT): for x in range(OUT_WIDTH): coord = ( @@ -53,27 +72,38 @@ for y in range(OUT_HEIGHT): pixel = pixels[coord] - imageArray.append(pixel) + if USE_PALETTE: + closestIndex = 0 + closestDiff = 1024 - pixels2[x,y] = pixel + # find the index of the closest color: + + for i in range(len(paletteColors)): + c = paletteColors[i] + diff = abs(pixel[0] - c[0]) + abs(pixel[1] - c[1]) + abs(pixel[2] - c[2]) + + if diff < closestDiff: + closestIndex = i + closestDiff = diff + + imageArray.append(closestIndex) + pixels2[x,y] = paletteColors[closestIndex] + else: + imageArray.append(pixel[0]) + imageArray.append(pixel[1]) + imageArray.append(pixel[2]) + pixels2[x,y] = pixel #----------------------- -if GUARDS: - print("#ifndef " + NAME.upper() + "_TEXTURE_H") - print("#define " + NAME.upper() + "_TEXTURE_H\n") +def printArray(array, name, sizeString): + print("uint8_t " + name + "[" + sizeString + "] = {") + arrayString = "" -print("#define " + NAME.upper() + "_WIDTH " + str(OUT_WIDTH)) -print("#define " + NAME.upper() + "_HEIGHT " + str(OUT_HEIGHT)) -print("") -print("uint8_t " + NAME + "Texture[" + NAME.upper() + "_WIDTH * " + NAME.upper() + "_HEIGHT * 3] = {") -arrayString = "" + lineLen = 0 -lineLen = 0 - -for v in imageArray: - for c in v: - item = str(c) + "," + for v in array: + item = str(v) + "," lineLen += len(item) @@ -83,8 +113,20 @@ for v in imageArray: arrayString += item -print(arrayString[:-1]) -print("}; // " + NAME + "Texture") + print(arrayString[:-1]) + print("}; // " + name + "\n") + +if GUARDS: + print("#ifndef " + NAME.upper() + "_TEXTURE_H") + print("#define " + NAME.upper() + "_TEXTURE_H\n") + +if USE_PALETTE: + printArray(paletteArray,NAME + "Palette",str(len(paletteArray))) + +printArray(imageArray,NAME + "Texture",NAME.upper() + "_WIDTH * " + NAME.upper() + "_HEIGHT * 3") + +print("#define " + NAME.upper() + "_WIDTH " + str(OUT_WIDTH)) +print("#define " + NAME.upper() + "_HEIGHT " + str(OUT_HEIGHT)) if GUARDS: print("\n#endif // guard") diff --git a/small3dlib.h b/small3dlib.h index a866b11..4ddcf0d 100644 --- a/small3dlib.h +++ b/small3dlib.h @@ -453,6 +453,7 @@ typedef struct - 0 none - 1 clock-wise - 2 counter clock-wise */ + int8_t visible; /**< Can be used to easily hide the model. */ } S3L_DrawConfig; void S3L_initDrawConfig(S3L_DrawConfig *config); @@ -1042,6 +1043,7 @@ void S3L_initPixelInfo(S3L_PixelInfo *p) // TODO: maybe non-pointer for p void S3L_initDrawConfig(S3L_DrawConfig *config) { config->backfaceCulling = 1; + config->visible = 1; } static inline void S3L_PIXEL_FUNCTION(S3L_PixelInfo *pixel); // forward decl @@ -1797,6 +1799,9 @@ void S3L_drawScene(S3L_Scene scene) for (modelIndex = 0; modelIndex < scene.modelCount; ++modelIndex) { + if (!scene.models[modelIndex].config.visible) + continue; + #if S3L_SORT != 0 if (S3L_sortArrayLength >= S3L_MAX_TRIANGES_DRAWN) break; diff --git a/testSDL.c b/testSDL.c index b833b42..90bade2 100644 --- a/testSDL.c +++ b/testSDL.c @@ -12,7 +12,7 @@ #define S3L_FLAT 0 #define S3L_STRICT_NEAR_CULLING 0 -#define S3L_PERSPECTIVE_CORRECTION 0 +#define S3L_PERSPECTIVE_CORRECTION11 #define S3L_SORT 0 #define S3L_Z_BUFFER 1 diff --git a/todo.txt b/todo.txt index 7275eac..1762122 100644 --- a/todo.txt +++ b/todo.txt @@ -8,6 +8,10 @@ features: - back-to-front (slower, better memory efficiency) DONE - front-to-back (faster, but needs 1bit stencil buffer) DONE +- dithered barycentric interpolation function that is faster than normal + interpolation -- it will randomly picky one of three values, with greater + probabilities at greater coords + - option to disable baycentric coordinates computing DONE - Z-buffer: