Load textured models :D
authorslack <slack@codemaniacs.com>
Mon, 6 Oct 2008 01:52:36 +0000 (03:52 +0200)
committerslack <slack@codemaniacs.com>
Mon, 6 Oct 2008 01:52:36 +0000 (03:52 +0200)
Makefile
game.cc
gfx.cc
main.cc
model.cc
model.h
models/ship.lob [new file with mode: 0644]
models/ship.tga [new file with mode: 0644]
texture.cc [new file with mode: 0644]
texture.h [new file with mode: 0644]
tools/blender_exporter.py

index 109a3d30fff83fe6be0f99c3209cab94bbd43a24..fea16dc3132c5c22d743fff3d321da73820228d4 100644 (file)
--- 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 9b24946003a59c70b42cf6443cb93215d169959a..2bcfab71ef8676dff7c9bd484b71626b4d3510d2 100644 (file)
--- a/game.cc
+++ b/game.cc
@@ -1,6 +1,7 @@
 #include "game.h"
 #include "gfx.h"
 #include "model.h"
+#include "texture.h"
 
 #include <pspgu.h>
 #include <pspgum.h>
@@ -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 12ff231018bc866933a2a35814f5d98c5e5d0829..6268c6cb4f9083e926f573053c67073bd220f31f 100644 (file)
--- 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 ef7a0aa415bcbcb76b28c3591837fc2688d81975..13f8a36b2e22336c08f9783aa42868f6364e9d9e 100644 (file)
--- 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();
index a329e874c3c654ffdb50a9dde56d86f22350966d..48820f98e57711fe16954f9a72588714c733f556 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -2,6 +2,11 @@
 #include <cstdio>
 #include <cstring>
 
+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<num_vertices; i++)
+       {
+               if (isnan(vertices[i].u) || isnan(vertices[i].v))
+                       printf("NaN!\n");
+       }
+
+       printf("%d vertices, %d faces\n", num_vertices, num_faces);
 }
 
 Model::~Model()
diff --git a/model.h b/model.h
index d3ad20ba1e2a67c9b1cead10df49597a76c7bb06..f217b01500db07b8c43524009357e43a076a6c32 100644 (file)
--- a/model.h
+++ b/model.h
@@ -5,7 +5,7 @@
 
 struct Vertex
 {
-       //float u,v;
+       float u,v;
        u32 color;
        float nx,ny,nz;
        float x,y,z;
diff --git a/models/ship.lob b/models/ship.lob
new file mode 100644 (file)
index 0000000..850e5c4
Binary files /dev/null and b/models/ship.lob differ
diff --git a/models/ship.tga b/models/ship.tga
new file mode 100644 (file)
index 0000000..0379476
Binary files /dev/null and b/models/ship.tga differ
diff --git a/texture.cc b/texture.cc
new file mode 100644 (file)
index 0000000..11b34c9
--- /dev/null
@@ -0,0 +1,88 @@
+#include "texture.h"
+
+#include <cstdio>
+#include <cstring>
+
+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 (file)
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
+
index b1955c98cbe0ca7ff2f76fa9a12b62e2df7ba369..7f0bc2dcfe5ca182e5911a127db77c1baf1e7813 100644 (file)
@@ -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("<H", i))