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));
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);
#define GFX_H
#include "singleton.h"
+#include "texture.h"
#include <psptypes.h>
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()
default:
printf("Error: Unsupported texture type\n");
}
- return -1;
+ return 0;
}
// LTX == Laz0r TeXture ;)
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;
}
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;
}
if(!fp)
{
- printf("Error, couldn't open file\n");
+ printf("Texture::readTGA: Error, couldn't open file %s\n", filename);
return;
}
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;
// Close the file
fclose(fp);
- swizzle();
- swizzle();
swizzle();
printf("Read TGA texture %s (%ux%u)\n", filename, width, height);
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];
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;
}
// 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)
{
// 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];
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;