From: slack Date: Thu, 16 Oct 2008 00:16:43 +0000 (+0200) Subject: Texture improvements. Renamed models/ to data/ X-Git-Url: http://slack.codemaniacs.com/git/?a=commitdiff_plain;h=f724e69acde2674e2175e7d369a727c900ac52df;p=laz0r.git Texture improvements. Renamed models/ to data/ - TGA GU_PSM_8888 to LTX GU_PSM_5650 conversion - Moved texture binding to Gfx in order to have all psp-dependent code together. - swizzling and unswizzling - models/ renamed to data/ --- diff --git a/models/ship.lob b/data/ship.lob similarity index 100% rename from models/ship.lob rename to data/ship.lob diff --git a/models/ship.tga b/data/ship.tga similarity index 100% rename from models/ship.tga rename to data/ship.tga diff --git a/models/shipdawhip.lob b/data/shipdawhip.lob similarity index 100% rename from models/shipdawhip.lob rename to data/shipdawhip.lob diff --git a/data/shipdawhip.ltx b/data/shipdawhip.ltx new file mode 100644 index 0000000..f4c4f8f Binary files /dev/null and b/data/shipdawhip.ltx differ diff --git a/models/shipdawhip.tga b/data/shipdawhip.tga similarity index 100% rename from models/shipdawhip.tga rename to data/shipdawhip.tga diff --git a/game.cc b/game.cc index 9099a59..07365fa 100644 --- a/game.cc +++ b/game.cc @@ -17,8 +17,8 @@ int game_main(int argc, char **argv) GFX.init(); //Model cube("colors-noUV.lob"); - Model cube("models/shipdawhip.lob"); - Texture texture("models/shipdawhip.tga"); + Model cube("data/shipdawhip.lob"); + Texture texture("data/shipdawhip.ltx"); //sceKernelDcacheWritebackRange(cube.vertices, cube.num_vertices * sizeof(Vertex)); //sceKernelDcacheWritebackRange(cube.indices, cube.num_faces * sizeof(unsigned short)); //sceKernelDcacheWritebackRange(&cube, sizeof(cube)); @@ -41,7 +41,7 @@ int game_main(int argc, char **argv) sceGumRotateY(0.001f*i); sceGumRotateZ(0.001f*i); sceGuColor(GU_COLOR(1.0f,1.0f,1.0f,0.0f)); - texture.bind(); + GFX.bind_texture(texture); sceGumDrawArray(GU_TRIANGLES, cube.vtype | GU_TRANSFORM_3D, cube.num_faces*3, cube.indices, cube.vertices); diff --git a/gfx.cc b/gfx.cc index c7f1b5d..b562585 100644 --- a/gfx.cc +++ b/gfx.cc @@ -114,4 +114,8 @@ void Gfx::look_at(float eye_x, float eye_y, float eye_z, sceGumLookAt(&eye, ¢er, &up); } - +void Gfx::bind_texture(const Texture& t) +{ + sceGuTexMode(t.mode, 0, 0, t.swizzled? 1: 0); + sceGuTexImage(0, t.width, t.height, t.width, t.pixels); +} diff --git a/gfx.h b/gfx.h index 7ef25e5..00d5848 100644 --- a/gfx.h +++ b/gfx.h @@ -2,6 +2,7 @@ #define GFX_H #include "singleton.h" +#include "texture.h" #include class Gfx: public Singleton @@ -25,11 +26,14 @@ class Gfx: public Singleton void update_fps(); void swap_buffers(); + // Camera controls; void perspective(float fov, float aspect, float znear, float zfar); - void look_at(ScePspFVector3 *eye, ScePspFVector3 *center, ScePspFVector3 *up); void look_at(float eye_x, float eye_y, float eye_z, float center_x, float center_y, float center_z, float up_x, float up_y, float up_z); + + // Texture + void bind_texture(const Texture& t); }; #define GFX Gfx::Instance() diff --git a/models/colors-noUV.lob b/models/colors-noUV.lob deleted file mode 100644 index bc44456..0000000 Binary files a/models/colors-noUV.lob and /dev/null differ diff --git a/texture.cc b/texture.cc index 46de12f..706a985 100644 --- a/texture.cc +++ b/texture.cc @@ -33,7 +33,7 @@ unsigned int Texture::get_bpp() default: printf("Error: Unsupported texture type\n"); } - return -1; + return 0; } // LTX == Laz0r TeXture ;) @@ -51,7 +51,7 @@ void Texture::readLTX(const char *filename) FILE *fp = fopen(filename, "rb"); if(!fp) { - printf("Error, couldn't open file\n"); + printf("Texture::readLTX: Error, couldn't open file %s\n", filename); return; } @@ -60,7 +60,7 @@ void Texture::readLTX(const char *filename) fread(&header, sizeof(LTXHeader), 1, fp); if (strncmp("lzrT", header.sig, 4)) { - printf("Error: not a LTX file\n"); + printf("Texture::readLTX: Error, %s is not a LTX file\n", filename); return; } @@ -97,7 +97,7 @@ void Texture::readTGA(const char *filename) if(!fp) { - printf("Error, couldn't open file\n"); + printf("Texture::readTGA: Error, couldn't open file %s\n", filename); return; } @@ -109,7 +109,7 @@ void Texture::readTGA(const char *filename) if(memcmp(unCompressHeader, tgaHeader, sizeof(unCompressHeader)) != 0) { fclose(fp); - printf("Error, not an uncompressed TGA file\n"); + printf("Texture::readTGA: Error, %s is not an uncompressed TGA file\n", filename); width=0; height=0; pixels=0; @@ -153,8 +153,6 @@ void Texture::readTGA(const char *filename) // Close the file fclose(fp); - swizzle(); - swizzle(); swizzle(); printf("Read TGA texture %s (%ux%u)\n", filename, width, height); @@ -167,17 +165,11 @@ Texture::~Texture() delete[] pixels; } -void Texture::bind() -{ - sceGuTexMode(mode, 0, 0, swizzled? 1: 0); - sceGuTexImage(0, width, height, width, pixels); -} - void Texture::swizzle() { unsigned int bytes_per_pixel = get_bpp()/8; unsigned int i,j; - unsigned int rowblocks = (width * bytes_per_pixel / 16); + unsigned int row_blocks = (width * bytes_per_pixel / 16); long size = width * height * long(bytes_per_pixel); unsigned char* out = new unsigned char[size]; @@ -191,14 +183,39 @@ void Texture::swizzle() unsigned int x = (i - blockx*16); unsigned int y = (j - blocky*8); - unsigned int block_index = blockx + ((blocky) * rowblocks); + unsigned int block_index = blockx + ((blocky) * row_blocks); unsigned int block_address = block_index * 16 * 8; out[block_address + x + y * 16] = pixels[i+j*width*bytes_per_pixel]; } } memcpy(pixels, out, size ); - swizzled = !swizzled; + swizzled = true; + delete[] out; +} + +void Texture::unswizzle() +{ + unsigned int bytes_per_pixel = get_bpp()/8; + long size = width * height * long(bytes_per_pixel); + unsigned int total_blocks = size/(16*8); + unsigned int row_blocks = total_blocks/(height/8); + + unsigned char* out = new unsigned char[size]; + + for (unsigned int block_index=0; block_index < total_blocks; block_index++) + { + for (unsigned int block_offset = 0; block_offset < 16*8; ++block_offset) + { + unsigned int block_y = block_index / row_blocks; + unsigned int block_x = block_index % row_blocks; + unsigned int y = block_offset/16; + unsigned int x = block_offset%16; + out[(block_y*8+y)*width*bytes_per_pixel+16*block_x+x] = pixels[block_index*(16*8)+block_offset]; + } + } + memcpy(pixels, out, size ); + swizzled = false; delete[] out; } @@ -211,12 +228,13 @@ bool Texture::saveLTX(const char *filename, int dst_mode) // FIXME: Swizzle != Unswizzle :( // Save current parameters + unsigned int src_bpp=get_bpp(); int src_mode = mode; unsigned char *src_pixels = pixels; bool src_swizzled = swizzled; // Undo swizzling, if any - if (src_swizzled) swizzle(); + if (src_swizzled) unswizzle(); if (mode != GU_PSM_8888) { @@ -230,7 +248,7 @@ bool Texture::saveLTX(const char *filename, int dst_mode) // convert pixel format int dst_index=0; - for (int src_index=0; src_index < width*height*4; src_index+=4) + for (int src_index=0; src_index < width*height*4; src_index+=src_bpp/8) { unsigned char r = src_pixels[src_index]; unsigned char g = src_pixels[src_index+1]; @@ -240,7 +258,7 @@ bool Texture::saveLTX(const char *filename, int dst_mode) switch(dst_mode) { case GU_PSM_5650: { - unsigned short val = ((r>>3) << 11) | ((g>>2) << 5) | (b>>3); + unsigned short val = ((b>>3) << 11) | ((g>>2) << 5) | (r>>3); ((unsigned short *)pixels)[dst_index] = val; dst_index++; break; diff --git a/texture.h b/texture.h index 9bd5f93..dfe090b 100644 --- a/texture.h +++ b/texture.h @@ -13,12 +13,11 @@ struct Texture Texture(const char *filename); ~Texture(); - void bind(); - bool saveLTX(const char *filename, int dst_mode); private: void swizzle(); + void unswizzle(); unsigned int get_bpp(); void readTGA(const char *filename); void readLTX(const char *filename);