Click here to Skip to main content
15,867,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add a tiled texture to the floor. The texture does not show up, instead all the vertices are showing, and the ball now has a floor that follows it's bounce. Any help would be appreciated.

//within winmain

    pGrid->CreateDirectXMesh(pd3dDevice);
    D3DXCreateSphere(pd3dDevice, 2, 20, 20, &pBallMesh, NULL);

	// Main message loop:
	// Enter the message loop
    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
		// check for messages
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
			TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		// this is called when no messages are pending
		else
		{
	        createCamera(0.1f, 1000.0f);		// near clip plane, far clip plane
	        moveCamera(camPos);
	        pointCamera(camLook);
            float speed, maxSpeed = 0.5;
            long volume;
            // update the ball velocity and position
            // fake some gravity here by increasing the ball's negative y velocity
            BallVel.y -= .01f;
            // limit the ball's speed to some maxSpeed value
            if (Abs(D3DXVec3Length(&BallVel)) > maxSpeed) {
                D3DXVec3Normalize(&BallVel, &BallVel);
                D3DXVec3Scale(&BallVel, &BallVel, maxSpeed);
            }   
            // update the ball's position
            BallPos = BallPos + BallVel;
            // rebound if ball hits the grid
            pGrid->GetGridPoint(&gridPt, BallPos.x, BallPos.z);
            if (BallPos.y < gridPt.y) {
                // adjust the volume based on the speed of the collision
                speed = D3DXVec3Length(&BallVel);
                volume = long(0.25 * DSBVOLUME_MIN * (1 - (speed/maxSpeed)));
                // if speed is really slow set volume to min value
                if (speed < maxSpeed * 0.1f) {
                    volume = DSBVOLUME_MIN;
                }
                hr = DSBuffer->SetVolume(volume);
                if( FAILED( hr  ) )    
                    DXTRACE_ERR( TEXT("Volume error "), hr );
	            // play this sound
	            playSound(DSBuffer);
                // reflect the ball's velocity in the y direction
                // make the rebound have some loss of energy
                BallVel.y *= -.8f;
                // adjust ball slightly above the grid
                BallPos.y = gridPt.y + .01f;
            }
            D3DXMatrixTranslation(&translate, BallPos.x, BallPos.y, BallPos.z);
	        D3DXMatrixIdentity(&id);

			// Clear the backbuffer to a black color
			pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(5, 5, 5), 1.0, 0 );

			// Clear the z buffer to a black color
			pd3dDevice->Clear( 0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(5, 5, 5), 1.0, 0 );

			// Call D3DXCreateTextureFromFile
			hr = D3DXCreateTextureFromFile( pd3dDevice, "patchy.jpg", &g_pTexture);

			// Check return code to make sure you have a valid texture
			if FAILED(hr)
				return false;

			pd3dDevice->BeginScene();
                 	pd3dDevice->SetTransform(D3DTS_WORLD, &id);
                        pGrid->DrawDirectXMesh(pd3dDevice);
                 	pd3dDevice->SetTransform(D3DTS_WORLD, &translate);
                        pBallMesh->DrawSubset(0);

			pd3dDevice->SetTexture(0, g_pTexture);
                        pGrid->DrawDirectXMesh(pd3dDevice);

			pd3dDevice->EndScene();
			// Present the backbuffer contents to the display
			pd3dDevice->Present( NULL, NULL, NULL, NULL );
		}
    }
	// release and shutdown Direct3D
	shutdownDirect3D();
	// Release the DirectSound buffer created above
	if (DSBuffer)
	{
		DSBuffer->Release();
		DSBuffer = NULL;
	}
	// shutdown DirectSound
	shutdownDirectSound();
	return (int) msg.wParam;
}
Posted
Updated 7-Jun-11 13:10pm
v3

1 solution

I can't see what's happening inside of DrawDirectXMesh but If you can only see the vertex points instead of the actual faces that I can only assume it's rendering with D3DPT_POINTLIST instead of D3DPT_TRIANGLELIST. It might be easier to set up the vertices for the floor yourself and then render them.


As for the floor following the ball, it's because you didn't reset the world matrix after rendering the ball. You also seem to be drawing the floor twice:
MIDL
pd3dDevice->SetTransform(D3DTS_WORLD, &id);
pGrid->DrawDirectXMesh(pd3dDevice); //<-Drawing the grid in the right place
pd3dDevice->SetTransform(D3DTS_WORLD, &translate);
pBallMesh->DrawSubset(0);

pd3dDevice->SetTexture(0, g_pTexture);
pGrid->DrawDirectXMesh(pd3dDevice); //<- Drawing the grid again with the same translation as the ball


What exactly is pGrid?
Please use the 'Add Comment' link to reply
 
Share this answer
 
Comments
Pramslam 9-Jun-11 16:02pm    
Thanks for the timely response!
pGrid is called within the main function before the main loop within winmain.cpp
<pre lang="midl">pGrid = new Grid(11, 11, &v0, &v1, &v2, &v3);

pGrid->CreateDirectXMesh(pd3dDevice);

D3DXCreateSphere(pd3dDevice, 2, 20, 20, &pBallMesh, NULL);

// Main message loop:</pre>

This is the grid constructor I am using.

<pre>
struct GRIDVERTEX
{
D3DXVECTOR3 p; // The untransformed, 3D position for the vertex
D3DXVECTOR3 n; // normalized normal
DWORD color; // color for this vertex
float u, v;
};
#define D3DFVF_GRIDVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1)
class Grid {
private:
int maxWidthVertices; // width in vertices
int maxHeightVertices; // height in vertices
int widthVertices; // width in vertices
int heightVertices; // height in vertices
int widthFaces; // widht in faces
int heightFaces; // height in faces
unsigned long maxNumVertices;
unsigned long numVertices;
int numFaces;
D3DXVECTOR3 v0, v1, v2, v3; // in D3D face order
D3DXVECTOR3 dv; // delta v from 0 to 2
GRIDVERTEX *vertices;
WORD *indexData;
LPD3DXMESH mesh;
public:
Grid(
int maxWidthVerticesP,
int maxHeightVerticesP,
D3DXVECTOR3 *v0P,
D3DXVECTOR3 *v1P,
D3DXVECTOR3 *v2P,
D3DXVECTOR3 *v3P
);
</pre>

<pre lang="cs">Grid::Grid(
int maxWidthVerticesP,
int maxHeightVerticesP,
D3DXVECTOR3 *v0P,
D3DXVECTOR3 *v1P,
D3DXVECTOR3 *v2P,
D3DXVECTOR3 *v3P
) {
int i, j, k;

maxWidthVertices = maxWidthVerticesP;
maxHeightVertices = maxHeightVerticesP;
widthVertices = maxWidthVertices;
heightVertices = maxHeightVertices;

numVertices = widthVertices * heightVertices;
widthFaces = widthVertices - 1;
heightFaces = heightVertices - 1;
numFaces = widthFaces * heightFaces * 2; // 2 triangles in each grid cell
v0 = *v0P;
v1 = *v1P;
v2 = *v2P;
v3 = *v3P;
dv = v2 - v0;
dv.x /= float(widthVertices - 1);
dv.y /= 1;
dv.z /= float(heightVertices - 1);

// set up the vertex and normal and texture data
vertices = new GRIDVERTEX[numVertices];

for (j=0, k=0; j<heightVertices; j++) {
for (i=0; i<widthVertices; i++, k++) {
GetGridPoint(&(vertices[k].p), float(v0.x + i * dv.x), float(v0.z + j * dv.z));
GetGridNormal(&(vertices[k].n), &(vertices[k].p));
vertices[k].color = D3DCOLOR_ARGB(255,0,255,255);
vertices[k].u = vertices[k].p.x;
vertices[k].v = vertices[k].p.z;
}
}</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900