changeset 174:559b48b69b76

getScale()、getTapestry から、span->length に適切なテクスチャの選択に成功
author gongo@localhost.localdomain
date Fri, 12 Dec 2008 12:23:20 +0900
parents 56be4a6e5513
children 3cc5f0000e29
files TaskManager/Test/test_render/Makefile.def TaskManager/Test/test_render/SceneGraph.cpp TaskManager/Test/test_render/polygon_pack.h TaskManager/Test/test_render/spe/CreatePolygon.cpp TaskManager/Test/test_render/spe/CreateSpan.cpp TaskManager/Test/test_render/spe/CreateSpan.h TaskManager/Test/test_render/spe/DrawSpan.cpp TaskManager/Test/test_render/sys.cpp TaskManager/Test/test_render/task/CreatePolygon.cpp TaskManager/Test/test_render/task/CreatePolygonFromSceneGraph.cpp TaskManager/Test/test_render/task/CreateSpan.cpp TaskManager/Test/test_render/task/CreateSpan.h TaskManager/Test/test_render/task/DrawSpan.cpp TaskManager/Test/test_render/texture.h
diffstat 14 files changed, 184 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/TaskManager/Test/test_render/Makefile.def	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/Makefile.def	Fri Dec 12 12:23:20 2008 +0900
@@ -12,7 +12,7 @@
 #CERIUM = ../../..
 
 CC      = g++
-CFLAGS  = -O0 -g -Wall# -DDEBUG
+CFLAGS  = -O9 -g -Wall# -DDEBUG
 
 INCLUDE = -I$(CERIUM)/include/TaskManager -I.
 LIBS = -L$(CERIUM)/TaskManager
\ No newline at end of file
--- a/TaskManager/Test/test_render/SceneGraph.cpp	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/SceneGraph.cpp	Fri Dec 12 12:23:20 2008 +0900
@@ -324,13 +324,13 @@
 		}
 
 		uint32 *tex_dest;
+		int scale = 1;
 
 		{
 		    int t = 0;
 		    int tex_w = texture_image->w;
 		    int tex_h = texture_image->h;
 		    Uint32 *tex_src = (Uint32*)texture_image->pixels;
-		    int scale = 1;
 		    int scale_cnt;
 		    int cur_w = tex_w;
 		    int cur_h = tex_h;
@@ -349,11 +349,10 @@
 		     */
 		    while (cur_w % TEXTURE_SPLIT_PIXEL == 0 && 
 			   cur_h % TEXTURE_SPLIT_PIXEL == 0) {
-			printf("(%3d, %3d) = %4d\n", cur_w, cur_h, cur_w*cur_h);
 			all_pixel_num += cur_w*cur_h;
-			cur_w >>= 1;
+			cur_w >>= 1; /* cur_w /= 2 */
 			cur_h >>= 1;
-			scale <<= 1;
+			scale <<= 1; /* scale *= 2 */
 		    }
 
 		    scale >>= 1;
@@ -361,8 +360,6 @@
 		    tex_dest
 			= (uint32*)manager->malloc(sizeof(int)*all_pixel_num);
 
-		    printf("scale = %d, all = %d\n", scale, all_pixel_num);
-
 		    int diff = TEXTURE_SPLIT_PIXEL;
 		    int p_diff = 1;
 		    while (scale_cnt) {
@@ -376,7 +373,7 @@
 				}
 			    }
 			}
-
+			
 			diff <<= 1;
 			p_diff <<= 1;
 			scale_cnt >>= 1;
@@ -387,14 +384,16 @@
 		list[id_count-1].t_h = texture_image->h;
 		list[id_count-1].pixels_orig = (Uint32*)texture_image->pixels;
 		list[id_count-1].pixels = tex_dest;
+		list[id_count-1].scale_max = scale;
 
 		texture_id = id_count-1;
 		texture_info.t_w = texture_image->w;
 		texture_info.t_h = texture_image->h;
 		texture_info.pixels_orig = (Uint32*)texture_image->pixels;
 		texture_info.pixels = tex_dest;
+		texture_info.scale_max = scale;
 
-		if(unlink(image_name))
+		if (unlink(image_name))
 		{
 		    cout << "unlink error\n";
 		}
@@ -411,6 +410,7 @@
 		texture_info.t_h = list[tex_id].t_h;;
 		texture_info.pixels_orig = list[tex_id].pixels_orig;
 		texture_info.pixels = list[tex_id].pixels;
+		texture_info.scale_max = list[tex_id].scale_max;
 	    }
 	}
     }
--- a/TaskManager/Test/test_render/polygon_pack.h	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/polygon_pack.h	Fri Dec 12 12:23:20 2008 +0900
@@ -13,16 +13,22 @@
     float z;
     float tex_x;
     float tex_y;
-}VertexPack, *VertexPackPtr;
+} VertexPack, *VertexPackPtr; // 20 byte
 
 
 typedef struct TrianglePack {
-    uint32 *tex_addr;
-    int tex_width, tex_height;
+    struct {
+	uint32 *addr;
+	int width;
+	int height;
+	int scale_max;
+    } tex_info;
+
     VertexPack ver1;
     VertexPack ver2;
     VertexPack ver3;
-} TrianglePack, *TrianglePackPtr;
+
+} TrianglePack, *TrianglePackPtr; //4*4 + 20*3 = 76
 
 
 typedef struct PolygonPack {
@@ -31,13 +37,14 @@
 	int light_pos[3];
 	int light_rgb[3];
     }info;
-    TrianglePack tri[MAX_SIZE_TRIANGLE];    // Variable length array
+
+    TrianglePack tri[MAX_SIZE_TRIANGLE];
 
     PolygonPack* next;
 
     void init(void) {
 	info.size = 0;
-       next = 0;
+	next = 0;
     }
 
     void clear(void) {
@@ -52,11 +59,10 @@
 	}
 	this->init();
     }
-} PolygonPack, *PolygonPackPtr;
+} PolygonPack, *PolygonPackPtr; // 4*7 + 76*128 + 4 = 9760
 
 typedef struct PolygonPackList {
     int size;
-    //PolygonPack *list[6];
     PolygonPack *list;
 } PolygonPackList;
 
--- a/TaskManager/Test/test_render/spe/CreatePolygon.cpp	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/spe/CreatePolygon.cpp	Fri Dec 12 12:23:20 2008 +0900
@@ -147,13 +147,13 @@
 		    triangle->ver3.tex_y = node->texture[nt+4+1];
 		
 #if 1
-		    triangle->tex_addr = node->tex_addr;
-		    triangle->tex_width = node->tex_width;
-		    triangle->tex_height = node->tex_height;
+		    triangle->tex_info.addr = node->tex_addr;
+		    triangle->tex_info.width = node->tex_width;
+		    triangle->tex_info.height = node->tex_height;
 #else
-		    triangle->tex_addr = node->texture_info.pixels;
-		    triangle->tex_width = node->texture_info.t_w;
-		    triangle->tex_height = node->texture_info.t_h;
+		    triangle->tex_info.addr = node->texture_info.pixels;
+		    triangle->tex_info.width = node->texture_info.t_w;
+		    triangle->tex_info.height = node->texture_info.t_h;
 #endif
 		}
 
--- a/TaskManager/Test/test_render/spe/CreateSpan.cpp	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/spe/CreateSpan.cpp	Fri Dec 12 12:23:20 2008 +0900
@@ -124,6 +124,51 @@
 }
 
 /**
+ * 与えられた scale から、実際に使うテクスチャの Tapestry を選択する
+ *
+ * テクスチャは、オリジナルのサイズから、可能なかぎり 1/2 で縮小していき、
+ * 下の図の様に連続した領域に入れられる
+ *
+ *   Tapestry (1)
+ * +---+---+---+---+
+ * | 0 | 1 | 2 | 3 |
+ * +---+---+---+---+
+ * | 4 | 5 | 6 | 7 |     (2)
+ * +---+---+---+---+  +---+---+
+ * | 8 | 9 | 10| 11|  | 16| 17|   (3)
+ * +---+---+---+---+  +---+---+  +---+
+ * | 12| 13| 14| 15|  | 18| 19|  | 20|
+ * +---+---+---+---+  +---+---+  +---|
+ *
+ * (1)                                                 (2)             (3)
+ *  +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
+ *  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | * | * | * | 14| 15| 16| 17| 18| 19| 20|
+ *  +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
+ *
+ * scale の値から、各 Tapestry の先頭アドレスを返す
+ *
+ * @param[in] tw       Width of texture
+ * @param[in] th       Height of texture
+ * @param[in] scale    テクスチャの縮小率 (= 2^n)
+ * @param[in] addr_top テクスチャの先頭アドレス (上の図での (1)
+ * @return scale に対応する Tapestry のアドレス (上の図での (1) or (2) or(3)
+ */
+static uint32*
+getTapestry(int tw, int th, int scale, uint32 *addr_top)
+{
+    int index = 0;
+
+    for (int s = 1; s < scale; s <<= 1) {
+	index += tw*th;
+	tw >>= 1; /* tw /= 2 */
+	th >>= 1;
+    }
+
+    return addr_top + index;
+}
+
+
+/**
  * span の width,height と texture の width,height を比べて
  * span を描画する際に使う texture の比率を求める
  *
@@ -131,14 +176,14 @@
  * @param height     Height of span
  * @param tex_width  Width of 1/1 texture that span use
  * @param tex_height Height of 1/1 texture that span use
+ * @param scale_max  この Span で使う texture の最大縮小率
+ *                   計算結果が scale_max 以上になるのを防ぐ
  * @return 描画に使う texture の比率
  *         width と height は 1/scale の画像を使う
  *
- * @todo scale_max を設定するべき
- *       縮小は 1/8 までしかやってなくて、scale が 16 になる場合もある
  */
 static int
-getScale(int width, int height, int tex_width, int tex_height)
+getScale(int width, int height, int tex_width, int tex_height, int scale_max)
 {
     int base, tex_base;
     int scale = 1;
@@ -163,7 +208,7 @@
 	}
     }
 
-    return scale;
+    return (scale > scale_max) ? scale_max : scale;
 }
 
 /**
@@ -186,6 +231,7 @@
 CreateSpan::half_triangle(SpanPackPtr *spackList,
 			  int charge_y_top, int charge_y_end,
 			  uint32 *tex_addr, int tex_width, int tex_height, 
+			  int tex_scale_max,
 			  VertexPack *vMin,VertexPack *vMid,VertexPack *vMid10,
 			  int y_length)
 {
@@ -357,9 +403,6 @@
 
 	Span *span = &spack->span[spack->info.size++];
 
-	span->tex_addr   = tex_addr;
-	span->tex_width  = tex_width;
-	span->tex_height = tex_height;
 	span->x          = x;
 	span->y          = y;
 	span->length_x   = length;
@@ -371,7 +414,15 @@
 	span->tex_y2     = end_tex_y;
 
 	int scale = getScale(span->length_x, y_length,
-			     span->tex_width, span->tex_height);
+			     span->tex_width, span->tex_height, tex_scale_max);
+
+	//scale = (16 > tex_scale_max) ? tex_scale_max : 16;
+
+	uint32 *tapestry = getTapestry(tex_width, tex_height, scale, tex_addr);
+
+	span->tex_addr   = tapestry;
+	span->tex_width  = tex_width/scale;
+	span->tex_height = tex_height/scale;
     }
 }
 
@@ -469,13 +520,17 @@
 	     * 二つの Triangle に分けている
 	     */
 	    half_triangle(spackList, charge_y_top, charge_y_end,
-			  triPack->tex_addr, triPack->tex_width,
-			  triPack->tex_height, vMin, vMid, vMid10,
-			  (int)(vMax->y - vMin->y));
+			  triPack->tex_info.addr,
+			  triPack->tex_info.width,
+			  triPack->tex_info.height,
+			  triPack->tex_info.scale_max,
+			  vMin, vMid, vMid10, (int)(vMax->y - vMin->y));
 	    half_triangle(spackList, charge_y_top, charge_y_end,
-			  pp->tri[0].tex_addr, pp->tri[0].tex_width,
-			  pp->tri[0].tex_height, vMax, vMid, vMid10,
-			  (int)(vMax->y - vMin->y));
+			  pp->tri[0].tex_info.addr,
+			  pp->tri[0].tex_info.width,
+			  pp->tri[0].tex_info.height,
+			  pp->tri[0].tex_info.scale_max,
+			  vMax, vMid, vMid10, (int)(vMax->y - vMin->y));
 	}
 
 	smanager->dma_wait(POLYGON_PACK_LOAD);	
--- a/TaskManager/Test/test_render/spe/CreateSpan.h	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/spe/CreateSpan.h	Fri Dec 12 12:23:20 2008 +0900
@@ -21,6 +21,7 @@
     void half_triangle(SpanPackPtr *spackList,
 		       int charge_y_top, int charge_y_end,
 		       uint32 *tex_addr, int tex_width, int tex_height,
+		       int tex_scale_max,
 		       VertexPack *vMin,VertexPack *vMid,VertexPack *vMid1,
 		       int y_length);
     void setTileInfoList(SpanPtr span);
--- a/TaskManager/Test/test_render/spe/DrawSpan.cpp	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/spe/DrawSpan.cpp	Fri Dec 12 12:23:20 2008 +0900
@@ -100,7 +100,6 @@
 Uint32
 DrawSpan::get_rgb(int tx, int ty, uint32 *addr)
 {
-    Uint8 red, green, blue, alpha;
     TilePtr tile;
 
     tile = hash->get(addr);
--- a/TaskManager/Test/test_render/sys.cpp	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/sys.cpp	Fri Dec 12 12:23:20 2008 +0900
@@ -36,7 +36,7 @@
   float cosz = cos(radz);
 
   matrix[0] = cosz*cosy+sinz*sinx*siny;
-  matrix[1] =sinz*cosx;
+  matrix[1] = sinz*cosx;
   matrix[2] = -cosz*siny+sinz*sinx*cosy;
   matrix[3] = 0;
   matrix[4] = -sinz*cosy+cosz*sinx*siny;
--- a/TaskManager/Test/test_render/task/CreatePolygon.cpp	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/task/CreatePolygon.cpp	Fri Dec 12 12:23:20 2008 +0900
@@ -147,13 +147,13 @@
 		    triangle->ver3.tex_y = node->texture[nt+4+1];
 		
 #if 1
-		    triangle->tex_addr = node->tex_addr;
-		    triangle->tex_width = node->tex_width;
-		    triangle->tex_height = node->tex_height;
+		    triangle->tex_info.addr = node->tex_addr;
+		    triangle->tex_info.width = node->tex_width;
+		    triangle->tex_info.height = node->tex_height;
 #else
-		    triangle->tex_addr = node->texture_info.pixels;
-		    triangle->tex_width = node->texture_info.t_w;
-		    triangle->tex_height = node->texture_info.t_h;
+		    triangle->tex_info.addr = node->texture_info.pixels;
+		    triangle->tex_info.width = node->texture_info.t_w;
+		    triangle->tex_info.height = node->texture_info.t_h;
 #endif
 		}
 
--- a/TaskManager/Test/test_render/task/CreatePolygonFromSceneGraph.cpp	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/task/CreatePolygonFromSceneGraph.cpp	Fri Dec 12 12:23:20 2008 +0900
@@ -117,9 +117,10 @@
 	    triangle->ver3.tex_x = sg->data[(i+2)*3 + sg->size*6];
 	    triangle->ver3.tex_y = sg->data[(i+2)*3 + sg->size*6+1];
 	
-	    triangle->tex_addr   = sg->texture_info.pixels;
-	    triangle->tex_width  = sg->texture_info.t_w;
-	    triangle->tex_height = sg->texture_info.t_h;
+	    triangle->tex_info.addr   = sg->texture_info.pixels;
+	    triangle->tex_info.width  = sg->texture_info.t_w;
+	    triangle->tex_info.height = sg->texture_info.t_h;
+	    triangle->tex_info.scale_max = sg->texture_info.scale_max;
 	}
 
 
--- a/TaskManager/Test/test_render/task/CreateSpan.cpp	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/task/CreateSpan.cpp	Fri Dec 12 12:23:20 2008 +0900
@@ -124,6 +124,51 @@
 }
 
 /**
+ * 与えられた scale から、実際に使うテクスチャの Tapestry を選択する
+ *
+ * テクスチャは、オリジナルのサイズから、可能なかぎり 1/2 で縮小していき、
+ * 下の図の様に連続した領域に入れられる
+ *
+ *   Tapestry (1)
+ * +---+---+---+---+
+ * | 0 | 1 | 2 | 3 |
+ * +---+---+---+---+
+ * | 4 | 5 | 6 | 7 |     (2)
+ * +---+---+---+---+  +---+---+
+ * | 8 | 9 | 10| 11|  | 16| 17|   (3)
+ * +---+---+---+---+  +---+---+  +---+
+ * | 12| 13| 14| 15|  | 18| 19|  | 20|
+ * +---+---+---+---+  +---+---+  +---|
+ *
+ * (1)                                                 (2)             (3)
+ *  +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
+ *  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | * | * | * | 14| 15| 16| 17| 18| 19| 20|
+ *  +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
+ *
+ * scale の値から、各 Tapestry の先頭アドレスを返す
+ *
+ * @param[in] tw       Width of texture
+ * @param[in] th       Height of texture
+ * @param[in] scale    テクスチャの縮小率 (= 2^n)
+ * @param[in] addr_top テクスチャの先頭アドレス (上の図での (1)
+ * @return scale に対応する Tapestry のアドレス (上の図での (1) or (2) or(3)
+ */
+static uint32*
+getTapestry(int tw, int th, int scale, uint32 *addr_top)
+{
+    int index = 0;
+
+    for (int s = 1; s < scale; s <<= 1) {
+	index += tw*th;
+	tw >>= 1; /* tw /= 2 */
+	th >>= 1;
+    }
+
+    return addr_top + index;
+}
+
+
+/**
  * span の width,height と texture の width,height を比べて
  * span を描画する際に使う texture の比率を求める
  *
@@ -131,14 +176,14 @@
  * @param height     Height of span
  * @param tex_width  Width of 1/1 texture that span use
  * @param tex_height Height of 1/1 texture that span use
+ * @param scale_max  この Span で使う texture の最大縮小率
+ *                   計算結果が scale_max 以上になるのを防ぐ
  * @return 描画に使う texture の比率
  *         width と height は 1/scale の画像を使う
  *
- * @todo scale_max を設定するべき
- *       縮小は 1/8 までしかやってなくて、scale が 16 になる場合もある
  */
 static int
-getScale(int width, int height, int tex_width, int tex_height)
+getScale(int width, int height, int tex_width, int tex_height, int scale_max)
 {
     int base, tex_base;
     int scale = 1;
@@ -163,7 +208,7 @@
 	}
     }
 
-    return scale;
+    return (scale > scale_max) ? scale_max : scale;
 }
 
 /**
@@ -186,6 +231,7 @@
 CreateSpan::half_triangle(SpanPackPtr *spackList,
 			  int charge_y_top, int charge_y_end,
 			  uint32 *tex_addr, int tex_width, int tex_height, 
+			  int tex_scale_max,
 			  VertexPack *vMin,VertexPack *vMid,VertexPack *vMid10,
 			  int y_length)
 {
@@ -357,9 +403,6 @@
 
 	Span *span = &spack->span[spack->info.size++];
 
-	span->tex_addr   = tex_addr;
-	span->tex_width  = tex_width;
-	span->tex_height = tex_height;
 	span->x          = x;
 	span->y          = y;
 	span->length_x   = length;
@@ -371,7 +414,15 @@
 	span->tex_y2     = end_tex_y;
 
 	int scale = getScale(span->length_x, y_length,
-			     span->tex_width, span->tex_height);
+			     span->tex_width, span->tex_height, tex_scale_max);
+
+	//scale = (16 > tex_scale_max) ? tex_scale_max : 16;
+
+	uint32 *tapestry = getTapestry(tex_width, tex_height, scale, tex_addr);
+
+	span->tex_addr   = tapestry;
+	span->tex_width  = tex_width/scale;
+	span->tex_height = tex_height/scale;
     }
 }
 
@@ -469,13 +520,17 @@
 	     * 二つの Triangle に分けている
 	     */
 	    half_triangle(spackList, charge_y_top, charge_y_end,
-			  triPack->tex_addr, triPack->tex_width,
-			  triPack->tex_height, vMin, vMid, vMid10,
-			  (int)(vMax->y - vMin->y));
+			  triPack->tex_info.addr,
+			  triPack->tex_info.width,
+			  triPack->tex_info.height,
+			  triPack->tex_info.scale_max,
+			  vMin, vMid, vMid10, (int)(vMax->y - vMin->y));
 	    half_triangle(spackList, charge_y_top, charge_y_end,
-			  pp->tri[0].tex_addr, pp->tri[0].tex_width,
-			  pp->tri[0].tex_height, vMax, vMid, vMid10,
-			  (int)(vMax->y - vMin->y));
+			  pp->tri[0].tex_info.addr,
+			  pp->tri[0].tex_info.width,
+			  pp->tri[0].tex_info.height,
+			  pp->tri[0].tex_info.scale_max,
+			  vMax, vMid, vMid10, (int)(vMax->y - vMin->y));
 	}
 
 	smanager->dma_wait(POLYGON_PACK_LOAD);	
--- a/TaskManager/Test/test_render/task/CreateSpan.h	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/task/CreateSpan.h	Fri Dec 12 12:23:20 2008 +0900
@@ -21,6 +21,7 @@
     void half_triangle(SpanPackPtr *spackList,
 		       int charge_y_top, int charge_y_end,
 		       uint32 *tex_addr, int tex_width, int tex_height,
+		       int tex_scale_max,
 		       VertexPack *vMin,VertexPack *vMid,VertexPack *vMid1,
 		       int y_length);
     void setTileInfoList(SpanPtr span);
--- a/TaskManager/Test/test_render/task/DrawSpan.cpp	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/task/DrawSpan.cpp	Fri Dec 12 12:23:20 2008 +0900
@@ -100,7 +100,6 @@
 Uint32
 DrawSpan::get_rgb(int tx, int ty, uint32 *addr)
 {
-    Uint8 red, green, blue, alpha;
     TilePtr tile;
 
     tile = hash->get(addr);
--- a/TaskManager/Test/test_render/texture.h	Thu Dec 11 20:17:01 2008 +0900
+++ b/TaskManager/Test/test_render/texture.h	Fri Dec 12 12:23:20 2008 +0900
@@ -7,18 +7,11 @@
 GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
 #endif
 
-#if 0
-struct texture_tmp{
-  Uint32 tx128[SSIZE];
-  Uint32 tx64[SS64];
-  Uint32 tx32[SS32];
-};
-#endif
-
 struct texture_list {
-  int t_w, t_h;
-  Uint32 *pixels_orig;
-  Uint32 *pixels;
+    int t_w, t_h;
+    Uint32 *pixels_orig;
+    Uint32 *pixels;
+    int scale_max;
 };
 
 #endif