• Advertisement
Sign in to follow this  

Textures...

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

Hi....i m learning directx and having a problem with one of my code.... i wrote this code to draw a texture....but when i execute it, i see the directx Window, but no texture :(....can u help me out... here is the code.......This is only the .CPP File... /* '01 Main' */ /*Mirus Window FrameWork Header*/ #include "mrWindow.h" /*Direct3D Header*/ #include <d3d8.h> #include <math.h> /*Our Custom Vertex Structure*/ class CustomVertex { public: /*Transformed Position*/ FLOAT x, y, z, rhw; /*Color*/ DWORD Color; /*Texture Coordinates*/ FLOAT tU, tV; }; /*Our Custom Vertex Type*/ #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1) /*Custom Derived Class*/ class D3DWindow:public mrWindow { /*Direct3D Interfaces*/ LPDIRECT3D8 m_pD3D; LPDIRECT3DDEVICE8 m_pD3DDevice; /*Direct3D texture Interface*/ LPDIRECT3DTEXTURE8 m_pD3DTexture; /*Direct3D Current Formatt*/ mrUInt32 m_iD3DFormat; /*Vertices*/ CustomVertex m_kVertices [4]; public: /*Constructor/Destructor*/ D3DWindow(void) {}; ~D3DWindow(void) {}; /*Setup and Shutdown Direct3D*/ HRESULT SetupDirect3D(void); HRESULT KillDirect3D(void); /*Window Manipulation Functions*/ mrBool32 Frame(void); }; /*Initializes Direct3D*/ HRESULT D3DWindow::SetupDirect3D(void) { /*Create the Direct3D Object*/ if(NULL == (m_pD3D = Direct3DCreate8 (D3D_SDK_VERSION))) { return E_FAIL; } /*Get the current Display Mode so we may know what bit depth we are*/ D3DDISPLAYMODE d3ddm; if(FAILED(m_pD3D->GetAdapterDisplayMode (D3DADAPTER_DEFAULT, &d3ddm))) { return E_FAIL; } /*Fille in the present prameters*/ D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp) ); /*We Want WindowedMode*/ d3dpp.Windowed =TRUE; /*Discard This*/ d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; /*Same Format as the current format (We Got this from g_pD3D->GetAdapterDisplayMode) */ d3dpp.BackBufferFormat = d3ddm.Format; /*Create The Device*/ if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice ) ) ) { return E_FAIL; } //return D3D_OK; /*Create The Texture*/ if (FAILED (m_pD3DDevice->CreateTexture (128, 128, 0 , 0, (D3DFORMAT) m_iD3DFormat, D3DPOOL_MANAGED, &m_pD3DTexture ) ) ) { return E_FAIL; } /*Lock Texture*/ D3DLOCKED_RECT kLockedRect; m_pD3DTexture->LockRect (0,&kLockedRect, NULL, 0); /*Cast The Pointer To Point To The First Pixel*/ DWORD * piTextureBuffer = (DWORD *) kLockedRect.pBits; /*Fill Texture*/ int iX, iY; for(iY = 0; iY<256; iY++) { for(iX=0; iX<256; iX++) { /*Form A Pattern*/ int iPower = (int)(sin (iY * iX) * 128 + cos (iY * -iX) * 128); piTextureBuffer [iX + iY* (kLockedRect.Pitch >> 2)] = D3DCOLOR_XRGB (iPower, iPower, iPower); } } /*Unlock*/ m_pD3DTexture->UnlockRect(0); /*Setup a temporary vertices information*/ CustomVertex kVertices []= { /*x , y, z, w, color, texture coordinates (u,v )*/ {0.0f, 0.0f, 0.5f, 1.0f, D3DCOLOR_ARGB (255, 255,255, 255),0, 0}, {50.0f,0.0f, 0.5f, 1.0f, D3DCOLOR_ARGB (255, 255, 255, 255),1, 0}, {50.0f,50.0f,0.5f, 1.0f, D3DCOLOR_ARGB (255, 255, 255, 255),1, 1}, {0.0f, 50.0f,0.5f, 1.0f, D3DCOLOR_ARGB (255, 255, 255, 255),0, 1}, }; /*Copy the vertices information to the vertex buffer*/ memcpy(m_kVertices, kVertices, sizeof (CustomVertex) *4); /*Dont Cull Polygons*/ m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); /*Dont Use Lighting*/ m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); /*Set Texture States*/ m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); return D3D_OK; } /*ShutDowns Direct3D*/ HRESULT D3DWindow::KillDirect3D(void) { /*If any of the Direct3D objects exist, release them*/ if(NULL !=m_pD3D) { m_pD3D->Release(); } if(NULL !=m_pD3DDevice) { m_pD3DDevice->Release(); } if(NULL !=m_pD3DTexture) { m_pD3DTexture->Release(); } return D3D_OK; } /*Draw The Entire Frame*/ mrBool32 D3DWindow::Frame(void) { /*Clear The Window To Blue*/ m_pD3DDevice->Clear (0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0); /*Start Rendering*/ m_pD3DDevice->BeginScene(); /*Set Texture Source*/ m_pD3DDevice->SetTexture(0, m_pD3DTexture); /*Set Vertex Source*/ m_pD3DDevice->SetVertexShader (D3DFVF_CUSTOMVERTEX); m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_kVertices, sizeof (CustomVertex)); /*End Rendering*/ m_pD3DDevice->EndScene(); /*Present the rendered scene to the screen*/ m_pD3DDevice->Present(NULL, NULL, NULL, NULL); return mrTrue; } /*'WinMain vs Main'*/ int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd) { /*Our Window*/ D3DWindow kWindow; /*Create Window*/ kWindow.Create (hInstance, "D3D Demo"); /*Setup Direct3D*/ kWindow.SetupDirect3D(); /*Enter Message Loop*/ kWindow.Run(); /*ShutDown Direct3D*/ kWindow.KillDirect3D(); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
1) from a quick glance over your code, it looks ok - I'll give it another look over later/tomorrow.


2) about the only thing I can think of at the moment: Is the D3DWindow::Run() method definitely calling D3DWindow::Frame()? - if the mrWindow base class also has a Frame() method, I'd put a breakpoint inside D3DWindow::Frame() just to check.


3) I would suggest it's a good habit be more 'explicit' when setting up the texture blending cascade for the first time rather than trusting current defaults - so instead of:

m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);

specify the whole of stage 0:

m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

NB: both alpha and colour should be keep active for the same number of stages.


4) if you use the [ source ] tag, your code will be a lot easier to read on the board.

Share this post


Link to post
Share on other sites
Quote:
Original post by maverick20
Hi....i m learning directx and having a problem with one of my code....
i wrote this code to draw a texture....but when i execute it, i see the directx Window, but no texture :(....can u help me out...
here is the code.......This is only the .CPP File...

/* '01 Main' */

/*Mirus Window FrameWork Header*/
#include "mrWindow.h"
/*Direct3D Header*/
#include <d3d8.h>
#include <math.h>


/*Our Custom Vertex Structure*/
class CustomVertex
{
public:
/*Transformed Position*/
FLOAT x, y, z, rhw;
/*Color*/
DWORD Color;
/*Texture Coordinates*/
FLOAT tU, tV;
};

/*Our Custom Vertex Type*/
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)

/*Custom Derived Class*/
class D3DWindow:public mrWindow
{
/*Direct3D Interfaces*/
LPDIRECT3D8 m_pD3D;
LPDIRECT3DDEVICE8 m_pD3DDevice;

/*Direct3D texture Interface*/
LPDIRECT3DTEXTURE8 m_pD3DTexture;
/*Direct3D Current Formatt*/
mrUInt32 m_iD3DFormat;

/*Vertices*/
CustomVertex m_kVertices [4];

public:
/*Constructor/Destructor*/
D3DWindow(void) {};
~D3DWindow(void) {};

/*Setup and Shutdown Direct3D*/
HRESULT SetupDirect3D(void);
HRESULT KillDirect3D(void);

/*Window Manipulation Functions*/
mrBool32 Frame(void);
};






/*Initializes Direct3D*/
HRESULT D3DWindow::SetupDirect3D(void)
{
/*Create the Direct3D Object*/
if(NULL == (m_pD3D = Direct3DCreate8 (D3D_SDK_VERSION)))
{
return E_FAIL;
}

/*Get the current Display Mode so we may know what bit depth
we are*/
D3DDISPLAYMODE d3ddm;
if(FAILED(m_pD3D->GetAdapterDisplayMode (D3DADAPTER_DEFAULT,
&d3ddm)))
{
return E_FAIL;
}

/*Fille in the present prameters*/
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp) );
/*We Want WindowedMode*/
d3dpp.Windowed =TRUE;
/*Discard This*/
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
/*Same Format as the current format
(We Got this from g_pD3D->GetAdapterDisplayMode) */
d3dpp.BackBufferFormat = d3ddm.Format;

/*Create The Device*/
if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
m_hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&m_pD3DDevice ) ) )
{
return E_FAIL;
}

//return D3D_OK;

/*Create The Texture*/
if (FAILED (m_pD3DDevice->CreateTexture (128, 128, 0 , 0,
(D3DFORMAT) m_iD3DFormat,
D3DPOOL_MANAGED,
&m_pD3DTexture ) ) )

{
return E_FAIL;
}

/*Lock Texture*/
D3DLOCKED_RECT kLockedRect;
m_pD3DTexture->LockRect (0,&kLockedRect, NULL, 0);

/*Cast The Pointer To Point To The First Pixel*/
DWORD * piTextureBuffer = (DWORD *) kLockedRect.pBits;

/*Fill Texture*/
int iX, iY;
for(iY = 0; iY<256; iY++)
{
for(iX=0; iX<256; iX++)
{
/*Form A Pattern*/
int iPower = (int)(sin (iY * iX) * 128 + cos (iY * -iX) * 128);
piTextureBuffer [iX + iY* (kLockedRect.Pitch >> 2)] =
D3DCOLOR_XRGB (iPower, iPower, iPower);
}
}

/*Unlock*/
m_pD3DTexture->UnlockRect(0);

/*Setup a temporary vertices information*/
CustomVertex kVertices []=
{ /*x , y, z, w, color, texture coordinates (u,v )*/
{0.0f, 0.0f, 0.5f, 1.0f, D3DCOLOR_ARGB (255, 255,255, 255),0, 0},
{50.0f,0.0f, 0.5f, 1.0f, D3DCOLOR_ARGB (255, 255, 255, 255),1, 0},
{50.0f,50.0f,0.5f, 1.0f, D3DCOLOR_ARGB (255, 255, 255, 255),1, 1},
{0.0f, 50.0f,0.5f, 1.0f, D3DCOLOR_ARGB (255, 255, 255, 255),0, 1},
};

/*Copy the vertices information to the vertex buffer*/
memcpy(m_kVertices, kVertices, sizeof (CustomVertex) *4);

/*Dont Cull Polygons*/
m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

/*Dont Use Lighting*/
m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

/*Set Texture States*/
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
return D3D_OK;
}
/*ShutDowns Direct3D*/
HRESULT D3DWindow::KillDirect3D(void)
{
/*If any of the Direct3D objects exist, release them*/
if(NULL !=m_pD3D)
{
m_pD3D->Release();
}
if(NULL !=m_pD3DDevice)
{
m_pD3DDevice->Release();
}
if(NULL !=m_pD3DTexture)
{
m_pD3DTexture->Release();
}

return D3D_OK;
}

/*Draw The Entire Frame*/
mrBool32 D3DWindow::Frame(void)
{
/*Clear The Window To Blue*/
m_pD3DDevice->Clear (0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,0,255), 1.0f, 0);

/*Start Rendering*/
m_pD3DDevice->BeginScene();

/*Set Texture Source*/
m_pD3DDevice->SetTexture(0, m_pD3DTexture);

/*Set Vertex Source*/
m_pD3DDevice->SetVertexShader (D3DFVF_CUSTOMVERTEX);
m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_kVertices,
sizeof (CustomVertex));

/*End Rendering*/
m_pD3DDevice->EndScene();

/*Present the rendered scene to the screen*/
m_pD3DDevice->Present(NULL, NULL, NULL, NULL);

return mrTrue;
}

/*'WinMain vs Main'*/
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,
LPSTR lpCmdLine, int nShowCmd)
{
/*Our Window*/
D3DWindow kWindow;

/*Create Window*/
kWindow.Create (hInstance, "D3D Demo");

/*Setup Direct3D*/
kWindow.SetupDirect3D();

/*Enter Message Loop*/
kWindow.Run();

/*ShutDown Direct3D*/
kWindow.KillDirect3D();

return 0;
}



Thanx or the help....here is the mrWindow.cpp file for better understandibilty..

/*Complement Header File*/

#include "mrWindow.h"

/*Default Constructor*/

mrWindow::mrWindow(void)
{
/*Do Nothing*/

}

/*Default Destructor*/

mrWindow::~mrWindow(void)
{
/*Do Nothing*/
}

/*Create The Window*/

mrError32 mrWindow::Create (HINSTANCE hInstance, LPSTR szTitle, mrInt iWidth, mrInt iHeight,
mrUInt32 istyle)
{
/*Visual Properties*/

m_kWndClass.hCursor =LoadCursor (NULL, IDC_ARROW);
m_kWndClass.hIcon =LoadIcon (NULL, IDI_APPLICATION);
m_kWndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

/*System Properties*/

m_kWndClass.hInstance =hInstance;
m_kWndClass.lpfnWndProc =WndProc;
m_kWndClass.lpszClassName ="Mirus Window";

/*Extra Properties*/

m_kWndClass.lpszMenuName = NULL;

m_kWndClass.cbClsExtra = NULL;
m_kWndClass.cbWndExtra = NULL;
m_kWndClass.style = NULL;


/*Try To Register Class*/

if(!RegisterClass (&m_kWndClass))
{
return mrErrorRegisterClass;

}

/*Create the window*/

m_hWindow = CreateWindow("Mirus Window", szTitle, istyle, CW_USEDEFAULT, CW_USEDEFAULT,
iWidth, iHeight, NULL, NULL, hInstance, (void *) this);

SetWindowText (m_hWindow, szTitle);

return mrNoError;

}


/* Normal Messsage Handler - direct messsages to our own */

LRESULT CALLBACK mrWindow::WndProc (HWND hWindow, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
mrWindow * pkWindow = NULL;
mrBool32 bProcessed = mrFalse;


switch(iMessage)
{
/* Window is Creating, set Custom Information */

case WM_NCCREATE:

/*Store custom data related to the window*/

SetWindowLong (hWindow, GWL_USERDATA, (long)((LPCREATESTRUCT (lParam))->lpCreateParams));
/*TypeCast lParam to an LPCREATESTRUCT and then
and then data into long*/
break;

/* Window message - Let our handler process it */

default:

pkWindow = (mrWindow *) GetWindowLong (hWindow, GWL_USERDATA);

if(NULL != pkWindow)
{
bProcessed = pkWindow->MessageHandler (iMessage, wParam, lParam);

}

break;
}

/* Message not processed - let windows handle it*/

if (mrFalse == bProcessed)

{
return DefWindowProc (hWindow, iMessage, wParam, lParam);

}

return 0;

}


/*Real time message loop*/

void mrWindow::Run(void)
{
while(1)
{
/*Query to see if there is any message in the queue*/

if (PeekMessage (&m_kMessage, m_hWindow, 0, 0, PM_REMOVE))

{
/*If it is the WM_QUIT Message, quit the loop*/

if (WM_QUIT == m_kMessage.message)
{
break;
}

/*Process the message normally*/

else
{
TranslateMessage (&m_kMessage);
DispatchMessage (&m_kMessage);

}

/*if (WM_CREATE == m_kMessage.message)
{
TranslateMessage (&m_kMessage);
DispatchMessage (&m_kMessage);

} */



}



/* No Message, Do Frame*/

else
{
Frame(); /*Code tht is executed each frame*/

}

/* if(mrFalse==Frame())
{
WM_QUIT;
}*/
}

}

/*Sets the window position */

void mrWindow::SetPosition(mrInt iX, mrInt iY)
{

SetWindowPos(m_hWindow, HWND_TOP, iX, iY, 0, 0, SWP_NOSIZE);
}


/*Returns a POINT structure containing x and y coordinates of the window*/

void mrWindow::GetPosition(POINT *pkPosition)
{
RECT rcWindow;
POINT pPosition;

/*Get Window Position*/
GetWindowRect(m_hWindow, &rcWindow);

pPosition.x = rcWindow.left;
pPosition.y = rcWindow.top;

memcpy(pkPosition, &pPosition, sizeof(POINT));

}


void mrWindow::SetSize(mrInt iWidth, mrInt iHeight)
{


SetWindowPos(m_hWindow, HWND_TOP, 0, 0, iWidth, iHeight, SWP_NOMOVE);

}


void mrWindow::GetSize(POINT *pkSize)
{
RECT rcWindow;
POINT pSize;

/* Get Window Position*/

GetWindowRect(m_hWindow, &rcWindow);

pSize.x = rcWindow.right - rcWindow.left;
pSize.y = rcWindow.bottom - rcWindow.top;

memcpy(pkSize, &pSize, sizeof(POINT));


}


void mrWindow::Show(mrInt iShow)
{
/*Change Window Visibility i.e; hidden or shown*/

ShowWindow(m_hWindow, iShow);

}



/*Our Message handler*/

mrBool32 mrWindow::MessageHandler (UINT iMessage, WPARAM wParam, LPARAM lParam)
{
switch(iMessage)
{
/* Close Window*/

case WM_CLOSE:
PostQuitMessage (0);
return mrTrue;

break;

/* Not Handled, let windows handle*/

default:

return mrFalse;

break;
}

}
[SOURCE]


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement