| 1 |
/* |
| 2 |
* マルチプラットフォーム描画エンジン「Sherry」 |
| 3 |
* Copyright(C) 2010-2011 SherryProject. all rights reserved. |
| 4 |
* |
| 5 |
* The MIT License |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
* of this software and associated documentation files (the "Software"), to deal |
| 9 |
* in the Software without restriction, including without limitation the rights |
| 10 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the Software is |
| 12 |
* furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in |
| 15 |
* all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 |
* THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
/* |
| 27 |
* shTexture.c |
| 28 |
* Copyright (C) 2010-2011 Cap5ule. all rights reserved. |
| 29 |
* |
| 30 |
* Date |Version |Author |Summary |
| 31 |
* 2010/11/16 |v0.01 |Cap5ule |初回版 |
| 32 |
* 2010/11/27 |v0.02 |onakah |PNGデータ構造体作成、PNGファイルの読み込み、 |
| 33 |
* | |破棄の関数を作成。 |
| 34 |
* 2010/12/23 |v0.03 |onakah |埋め込みテクスチャリソースの読み込みを追加 |
| 35 |
*/ |
| 36 |
|
| 37 |
/*---- インクルードファイル ----*/ |
| 38 |
|
| 39 |
#include "sherry.h" //!<共通ヘッダー |
| 40 |
#include "shApplication.h" //!<必要なSHヘッダー |
| 41 |
#include "shTexture.h" //!<ヘッダー |
| 42 |
|
| 43 |
#include <png.h> //!<PNGライブラリ |
| 44 |
|
| 45 |
/*---- マクロ定義 ----*/ |
| 46 |
|
| 47 |
|
| 48 |
/*---- スタティックプロトタイプ宣言 ----*/ |
| 49 |
|
| 50 |
|
| 51 |
/*---- グローバル変数 ----*/ |
| 52 |
|
| 53 |
|
| 54 |
/*---- スタティック関数 ----*/ |
| 55 |
|
| 56 |
|
| 57 |
/*---- 関数 ----*/ |
| 58 |
|
| 59 |
/* |
| 60 |
* int shCreateTextureFromPNG(shTexture* stex, const char* filepath) |
| 61 |
* |
| 62 |
* <引数> |
| 63 |
* shTexture* stex : テクスチャ構造体ポインタ |
| 64 |
* char* filepath : ファイルパス |
| 65 |
* <戻り値> |
| 66 |
* int : エラーコードが返ります |
| 67 |
* <説明> |
| 68 |
* テクスチャデータを作成する。 |
| 69 |
* PNGフォーマットからテクスチャデータを読み込む |
| 70 |
*/ |
| 71 |
int shCreateTextureFromPNG(shTexture* stex, const char* filepath) |
| 72 |
{ |
| 73 |
// PNGフォーマット構造体 |
| 74 |
struct { |
| 75 |
char* data; // 生データを保存する |
| 76 |
unsigned int width; // 横幅 |
| 77 |
unsigned int height; // 縦幅 |
| 78 |
int depth; |
| 79 |
int colortype; |
| 80 |
int interlacetype; |
| 81 |
} pngFormat; |
| 82 |
|
| 83 |
int rb; // メモリカウント用 |
| 84 |
unsigned int i; // カウンタ |
| 85 |
png_bytepp image; // PNGイメージデータ |
| 86 |
FILE* fp; |
| 87 |
png_structp sp; // PNG情報 |
| 88 |
png_infop ip; |
| 89 |
|
| 90 |
// ファイルを開く |
| 91 |
fp = fopen(filepath, "rb"); |
| 92 |
if (!fp) |
| 93 |
return 1; |
| 94 |
|
| 95 |
sp = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 96 |
ip = png_create_info_struct(sp); |
| 97 |
png_init_io(sp, fp); |
| 98 |
png_read_info(sp, ip); |
| 99 |
png_get_IHDR(sp, ip, (png_uint_32*)&pngFormat.width, (png_uint_32*)&pngFormat.height, |
| 100 |
&pngFormat.depth, &pngFormat.colortype, &pngFormat.interlacetype, NULL, NULL); |
| 101 |
// メモリ領域確保 |
| 102 |
rb = png_get_rowbytes(sp, ip); |
| 103 |
|
| 104 |
pngFormat.data = (char*)malloc(sizeof(unsigned char) * (pngFormat.height) * rb); |
| 105 |
image = (png_bytepp)malloc(sizeof(unsigned char*) * (pngFormat.height)); |
| 106 |
|
| 107 |
for (i = 0; i < pngFormat.height; i++) |
| 108 |
image[i] = (png_byte*)&pngFormat.data[i * rb]; |
| 109 |
|
| 110 |
// png読み込み |
| 111 |
png_read_image(sp, image); |
| 112 |
png_read_end(sp, ip); |
| 113 |
png_destroy_read_struct(&sp, &ip, NULL); |
| 114 |
|
| 115 |
fclose(fp); |
| 116 |
free(image); |
| 117 |
|
| 118 |
// テクスチャデータ作成 |
| 119 |
if(shCreateTexture(stex,pngFormat.width,pngFormat.height,pngFormat.colortype,pngFormat.data)) |
| 120 |
return 1; /*作成に失敗*/ |
| 121 |
|
| 122 |
// 使用後は解放 |
| 123 |
if(pngFormat.data != NULL) |
| 124 |
free(pngFormat.data); |
| 125 |
pngFormat.width = 0; |
| 126 |
pngFormat.height = 0; |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* int shCreateTextureFromData(shTexture* stex, int w, int h, |
| 133 |
* unsigned char* data) |
| 134 |
* |
| 135 |
* <引数> |
| 136 |
* shTexture* stex : テクスチャ構造体ポインタ |
| 137 |
* int w : テクスチャサイズ横幅 |
| 138 |
* int h : テクスチャサイズ縦幅 |
| 139 |
* unsigned char* data : テクスチャデータ本体 |
| 140 |
* <戻り値> |
| 141 |
* int : エラーコードが返ります |
| 142 |
* <説明> |
| 143 |
* テクスチャデータを作成する。 |
| 144 |
* 埋め込みデータからテクスチャデータを生成する。 |
| 145 |
*/ |
| 146 |
int shCreateTextureFromData(shTexture* stex, int w, int h, unsigned char* data) |
| 147 |
{ |
| 148 |
if(g_sh_DrawMode == OpenGL) { |
| 149 |
|
| 150 |
// OpenGLテクスチャを生成 |
| 151 |
glGenTextures(1, &stex->gltex); //テクスチャバッファ作成 |
| 152 |
if(stex->gltex == 0) |
| 153 |
return 1; /*生成エラー*/ |
| 154 |
|
| 155 |
glBindTexture(GL_TEXTURE_2D, stex->gltex); |
| 156 |
|
| 157 |
// テクスチャ属性の設定(デフォルト) |
| 158 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 159 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 160 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 161 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 162 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 163 |
|
| 164 |
// テクスチャデータを入れる |
| 165 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 166 |
|
| 167 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 168 |
|
| 169 |
} else { |
| 170 |
/*使用しない*/ |
| 171 |
} |
| 172 |
|
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
* int shCreateTexture(shTexture* stex, int w, int h, |
| 178 |
* int colortype, char* texbuf) |
| 179 |
* |
| 180 |
* <引数> |
| 181 |
* shTexture* stex : テクスチャ構造体ポインタ |
| 182 |
* int w : テクスチャサイズ横幅 |
| 183 |
* int h : テクスチャサイズ縦幅 |
| 184 |
* int colortype : テクスチャデータ色配置 |
| 185 |
* int texbuf : テクスチャバッファポインタ |
| 186 |
* <戻り値> |
| 187 |
* int : エラーコードが返ります |
| 188 |
* <説明> |
| 189 |
* テクスチャデータを作成する。データを描画に使えるようにする。 |
| 190 |
*/ |
| 191 |
int shCreateTexture(shTexture* stex, int w, int h, |
| 192 |
int colortype, char* texbuf) |
| 193 |
{ |
| 194 |
unsigned int type = 0; |
| 195 |
|
| 196 |
if(g_sh_DrawMode == OpenGL) { |
| 197 |
|
| 198 |
//色配置のタイプを指定 |
| 199 |
switch(colortype) { |
| 200 |
//case PNG_COLOR_TYPE_GRAY: type = GL_RGB; |
| 201 |
// break; |
| 202 |
case PNG_COLOR_TYPE_RGB: type = GL_RGB; |
| 203 |
break; |
| 204 |
case PNG_COLOR_TYPE_RGB_ALPHA: type = GL_RGBA; |
| 205 |
break; |
| 206 |
} |
| 207 |
// OpenGLテクスチャを生成 |
| 208 |
glGenTextures(1, &stex->gltex); //テクスチャバッファ作成 |
| 209 |
if(stex->gltex == 0) |
| 210 |
return 1; /*生成エラー*/ |
| 211 |
|
| 212 |
glBindTexture(GL_TEXTURE_2D, stex->gltex); |
| 213 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 214 |
|
| 215 |
// テクスチャ属性の設定(デフォルト) |
| 216 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 217 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 218 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 219 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 220 |
|
| 221 |
// テクスチャデータを入れる |
| 222 |
glTexImage2D(GL_TEXTURE_2D, 0, type, w, h, 0, type, GL_UNSIGNED_BYTE, texbuf); |
| 223 |
|
| 224 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 225 |
|
| 226 |
} else { |
| 227 |
/*使用しない*/ |
| 228 |
} |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
* int shDeleteTexture(shTexture* stex) |
| 235 |
* |
| 236 |
* <引数> |
| 237 |
* shTexture* stex : テクスチャ構造体ポインタ |
| 238 |
* <戻り値> |
| 239 |
* int : エラーコードが返ります |
| 240 |
* <説明> |
| 241 |
* テクスチャデータを削除する。メモリを開放します。 |
| 242 |
*/ |
| 243 |
int shDeleteTexture(shTexture* stex) |
| 244 |
{ |
| 245 |
if(g_sh_DrawMode == OpenGL) { |
| 246 |
//テクスチャ削除 |
| 247 |
glDeleteTextures(1, &stex->gltex); |
| 248 |
} else { |
| 249 |
/*使用しない*/ |
| 250 |
} |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
/* |
| 255 |
* void shSetTextureWarp(texparam u, texparam v) |
| 256 |
* |
| 257 |
* <引数> |
| 258 |
* texparam u : U座標テクスチャワープパラメータ |
| 259 |
* texparam v : V座標テクスチャワープパラメータ |
| 260 |
* <戻り値> |
| 261 |
* なし |
| 262 |
* <説明> |
| 263 |
* テクスチャのワープの設定をします。 |
| 264 |
* [SH_TP_REPEAT][SH_TP_CLAMP] |
| 265 |
*/ |
| 266 |
void shSetTextureWarp(texparam u, texparam v) |
| 267 |
{ |
| 268 |
if(g_sh_DrawMode == OpenGL) { |
| 269 |
//テクスチャ設定 |
| 270 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, u); |
| 271 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, v); |
| 272 |
} else { |
| 273 |
/*使用しない*/ |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
* void shSetTextureFilter(texparam min, texparam mag) |
| 279 |
* |
| 280 |
* <引数> |
| 281 |
* texparam min : MINテクスチャフィルタパラメータ |
| 282 |
* texparam mag : MAGテクスチャフィルタパラメータ |
| 283 |
* <戻り値> |
| 284 |
* なし |
| 285 |
* <説明> |
| 286 |
* テクスチャのフィルタの設定をします。 |
| 287 |
* [SH_TP_NEAREST][SH_TP_LINEAR] |
| 288 |
*/ |
| 289 |
void shSetTextureFilter(texparam min, texparam mag) |
| 290 |
{ |
| 291 |
if(g_sh_DrawMode == OpenGL) { |
| 292 |
//テクスチャ削除 |
| 293 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min); |
| 294 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag); |
| 295 |
} else { |
| 296 |
/*使用しない*/ |
| 297 |
} |
| 298 |
} |
| 299 |
|