Archived

This topic is now archived and is closed to further replies.

Removing member var -> error?

This topic is 5091 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

I have a class, THING, that is currently storing a bunch of information, including a MESH class. My problem began when I decided it seemed logical to store the mesh data (verts, indices, planes) in the MESH class, instead of the THING class. All of this worked fine, compiled fine, et al, but at runtime it began throwing System.NullReferenceException for whatever reason. Ok, so I go back through the code and make 100% sure that there are no references to any of the three variables I removed from the THING class, and there aren''t. After a while debugging I''ve found that my program doesn''t seem to like me removing one specific line, WORD Indices[99999], which I''ve flagged in the code below (I didn''t realize it but my indices were still using ghetto hard-coded arrays). Here''s my current THING class:
class THING
{
public:
	LPDIRECT3DDEVICE9 pd3dDevice;
	DWORD       ID;
	DWORD       CreationTime;
	DWORD       Flags;
	MESH        Mesh;
	char*       MeshFile;
protected:
	D3DXVECTOR3 Velocity;
	D3DXVECTOR3 Position;
	D3DXVECTOR3 Orientation;
	D3DXVECTOR3 RotVelocity;


public:
	int          Type;
	D3DXVECTOR3  CenterOfMass;
        D3DXPLANE*   Planes; //Doesn''t mind me removing this

        MODELVERTEX* Vertices; // or this

	WORD         Indices[99999]; //but removing this is bad

	int          NumVerts;
	int          NumFaces;
	BoundingSphere  root;
	int MaxHealth;
	int Health;

//Functions and blah blah blah...

};
And here''s how I want it:
class THING
{
public:
	LPDIRECT3DDEVICE9 pd3dDevice;
	DWORD       ID;
	DWORD       CreationTime;
	DWORD       Flags;
	MESH        Mesh;
	char*       MeshFile;
protected:
	D3DXVECTOR3 Velocity;
	D3DXVECTOR3 Position;
	D3DXVECTOR3 Orientation;
	D3DXVECTOR3 RotVelocity;


public:
	int          Type;
	D3DXVECTOR3  CenterOfMass;
	int          NumVerts;
	int          NumFaces;
	BoundingSphere  root;
	int MaxHealth;
	int Health;

//Functions and blah blah blah...

};
Any ideas as to what this is? I even did a search for "Indices" in VC++, and manually checked EVERY time that word came up, and there was no dereferencing pointers, or calling anything at all!

Share this post


Link to post
Share on other sites
Clean build meaning rebuild (deleting all *.obj and all)? If so, then yes.

As for some code using thing, exactly what do you need? I have several THINGs in my code right now. Not too many however, I believe that it''s just the player, the level, and a pointer/array to ten items (only four of which are used, I believe). This is all stored in a vector<thING*> called g_Things and I have THING* g_Player Initialized in my Init code. If there''s any specific bit of code you need to see please ask, as I need all the help I can get, however I have several thousand lines of code revolving around that class, and a couple hundred that use that class directly. Anyway, the most relevant code I can think to show is below:

All member functions of THING

vector<THING*> extern g_Things;
void AddMessage(char*);
THING::THING()
{
ID = g_Things.size();
}
THING::THING(LPDIRECT3DDEVICE9 pd3dd, char* file = "", D3DXVECTOR3 p = D3DXVECTOR3(0.0f, 0.0f, 0.0f), int t = TT_THING)
{
ID = g_Things.size();
Health = MaxHealth = 100;
Velocity = Orientation = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
pd3dDevice = pd3dd;
MeshFile = file;
Position = p;
Type = t;
m_CurWeapon = -1;
Mesh = MESH();
Mesh.InitMesh(pd3dDevice, MeshFile);
CreateSpheres();
}
THING::~THING()
{
SAFE_RELEASE(pd3dDevice);
}
void THING::SetVelocity(D3DXVECTOR3 v)
{
Velocity = v;
}
D3DXVECTOR3 THING::GetVelocity()
{
return Velocity;
}
void THING::SetPosition(D3DXVECTOR3 p)
{
Position = p;
Mesh.m_Pos = p;
}
D3DXVECTOR3 THING::GetPosition()
{
if(this == NULL)
return D3DXVECTOR3(0.0f, 0.0f, 0.0f);
return Position;
}
void THING::SetOrientation(D3DXVECTOR3 o)
{
Orientation = o;
Mesh.m_Pitch = o.x;
Mesh.m_Yaw = o.y;
Mesh.m_Roll = o.z;
}
D3DXVECTOR3 THING::GetOrientation()
{
return Orientation;
}
void THING::SetMesh(char* file)
{
MeshFile = &(*file);
Mesh.InitMesh(pd3dDevice, file);
}
char* THING::GetMesh()
{
return MeshFile;
}
void THING::Update(float elapsed, bool gravity)
{
if(fabsf(Velocity.x) < 0.001f)
Velocity.x = 0.0f;
if(fabsf(Velocity.y) < 0.001f)
Velocity.y = 0.0f;
if(fabsf(Velocity.z) < 0.001f)
Velocity.z = 0.0f;
Position += Velocity*elapsed;
Mesh.m_Pos = Position;
if(Health > MaxHealth) Health = MaxHealth;
if(Health < 0) Health = 0;
}
void THING::Render()
{
if(Type != TT_ROOM)
Mesh.RenderMesh();
else
Mesh.RenderMesh(D3DCULL_NONE);
}

void THING::CreateSpheres(int level)
{
root.level = 1;
NumVerts = Mesh.m_NumVertices;
D3DXComputeBoundingSphere(&Mesh.m_Vertices[0].pos, Mesh.m_NumVertices, sizeof(MODELVERTEX), &root.center, &root.radius);
}

void THING::Event(DWORD event, DWORD sender, bool issender, void* param)
{
switch(event)
{
case EVENT_NULL:
{
} break;
case EVENT_TOUCHED:
{
AddMessage("
Two things just touched!");
} break;
case EVENT_DAMAGED:
{
int amt = *(int*)(param);
Health -= amt;
if(Health <= 0)
{
Health = 0;
this->Event(EVENT_KILLED, sender, issender, 0);
}
if(Health >= MaxHealth)
Health = MaxHealth;
} break;
case EVENT_KILLED:
{
//...

} break;
case EVENT_RESPAWN:
{
//...

} break;
case EVENT_CREATED:
{
//...

} break;
case EVENT_FIRE:
{
//...

} break;
}
}


Init code: (ACTOR is a derived class from THING)

ACTOR Player = ACTOR(g_pd3dDevice, "meshes//player.x", D3DXVECTOR3(0.0f, 2.44f, 10.0f));

g_Player = &Player;
g_Things.push_back(&Player);


Part of MESH::InitMesh that intializes data of vertices, indices, and faces:

if(m_Vertices)
delete[] m_Vertices;
if(m_Indices)
delete[] m_Indices;
if(m_Faces)
delete[] m_Faces;
m_Vertices = 0;
m_Faces = 0;
m_Indices = 0;
m_Vertices = new MODELVERTEX[m_pMesh->GetNumVertices()];
m_Faces = new D3DXPLANE[m_pMesh->GetNumFaces()];
m_Indices = new WORD[m_pMesh->GetNumFaces()*3];
byte *ptr=NULL;
DWORD numVerts;
m_NumVertices = numVerts = m_pMesh->GetNumVertices();
DWORD fvf=m_pMesh->GetFVF();
DWORD vertSize=D3DXGetFVFVertexSize(fvf);
WORD* indices;
hr = m_pMesh->LockVertexBuffer(D3DLOCK_READONLY,(void**)&ptr);
if(FAILED(hr)) FatalError();
hr = m_pMesh->LockIndexBuffer(D3DLOCK_READONLY, (void**)&indices);
if(FAILED(hr)) FatalError();
m_NumFaces = m_pMesh->GetNumFaces();
for (DWORD i=0;i<numVerts;i++)
{
m_Vertices[i] = *(MODELVERTEX*)(ptr);
m_CenterOfMass += m_Vertices[i].pos;
ptr+=vertSize;
}
m_pMesh->UnlockVertexBuffer();
for(unsigned int i=0;i<m_pMesh->GetNumFaces();i++)
{
m_Indices[i*3] = indices[i*3];
m_Indices[i*3+1] = indices[i*3+1];
m_Indices[i*3+2] = indices[i*3+2];
D3DXPLANE temp;
D3DXVECTOR3 t0 = m_Vertices[m_Indices[3*i]].pos;
D3DXVECTOR3 t1 = m_Vertices[m_Indices[3*i + 1]].pos;
D3DXVECTOR3 t2 = m_Vertices[m_Indices[3*i + 2]].pos;
D3DXPlaneFromPoints(&temp, &t0, &t1, &t2);
m_Faces[i] = temp;
}
m_CenterOfMass /= m_NumVertices*1.0f;
m_pMesh->UnlockIndexBuffer();


I have a lot more waiting, just ask. Thank you a thousand times for your help and time!

Share this post


Link to post
Share on other sites
Sorry for above coloring problems, GameDev's source boxes don't seem to be 100% effective.

Edited to add another note (rather not waste bandwidth with another post). Whenever I delete that line, it gives me the error, but in the debugger, the System.NullReferenceException points to some bogus place in a completely different class file (that has NO interaction whatsoever with either THING nor MESH). It also begins saying that it can't find media file àx%%%%%%%\n#### or something (not sure about the actually ASCII, but it's definately random garbage).

[edited by - ms291052 on January 3, 2004 12:50:39 AM]

Share this post


Link to post
Share on other sites
Check for all uses of Indicies.

My bump tells me that you are walking off the end of the array somewhere.

I''ve seen similiar weird random errors and they come from bad memory accesses.

Also: check all your new''s and delete''s.

Perhaps you are walking off into Indicies space?

What you could do is step through the program.

Focus on the spot where the media file error comes in.
It might be associated with that.

Share this post


Link to post
Share on other sites
Total uses of THING::Indices = 0
Total uses of Mesh::m_Indices = 12

I''m disinclined to believe that it has anything to do with overwriting anything, as that array is never used for anything. For the same reason, I don''t think I could ''walk off'' into its space, as I''m not using it, and ideally, it wouldn''t have any space to walk in to. The media errors seem to me to be completely unrelated but I shall atleast try to do a step-by-step and try to pinpoint exactly what''s going on. Thanks for all your help!

Share this post


Link to post
Share on other sites
Well, THING::Planes and THING::Vertices are both pointers, but THING::Indices is a static array -- all 99999 elements are a part of the THING structure, so its size is += 2*99999 (2=sizeof WORD). This could be related to the reason it alone causes your program to fail.

Also the error could be not with Indices, but by sheer chance you don't get an exception if it's in THING. That would mean you just have a memory error elsewhere and you need to debug.

What happens if you move Indices to MESH but you keep it in THING as well? First put it in MESH but use the THING one, and ignore the mesh one. Then stop using the thing one and use the mesh's, without deleting the thing one.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

[edited by - CGameProgrammer on January 4, 2004 4:51:43 AM]

Share this post


Link to post
Share on other sites
I've successfully created my desired MESH class, all that's left is that damn Indices[99999] in the THING class. I do, however realize it's insane size, which is why I'm trying so hard to clear that artifact from my THING class. As of now MESH::m_Indices is the only reference being used throughout the program. THING::Indices is not used at all, but the program complains if I remove it.

Edited to state that in the MESH class Indices is indeed a WORD*, not a WORD[].

[edited by - ms291052 on January 4, 2004 6:19:26 AM]

Share this post


Link to post
Share on other sites
Another run through the debugger reveals this:

Memory Address: 003950bc lAllocID=1 dwSize=000047f8, ReturnAddr=03903796 (pid=000006ec)
Memory Address: 0039b2b4 lAllocID=9 dwSize=00000bd0, ReturnAddr=038fe1e9 (pid=000006ec)
Memory Address: 00393084 lAllocID=10 dwSize=00000008, ReturnAddr=038fe289 (pid=000006ec)
Memory Address: 0039bebc lAllocID=11 dwSize=00000140, ReturnAddr=03902f45 (pid=000006ec)
Memory Address: 0039c2cc lAllocID=20 dwSize=000006bc, ReturnAddr=0390f12f (pid=000006ec)
Memory Address: 003998ec lAllocID=22 dwSize=00001008, ReturnAddr=0390478b (pid=000006ec)
Memory Address: 00393044 lAllocID=24 dwSize=00000008, ReturnAddr=039048bc (pid=000006ec)
Memory Address: 03ac0064 lAllocID=26 dwSize=00003500, ReturnAddr=03903796 (pid=000006ec)
Memory Address: 0039c9bc lAllocID=27 dwSize=00000198, ReturnAddr=03903796 (pid=000006ec)
Memory Address: 0039c034 lAllocID=28 dwSize=00000030, ReturnAddr=03903796 (pid=000006ec)
Memory Address: 03ac359c lAllocID=29 dwSize=00001020, ReturnAddr=03903796 (pid=000006ec)


Could a memory leak have anything to do with the problem? If so (or even if not) does anyone know how I can tell which variables are being stored at these memory addresses so that I can make sure to delete them?

Share this post


Link to post
Share on other sites
Another odd note, that I just realized (yes, I''ve been working through the night on this!) is that as I decrease the size of Indices[] nothing happens. That is, until I start getting within range of my larget mesh''s face count * 3. When I get within two of that the app begins to act weird. Any lower and it often crashes all together. Is this a clue (I''m sure it is)? But I''m 100% POSITIVE that I''m not referencing that variable ANYWHERE in my code anymore. I''ve run search after search to sift through my code and nothing comes up at all.

Share this post


Link to post
Share on other sites
This code seems very wrong to me and might be the memory bug

It is from MESH::InitMesh

for(unsigned int i=0;i<m_pMesh->GetNumFaces();i++)
{
m_Indices[i*3] = indices[i*3];
m_Indices[i*3+1] = indices[i*3+1];
m_Indices[i*3+2] = indices[i*3+2];
D3DXPLANE temp;
D3DXVECTOR3 t0 = m_Vertices[m_Indices[3*i]].pos;
D3DXVECTOR3 t1 = m_Vertices[m_Indices[3*i + 1]].pos;
D3DXVECTOR3 t2 = m_Vertices[m_Indices[3*i + 2]].pos;
D3DXPlaneFromPoints(&temp, &t0, &t1, &t2);
m_Faces[i] = temp;
}


You initialize m_Indices with:
 new WORD[m_pMesh->GetNumFaces()*3];   

That means that the maximum size of m_Indices is GetNumFaces()*3 - 1! BUT! Here is the problem, in your loop you access it like this in the last and second clausule:

3*i + 1
3*i + 2

And i goes to GetNumFaces() - 1. Do you see the problem yet? When i is GetNumFaces() - 1 (the last one in the loop) you will try to access the array index GetNumFaces() and (GetNumFaces() + 1) which are BOTH non-existant. It is probably so that the Indices[] in the THING class is memory that is right next to the m_Indices and thus this overstep doesn't really do anything, but if you remove it there is no other memory to step into and thus you have a debug error. Try a quick-fix by changing the line where you init m_Indices to:

m_Indices = new WORD[m_pMesh->GetNumFaces()*3 + 2];

and comment out the Indices[99999] out from the THING class.

Hope that's the solution.

Edit: Fixed the formatting of the source box. Looked ugly.

[edited by - biovenger on January 4, 2004 8:41:44 AM]

Share this post


Link to post
Share on other sites
If I''m not mistaken when i = GetNumFaces() - 1 [3*i] is the third-to-last index, therefore 3*i + 1, and 3*i + 2 should be valid. Anyway, I tried the above modifications in the code, and there was no change.

P.S. Another note: Upon logging all the indices of a certain mesh (namely the player) I found that the number of indices indeed should be NumFaces*3 without any +2. Also, as expected, the Indices[] array is filled with naught but zeros.

Share this post


Link to post
Share on other sites
Yeah, seems it's too early in the morning for me, heh. But anyhow, I thought of the above scenario only because I had a very similar problem once and it was exactly as I stated above, I had used the memory in one array by overstepping another.

Although, that can't be the case for you since the Indices array is filled with zeros. I'm somehow now tending to bend towards a bad destructor. Perhaps the memory in that array is keeping some destructor from doing serious memory problems? Could you check all your destructors to see if there are any problems? Also, make sure, if you are sharing pointers between classes, to always check for null in a pointer before deleting and after deletion set it to null.

Edit: Quick thought... It seems that -every- mesh needs a full Indices array of 99999 elements. Since you allocate a new THING class every time you push something in the g_Things vector. What happens if you put the Indices as a static variable? Will it crash then? Maybe another clue.

[edited by - biovenger on January 4, 2004 9:15:43 AM]

Edit2: Another thought, you have a pointer 'indices' in the MESH::InitMesh code. Are you sure it isn't being overstepped? Can you find out its size?

[edited by - biovenger on January 4, 2004 9:22:56 AM]

Share this post


Link to post
Share on other sites
I'm not using any shared pointers in either of these classes.
However, making Indices[] static makes it throw the exception, but I hope that means more to you than it does to me . And lastly, the lowercase indices pointer in InitMesh is the pointer that I use to LockIndexBuffer. That's in the code. Thanks for all your help thusfar, and hopefully that static clue will be of further interest to you.

Edited to add: [random] Is it just me or are the GameDev clocks fast by about ten minutes?[/random]

[edited by - ms291052 on January 4, 2004 9:41:18 AM]

Share this post


Link to post
Share on other sites
They are fast, yup.

Oh, and about the static, it does mean a lot really. See, it means that -every- mesh needs that extra memory in some way. It tells us that the error is bound to the mesh class. Since a static variable is only instantiated once per class type .

So it means that your code throws an exception if every mesh class doesn't have twice as much amount of memory as it really needs for its faces.

I'll read-through your code more, also, post more methods of the mesh class, it might be neccecary.

Dumb question: Are you absolutely positive that all your models in your game are triangulated? (Have triangles as faces)

EDIT: Could you also try tripling the amount you initiate your three pointers in the Mesh class? (m_Indices, m_Vertices and m_Faces) Triple one at a time while commenting out the array in the THING class. See what happens. If nothing does, try tripling all three of them and commenting out THING. Heck, if that doesn't work then try all combinations with tripling data for the three pointers, there aren't many.

[edited by - biovenger on January 4, 2004 10:03:09 AM]

Share this post


Link to post
Share on other sites
MESH.cpp

#include <d3dx9.h>
#include <iostream.h>
#include <windows.h>
#include <string>
#include "mesh.h"
#include "errors.h"
using namespace std;
#define SAFE_RELEASE(p) if(p != NULL){(p)->Release();(p)=NULL;}
LPDIRECT3DTEXTURE9 extern g_TransparentTexture;
LPDIRECT3DDEVICE9 extern g_pd3dDevice;
HWND extern hWnd;
string extern TransparentFile;
MESH::MESH()
{
m_Vertices = 0;
m_Faces = 0;
m_Indices = 0;
m_FileName = new char[999];
ZeroMemory(m_FileName, sizeof(m_FileName));
Initialized = false;
m_pd3dDevice = NULL;
m_pMesh = NULL;
m_pMeshMaterials = NULL;
m_pMeshTextures = NULL;
m_dwNumMaterials = 0L;
m_Pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
m_Yaw = m_Pitch = m_Roll = 0.0f;
m_NumVertices = 0;
}
MESH::~MESH()
{
//SAFE_RELEASE(m_pMesh);

// SAFE_RELEASE(m_pd3dDevice);

}
void MESH::InitMesh(LPDIRECT3DDEVICE9 pd3dDevice, char* strPath)
{
cout << "Beginning to load MESH " << strPath << endl;
strcpy(m_FileName, strPath);
m_pd3dDevice = pd3dDevice;
HRESULT hr = D3DXLoadMeshFromX( strPath, D3DXMESH_MANAGED, pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &m_dwNumMaterials, &m_pMesh );
if(FAILED(hr))
{
MediaNotFound(strPath);
return;
}
m_NumVertices = m_pMesh->GetNumVertices();
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
m_pMeshMaterials = new D3DMATERIAL9[m_dwNumMaterials];
m_pMeshTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials];

for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
m_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;

m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;

m_pMeshTextures[i] = NULL;
if( d3dxMaterials[i].pTextureFilename != NULL && lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
{
if(!strcmpi(d3dxMaterials[i].pTextureFilename, TransparentFile.c_str()))
{
m_pMeshTextures[i] = g_TransparentTexture;
continue;
}
string newfilename = "meshes//textures//";

newfilename += d3dxMaterials[i].pTextureFilename;
LPSTR newfilenamestr = (char*)(newfilename.c_str());
hr = D3DXCreateTextureFromFileEx( pd3dDevice, newfilenamestr, 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0 ,255), NULL, NULL, &m_pMeshTextures[i]);
if(FAILED(hr))
{
MediaNotFound(newfilenamestr);
}
const TCHAR* strPrefix = TEXT("..\\");
const int lenPrefix = lstrlen( strPrefix );
TCHAR strTexture[MAX_PATH];
lstrcpyn( strTexture, strPrefix, MAX_PATH );
lstrcpyn( strTexture + lenPrefix, d3dxMaterials[i].pTextureFilename, MAX_PATH - lenPrefix );
}
}


if(m_Vertices)
delete[] m_Vertices;
if(m_Indices)
delete[] m_Indices;
if(m_Faces)
delete[] m_Faces;
m_Vertices = 0;
m_Faces = 0;
m_Indices = 0;
m_Vertices = new MODELVERTEX[m_pMesh->GetNumVertices()];
m_Faces = new D3DXPLANE[m_pMesh->GetNumFaces()];
m_Indices = new WORD[m_pMesh->GetNumFaces()*3];
byte *ptr=NULL;
DWORD numVerts;
m_NumVertices = numVerts = m_pMesh->GetNumVertices();
DWORD fvf=m_pMesh->GetFVF();
DWORD vertSize=D3DXGetFVFVertexSize(fvf);
WORD* indices;
hr = m_pMesh->LockVertexBuffer(D3DLOCK_READONLY,(void**)&ptr);
if(FAILED(hr)) FatalError();
hr = m_pMesh->LockIndexBuffer(D3DLOCK_READONLY, (void**)&indices);
if(FAILED(hr)) FatalError();
m_NumFaces = m_pMesh->GetNumFaces();
for (DWORD i=0;i<numVerts;i++)
{
m_Vertices[i] = *(MODELVERTEX*)(ptr);
m_CenterOfMass += m_Vertices[i].pos;
ptr+=vertSize;
}
m_pMesh->UnlockVertexBuffer();
for(unsigned int i=0;i<m_pMesh->GetNumFaces();i++)
{
m_Indices[i*3] = indices[i*3];
m_Indices[i*3+1] = indices[i*3+1];
m_Indices[i*3+2] = indices[i*3+2];
D3DXPLANE temp;
D3DXVECTOR3 t0 = m_Vertices[m_Indices[3*i]].pos;
D3DXVECTOR3 t1 = m_Vertices[m_Indices[3*i + 1]].pos;
D3DXVECTOR3 t2 = m_Vertices[m_Indices[3*i + 2]].pos;
D3DXPlaneFromPoints(&temp, &t0, &t1, &t2);
m_Faces[i] = temp;
}
m_CenterOfMass /= m_NumVertices*1.0f;
m_pMesh->UnlockIndexBuffer();
Initialized = true;
}


void MESH::RenderMesh(DWORD rs)
{
if(m_pd3dDevice == NULL)
{
FatalError("Device of a mesh was NULL");
m_pd3dDevice = g_pd3dDevice;
}
D3DXMATRIX old;
DWORD oldc;
m_pd3dDevice->GetTransform(D3DTS_WORLD, &old);
m_pd3dDevice->GetRenderState(D3DRS_CULLMODE, &oldc);
D3DXMATRIX trans;
D3DXMATRIX rot;
D3DXMATRIX mYaw;
D3DXMATRIX mPitch;
D3DXMATRIX mRoll;
D3DXMatrixTranslation(&trans, m_Pos.x, m_Pos.y, m_Pos.z);
D3DXMatrixRotationX(&mYaw, m_Yaw);
D3DXMatrixRotationY(&mPitch, m_Pitch);
D3DXMatrixRotationZ(&mRoll, m_Roll);
rot = mYaw * mPitch * mRoll;
D3DXMATRIX m = rot*trans;
m_pd3dDevice->SetTransform( D3DTS_WORLD, &m );
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, rs );

for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
m_pd3dDevice->SetMaterial( &m_pMeshMaterials[i] );
if(!(m_pMeshTextures[i] == g_TransparentTexture))
{
m_pd3dDevice->SetTexture( 0, m_pMeshTextures[i] );
m_pMesh->DrawSubset( i );
}
}
m_pd3dDevice->SetTransform( D3DTS_WORLD, &old );
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, oldc );
}

void MESH::SetTexture(char* filename)
{
for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
m_pMeshTextures[i] = NULL;
if( lstrlen(filename) > 0 )
{
D3DXCreateTextureFromFileEx( m_pd3dDevice, filename, 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0 ,255), NULL, NULL, &m_pMeshTextures[i]);
const TCHAR* strPrefix = TEXT("..\\");
const int lenPrefix = lstrlen( strPrefix );
TCHAR strTexture[MAX_PATH];
lstrcpyn( strTexture, strPrefix, MAX_PATH );
lstrcpyn( strTexture + lenPrefix, filename, MAX_PATH - lenPrefix );
}
}
}


MESH.h

#ifndef MESH__H
#define MESH__H
#include <d3dx9.h>
struct MODELVERTEX
{
D3DXVECTOR3 pos;
D3DXVECTOR3 n;
float tu, tv;
};
#define D3DFVF_MODELVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
class MESH {
public:
char* m_FileName;
LPDIRECT3DDEVICE9 m_pd3dDevice;
LPD3DXBUFFER pD3DXMtrlBuffer;
LPD3DXMESH m_pMesh;
D3DMATERIAL9* m_pMeshMaterials;
LPDIRECT3DTEXTURE9* m_pMeshTextures;
DWORD m_dwNumMaterials;
D3DXVECTOR3 m_CenterOfMass;
D3DXVECTOR3 m_Pos;
MODELVERTEX* m_Vertices;
D3DXPLANE* m_Faces;
WORD* m_Indices;
int m_NumVertices;
int m_NumFaces;
float m_Yaw, m_Pitch, m_Roll;
bool Initialized;
MESH();
~MESH();
public:
void RenderMesh(DWORD rs = D3DCULL_CCW);
void InitMesh(LPDIRECT3DDEVICE9 pd3dDevice, char* strPath);
void SetTexture(char*);
};
#endif


Yes, all the MESHes are triangulated. I'll go through and try tripling alloted memory for those three variables. Thanks so incredibly much for you help!

Edit: Again please excuse the darn coloring errors!

[edited by - ms291052 on January 4, 2004 1:42:25 PM]

[edited by - ms291052 on January 4, 2004 1:45:50 PM]

Share this post


Link to post
Share on other sites
Wow, I just realized that for some reason or another my app is now saying I'm using gun #59852081 (i only have three that work). More variable garbage .

Anyway, tripling each, and deleting THING::Indices results in NullReferenceException on the line
D3DXComputeBoundingSphere(&Mesh.m_Vertices[0].pos, Mesh.m_NumVertices, sizeof(MODELVERTEX), &root.center, &root.radius);

Vertices only tripled - No good.
Planes only tripled - No good.
Indices only tripled - No good.
Verts and Planes - No good.
Indices and Planes - No good.
Verts and Indices - No good.



[edited by - ms291052 on January 4, 2004 2:00:29 PM]

Share this post


Link to post
Share on other sites
I find it -very- odd that the compiler gives you such strange errors...

Perhaps you could try to catch the exception and print whatever message it holds?
Try a catch block and see.

Also, I'm assuming that all variables are in fact existant instances. It looks so from the code anyway.

PS: This will probably solve nothing, but if you change the MESH variable in the THING class to a pointer and create it with new in the THING constructor, see if it changes anything? Also, leave the data tripled, all three of them.

Edit: Did your bounding spheres work when you have the Indices[99999] variable intact?

[edited by - biovenger on January 4, 2004 2:11:47 PM]

Share this post


Link to post
Share on other sites
Does it matter at all that neither MESH nor THING have overloaded operator=? Just curious because in the past it's acted funky whenever I do things like MESH mesh = MESH() or THING* thing = new THING(...).


Edit: wow, we actually hit two pages lol

[edited by - ms291052 on January 4, 2004 2:19:00 PM]

Share this post


Link to post
Share on other sites
MESH Mesh is not MESH* Mesh. I updated all the code to use new and -> etc. However, at runtime, the first thing that greets me is our same friendly error except this time pointing to:

void THING::SetPosition(D3DXVECTOR3 p)
{
Position = p;
Mesh->m_Pos = p; //This line
}

Share this post


Link to post
Share on other sites
I notice in your mesh code, you are mixing char and TCHAR types. Now, normally that wouldn''t be a problem. But it might be a problem if UNICODE was defined.

Share this post


Link to post
Share on other sites
Oh, I think I'm onto something! Quick, change this line:

From MESH::MESH()

ZeroMemory(m_FileName, sizeof(m_FileName));



you are giving the size of a pointer to a ZeroMemory function. That means it will only Null 4 bytes of the data stream instead of 999. Change the line to:


ZeroMemory(m_FileName, sizeof(char) * 999);


I'm not sure that will fix all problems, but it might fix some of them.
Just a quick comment. Use std::string instead of char *. std::string allocates memory by itself and you don't have to define an array of 999 elements.

Edit: Typos.

[edited by - biovenger on January 4, 2004 4:35:20 PM]

Share this post


Link to post
Share on other sites