Sign in to follow this  
theo2005

Question about loading and rendering/drawing

Recommended Posts

[CODE]
void LoadXFileStatic(LPCSTR filename)
{
LPD3DXBUFFER materialBuffer;
DWORD numMaterials; // Note: DWORD is a typedef for unsigned long
LPD3DXMESH mesh;
// Load the mesh from the specified file
HRESULT hr=D3DXLoadMeshFromX(filename, D3DXMESH_SYSTEMMEM,
d3ddev, NULL,
&materialBuffer,NULL, &numMaterials,
&mesh );

if(S_OK != hr) // Check to determine whether method failed
{
MessageBox(NULL,
"Failed to load one of the Objects",
"Error!",
MB_ICONERROR | MB_OK);
}

D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer();
static D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials];
static LPDIRECT3DTEXTURE9 *meshTextures = new LPDIRECT3DTEXTURE9[numMaterials];
for (DWORD i=0; i < numMaterials; i++)
{
// Copy the material
meshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
meshMaterials[i].Ambient = meshMaterials[i].Diffuse;

// Create the texture if it exists - it may not
meshTextures[i] = NULL;
if (d3dxMaterials[i].pTextureFilename)
{
D3DXCreateTextureFromFile(d3ddev, d3dxMaterials[i].pTextureFilename, &meshTextures[i]);
}
}
materialBuffer->Release();
for (DWORD i=0; i < numMaterials; i++)
{
// Set the material and texture for this subset
d3ddev->SetMaterial(&meshMaterials[i]);
d3ddev->SetTexture(0,meshTextures[i]);

// Draw the mesh subset
mesh->DrawSubset( i );
}
}
[/CODE]

So that's the code for non-animated .x file loading.

Let's say it loads one object and renders after I set the world transform. Since everything goes in a loop, this causes great lag and soon lags me too much to exit the application. That I can understand.

I've tried:

Separating the loading part with an if statement, which successfully loads that one object once, and then proceeds to continuously rendering. What I did is put the meshMaterials, meshTextures and mesh variables outside of the if statement, so that they don't go out of scope. I also declared them static, because otherwise the program crashed for some reason. After I made those static, everything seemed to render, but the texture on the object I loaded was very dark and shaded in blue. That object is the one I made myself. If I used a skeleton (another model I did not make), it was textureless. If I did some more tweaking here and there, and added more than one object, the rest of the objects rendered but had the same textures as the first one. No amount of further tweaking fixed this.

I also tried adding multiple objects by making a dynamic array of those variables. I was probably getting ahead of myself.

I used to use XAnimator and I successfully automated everything. I could render every object and walk around with my character and easily enter/modify the coordinates. I scrapped everything when I've been working on the collision detection for month's and when I've finally gotten everything to work and rendered the bounds of the boxes on the screen. The problem was that for absolutely mysterious reasons, XAnimator decided that just one object, regardless which object I load in which order, has to be several float values longer when dealing with bounds.
--------------------------
So my problem right now:

I do not know how do I successfully separate rendering from loading, load only once without writing messy code.
I still have a suspicion that perhaps there is no way but to always load and redraw. If my suspicions are correct, I will probably make it load and draw only after one of the objects has changed a coordinate/rotated/scaled. I still fear that it will cause major lag and that it is the incorrect method of handling things.

With XAnimator, the loading and rendering were two different functions. I cannot seem to separate those two in the code above without getting some sort of errors or crashes.

Please help!

Theo Edited by theo2005

Share this post


Link to post
Share on other sites
I wonder how it's possible that this thread still has 0 views.
Never mind, it seemed to have shown the view count after the first post.

Anybody?

Well.. I could not still separate those two, but I decided to make use of goto, even though it's not recommended.
Here's the code for multiple objects I came up with:

[CODE]
void LoadXFileStatic(LPCSTR filename, int ObjNum)
{
static LPD3DXBUFFER * materialBuffer = new LPD3DXBUFFER[ObjectQuantity];
static DWORD * numMaterials = new DWORD[ObjectQuantity]; // Note: DWORD is a typedef for unsigned long
static LPD3DXMESH * mesh = new LPD3DXMESH[ObjectQuantity];
if (FirstLoad[ObjNum] == FALSE)
{
goto Render;
}
// Load the mesh from the specified file
HRESULT hr=D3DXLoadMeshFromX(filename, D3DXMESH_SYSTEMMEM,
d3ddev, NULL,
&materialBuffer[ObjNum],NULL, &numMaterials[ObjNum],
&mesh[ObjNum] );

if(S_OK != hr) // Check to determine whether method failed
{
MessageBox(NULL,
"Failed to load one of the Objects",
"Error!",
MB_ICONERROR | MB_OK);
}

static D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer[ObjNum]->GetBufferPointer();
static D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials[ObjNum]];
static LPDIRECT3DTEXTURE9 *meshTextures = new LPDIRECT3DTEXTURE9[numMaterials[ObjNum]];
for (DWORD i=0; i < numMaterials[ObjNum]; i++)
{
// Copy the material
meshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
meshMaterials[i].Ambient = meshMaterials[i].Diffuse;

// Create the texture if it exists - it may not
meshTextures[i] = NULL;
if (d3dxMaterials[i].pTextureFilename)
{
D3DXCreateTextureFromFile(d3ddev, d3dxMaterials[i].pTextureFilename, &meshTextures[i]);
}
}
materialBuffer[ObjNum]->Release();
Render:
for (DWORD i=0; i < numMaterials[ObjNum]; i++)
{
// Set the material and texture for this subset
d3ddev->SetMaterial(&meshMaterials[i]);
d3ddev->SetTexture(0,meshTextures[i]);

// Draw the mesh subset
mesh[ObjNum]->DrawSubset( i );
}
FirstLoad[ObjNum] = FALSE;

}
[/CODE]

Nope, I need to figure something out. The rest of the objects use the textures of the first object.
Perhaps a multidimensional array will do the trick.. Edited by theo2005

Share this post


Link to post
Share on other sites
[quote name='Fredericvo' timestamp='1341146400' post='4954521']
Why goto render? Just make render a function and call it in the if clause.
[/quote]

Because my objective is to stop it from loading every time and just render the object in question when needed after the first load had already been done.

However I've run into a problem the way the code is above. I need these variables as a dynamic multidimensional array:

static D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials[ObjNum]];
static LPDIRECT3DTEXTURE9 (*meshTextures) = new LPDIRECT3DTEXTURE9[numMaterials[ObjNum]];

But it doesn't work because it wants ObjNum to be a constant.. So I'm thinking of a way to bypass that.
Help is appreciated!

Share this post


Link to post
Share on other sites
[quote name='theo2005' timestamp='1340989825' post='4954004']

I do not know how do I successfully separate rendering from loading, load only once without writing messy code.

[/quote]

Your attempt to avoid messy code has created messy code! Trying to render inside a loading function is a terrible idea, as is having to use a goto statement. You need to seriously rethink your what you're trying to achieve.

Normal practice would be to load everything at application startup, then render what you need.

Share this post


Link to post
Share on other sites
[quote name='mark ds' timestamp='1341158204' post='4954558']
[quote name='theo2005' timestamp='1340989825' post='4954004']
I do not know how do I successfully separate rendering from loading, load only once without writing messy code.

[/quote]

Your attempt to avoid messy code has created messy code! Trying to render inside a loading function is a terrible idea, as is having to use a goto statement. You need to seriously rethink your what you're trying to achieve.

Normal practice would be to load everything at application startup, then render what you need.
[/quote]

It was not my idea. I want to load my objects ONCE, and be able to render them when needed. I just have no idea how to separate those two. Would you be so kind to help with that? I'm at a loss because if I separate the two, I can do the loading but then the variables that are needed for rendering go out of scope. Edited by theo2005

Share this post


Link to post
Share on other sites
So how would you do this. I rearranged my functions.

[CODE]
void LoadXFileStatic(LPCSTR filename, int ObjNum)
{
LPD3DXBUFFER materialBuffer[ObjectQuantity];
DWORD numMaterials[ObjectQuantity]; // Note: DWORD is a typedef for unsigned long
LPD3DXMESH mesh[ObjectQuantity];

// Load the mesh from the specified file
HRESULT hr=D3DXLoadMeshFromX(filename, D3DXMESH_SYSTEMMEM,
d3ddev, NULL,
&materialBuffer[ObjNum],NULL, &numMaterials[ObjNum],
&mesh[ObjNum] );

if(S_OK != hr) // Check to determine whether method failed
{
MessageBox(NULL,
"Failed to load one of the Objects",
"Error!",
MB_ICONERROR | MB_OK);
}

D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer[ObjNum]->GetBufferPointer();
D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials[ObjNum]];
LPDIRECT3DTEXTURE9 (*meshTextures) = new LPDIRECT3DTEXTURE9[numMaterials[ObjNum]];
for (DWORD i=0; i < numMaterials[ObjNum]; i++)
{
// Copy the material
meshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
meshMaterials[i].Ambient = meshMaterials[i].Diffuse;

// Create the texture if it exists - it may not
meshTextures[i] = NULL;
if (d3dxMaterials[i].pTextureFilename)
{
D3DXCreateTextureFromFile(d3ddev, d3dxMaterials[i].pTextureFilename, &meshTextures[i]);
}
}
materialBuffer[ObjNum]->Release();
}

inline void RenderStaticObject (int ObjNum)
{
for (DWORD i=0; i < numMaterials[ObjNum]; i++)
{
// Set the material and texture for this subset
d3ddev->SetMaterial(&meshMaterials[i]);
d3ddev->SetTexture(0,meshTextures[i]);

// Draw the mesh subset
mesh[ObjNum]->DrawSubset( i );
}
}
[/CODE]
Loading is outside of the main loop (while loop)

So how would I pass the arguments?

Share this post


Link to post
Share on other sites
Your mesh object and materials array are declared in that first function. You could declare them in main, pass them by address to the loading function then again to the render function. If you still find this too overwhelming then you could first make them global as a quick and dirty solution but I'll get flamed for saying this lol.

Share this post


Link to post
Share on other sites
[quote name='Fredericvo' timestamp='1341171246' post='4954620']
Your mesh object and materials array are declared in that first function. You could declare them in main, pass them by address to the loading function then again to the render function. If you still find this too overwhelming then you could first make them global as a quick and dirty solution but I'll get flamed for saying this lol.
[/quote]

Thank you Fredericvo.

After thinking for a while, I decided to settle with this one function, instead of passing arguments, because in that case I'd have to pass them twice from function to function.
It's just more logical for me to prefer the way it is. Seems simple enough now, at least everything works.

[CODE]
void LoadXRenderFileStatic(LPCSTR filename, int ObjNum)
{
static LPD3DXBUFFER materialBuffer[ObjectQuantity];
static DWORD numMaterials[ObjectQuantity]; // Note: DWORD is a typedef for unsigned long
static LPD3DXMESH mesh[ObjectQuantity];
static D3DXMATERIAL* d3dxMaterials[ObjectQuantity];
static D3DMATERIAL9 * meshMaterials[ObjectQuantity];
static LPDIRECT3DTEXTURE9 * meshTextures[ObjectQuantity];
if (FirstLoad[ObjNum] == TRUE)
{
// Load the mesh from the specified file
HRESULT hr=D3DXLoadMeshFromX(filename, D3DXMESH_SYSTEMMEM,
d3ddev, NULL,
&materialBuffer[ObjNum],NULL, &numMaterials[ObjNum],
&mesh[ObjNum] );

if(S_OK != hr) // Check to determine whether method failed
{
MessageBox(NULL,
"Failed to load one of the Objects",
"Error!",
MB_ICONERROR | MB_OK);
}


d3dxMaterials[ObjNum] = (D3DXMATERIAL*)materialBuffer[ObjNum]->GetBufferPointer();
meshMaterials[ObjNum] = new D3DMATERIAL9[numMaterials[ObjNum]];
meshTextures[ObjNum] = new LPDIRECT3DTEXTURE9[numMaterials[ObjNum]];
for (DWORD i=0; i < numMaterials[ObjNum]; i++)
{
// Copy the material
meshMaterials[ObjNum][i] = d3dxMaterials[ObjNum][i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
meshMaterials[ObjNum][i].Ambient = meshMaterials[ObjNum][i].Diffuse;

// Create the texture if it exists - it may not
meshTextures[ObjNum][i] = NULL;
if (d3dxMaterials[ObjNum][i].pTextureFilename)
{
D3DXCreateTextureFromFile(d3ddev, d3dxMaterials[ObjNum][i].pTextureFilename, &meshTextures[ObjNum][i]);
}
}
materialBuffer[ObjNum]->Release();
}

FirstLoad[ObjNum] = FALSE;
for (DWORD i=0; i < numMaterials[ObjNum]; i++)
{
// Set the material and texture for this subset
d3ddev->SetMaterial(&meshMaterials[ObjNum][i]);
d3ddev->SetTexture(0,meshTextures[ObjNum][i]);

// Draw the mesh subset
mesh[ObjNum]->DrawSubset( i );
}
}
[/CODE]

So this was a wall I had to pass through, I get these all the time. I'll see if I can optimize what I wrote in any way,
and dive into the head aching journey of loading the files with the hierarchy. Edited by theo2005

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this