Basic texture packing works and outputs a file
This commit is contained in:
parent
21e7d15a1b
commit
6c23b1a0c7
@ -6,6 +6,8 @@
|
|||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#define STBI_FAILURE_USERMSG
|
#define STBI_FAILURE_USERMSG
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
|
#include "stb_image_write.h"
|
||||||
|
|
||||||
struct image {
|
struct image {
|
||||||
char* filename;
|
char* filename;
|
||||||
@ -63,31 +65,18 @@ int main(int argc, char* argv[]) {
|
|||||||
int max = totalh > maxw ? totalh : maxw;
|
int max = totalh > maxw ? totalh : maxw;
|
||||||
int exp = (int)log2(max) + 1;
|
int exp = (int)log2(max) + 1;
|
||||||
int size = (int)pow(2, exp);
|
int size = (int)pow(2, exp);
|
||||||
unsigned char* buf = calloc(size*size, sizeof(char));
|
unsigned char* buf = calloc(size*size*4, sizeof(char));
|
||||||
(void)buf;
|
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
// Now load each texture one by one, write to the new image buffer, and free, before
|
// Now load the data into memory, for now, let's just load all images in one go,
|
||||||
// loading the next one
|
// however, it would be more optimized if we loaded each image sequentially or
|
||||||
|
// better yet in parallel so that we can write to the different parts of the
|
||||||
|
// texture atlas concurrently
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
for (int i = 0; i < argc - 1; i++) {
|
|
||||||
images[i].filename = argv[i+1];
|
|
||||||
|
|
||||||
// int width, height, channels;
|
for (int i = 0; i < image_count; i++) {
|
||||||
// images[i].image_data = (unsigned char*)stbi_load(images[i].filename,
|
int w,h,c;
|
||||||
// &width,
|
images[i].image_data = (unsigned char*)stbi_load(images[i].filename, &w, &h, &c, 4);
|
||||||
// &height,
|
|
||||||
// &channels,
|
|
||||||
// 4);
|
|
||||||
|
|
||||||
// images[i].width = (short)width;
|
|
||||||
// images[i].height = (short)height;
|
|
||||||
// images[i].channels = (char)channels;
|
|
||||||
images[i].image_data = (unsigned char*)stbi_load(images[i].filename,
|
|
||||||
&images[i].width,
|
|
||||||
&images[i].height,
|
|
||||||
&images[i].channels,
|
|
||||||
4);
|
|
||||||
if (images[i].image_data == NULL) {
|
if (images[i].image_data == NULL) {
|
||||||
char *errorMsg = "Could not load data for image '%s': %s";
|
char *errorMsg = "Could not load data for image '%s': %s";
|
||||||
fprintf(stderr, errorMsg, images[i].filename, stbi_failure_reason());
|
fprintf(stderr, errorMsg, images[i].filename, stbi_failure_reason());
|
||||||
@ -95,18 +84,40 @@ int main(int argc, char* argv[]) {
|
|||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// unsigned char* image_bytes = images[0].image_data;
|
|
||||||
// int b = 0;
|
//-------------------------------------
|
||||||
// while (image_bytes) {
|
// Just write the bytes directly for now, we don't have any "rect" data, which
|
||||||
// if (image_bytes[b]) {
|
// we need to make this work, since we're "hardcoding" the positions of the textures
|
||||||
// printf("R:%u G:%u B:%u A:%u\n", image_bytes[b], image_bytes[b+1], image_bytes[b+2], image_bytes[b+3]);
|
//-------------------------------------
|
||||||
// }
|
|
||||||
// b += 4;
|
int atlasrow = 0;
|
||||||
// image_bytes += 4;
|
for (int i = 0; i < image_count; i++) {
|
||||||
// }
|
int width = images[i].width;
|
||||||
|
int length = images[i].width * images[i].height * images[i].channels;
|
||||||
|
int color_row = 0;
|
||||||
|
int color_col = 0;
|
||||||
|
int c = 0;
|
||||||
|
while (c < length) {
|
||||||
|
int bidx = atlasrow * maxw * 4 + color_row;
|
||||||
|
int iidx = color_col * width * 4 + color_row;
|
||||||
|
buf[bidx] = images[i].image_data[iidx];
|
||||||
|
color_row++;
|
||||||
|
if (color_row > width * 4) {
|
||||||
|
color_row = 0;
|
||||||
|
color_col += 1;
|
||||||
|
atlasrow += 1;
|
||||||
|
}
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stbi_write_png("test.png", maxw, totalh, 4, buf, maxw * 4);
|
||||||
|
|
||||||
|
//-------------------------------------
|
||||||
|
// Free them all, but eventually we would free after we finished 1
|
||||||
|
//-------------------------------------
|
||||||
for (int i = 0; i < image_count; i++) {
|
for (int i = 0; i < image_count; i++) {
|
||||||
stbi_image_free(images[i].image_data);
|
stbi_image_free(images[i].image_data);
|
||||||
}
|
}
|
||||||
// for (int i = )
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user