Jump to content
  • Advertisement
Sign in to follow this  
JackBob

Loading drawing Meshes with functions?

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I've been searching everywhere for just something on this. Anything I do find (like on this site) the posted code just no longer appears.

but my problem, I seem to be able to load a mesh from a file and set it to a mesh function

MYMESH loadMesh(LPCTSTR namet)

{

MYMESH temp;

temp.name=namet;

LPD3DXBUFFER bufShipMaterial;

D3DXLoadMeshFromX(temp.name,

D3DXMESH_SYSTEMMEM,

g_pd3dDevice,

NULL,

&bufShipMaterial,

NULL,

&temp.NumMaterials,

&temp.Mesh);



D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();



temp.mmaterial = D3DMATERIAL9[temp.NumMaterials];

temp.ttexture = LPDIRECT3DTEXTURE9[temp.NumMaterials];

(DWORD i = 0; i < temp.NumMaterials; i++)

{

temp.mmaterial = tempMaterials.MatD3D;

temp.mmaterial.Ambient = temp.mmaterial.Diffuse;



if(FAILED(D3DXCreateTextureFromFileA(g_pd3dDevice,

tempMaterials.pTextureFilename,

&temp.ttexture)))

temp.ttexture = NULL;

return temp;

}

}


and setting it to MYMESH meshy = loadMesh(L"bananas.x"); along those lines,
that seems to work
but my problem is when I try to draw it.
for some reason it doesn't like when I use meshy.Mesh or meshy.NumMaterials it just doesn't like it. When I load up the program it instantly crashes visual studio and the program.
I know something i'm doing is not passing an address correctly and I haven't figured out how or why.
Any help would be great and even just links to other threads would help me alot. So far in my attempts I haven't found squat and I figured there would be alot on this. I am new to C++. I used java alot and decided to switch since directx is much better than java3d
I know this is a silly question that a beginner would ask but i'm just stumped.
Once again any help would be awesome

Thanks all

Share this post


Link to post
Share on other sites
Advertisement
Just a suggestion, I know pointers can be tricky sometimes, but try returning a pointer to the temp mesh in the function

return &temp;

then when calling the function, you could try

[color="#1C2837"]MYMESH meshy = &loadMesh(L"bananas.x");

[color="#1C2837"]what would work even better though, is this

[color="#1C2837"]MYMESH meshy;

[color="#1C2837"]loadMesh(L"bananas.x", meshy);

Share this post


Link to post
Share on other sites
if I were to do MYMESH meshy
loadMesh(blah, meshy);
wouldn't I need to make it &meshy? so that way it passes the address to be able to access and change MYMESH variables?

also your code shows up like [color=#666600][color=#666600][color=#666600] and stuff.
any idea why that is? makes reading your code impossible =(
p.s. thanks for the reply. I also did try return &temp; it didn't work. everytime I do something its always like MYMESH * cannont covert to MYMESH. So frustrating.

Share this post


Link to post
Share on other sites
I know what you mean about all that extra stuff. You must have posted that while i was editing my post... Stupid code tag never seems to work for me so i just removed it.

and yeah, i think your right about the &meshy, my bad. I was looking at the D3DX10Mesh interface as a parameter when i wrote that.

but anyway, try the &meshy and see if it works

BTW, how did i get a -1 for my last post? Not that I care too much, i guess my code was hard to read because of the code tag problem, but I only see an option to give people a thumbs up, haha, not a thumbs down Edited by iedoc

Share this post


Link to post
Share on other sites
Hidden
oooh sorry, ok, everything you have looks fine actually, you said your problem is when using meshy.mesh and meshy.numMaterials right? Thats because those are pointers, so when you use them i believe you should be using them like this maybe?

&meshy.mesh
&meshy.numMaterials


maybe i'm wrong about that, but if you haven't tried it, try it

other than that it looks like your code is how it should be

Share this post


Link to post

BTW, how did i get a -1 for my last post?


because you just suggested to return the address to a local stack variable... that's an instant crash. Go back to chapter 2 of whatever book you are using.#



Share this post


Link to post
Share on other sites

I know what you mean about all that extra stuff. You must have posted that while i was editing my post... Stupid code tag never seems to work for me so i just removed it.

and yeah, i think your right about the &meshy, my bad. I was looking at the D3DX10Mesh interface as a parameter when i wrote that.

but anyway, try the &meshy and see if it works

BTW, how did i get a -1 for my last post? Not that I care too much, i guess my code was hard to read because of the code tag problem, but I only see an option to give people a thumbs up, haha, not a thumbs down

I did not give it to you, but your post was not so helpful. If he had followed your advice he would have returned a pointer to an object that becomes invalid as soon as the pointer to it is returned.



temp.mmaterial = D3DMATERIAL9[temp.NumMaterials];
temp.ttexture = LPDIRECT3DTEXTURE9[temp.NumMaterials];

I assume you meant:

temp.mmaterial = new D3DMATERIAL9[temp.NumMaterials];
temp.ttexture = new LPDIRECT3DTEXTURE9[temp.NumMaterials];



Also, you returned temp inside the for loop in the code you posted. I assume you made a mistake when posting it here.
You posted:

/*for?*/ (DWORD i = 0; i < temp.NumMaterials; i++) {
temp.mmaterial = tempMaterials.MatD3D;
temp.mmaterial.Ambient = temp.mmaterial.Diffuse;
if(FAILED(D3DXCreateTextureFromFileA(g_pd3dDevice,
tempMaterials.pTextureFilename,
&temp.ttexture))) {
temp.ttexture = NULL;
}

// You returned inside the for loop?
return temp;

}
}


when you should have posted:

for (DWORD i = 0; i < temp.NumMaterials; i++) {
temp.mmaterial = tempMaterials.MatD3D;
temp.mmaterial.Ambient = temp.mmaterial.Diffuse;
if(FAILED(D3DXCreateTextureFromFileA(g_pd3dDevice,
tempMaterials.pTextureFilename,
&temp.ttexture))) {
temp.ttexture = NULL;
}

}
return temp;
}





Does your MYMESH have a destructor? A custom copy operator?
In order to eliminate the possibilities of these kinds of hiccups, reformat your function to accept a reference and then fill that variable directly inside the function.

MYMESH meshy;
if ( !loadMesh( _T( "L. Spiro Rulez.x" ), meshy ) ) { return false; } // By the way, don’t forget _T() here.
// Else meshy is now ready to be used.



bool loadMesh( LPCTSTR namet, MYMESH &temp) {
// Remove the declaration of temp from this function in the old code so that you are now working on the reference.
// Return true or false for errors instead of invalid structures. Or error codes that can tell exactly why it fails.
}



L. Spiro

Share this post


Link to post
Share on other sites
haha, wow, your right, i'm sorry about that, I should have known better... laugh.gif


but yeah, that's what i was trying to suggest above, is to send a MYMESH object to the function, so that the function can fill it out directly.

Share this post


Link to post
Share on other sites
Holy hell. I must have been tired. Yeah my problem was that I returned in the for loop so obvious it didn't get all the stuff needed and since I got one run. Everytime I checked for null it would have something but not all of it. Silly mistake by me. Well atleast I knew I had the right idea by creating a return class to avoid passing memory =) just I failed at the most basic thing.

Anyhow, Thanks iedoc and yogurt emperor =) you guys were really helpful with my sillyness =)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!