D3DXCreateTextureFromFile

Started by
25 comments, last by sushi-one 19 years, 6 months ago
why does this crash??:

D3DXCreateTextureFromFile(
 g_pd3dDevice, cObject->cASEFile[0].sMaterial.azBitmap, &cObject->g_pTexture );


Hans-Petter Harveg
Advertisement
cObject is probably null. You need to tell us more, though - for example, the type of crash (I predict access violation).

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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();}
Hans-Petter Harveg
anyone??:)
Hans-Petter Harveg
Forum FAQ

Pay particular attention to the 'How do I debug DirectX programs?' section.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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");	}
Hans-Petter Harveg
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.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

I added the colde just above where the crash occurs, but I still get the same f... error.. do you feel me now??:) hehehe

Hans-Petter Harveg
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
Chris ByersMicrosoft DirectX MVP - 2005
If I told you that I also did that, would you even believe me?? hehehe :) I have to say that OpenGL is rocking the ass of DX..
Hans-Petter Harveg

This topic is closed to new replies.

Advertisement