|
0 registered members (),
631
guests, and 2
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: How can i make a copy of a MATERIAL?
[Re: Aku_Aku]
#400883
05/09/12 10:13
05/09/12 10:13
|
Joined: Jul 2001
Posts: 6,904
HeelX
Senior Expert
|
Senior Expert
Joined: Jul 2001
Posts: 6,904
|
The struct definition is like this (taken from the last atypes.h in the include folder of the A8 installation):
typedef struct MATERIAL {
C_LINK link;
var ambient_blue,ambient_green,ambient_red;
var diffuse_blue,diffuse_green,diffuse_red;
var specular_blue,specular_green,specular_red;
var emissive_blue,emissive_green,emissive_red;
var map_blue,map_green,map_red;
var alpha;
var power; // for specular reflection
var albedo; // sun albedo
var scale1,scale2;
var cycle;
var skill[20];
float matrix[16];
long flags;
char *effect;
var lod; // LOD offset for shader suppression
BMAP *skin1,*skin2,*skin3,*skin4;
EVENT event; // event function
void* d3deffect; // compiled LPD3DXEFFECT
void* d3dmaterial; // D3DMATERIAL9
char* technique; // preferred technique name
var maxbones; // max # of bones matrices in the shader
byte pad[PAD_MATERIAL];
} MATERIAL;
important is, that you create a new material with mtl_create and copy the contents over, which can be copied 1:1, the effect and the internal stuff should not be changed! I would code it this way (untested!):
MATERIAL* mtl_clone (MATERIAL* src)
{
MATERIAL* dst = NULL;
if (src != NULL)
{
dst = mtl_create();
memcpy(&(dst->ambient_blue), &(src->ambient_blue), sizeof(var) * 41);
memcpy(&(dst->matrix), &(src->matrix), sizeof(float) * 16);
dst->flags = src->flags;
effect_cpy(dst, src);
dst->lod = src->lod;
// TODO:
// should ptrs to skin1...4 be copied?
// should the material have the same event?
// should the material select the same technique?
}
return(dst);
}
What remains is the question if you want to keep the skin1...4 pointers (or should they be duplicated as well?), if you want to keep the event and the technique selection. I can't answer that and I would extend the signature by some booleans that can be used to select the wanted behaviour.
Last edited by HeelX; 05/09/12 10:23.
|
|
|
Re: How can i make a copy of a MATERIAL?
[Re: HeelX]
#400886
05/09/12 10:31
05/09/12 10:31
|
Joined: Sep 2009
Posts: 987 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 987
Budapest
|
Thanks for your helpful reply. important is, that you create a new material with mtl_create and copy the contents over, which can be copied 1:1, the effect and the internal stuff should not be changed! I would like to do exactly that you wrote. I noticed that when i change my code to this, it seems to me, the program works well.
memcpy(selected.material,mat_model,sizeof(MATERIAL));
But another problem arose immediatly. I can change the new material's ambient values later, those are stored, but i can't see the result.
|
|
|
Re: How can i make a copy of a MATERIAL?
[Re: Rei_Ayanami]
#400889
05/09/12 10:43
05/09/12 10:43
|
Joined: Sep 2009
Posts: 987 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 987
Budapest
|
When you simply memcpy the material, you will mess up the engine link-system. Hmmm... That is right, thanks. I didn't count that. I try to solve this problem, if it will not work, i will use HeelX suggestion. Edit: I added this line after the memcpy function.
memcpy(selected.material.link,pmtl.link,sizeof(C_LINK));
Everything seems to me works...
Last edited by Aku_Aku; 05/09/12 11:00.
|
|
|
Re: How can i make a copy of a MATERIAL?
[Re: HeelX]
#400901
05/09/12 13:46
05/09/12 13:46
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
There are mor things than just the C_LINK section I wouldn't touch! If the parent material gets destroyed, you only wreak havoc upon yourself if you continue to use your method. (You also leak memory)
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: How can i make a copy of a MATERIAL?
[Re: WretchedSid]
#400907
05/09/12 15:20
05/09/12 15:20
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
mtl_create and set the values to this material That actually is the smart way, because 1. It's easy to implement. 2. The resulting code is easy to understand. 3. None of the problems described above arise from it. You should also have a look at mtlfx.c in the include folder. There is a function that copies the matieral parameters. You can take that as a template.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: How can i make a copy of a MATERIAL?
[Re: PadMalcom]
#400909
05/09/12 15:36
05/09/12 15:36
|
Joined: Jul 2001
Posts: 6,904
HeelX
Senior Expert
|
Senior Expert
Joined: Jul 2001
Posts: 6,904
|
Use: effect_cpy(MATERIAL* to,MATERIAL* from); That copies only the effect, not the remaining material values!
|
|
|
Re: How can i make a copy of a MATERIAL?
[Re: WretchedSid]
#400912
05/09/12 16:18
05/09/12 16:18
|
Joined: Sep 2009
Posts: 987 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 987
Budapest
|
There are mor things than just the C_LINK section I wouldn't touch! If the parent material gets destroyed, you only wreak havoc upon yourself if you continue to use your method. (You also leak memory) I can't see what problem should be when PARENT material gets destroyed. I have a solid copy of that. IMHO the new one isn't child, just another another, independent material. And how could be memory leak happen?
|
|
|
|