From: slack Date: Mon, 6 Oct 2008 01:52:36 +0000 (+0200) Subject: Load textured models :D X-Git-Url: http://slack.codemaniacs.com/git/?a=commitdiff_plain;h=4973666952e1bc2d65e51244ee2d83b14528ccc7;p=laz0r.git Load textured models :D --- diff --git a/Makefile b/Makefile index 109a3d3..fea16dc 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ TARGET = laz0r -OBJS = main.o game.o gfx.o model.o +OBJS = main.o game.o gfx.o model.o texture.o PSP_FW_VERSION=401 BUILD_PRX=1 diff --git a/game.cc b/game.cc index 9b24946..2bcfab7 100644 --- a/game.cc +++ b/game.cc @@ -1,6 +1,7 @@ #include "game.h" #include "gfx.h" #include "model.h" +#include "texture.h" #include #include @@ -16,7 +17,8 @@ int game_main(int argc, char **argv) GFX.init(); //Model cube("colors-noUV.lob"); - Model cube("models/colors-noUV.lob"); + Model cube("models/ship.lob"); + Texture texture("models/ship.tga"); //sceKernelDcacheWritebackRange(cube.vertices, cube.num_vertices * sizeof(Vertex)); //sceKernelDcacheWritebackRange(cube.indices, cube.num_faces * sizeof(unsigned short)); //sceKernelDcacheWritebackRange(&cube, sizeof(cube)); @@ -42,13 +44,14 @@ 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)); + sceGuTexImage( 0, texture.width, texture.height, texture.width, texture.pixels ); sceGumDrawArray(GU_TRIANGLES, - /*GU_TEXTURE_32BITF |*/ GU_COLOR_8888 | GU_NORMAL_32BITF | + GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_NORMAL_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_3D | GU_INDEX_16BIT, cube.num_faces*3, cube.indices, cube.vertices); - ScePspFVector3 move={2,0,0}; + ScePspFVector3 move={4,0,0}; sceGumLoadIdentity(); sceGumRotateZ(0.001f*i); sceGumTranslate(&move); diff --git a/gfx.cc b/gfx.cc index 12ff231..6268c6c 100644 --- a/gfx.cc +++ b/gfx.cc @@ -13,7 +13,7 @@ void Gfx::init() { - display_list = memalign(16, 2048); + display_list = memalign(16, 8192); fbp0 = 0; pspDebugScreenInit(); @@ -39,14 +39,22 @@ void Gfx::init() sceGuEnable( GU_DEPTH_TEST ); sceGuShadeModel( GU_SMOOTH ); sceGuFrontFace( GU_CW ); - sceGuEnable( GU_CULL_FACE ); - sceGuEnable( GU_CLIP_PLANES ); + //sceGuEnable( GU_CULL_FACE ); + //sceGuEnable( GU_CLIP_PLANES ); // set clear color/depth - sceGuClearColor( GU_COLOR( 0.0f, 0.0f, 0.0f, 1.0f ) ); + sceGuClearColor( GU_COLOR( 1.0f, 0.0f, 1.0f, 1.0f ) ); sceGuClearDepth(0); - sceGuFinish(); + // setup texture + sceGuEnable(GU_TEXTURE_2D); //Enable Texture2D + sceGuTexMode( GU_PSM_8888, 0, 0, 0 ); // last 0 must be 1 if swizzled + sceGuTexFunc( GU_TFX_DECAL, GU_TCC_RGB ); + sceGuTexFilter( GU_LINEAR, GU_LINEAR ); // Linear filtering (Good Quality) (NEW) + sceGuTexScale( 1.0f, 1.0f ); // No scaling + sceGuTexOffset( 0.0f, 0.0f ); + + sceGuFinish(); sceGuSync(0,0); sceDisplayWaitVblankStart(); diff --git a/main.cc b/main.cc index ef7a0aa..13f8a36 100644 --- a/main.cc +++ b/main.cc @@ -11,6 +11,8 @@ PSP_MODULE_INFO("laz0r", 0, 1, 1); /* Define the main thread's attribute value (optional) */ PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU); +PSP_HEAP_SIZE_MAX(); + // Exit callback int exit_callback(int arg1, int arg2, void *common) { sceKernelExitGame(); diff --git a/model.cc b/model.cc index a329e87..48820f9 100644 --- a/model.cc +++ b/model.cc @@ -2,6 +2,11 @@ #include #include +inline bool isnan(float x) +{ + return x!=x; +} + struct ModelHeader { char sig[8]; // "laz0rOBJ" @@ -31,6 +36,14 @@ Model::Model(const char *filename) fread(vertices, sizeof(Vertex), num_vertices, f); fread(indices, sizeof(unsigned short), 3*num_faces, f); fclose(f); + + for (int i=0; i +#include + +Texture::Texture(const char *filename) +{ + FILE *pFile = 0; + + // Variables to hold image info + unsigned char tempColor; + unsigned char bitCount; + int colorMode; + long tgaSize; + unsigned char unCompressHeader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + unsigned char tgaHeader[12]; + unsigned char header[6]; + + swizzled=false; + + // Open file + pFile = fopen( filename, "rb" ); + + if( !pFile ) { + // if couldn't open, exit + return; + } + + // Read in BitmapHeader info into the structure + fread( &tgaHeader, 1, sizeof(tgaHeader), pFile ); + + // We only want to read uncompressed TGA's. Chech the + // header if it is of an uncompressed one. + if( memcmp( unCompressHeader, tgaHeader, sizeof(unCompressHeader)) != 0 ) { + fclose( pFile ); + printf("Error, not an uncompressed TGA file\n"); + width=0; + height=0; + pixels=0; + return; + } + + // Read Image info + fread( header, 1, sizeof(header), pFile ); + + // Calculate and save the Width & Height of Image + width = header[1] * 256 + header[0]; + height = header[3] * 256 + header[2]; + bitCount = header[4]; + + // Calculate color mode & image size + colorMode = bitCount / 8; + tgaSize = width * height * colorMode; + printf("width=%hd, height=%hd, tgaSize=%ld\n", width, height, tgaSize); + + // Allocate memory for the image and load it + pixels = new unsigned char[tgaSize]; + + fread( pixels, sizeof(unsigned char), tgaSize, pFile ); + + // Convert from BGR to RGB format. + for(long index = 0; index < tgaSize; index += colorMode) { + tempColor = pixels[index]; + pixels[index] = pixels[index + 2]; + pixels[index + 2] = tempColor; + } + + // Close the file + fclose( pFile ); + + printf("Read TGA texture %s (%ux%u)\n", filename, width, height); + + return; +} + +Texture::~Texture() +{ + delete[] pixels; +} + +void swizzle() +{ + +} + + + + diff --git a/texture.h b/texture.h new file mode 100644 index 0000000..21ef07c --- /dev/null +++ b/texture.h @@ -0,0 +1,18 @@ +#if !defined(TEXTURE_H) +#define TEXTURE_H + +struct Texture +{ + unsigned short width; + unsigned short height; + unsigned char *pixels; + bool swizzled; + + Texture(const char *filename); + ~Texture(); + + void swizzle(); +}; + +#endif + diff --git a/tools/blender_exporter.py b/tools/blender_exporter.py index b1955c9..7f0bc2d 100644 --- a/tools/blender_exporter.py +++ b/tools/blender_exporter.py @@ -47,6 +47,7 @@ def write(filename): if mesh.faceUV: u,v=face.uv[vert_i] + print "(u,v)=(%f,%f)"%(u,v) else: u,v=0,0 @@ -87,7 +88,7 @@ def write(filename): index_list.append(matching_index) #print matching_index - #print "end face" + print "end face" # write header f = file(filename, "w") @@ -99,8 +100,8 @@ def write(filename): r,g,b,a = vert["col"] nx,ny,nz = vert["no"] x,y,z = vert["co"] - #f.write(struct.pack("< ff BBBB fff fff", u,v, r,g,b,a, nx, ny, nz, x,y,z)) # 36 bytes each - f.write(struct.pack("< BBBB fff fff", r,g,b,a, nx, ny, nz, x,y,z)) # 36 bytes each + f.write(struct.pack("< ff BBBB fff fff", u,v, r,g,b,a, nx, ny, nz, x,y,z)) # 36 bytes each + #f.write(struct.pack("< BBBB fff fff", r,g,b,a, nx, ny, nz, x,y,z)) # 28 bytes each for i in index_list: f.write(struct.pack("