Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/05/23 14:22
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
4 registered members (AndrewAMD, Quad, soulman3, Ayumi), 675 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 3 1 2 3
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
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

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):

Code:
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!):

Code:
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 Offline OP
User
Aku_Aku  Offline OP
User

Joined: Sep 2009
Posts: 987
Budapest
Thanks for your helpful reply.
Quote:
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.
Code:
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: Aku_Aku] #400888
05/09/12 10:38
05/09/12 10:38
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Rei_Ayanami Offline
Expert
Rei_Ayanami  Offline
Expert

Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
When you simply memcpy the material, you will mess up the engine link-system. The first element in the struct definition is the C_LINK link which points to another element. (I guess it is some sort of linked list). So if you simply copy that part over from another material, it could be that your copied material messes up that list.

So better copy the elements one by one as HeelX suggested.

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 Offline OP
User
Aku_Aku  Offline OP
User

Joined: Sep 2009
Posts: 987
Budapest
Quote:
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.
Code:
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: Aku_Aku] #400899
05/09/12 13:07
05/09/12 13:07
Joined: Jul 2001
Posts: 6,904
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
I wouldn't rely on that, simply because it is more a hack and if anything changes, you are messed up. This happens also, if you use my code, because I assume the fields to be in that order and so on... but if you copy everything one by one, you are safe. I think smile

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 Offline
Expert
WretchedSid  Offline
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 Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Originally Posted By: GameScore
mtl_create and set the values to this material

Originally Posted By: Aku_Aku
And the smart way?

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: Uhrwerk] #400908
05/09/12 15:22
05/09/12 15:22
Joined: Mar 2006
Posts: 1,993
Karlsruhe
PadMalcom Offline
Serious User
PadMalcom  Offline
Serious User

Joined: Mar 2006
Posts: 1,993
Karlsruhe
Use:

effect_cpy(MATERIAL* to,MATERIAL* from);

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
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
Originally Posted By: PadMalcom
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 Offline OP
User
Aku_Aku  Offline OP
User

Joined: Sep 2009
Posts: 987
Budapest
Originally Posted By: JustSid
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?

Page 2 of 3 1 2 3

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1