Sign in to follow this  

D3DXCreateTextureFromFile

This topic is 4834 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, here are the source (I modifyed the code a little, but I get the same error at the same place in the code), and yes superpig, it is access violation lol :)


//-----------------------------------------------------------------------------
// Loads an 3D mesh with morphing.
//
// Author: Hans-Petter Harveg <hanspetter@spillerom.com>
//-----------------------------------------------------------------------------
void Object::Load( char* pzFilename ) {
// Make sure the file is ether .ase or .smf
int vLength = strlen(pzFilename);

if( tolower((int)pzFilename[vLength-3]) == 'a' &&
tolower((int)pzFilename[vLength-2]) == 's' &&
tolower((int)pzFilename[vLength-1]) == 'e' ) {

// Turn morphing off
setMorph(false);

// Read the object's .ase file
cASEFile = new ASE[1];
cASEFile[0].Load(pzFilename);

//
GetASEInfo();
} else if( tolower((int)pzFilename[vLength-3]) == 's' &&
tolower((int)pzFilename[vLength-2]) == 'm' &&
tolower((int)pzFilename[vLength-1]) == 'f' ) {

// Turn morphing on
setMorph(true);

// Set the first key to be 0
iKeyCounter = 0;

// Open config file so we can parse the .smf file
// to get all the keyframes in the morph
cConfig.OpenFile(pzFilename);

// Get number of keys
nKeys = (int)cConfig.GetNumber("NumKeys");
sMorphKey = new MorphKey_t[nKeys];

vSteps = (int)cConfig.GetNumber("Steps");

if( cConfig.GetNumber("IsActive") != 0 ) {
isActive = true;
} else {
isActive = false;
}

if( cConfig.GetNumber("IsLooping") != 0 ) {
isLooping = true;
} else {
isLooping = false;
}

int iMorph;
// Load .ase files
cASEFile = new ASE[nKeys];
for( iMorph=0; iMorph<nKeys; iMorph++ ) {
cASEFile[iMorph].Load( cConfig.GetString("KeyMesh%d", iMorph ) );
}

//
GetASEInfo();

// Temp buffer (when morphing)
pCurrentMorph = new D3DXVECTOR3[nFaces*6];

for( iMorph=0; iMorph<nKeys; iMorph++ ) {
// Allocate memory for the vertex data
sMorphKey[iMorph].psVertex = new D3DXVECTOR3[nVertices];

// Allocate memory for the face normal data
sMorphKey[iMorph].psNormal = new D3DXVECTOR3[nFaces];
}

// Loop threw all keys and get vertex data, normals, face data and uv coords
for( iMorph=0; iMorph<nKeys; iMorph++ ) {
// Get vertex list
for( int iVertex=0; iVertex<nVertices; iVertex++ ) {
sMorphKey[iMorph].psVertex[iVertex].x = ((cASEFile[iMorph].sObject.sVertices[iVertex].x)/100)-sPosition.vX;
sMorphKey[iMorph].psVertex[iVertex].y = ((cASEFile[iMorph].sObject.sVertices[iVertex].y)/100)-sPosition.vY;
sMorphKey[iMorph].psVertex[iVertex].z = ((cASEFile[iMorph].sObject.sVertices[iVertex].z)/100)-sPosition.vZ;

sMorphKey[iMorph].psNormal[iVertex].x = cASEFile[iMorph].sObject.sNormals[iVertex].x;
sMorphKey[iMorph].psNormal[iVertex].y = cASEFile[iMorph].sObject.sNormals[iVertex].y;
sMorphKey[iMorph].psNormal[iVertex].z = cASEFile[iMorph].sObject.sNormals[iVertex].x;
}

// Get face list
int iFace=0;
sMorphKey[iMorph].aFace = new int* [nFaces];
for( iFace=0; iFace<nFaces; iFace++ ) {
sMorphKey[iMorph].aFace[iFace] = new int [6];
}
for( iFace=0; iFace<nFaces; iFace++ ) {
sMorphKey[iMorph].aFace[iFace][0] = cASEFile[iMorph].sObject.apFaceList[iFace][0];
sMorphKey[iMorph].aFace[iFace][1] = cASEFile[iMorph].sObject.apFaceList[iFace][1];
sMorphKey[iMorph].aFace[iFace][2] = cASEFile[iMorph].sObject.apFaceList[iFace][2];
sMorphKey[iMorph].aFace[iFace][3] = cASEFile[iMorph].sObject.apFaceList[iFace][3];
sMorphKey[iMorph].aFace[iFace][4] = cASEFile[iMorph].sObject.apFaceList[iFace][4];
sMorphKey[iMorph].aFace[iFace][5] = cASEFile[iMorph].sObject.apFaceList[iFace][5];
}

// Allocate memory for the vertex data
sMorphKey[iMorph].pTex = new D3DXVECTOR3[nTextureVertices];
for( int iTex=0; iTex<nTextureVertices; iTex++ ) {
sMorphKey[iMorph].pTex[iTex].x = cASEFile[iMorph].sObject.sTexVertices[iTex].x;
sMorphKey[iMorph].pTex[iTex].y = cASEFile[iMorph].sObject.sTexVertices[iTex].y;
sMorphKey[iMorph].pTex[iTex].z = cASEFile[iMorph].sObject.sTexVertices[iTex].z;
}

// Get face list
sMorphKey[iMorph].ppTFace = new int* [nTextureFaces];
for( iFace=0; iFace<nTextureFaces; iFace++ ) {
sMorphKey[iMorph].ppTFace[iFace] = new int [3];
}
for( iFace=0; iFace<nTextureFaces; iFace++ ) {
sMorphKey[iMorph].ppTFace[iFace][0] = cASEFile[iMorph].sObject.apTexFaces[iFace][0];
sMorphKey[iMorph].ppTFace[iFace][1] = cASEFile[iMorph].sObject.apTexFaces[iFace][1];
sMorphKey[iMorph].ppTFace[iFace][2] = cASEFile[iMorph].sObject.apTexFaces[iFace][2];
}
}

// Set up the first two objects to morph between
sSourceKey = &sMorphKey[0];
sDestKey = &sMorphKey[1];

// Close config file
cConfig.CloseFile();
} else {
Debug_Panic("Mesh file need to be ether .ase or .smf");
}
}


//-----------------------------------------------------------------------------
//
//
// Author: Hans-Petter Harveg <hanspetter@spillerom.com>
//-----------------------------------------------------------------------------
//static void GetASEInfo(Object* cObject) {
void Object::GetASEInfo() {
// Set the material
ZeroMemory( &sMaterial, sizeof(sMaterial) );
sMaterial.Ambient.r = cASEFile[0].sMaterial.sAmbient.vRed;
sMaterial.Ambient.g = cASEFile[0].sMaterial.sAmbient.vGreen;
sMaterial.Ambient.b = cASEFile[0].sMaterial.sAmbient.vBlue;
sMaterial.Ambient.a = 1.0f;
sMaterial.Diffuse.r = cASEFile[0].sMaterial.sDiffuse.vRed;
sMaterial.Diffuse.g = cASEFile[0].sMaterial.sDiffuse.vGreen;
sMaterial.Diffuse.b = cASEFile[0].sMaterial.sDiffuse.vBlue;
sMaterial.Diffuse.a = 1.0f;
sMaterial.Specular.r = cASEFile[0].sMaterial.sSpecular.vRed;
sMaterial.Specular.g = cASEFile[0].sMaterial.sSpecular.vGreen;
sMaterial.Specular.b = cASEFile[0].sMaterial.sSpecular.vBlue;
sMaterial.Specular.a = 1.0f;

// Load a texture
if( D3DXCreateTextureFromFile( g_pd3dDevice, cASEFile[0].sMaterial.azBitmap, &g_pTexture ) != D3D_OK ) {
Debug_Panic("Could not create texture|");
}

// Get number of vertices
nVertices = cASEFile[0].sObject.nVertex;

// Get number of faces
nFaces = cASEFile[0].sObject.nFaces;

// Get number of texture vertices
nTextureVertices = cASEFile[0].sObject.nTexVertex;

// Get number of texture faces
nTextureFaces = cASEFile[0].sObject.nTexFaces;

// Get position
sPosition.vX = cASEFile[0].sObject.sPivot.x/100;
sPosition.vY = cASEFile[0].sObject.sPivot.y/100;
sPosition.vZ= cASEFile[0].sObject.sPivot.z/100;

// Get scale values
sScale.vX = cASEFile[0].sObject.sScale.x;
sScale.vY = cASEFile[0].sObject.sScale.y;
sScale.vZ = cASEFile[0].sObject.sScale.z;

// Create the vertex buffer.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 9*nFaces*sizeof(ObjectVertexStruct_t),
0, OBJECT_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL ) ) ) {
Debug_Panic("%s: Could not create vertex buffer", MODULE);
}

// Allocate memory for the vertex data
pVertex = new D3DXVECTOR3[nVertices];

// Allocate memory for the normal data
pNormal = new D3DXVECTOR3[nVertices];

// Get vertex list / normals
for( int iVertex=0; iVertex<nVertices; iVertex++ ) {
pVertex[iVertex].x = ((cASEFile[0].sObject.sVertices[iVertex].x)/100)-sPosition.vX;
pVertex[iVertex].y = ((cASEFile[0].sObject.sVertices[iVertex].y)/100)-sPosition.vY;
pVertex[iVertex].z = ((cASEFile[0].sObject.sVertices[iVertex].z)/100)-sPosition.vZ;

pNormal[iVertex].x = cASEFile[0].sObject.sNormals[iVertex].x;
pNormal[iVertex].y = cASEFile[0].sObject.sNormals[iVertex].y;
pNormal[iVertex].z = cASEFile[0].sObject.sNormals[iVertex].x;

}

// Get face list
int iFace=0;
aFace = new int* [nFaces];
for( iFace=0; iFace<nFaces; iFace++ ) {
aFace[iFace] = new int [6];
}
for( iFace=0; iFace<nFaces; iFace++ ) {
aFace[iFace][0] = cASEFile[0].sObject.apFaceList[iFace][0];
aFace[iFace][1] = cASEFile[0].sObject.apFaceList[iFace][1];
aFace[iFace][2] = cASEFile[0].sObject.apFaceList[iFace][2];
aFace[iFace][3] = cASEFile[0].sObject.apFaceList[iFace][3];
aFace[iFace][4] = cASEFile[0].sObject.apFaceList[iFace][4];
aFace[iFace][5] = cASEFile[0].sObject.apFaceList[iFace][5];
}

// Allocate memory for the vertex data
pTex = new D3DXVECTOR3[nTextureVertices];
for( int iTex=0; iTex<nTextureVertices; iTex++ ) {
pTex[iTex].x = cASEFile[0].sObject.sTexVertices[iTex].x;
pTex[iTex].y = cASEFile[0].sObject.sTexVertices[iTex].y;
pTex[iTex].z = cASEFile[0].sObject.sTexVertices[iTex].z;
}

// Get face list
ppTFace = new int* [nTextureFaces];
for( iFace=0; iFace<nTextureFaces; iFace++ ) {
ppTFace[iFace] = new int [3];
}
for( iFace=0; iFace<nTextureFaces; iFace++ ) {
ppTFace[iFace][0] = cASEFile[0].sObject.apTexFaces[iFace][0];
ppTFace[iFace][1] = cASEFile[0].sObject.apTexFaces[iFace][1];
ppTFace[iFace][2] = cASEFile[0].sObject.apTexFaces[iFace][2];
}

// Generate surface to hold the image
ObjectVertexStruct_t *pVertices;
if( FAILED( g_pVertexBuffer->Lock( 0, sizeof(ObjectVertexStruct_t), (void**)&pVertices, 0 ) ) ) {
Debug_Panic("%s: Could not lock vertex buffer", MODULE);
}

int iFace2=0;
for( iFace=0; iFace<nFaces; iFace++ ) {
for( iVertex=0; iVertex<6; iVertex++ ) {
// Copy vertices to the vertex buffer
pVertices[iFace2].vPosition.x = pVertex[aFace[iFace][iVertex]].x;
pVertices[iFace2].vPosition.y = pVertex[aFace[iFace][iVertex]].y;
pVertices[iFace2].vPosition.z = pVertex[aFace[iFace][iVertex]].z;

// Copy normals to the vertex buffer
pVertices[iFace2].vNormal.x = pNormal[aFace[iFace][iVertex]].x;
pVertices[iFace2].vNormal.y = pNormal[aFace[iFace][iVertex]].y;
pVertices[iFace2].vNormal.z = pNormal[aFace[iFace][iVertex]].z;

// Copy the UV coordinates
pVertices[iFace2].vTu = pTex[ppTFace[iFace][iVertex%3]].x;
pVertices[iFace2].vTv = pTex[ppTFace[iFace][iVertex%3]].y;

// Copy the first mesh to a temp buffer for morphing
if( isMorph() ) {
pCurrentMorph[iFace2].x = pVertex[aFace[iFace][iVertex]].x;
pCurrentMorph[iFace2].y = pVertex[aFace[iFace][iVertex]].y;
pCurrentMorph[iFace2].z = pVertex[aFace[iFace][iVertex]].z;
}
iFace2++;
}
}
g_pVertexBuffer->Unlock();

}



Share this post


Link to post
Share on other sites
well, I have been debuging *alot*.. but still, this stupid ass error wont go away:) none of the pointers are missing an adress and the filename is ok, but when I run it I get a memory could not be "read" error??
Im not even able to get the error message from the D3DXCreateTextureFromFile function because the it crashes before any message is caught..


// Load a texture
if( D3DXCreateTextureFromFile( g_pd3dDevice, cASEFile[0].sMaterial.azBitmap, &g_pTexture ) != D3D_OK ) {
Debug_Panic("Could not create texture");
}

Share this post


Link to post
Share on other sites
So you're telling me the DirectX debug runtime doesn't give you any error messages leading up to the crash?

I'd recommend you add the following couple of lines of code before the call to D3DXCreateTextureFromFile:


assert(g_pd3dDevice, "g_pd3dDevice is NULL when I try calling the function!");
assert(cASEFile, "cASEFile is NULL when I try calling the function!");
assert(this, "I'm an invalid object!");


You also need to be sure you're initialising these variables - g_pd3dDevice, cASEFile and the like - to zero at startup.

Share this post


Link to post
Share on other sites
Breakpoint just above the call to D3DXCreateTextureFromFile on the last executable line that was run, then look at the values of everything you are passing into the function. You will find out what's wrong if you do this.

Chris

Share this post


Link to post
Share on other sites
hahaha, you are just so lame, what kind of attitude is that on a forum like this?? I have a problem, and I ask people for help because I cant find it out my self, simple as that..

btw since you are so incredible smart, cant you just tell me right away what this can be?? becaouse you see, I do not get any asserts, all the parameters are initialized, the file / path are ok..

so if you do not have the skills to help me, thats ok, but dont give me that shit..

Share this post


Link to post
Share on other sites
Is the texture you're reading valid? D3DX should protect against this, but its a possibility. Tyr a different texture file. Double and tripple check that cASEFile[0].sMaterial.azBitmap is a valid string (OutputDebugString(cASEFile[0].sMaterial.azBitmap); OutputDebugString("\n");).

What about if you run this code:

LPDIRECT3DTEXTURE9 pTexture;
D3DXCreateTextureFromFile(g_pd3dDevice, "Filename.ext", &pTexture);


Where "Filename.ext" is the path to a texture file. Does that crash? If so, theres something wrong with your device.
Have you accidently called g_pd3dDevice->Release()? Look at the virtual function table entries in the variable watch window in the debugger. If they're 0xfeeefeee, then you've called g_pd3dDevice->Release(), and released the object.

If the above code works, it means that either cObject is NULL, or the path is invalid.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
after some more tests I've found out that all calls to the DirectX device fails.. but how can this be when my init method is not giving me any errors and the device actually have an adress???

Share this post


Link to post
Share on other sites
Failed device creation does not raise a "classic" c++ exception; you should check the HRESULT value instead, to determine the outcome of the function.

As for non-null device handle - c++ does not automatically empty variables for you, so if you haven't set it to 0 prior to the call, the value is probably not 0 even if the creation function fails.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
after some more tests I've found out that all calls to the DirectX device fails.. but how can this be when my init method is not giving me any errors and the device actually have an adress???


1) If a call to DirectX fails with an error return code, then the reason for that failure -=*WILL*=- be sent to the output of your debugger if you are using the debug DirectX runtimes and you've set the output level to high.

Bear in mind that I'm NOT talking about debug builds of YOUR application here. I mean the debug version of DirectX, as enabled through the DirectX applet in the system Control Panel.

Could you post the output from the debug Direct3D runtime when it fails ?



2) On a related note, did you definately link with the DEBUG version of the D3DX library? - i.e. you should link with D3DX9D.LIB and not D3DX9.LIB - that will give you more complete validation and error reporting from that.



3) If you have created the device with the D3DCREATE_PUREDEVICE flag, then try removing it until you have found out where the bug is. A pure device will hide some problems because it bypasses much of the validation in the debug D3D runtime.



4)
Quote:
hahaha, you are just so lame, what kind of attitude is that on a forum like this?? I have a problem, and I ask people for help because I cant find it out my self, simple as that..

btw since you are so incredible smart, cant you just tell me right away what this can be?? becaouse you see, I do not get any asserts, all the parameters are initialized, the file / path are ok..

so if you do not have the skills to help me, thats ok, but dont give me that shit..


TBH that's really a pretty "lame" attitude for YOU to have with people who are spending/wasting their own free time trying to help you!!

IMO the people who have already tried to help most definately do have the "skills" as they're people who are more often than not posting helpful replies in this forum rather than vague questions.

Nobody is trying to be patronising or be nasty to you; but there ARE some obvious questions that DO need to be asked because you haven't provided enough details in your posts.

Nobody is perfect, I'm not, you're not, and you've posted here because you admit there seems to be a bug in your code (or use of D3D/D3DX); so while you may think you've already tried every possibility for debugging, it is possible you've missed something!

Please don't get upset when people do ask/mention those "obvious" things - it's just a part of problem solving, you exclude the simplest things before digging any deeper.



5) While putting asserts in relevent places in the code is always a good idea - asserting that a pointer isn't NULL won't catch all issues.

For example you have a member of a class which was allocated with "new" or "malloc", which is a pointer then until it is initialised, it's initial value will be 0xCDCDCDCD rather than NULL and so the assert will never catch it.



6) Assuming the failure isn't related to your device pointer being trashed, is the filename definately ok?, and the memory pointed to by the cObject pointer ? Could you post them here (including the address in hexadecimal). Check at the point of the call to D3DX, not earlier.



7) And the texture file itself is definately where it's meant to be, and in a valid format ?



8) Apart from some possibly risky filename extension checking (what would it do if fed a filename such as "a.x"), from the two functions posted, there isn't anything _obviously_ wrong.

BUT: since the class definition (and so data-types) and nothing related to the D3D device creation has been posted, the above is about all that can be said I think!

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
after some more tests I've found out that all calls to the DirectX device fails.. but how can this be when my init method is not giving me any errors and the device actually have an adress???

Define "fails". Whats the return value of the functions? (If they're not all the same value, just post a sample 2 or 3)

Share this post


Link to post
Share on other sites
Quote:
Original post by sushi-one
hahaha, you are just so lame, what kind of attitude is that on a forum like this?? I have a problem, and I ask people for help because I cant find it out my self, simple as that..

btw since you are so incredible smart, cant you just tell me right away what this can be?? becaouse you see, I do not get any asserts, all the parameters are initialized, the file / path are ok..

so if you do not have the skills to help me, thats ok, but dont give me that shit..


Perhaps you should stop being a dumbass and start trying to figure out what is wrong? People are assuming what might have caused the error since they don't have the complete code and can't compile. They are helping, and yet you see them as stupid, unhelpfull, etc.

And it's Coder's 100% full right to say those things, it's his forum and he has this special powers called "moderator".

Behave!

Share this post


Link to post
Share on other sites
thnx gyes! and about my rud answer to "coder", yupp, it was very childish of me, but he could have said something else to, even if this is he's forum.. there is noe reason to be rud (hehe even on a reply like I did.. lol), even if i am stupid lol

S1SA: I do get this adress on texture before I pass it to the function.. 0xCDCDCDCD

can I post my 3D engine on this site, så everyone can see it? cuz Im almost giving up here:p

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by superpig
Quote:
Original post by sushi-one
I do get this adress on texture before I pass it to the function.. 0xCDCDCDCD
Say hello to an uninitialised variable.


hehe :)

Share this post


Link to post
Share on other sites
To clarify a couple of things:
- This really isn't "my" forum. I'm just a moderator - someone who volunteered to provide maintainance for this forum.

- If you look closely at my post, you'll find that I said that in reply to:
Quote:
I have to say that OpenGL is rocking the ass of DX..

Which is simply a case of blaming the tool (DX) for something that isn't its fault. That's why I said:
Quote:
A bad smith blames his tools instead of learning how to use them.


Which means the problem's not with DX, and that you should question your code instead of blaming it on DX. That's all.

If my initial post was rude, I apologize.

Share this post


Link to post
Share on other sites

This topic is 4834 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.

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