Texture improvements. Renamed models/ to data/
authorslack <slack@codemaniacs.com>
Thu, 16 Oct 2008 00:16:43 +0000 (02:16 +0200)
committerslack <slack@codemaniacs.com>
Thu, 16 Oct 2008 00:16:43 +0000 (02:16 +0200)
- 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/

data/ship.lob [moved from models/ship.lob with 100% similarity]
data/ship.tga [moved from models/ship.tga with 100% similarity]
data/shipdawhip.lob [moved from models/shipdawhip.lob with 100% similarity]
data/shipdawhip.ltx [new file with mode: 0644]
data/shipdawhip.tga [moved from models/shipdawhip.tga with 100% similarity]
game.cc
gfx.cc
gfx.h
models/colors-noUV.lob [deleted file]
texture.cc
texture.h

similarity index 100%
rename from models/ship.lob
rename to data/ship.lob
similarity index 100%
rename from models/ship.tga
rename to data/ship.tga
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 (file)
index 0000000..f4c4f8f
Binary files /dev/null and b/data/shipdawhip.ltx differ
similarity index 100%
rename from models/shipdawhip.tga
rename to data/shipdawhip.tga
diff --git a/game.cc b/game.cc
index 9099a590b0ea4ccbd265efbc173fbb1999d0c219..07365fa76df90ae53d58725cbf8237e9bf5e305c 100644 (file)
--- 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 c7f1b5dd6d3e7b457e0f99282c5bc5e72f7961fc..b562585da5737cd79d4153dcee93865dc0819f45 100644 (file)
--- 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, &center, &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 7ef25e5d03d6a64dae8633f409b9d052cc3d5887..00d584883b9f67b507405f4881b1b9d68a1f4071 100644 (file)
--- a/gfx.h
+++ b/gfx.h
@@ -2,6 +2,7 @@
 #define GFX_H
 
 #include "singleton.h"
+#include "texture.h"
 #include <psptypes.h>
 
 class Gfx: public Singleton<Gfx>
@@ -25,11 +26,14 @@ class Gfx: public Singleton<Gfx>
        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 (file)
index bc44456..0000000
Binary files a/models/colors-noUV.lob and /dev/null differ
index 46de12f51c6e75ef4dd55323e4b64742ab6bfcb1..706a985cf34804f4d87c10062a22dcbcc607ae99 100644 (file)
@@ -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;
index 9bd5f936cdf88bc9ba07bfe642b42f992193fa91..dfe090bb54534151149a22c8ab0c5dc79b3e6eae 100644 (file)
--- 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);