JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
MaterialParam.cpp
Go to the documentation of this file.
1
2#include "MaterialParam.h"
3
4
5using namespace jbxl;
6
7
9// TextureParam クラス
10//
11
13{
14 name = init_Buffer();
15
16 for (int i=0; i<4; i++) color[i] = 1.0;
17
18 alphaChannel = false;
19 alphaCutoff = 0.0;
20 alphaMode = MATERIAL_ALPHA_BLENDING; //MATERIAL_ALPHA_NONE;
21
22 shiftU = 0.0;
23 shiftV = 0.0;
24 scaleU = 1.0;
25 scaleV = 1.0;
26 rotate = 0.0;
27
28 flipU = false;
29 flipV = false;
30
31 return;
32}
33
34
36{
38 return;
39}
40
41
43{
44 *this = t;
45 name = dup_Buffer(t.name);
46}
47
48
50{
51 for (int i=0; i<num; i++) {
52 uv[i].u += shiftU;
53 uv[i].v += shiftV;
54 }
55}
56
57
59{
60 for (int i=0; i<num; i++) {
61 uv[i].u -= shiftU;
62 uv[i].v -= shiftV;
63 }
64}
65
66
68{
69 double uu, vv;
70
71 for (int i=0; i<num; i++) {
72 uu = (uv[i].u - 0.5)*scaleU;
73 vv = (uv[i].v - 0.5)*scaleV;
74 uv[i].u = uu + 0.5;
75 uv[i].v = vv + 0.5;
76 }
77}
78
79
81{
82 if (scaleU<=0.0 || scaleV<=0.0) return;
83 double uu, vv;
84
85 for (int i=0; i<num; i++) {
86 uu = (uv[i].u - 0.5)/scaleU;
87 vv = (uv[i].v - 0.5)/scaleV;
88 uv[i].u = uu + 0.5;
89 uv[i].v = vv + 0.5;
90 }
91}
92
93
95{
96 double uu, vv;
97 double cs = cos(rotate);
98 double sn = sin(rotate);
99
100 for (int i=0; i<num; i++) {
101 uu = uv[i].u - 0.5;
102 vv = uv[i].v - 0.5;
103 uv[i].u = uu*cs + vv*sn + 0.5;
104 uv[i].v = -uu*sn + vv*cs + 0.5;
105 }
106}
107
108
110{
111 double uu, vv;
112 double cs = cos(rotate);
113 double sn = -sin(rotate);
114
115 for (int i=0; i<num; i++) {
116 uu = uv[i].u - 0.5;
117 vv = uv[i].v - 0.5;
118 uv[i].u = uu*cs + vv*sn + 0.5;
119 uv[i].v = -uu*sn + vv*cs + 0.5;
120 }
121}
122
123
125{
126 DEBUG_MODE PRINT_MESG("JBXL::TextureParam::execTrans: shift = (%f %f), scale = (%f %f), rot = %f\n", shiftU, shiftV, scaleU, scaleV, rotate);
127 if (flipU) { execFlipU(uv, num); flipU = false;}
128 if (flipV) { execFlipV(uv, num); flipV = false;}
129 if (isSetRotate()) execRotate(uv, num);
130 if (isSetScale()) execScale (uv, num);
131 if (isSetShift()) execShift (uv, num);
132
133 return;
134}
135
136
138{
139 if (flipU) { execFlipU(uv, num); flipU = false;}
140 if (flipV) { execFlipV(uv, num); flipV = false;}
141 if (isSetShift()) execInvShift (uv, num);
142 if (isSetScale()) execInvScale (uv, num);
143 if (isSetRotate()) execInvRotate(uv, num);
144
145 return;
146}
147
148
150{
151 fprintf(fp, "TextureParam.name = %s\n", name.buf);
152 fprintf(fp, "TextureParam.color = (%f,%f,%f,%f) [RGBA]\n", color[0], color[1], color[2], color[3]);
153 fprintf(fp, "TextureParam.shiftU = %f\n", shiftU);
154 fprintf(fp, "TextureParam.shiftV = %f\n", shiftV);
155 fprintf(fp, "TextureParam.scaleU = %f\n", scaleU);
156 fprintf(fp, "TextureParam.scaleV = %f\n", scaleV);
157 fprintf(fp, "TextureParam.rotate = %f\n", rotate);
158
159 if (flipU) fprintf(fp, "TextureParam.flipU = true\n");
160 else fprintf(fp, "TextureParam.flipU = false\n");
161 if (flipV) fprintf(fp, "TextureParam.flipV = true\n");
162 else fprintf(fp, "TextureParam.flipV = false\n");
163
164 if (alphaChannel) fprintf(fp, "TextureParam.alphaChannel = true\n");
165 else fprintf(fp, "TextureParam.alphaChannel = flase\n");
166
167 fprintf(fp, "TextureParam.alphaMode = %d (%d)\n", alphaMode, getAlphaMode());
168 fprintf(fp, "TextureParam.alphaCutoff = %f\n", alphaCutoff);
169
170 fflush(fp);
171 return;
172}
173
174
175
177
183{
184 char* a_name = a.getName();
185 char* b_name = b.getName();
186 if (a_name!=NULL && b_name!=NULL) {
187 if (strcmp(a_name, b_name)) return false;
188 }
189 else if (a_name!=NULL || b_name!=NULL) return false;
190
191 //
192 for (int i=0; i<4; i++) {
193 if (a.getColor(i)!=b.getColor(i)) return false;
194 }
195
196 return true;
197}
198
199
200
202// MaterialParam クラス
203//
204
206{
207 enable = false;
208
209 texture.init();
210 bumpmap.init();
211 specmap.init();
212
214
215// transparent = 1.0;
216 kind = 'O'; // OBJECT
217 shininess = 0.0;
218 glow = 0.0;
219 bright = 0.0;
220 glossiness = 0.0;
221 environment = 0.0;
222 light = 0.0;
223
224 mapping = 0;
225 mflags = 0;
226 others = 0;
227
228 return;
229}
230
231
233{
234 texture.free();
235 bumpmap.free();
236 specmap.free();
237
239
240 return;
241}
242
243
245{
246 *this = m;
247
251
253}
254
255
257{
258 if (texture.isSetTexture()) {
259 return texture.getName();
260 }
261 if (bumpmap.isSetTexture()) {
262 return bumpmap.getName();
263 }
264 if (specmap.isSetTexture()) {
265 return specmap.getName();
266 }
267 return NULL;
268}
269
270
274void MaterialParam::setFullName(const char* ext)
275{
276 if (ext==NULL) return;
277
278 if (texture.isSetTexture()) {
279 if (ext[0]!='.') texture.addName(".");
280 texture.addName(ext);
281 }
282 if (bumpmap.isSetTexture()) {
283 if (ext[0]!='.') bumpmap.addName(".");
284 bumpmap.addName(ext);
285 }
286 if (specmap.isSetTexture()) {
287 if (ext[0]!='.') specmap.addName(".");
288 specmap.addName(ext);
289 }
290}
291
292
293/*
294double MaterialParam::getTransparent(void)
295{
296 int almode = texture.getAlphaMode();
297 double cutoff = texture.getAlphaCutoff();
298 double transp = transparent;
299 double alpha = texture.getColor(3);
300 if (alpha<0.99) {
301 almode = MATERIAL_ALPHA_BLENDING; // Blending Mode
302 cutoff = 0.0;
303 transp = alpha;
304 texture.setAlphaMode(almode);
305 texture.setAlphaCutoff(cutoff);
306 }
307 if (cutoff==0.0 && almode!=MATERIAL_ALPHA_BLENDING) transp = 1.0; // cutoff==0.0 のときは Blending Modeとするため
308 transparent = transp;
309 return transp;
310}
311*/
312
313
315{
316 if (!enable) {
317 fprintf(fp, "MaterialParam::printParam: MaterialParam is disable\n");
318 return;
319 }
320
321 fprintf(fp, "----------------------------------------------\n");
322
323 fprintf(fp, "Texture\n");
325 fprintf(fp, "\n");
326 if (specmap.isSetTexture()) {
327 fprintf(fp, "SpecMap\n");
329 fprintf(fp, "\n");
330 }
331 if (bumpmap.isSetTexture()) {
332 fprintf(fp, "BumpMap\n");
334 fprintf(fp, "\n");
335 }
336
337 fprintf(fp, "----------------------------------------------\n");
338 fprintf(fp, "MaterialParam.paramstr = %s\n", paramstr.buf);
339// fprintf(fp, "MaterialParam.transparent = %f\n", transparent);
340 fprintf(fp, "MaterialParam.shininess = %f\n", shininess);
341 fprintf(fp, "MaterialParam.glow = %f\n", glow);
342 fprintf(fp, "MaterialParam.bright = %f\n", bright);
343 fprintf(fp, "MaterialParam.mapping = %d\n", mapping);
344 fprintf(fp, "MaterialParam.mflags = %d\n", mflags);
345 fprintf(fp, "MaterialParam.others = %d\n", others);
346
347 fprintf(fp, "MaterialParam.glossiness = %f\n", glossiness);
348 fprintf(fp, "MaterialParam.environment = %f\n", environment);
349
350 fflush(fp);
351 return;
352}
353
354
362char* MaterialParam::getBase64Params(unsigned char cc)
363{
364 //printParam(stderr);
365
367
368 double red = texture.getColor(0);
369 double green = texture.getColor(1);
370 double blue = texture.getColor(2);
371 double transp = texture.getColor(3);
372 //double transp = getTransparent();
373 double cutoff = texture.getAlphaCutoff();
374 int alphaMode = texture.getAlphaMode();
375 bool hasAlpha = texture.hasAlphaChannel();
376 /*
377 short int rotate = (short int)((int)(texture.getRotate()*2000.)%32768); // 2Byte化
378 short int shiftu = (short int)((int)(texture.getShiftU()*2000.)%32768);
379 short int shiftv = (short int)((int)(texture.getShiftV()*2000.)%32768);
380 short int scaleu = (short int)((int)(texture.getScaleU()*100. )%32768);
381 short int scalev = (short int)((int)(texture.getScaleV()*100. )%32768);
382 */
383 int alphamode = 0;
384 if (hasAlpha) alphamode = alphaMode*10 + 1;
385 else alphamode = alphaMode*10;
386
387 memset(attr, 0, MATERIAL_ATTR_LEN);
388 attr[MATERIAL_ATTR_COLOR_RED] = (uByte)((1.0 - red )*255);
389 attr[MATERIAL_ATTR_COLOR_GREEN] = (uByte)((1.0 - green )*255);
390 attr[MATERIAL_ATTR_COLOR_BLUE] = (uByte)((1.0 - blue )*255);
391 attr[MATERIAL_ATTR_TRANSPARENT] = (uByte)((1.0 - transp)*255);
392 attr[MATERIAL_ATTR_ALPHACUTOFF] = (uByte)(cutoff*255);
394 attr[MATERIAL_ATTR_GLOW] = (uByte)(glow*255);
395 attr[MATERIAL_ATTR_BRIGHT] = (uByte)(bright*255);
396 attr[MATERIAL_ATTR_LIGHT] = (uByte)(light*255);
397 attr[MATERIAL_ATTR_ALPHAMODE] = (uByte)(alphamode);
398 /*
399 memcpy(attr + MATERIAL_ATTR_SHIFT_U, &shiftu, 2);
400 memcpy(attr + MATERIAL_ATTR_SHIFT_V, &shiftv, 2);
401 memcpy(attr + MATERIAL_ATTR_SCALE_U, &scaleu, 2);
402 memcpy(attr + MATERIAL_ATTR_SCALE_V, &scalev, 2);
403 memcpy(attr + MATERIAL_ATTR_ROTATE, &rotate, 2);
404 */
406
407 char* params = (char*)encode_base64_filename(attr, MATERIAL_ATTR_LEN, cc); // 要 free / -> cc
408
409 return params;
410}
411
412
413
415
421{
422 if (!isSameTexture(a.texture, b.texture)) return false;
423 if (!isSameTexture(a.bumpmap, b.bumpmap)) return false;
424 if (!isSameTexture(a.specmap, b.specmap)) return false;
425
426 //
427 bool ret = true;
428
429 char* a_param = a.getBase64Params();
430 char* b_param = b.getBase64Params();
431 //
432 if (a_param!=NULL && b_param!=NULL) {
433 if (strcmp(a_param, b_param)) ret = false;
434 }
435 else if (a_param!=NULL || b_param!=NULL) {
436 ret = false;
437 }
438 if (a_param!=NULL) ::free(a_param);
439 if (b_param!=NULL) ::free(b_param);
440
441 return ret;
442}
443
#define MATERIAL_ATTR_TRANSPARENT
#define MATERIAL_ATTR_ALPHACUTOFF
#define MATERIAL_ATTR_COLOR_RED
#define MATERIAL_ATTR_BRIGHT
#define MATERIAL_ATTR_GLOW
#define MATERIAL_ATTR_SHININESS
#define MATERIAL_ATTR_LIGHT
#define MATERIAL_ATTR_ALPHAMODE
#define MATERIAL_ATTR_OBJECT
#define MATERIAL_ATTR_COLOR_GREEN
#define MATERIAL_ATTR_COLOR_BLUE
#define MATERIAL_ALPHA_BLENDING
#define MATERIAL_ATTR_LEN
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition buffer.cpp:128
Buffer init_Buffer()
初期化したBuffer型変数を返す.
Definition buffer.cpp:47
Buffer dup_Buffer(Buffer buf)
Buffer型変数のコピーをつくる.
Definition buffer.cpp:211
#define make_Buffer_str(str)
set_Buffer()
Definition buffer.h:61
TextureParam bumpmap
Bumpmap テクスチャ
double bright
明るさ
double glossiness
滑らかさ
int mapping
マッピング方法
char * getBase64Params(unsigned char cc='-')
char kind
オブジェクトの種類.'O', 'T', 'G', 'E'
char * getParamString(void)
void printParam(FILE *fp)
int mflags
メディアフラグ
double shininess
輝き
void setFullName(const char *extname)
double light
周りを照らすライト
int others
その他のフラグ
Buffer paramstr
パラメータ文字列 (Base64文字列)
TextureParam texture
テクスチャ
double environment
環境光
void dup(MaterialParam m)
TextureParam specmap
Specular マップ テクスチャ
double alphaCutoff
アルファチャンネルの Cutoff値.alphaMode==MATERIAL_ALPHA_MASKING の場合に有効
void execFlipV(UVMap< double > *uv, int n)
double scaleV
UVマップのスケール (V方向)
bool isSetRotate(void)
char * getName(void)
double color[4]
RGBA.
void addName(const char *nm)
int alphaMode
アルファチャンネルのモード
void execInvShift(UVMap< double > *uv, int n)
void printParam(FILE *fp)
bool alphaChannel
テクスチャデータがアルファチャンネルを持っているかどうか.
bool flipV
UVマップで V方向の反転を行うか
double shiftV
UVマップのシフト (V方向)
void execShift(UVMap< double > *uv, int n)
double scaleU
UVマップのスケール (U方向)
bool isSetScale(void)
Buffer name
テクスチャ名
bool isSetTexture(void)
double getAlphaCutoff(void)
bool hasAlphaChannel(void)
double rotate
UVマップの回転
bool isSetShift(void)
void execInvTrans(UVMap< double > *uv, int n)
Shift -> Scale -> Rotate.
Vector< double > getColor(void)
void dup(TextureParam m)
void execFlipU(UVMap< double > *uv, int n)
void execInvScale(UVMap< double > *uv, int n)
void execInvRotate(UVMap< double > *uv, int n)
void execRotate(UVMap< double > *uv, int n)
void execTrans(UVMap< double > *uv, int n)
Rotate -> Scale -> Shift.
double shiftU
UVマップのシフト (U方向)
bool flipU
UVマップで U方向の反転を行うか
void execScale(UVMap< double > *uv, int n)
unsigned char uByte
1Byte
Definition common.h:332
Definition Brep.h:29
bool isSameMaterial(MaterialParam a, MaterialParam b)
compare each texture names and colors
bool isSameTexture(TextureParam a, TextureParam b)
compare texture ma,e and color
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.
Definition buffer.h:39
unsigned char * encode_base64_filename(unsigned char *buf, int sz, unsigned char cc)
バイナリデータ bufを Base64で encodeしてファイル名を作る.ただし '/' は cc として扱う.要 free()
Definition tools.cpp:2907
#define PRINT_MESG(...)
環境依存用の出力関数.MS Windows用は未実装
Definition tools.h:469
#define DEBUG_MODE
Definition tools.h:502