Archived

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

please help with my d3d code...

This topic is 5797 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 been looking at/messing with this code for a couple hours and still cannot figure it out. i get no errors, not even in the debugger, yet my vertices are not rendered. they are read from a file, which i believe is working fine, but something in the Render() function ( i think ), is going wrong...if you have time ( and a heart ), please try and help:
    

#include "d3dx8.h"
#include <fstream.h>

#pragma comment(lib,"d3d8.lib")
#pragma comment(lib,"d3dx8.lib")

#ifndef C3DEngine_H
#define C3DEngine_H

struct VERTEX
{
	float x,y,z;
	float tu,tv;
};

#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_TEX1)

class C3DEngine { 
public:
	C3DEngine();
	HRESULT InitD3D(HWND hWnd, bool Window);
	HRESULT InitVB(char *theFile);
	void SetNormals();
	void Render();
	void ShutDownD3D();
	~C3DEngine();

	LPDIRECT3D8 d3dObj;
	LPDIRECT3DDEVICE8 d3dDevice;
	LPDIRECT3DVERTEXBUFFER8 d3dVB;
	LPDIRECT3DTEXTURE8 theTexture;
	VERTEX *vertices;
};

HRESULT C3DEngine::InitD3D(HWND hWnd,bool Window)
{  
	theTexture = NULL;
	d3dObj = NULL;
	d3dDevice = NULL;
	d3dVB = NULL; 

	if( NULL == ( d3dObj = Direct3DCreate8( D3D_SDK_VERSION ) ) )

		return E_FAIL;

	D3DDISPLAYMODE d3dDisplay;

	if( FAILED( d3dObj->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3dDisplay ) ) )

		return E_FAIL;
	
	D3DPRESENT_PARAMETERS d3dPP;
	
	ZeroMemory(&d3dPP, sizeof(d3dPP));
if (Window)
{
d3dPP.SwapEffect                      = D3DSWAPEFFECT_FLIP;
d3dPP.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
    }
    else
    {
d3dPP.BackBufferWidth                 = d3dDisplay.Width;
d3dPP.BackBufferHeight                = d3dDisplay.Height;
d3dPP.SwapEffect                      = D3DSWAPEFFECT_FLIP;
d3dPP.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_TWO;
}
d3dPP.Windowed               = Window;
d3dPP.BackBufferFormat = d3dDisplay.Format;
d3dPP.EnableAutoDepthStencil = TRUE;
d3dPP.AutoDepthStencilFormat = D3DFMT_D16;

if( FAILED( d3dObj->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                  &d3dPP, &d3dDevice ) ) )
		return E_FAIL;

	return S_OK;
}

HRESULT C3DEngine::InitVB(char *theFile)
{
	ifstream somefile(theFile);
	char *aString = new char[40];
	vertices = new VERTEX[3];

	if (FAILED(D3DXCreateTextureFromFile(d3dDevice,"c:\\windows\\desktop\\test.bmp",
	&theTexture))) 
	MessageBox(0,"Texture failed.","Error",0);

	for (int i = 0; i < 3; i++) {
	somefile >> aString;
        vertices[i].x = (float)atof(aString);
	somefile >> aString;
	vertices[i].y = (float)atof(aString);
	somefile >> aString;
	vertices[i].z = (float)atof(aString);
	somefile >> aString;
	vertices[i].tu = (float)atof(aString);
	somefile >> aString;
	vertices[i].tv = (float)atof(aString);
	}
	somefile.close();

	delete [] aString;

if( FAILED( d3dDevice->CreateVertexBuffer(3*sizeof(VERTEX), 0 , D3DFVF_VERTEX,D3DPOOL_DEFAULT, &d3dVB ) ) )
		return E_FAIL;

	VOID* theVertices;
	if( FAILED( d3dVB->Lock( 0,sizeof(vertices), (BYTE**)&theVertices, 0 ) ) )
		
		return E_FAIL;
	
	memcpy( theVertices, vertices, sizeof(vertices));
	d3dVB->Unlock();

	return D3D_OK;
}

void C3DEngine::ShutDownD3D()
{
    if( d3dVB != NULL )
        d3dVB->Release();

	delete [] vertices;

    if( d3dDevice != NULL )
        d3dDevice->Release();

    if( d3dObj != NULL )
        d3dObj->Release();

	if ( theTexture != NULL )
	theTexture->Release();
}

void C3DEngine::SetNormals()
{
	D3DXMATRIX matWorld;
	d3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

	D3DXMATRIX matView;
	D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 1.0f, -1.0f ),&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),&D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
	d3dDevice->SetTransform( D3DTS_VIEW, &matView );

	D3DXMATRIX matProj;
        D3DXMatrixPerspectiveFovLH( &matProj,D3DX_PI/4,1.0f,1.0f,100.0f);
    d3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}

void C3DEngine::Render()
{
	d3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
    d3dDevice->BeginScene();
	d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
	d3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
	d3dDevice->SetTexture(0,theTexture);
	d3dDevice->SetVertexShader(D3DFVF_VERTEX);
	d3dDevice->SetStreamSource(0, d3dVB, sizeof(VERTEX));
	d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,1);
	d3dDevice->EndScene();
	d3dDevice->Present(NULL,NULL,NULL,NULL);
	d3dDevice->SetTexture(0,NULL);
}

#endif


    
Edited by - u0_80 on January 31, 2002 6:17:20 AM Edited by - u0_80 on January 31, 2002 6:33:16 AM

Share this post


Link to post
Share on other sites
Hello,
You seam to have the same problem as I had (posted yesterday)
      
void C3DEngine::Render()
{
d3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
d3dDevice->BeginScene();
d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
d3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
d3dDevice->SetTexture(0,theTexture);
d3dDevice->SetVertexShader(D3DFVF_VERTEX);
d3dDevice->SetStreamSource(0, d3dVB, sizeof(VERTEX));
d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,1);
d3dDevice->EndScene();
d3dDevice->Present(NULL,NULL,NULL,NULL);
d3dDevice->SetTexture(0,NULL);
}


The key is the | D3DCLEAR_ZBUFFER in your clear device statement. You need to clear the Z-buffer, otherwise D3D thinks there is something in front of your model, and doesn't draw it.

Hope that works
Gordon.

PS CREDIT to Steveyboy as he pointed this out to me yesterday.. cheers mate.

P|-|34R t|-|3 |\|3\/\/|\|3SS

Edited by - vImTo on January 31, 2002 7:14:06 AM

Edited by - vImTo on January 31, 2002 7:14:41 AM

Share this post


Link to post
Share on other sites
what do you mean youre not using a z buffer? (i hope that didnt sound too harsh), i think that d3d either expects you to tell it to automatically handle the z buffer, or you will write your own z buffer, so the D3DCLEAR_ZBUFFER is probably the solution

Share this post


Link to post
Share on other sites
You are using a z-buffer:

d3dPP.EnableAutoDepthStencil = TRUE;d3dPP.AutoDepthStencilFormat = D3DFMT_D16;

Clear it.

Neil

WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think the problem lies in where you are copying the vertices to your vertexbuffer.

memcpy( theVertices, vertices, sizeof(vertices));

Your memcpy is using sizeof(vertices) which will return the size of the pointer (4 bytes). Try sizeof(VERTEX)*num_vertices. I''m not exactly a C++ expert, but this is a pretty standard C error.

HTH,
Squid56

P.S. You may have the same problem in other places (I noticed you did the same think in the Lock call).

Share this post


Link to post
Share on other sites