• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
theo2005

Question about loading and rendering/drawing

9 posts in this topic

[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
0

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
0

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!
0

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.
0

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
0

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?
0

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.
0

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
0

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  
Followers 0