   Vertex Displacement - Can you find the bug??
  Hi,I'm trying to do vertex displacement using shaders. When I apply a sine/ cos function, it works and vertices get displaced. But when I sample from a displacement map and try to displace them, I end up with a blank screen.A really ugly version of my application :#include<windows.h> #include<d3d9.h> #include<d3dx9.h>  #define WIDTH    64#define HEIGHT    64#define FVF D3DFVF_XYZ|D3DFVF_TEX1  struct OURCUSTOMVERTEX {float x,y,z; D3DXVECTOR3 normal; float tu,tv;  }; LPDIRECT3DDEVICE9 p_Device = NULL;  int int_AppRunning = 1; LPDIRECT3DTEXTURE9  p_dx_Texture = NULL;   //POINTER TO BASE TEXTURE LPDIRECT3DVERTEXDECLARATION9  p_dx_VertexDeclaration = NULL;  //POINTER TO VERTEX DECLARATIONLPDIRECT3DVERTEXBUFFER9  p_dx_VB   = NULL;//POINTER TO VERTEX BUFFERLPDIRECT3DINDEXBUFFER9   p_dx_IB   = NULL;//POINTER TO INDEX BUFFER LPD3DXEFFECT   p_dx_Effect    = NULL;//POINTER TO D3DX EFFECTLPDIRECT3DTEXTURE9  p_dx_Texture2  = NULL;//DISPLACEMENT MAP TEXTURELPDIRECT3DTEXTURE9  p_dx_Texture3  = NULL;//POINTER TO TEXT TEXTURED3DVERTEXELEMENT9 decl[] ={   {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},   {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},   D3DDECL_END()};D3DXMATRIXA16 m_Extra,m_wvp;  D3DXMATRIXA16 m_View,m_Projection;void DrawScene(LPDIRECT3DDEVICE9 p_dx_Device, LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer, LPDIRECT3DINDEXBUFFER9 p_dx_IndexBuffer) {p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);p_dx_Device->BeginScene(); p_dx_Device->SetStreamSource( 0, p_dx_VertexBuffer, 0, sizeof(OURCUSTOMVERTEX) ); p_dx_Device->SetFVF(FVF);p_dx_Device->SetIndices(p_dx_IndexBuffer); D3DXMATRIX m_Translation;    D3DXMatrixTranslation(&m_Translation,36,-30,0);m_wvp = m_Translation * m_View * m_Projection;D3DXMatrixTranslation(&m_Translation,32,-32,0);p_dx_Device->SetTransform(D3DTS_WORLD, & m_Translation); p_dx_Effect->SetMatrix("worldViewProj",&m_wvp); p_dx_Effect->SetTechnique("MyTechnique"); p_dx_Effect->SetTexture("tex0",p_dx_Texture); p_dx_Effect->SetTexture("tex1",p_dx_Texture2); p_dx_Effect->SetTexture("tex2",p_dx_Texture3); p_dx_Device->SetVertexDeclaration(p_dx_VertexDeclaration); p_dx_Effect->Begin(0,NULL); p_dx_Effect->BeginPass(0); p_dx_Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,WIDTH*HEIGHT,0,(WIDTH-1)*(HEIGHT-1)*2); p_dx_Effect->EndPass(); p_dx_Effect->End(); p_dx_Device->EndScene();p_dx_Device->Present(NULL, NULL, NULL, NULL); } //FUNCTION TO INIT EFFECTbool InitFX(HWND han_Win,LPDIRECT3DDEVICE9 p_dx_Device){  LPD3DXBUFFER error;  if(FAILED(D3DXCreateEffectFromFile(p_dx_Device,"final.fx", NULL, NULL, 0, NULL, &p_dx_Effect,&error)))  {MessageBox(han_Win,(const char*)error->GetBufferPointer(),"FX COMPILE ERROR",MB_OK);return false;}  p_dx_Device->CreateVertexDeclaration(decl,&p_dx_VertexDeclaration);return true;} //WINDOWS CALLBACK PROCEDURE  LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM wParam,LPARAM lParam) {switch(uint_Message){    case WM_KEYDOWN:  if(wParam == VK_ESCAPE){int_AppRunning = 0;}    break;} return DefWindowProc(han_Wind,uint_Message,wParam,lParam); }  HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height) {WNDCLASSEX wnd_Structure; wnd_Structure.cbSize = sizeof(WNDCLASSEX);wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;wnd_Structure.lpfnWndProc = OurWindowProcedure;wnd_Structure.cbClsExtra = 0;wnd_Structure.cbWndExtra = 0;wnd_Structure.hInstance = GetModuleHandle(NULL);wnd_Structure.hIcon = NULL;wnd_Structure.hCursor = NULL;wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);wnd_Structure.lpszMenuName = NULL;wnd_Structure.lpszClassName = "WindowClassName";wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION); RegisterClassEx(&wnd_Structure); return CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClassName", str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE | WS_MAXIMIZE , int_XPos, int_YPos, int_Width, int_Height, NULL, NULL, GetModuleHandle(NULL), NULL); }  LPDIRECT3DDEVICE9 InitializeDevice(HWND han_WindowToBindTo) {LPDIRECT3D9 p_dx_Object;LPDIRECT3DDEVICE9 p_dx_Device; p_dx_Object = Direct3DCreate9(D3D_SDK_VERSION);if (p_dx_Object == NULL){    MessageBox(han_WindowToBindTo,"DirectX Runtime library not installed!","InitializeDevice()",MB_OK);} D3DPRESENT_PARAMETERS dx_PresParams;ZeroMemory( &dx_PresParams, sizeof(dx_PresParams) );dx_PresParams.Windowed = TRUE;dx_PresParams.SwapEffect = D3DSWAPEFFECT_DISCARD;dx_PresParams.BackBufferFormat = D3DFMT_UNKNOWN; dx_PresParams.BackBufferCount = 1; //dx_PresParams.EnableAutoDepthStencil= TRUE; //dx_PresParams.AutoDepthStencilFormat = D3DFMT_D16;  if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, han_WindowToBindTo, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device))){    if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, han_WindowToBindTo, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))    {   MessageBox(han_WindowToBindTo,"Failed to create even the reference device!","InitializeDevice()",MB_OK);    }  } p_dx_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);p_dx_Device->SetRenderState(D3DRS_LIGHTING,false);p_dx_Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID); p_dx_Device->SetRenderState(D3DRS_ZENABLE, TRUE); p_dx_Device->SetRenderState(D3DRS_DITHERENABLE, TRUE);   D3DXCreateTextureFromFile(p_dx_Device,"cwater.bmp",&p_dx_Texture); D3DXCreateTextureFromFile(p_dx_Device,"testimage.bmp",&p_dx_Texture2); D3DXCreateTextureFromFile(p_dx_Device,"red.bmp",&p_dx_Texture3); return p_dx_Device; }  LPDIRECT3DVERTEXBUFFER9 FillVertices(HWND han_Window, LPDIRECT3DDEVICE9 p_dx_Device) {LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;OURCUSTOMVERTEX cv_Vertices[WIDTH*HEIGHT];   for (int x=0;x< WIDTH;x++)   {  for (int y=0; y< HEIGHT;y++)  { cv_Vertices[y*WIDTH + x].x = -x; cv_Vertices[y*WIDTH + x].y = y;cv_Vertices[y*WIDTH + x].z = 0;   cv_Vertices[y*WIDTH + x].tu= (float) (1-x)/WIDTH;cv_Vertices[y*WIDTH + x].tv= (float) (1-y)/HEIGHT;cv_Vertices[y*WIDTH + x].normal = D3DXVECTOR3(0,0,-1.0F);  }   }   if (FAILED(p_dx_Device->CreateVertexBuffer(WIDTH*HEIGHT*sizeof(OURCUSTOMVERTEX), 0, FVF, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL))){   MessageBox(han_Window,"Error while creating VertexBuffer","FillVertices()",MB_OK);} VOID* p_Vertices;if (FAILED(p_dx_VertexBuffer->Lock(0, WIDTH*HEIGHT*sizeof(OURCUSTOMVERTEX), (void**)&p_Vertices, 0))){    MessageBox(han_Window,"Error trying to lock","FillVertices()",MB_OK);}else{    memcpy(p_Vertices, cv_Vertices, WIDTH*HEIGHT*sizeof(OURCUSTOMVERTEX));    p_dx_VertexBuffer->Unlock();} return p_dx_VertexBuffer; }  LPDIRECT3DINDEXBUFFER9 FillIndices(HWND han_Window, LPDIRECT3DDEVICE9 p_dx_Device) {LPDIRECT3DINDEXBUFFER9 p_dx_IndexBuffer;short s_Indices[(WIDTH-1)*(HEIGHT-1)*6];for (int x=0;x< WIDTH-1;x++)    {   for (int y=0; y< HEIGHT-1;y++)   {  s_Indices[(x+y*(WIDTH-1))*6+2] = x+y*WIDTH;  s_Indices[(x+y*(WIDTH-1))*6+1] = (x+1)+y*WIDTH;  s_Indices[(x+y*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH;  s_Indices[(x+y*(WIDTH-1))*6+3] = (x+1)+(y+1)*WIDTH;  s_Indices[(x+y*(WIDTH-1))*6+4] = x+y*WIDTH;  s_Indices[(x+y*(WIDTH-1))*6+5] = x+(y+1)*WIDTH;   }    }    if (FAILED(p_dx_Device->CreateIndexBuffer((WIDTH-1)*(HEIGHT-1)*6*sizeof(short),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&p_dx_IndexBuffer,NULL)))    {   MessageBox(han_Window,"Error while creating IndexBuffer","FillIndices()",MB_OK);    }    VOID* p_Indices;    if (FAILED(p_dx_IndexBuffer->Lock(0, (WIDTH-1)*(HEIGHT-1)*6*sizeof(short), (void**)&p_Indices, 0)))    {   MessageBox(han_Window,"Error trying to lock","FillIndices()",MB_OK);    }else{   memcpy(p_Indices, s_Indices, (WIDTH-1)*(HEIGHT-1)*6*sizeof(short));   p_dx_IndexBuffer->Unlock();    }    return p_dx_IndexBuffer;}void SetUpCamera(LPDIRECT3DDEVICE9 p_dx_Device){D3DXVECTOR3 m_EyePos(0, 0, -100); D3DXVECTOR3 m_TargetPos(0, 0, 0);    D3DXVECTOR3 m_UpVector(0, 1, 0);   D3DXMatrixLookAtLH(&m_View, &m_EyePos, &m_TargetPos, &m_UpVector);p_dx_Device->SetTransform(D3DTS_VIEW, &m_View);// m_Extra = m_View; //D3DXMatrixIdentity(&m_Extra);D3DXMatrixPerspectiveFovLH(&m_Projection, D3DX_PI/4, 640/480, 0.1f, 1000);p_dx_Device->SetTransform(D3DTS_PROJECTION, &m_Projection); }  int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow) {MSG msg_Message;HWND han_Window = NewWindow("Display Window",100,100,640,480);p_Device = InitializeDevice(han_Window); p_dx_VB = FillVertices(han_Window, p_Device); if(p_dx_VB == NULL) { MessageBox(han_Window,"Error creating VB","VB CREATE ERROR",MB_OK); return 0; }p_dx_IB = FillIndices(han_Window, p_Device); if(p_dx_IB == NULL) { MessageBox(han_Window,"Error creating index buffer","IB CREATE ERROR",MB_OK); return 0; }SetUpCamera(p_Device);  if(!InitFX(han_Window,p_Device)) return 0; while(int_AppRunning){    if(PeekMessage(&msg_Message,han_Window,0,0,PM_REMOVE))    {   DispatchMessage(&msg_Message);    }DrawScene(p_Device, p_dx_VB, p_dx_IB); } p_dx_VB->Release();p_dx_IB->Release();p_Device->Release();DestroyWindow(han_Window); return 0; }My Effects file :float4x4 worldViewProj  : WorldViewProjection;texture tex0;texture tex1;texture tex2;struct VS_OUTPUT{  float4 Position : POSITION;  float2 Tex : TEXCOORD0;};sampler  MySampler{Texture = (tex0);magfilter = POINT;minfilter = POINT;mipfilter = POINT;};sampler SecondSampler{Texture = (tex2);magfilter = POINT;minfilter = POINT;mipfilter = POINT;};sampler DisplaceSampler{Texture = (tex1);magfilter = POINT;minfilter = POINT;mipfilter = POINT;};VS_OUTPUT VS (float3 Pos : POSITION,float3 norm : NORMAL,float2 Tex : TEXCOORD0){  VS_OUTPUT Output = (VS_OUTPUT)0;   //If I uncomment next 3 lines, I end up with a blank screen   //float4 displaceVal = tex2Dlod(DisplaceSampler,float4(Tex.x,Tex.y,0,0));   //float df = displaceVal.r + displaceVal.g + displaceVal.b;// Pos.z = frac(Pos.x + df);  //If I uncomment next line, I get displacement  //Pos.z = Pos.z + sin (Pos.x);  Output.Position = mul( float4(Pos,1),worldViewProj);  Output.Tex   =    Tex;  return Output;}void PS(in float2 vTex : TEXCOORD0,out float4 oColor : COLOR0){float4 Color1,Color2;Color1=tex2D(MySampler,vTex);Color2= tex2D(SecondSampler,vTex);float factor = Color2.r + Color2.g + Color2.b;if (factor <= 0.0f) {   oColor = Color2; } else   {    oColor = Color1;   }  }technique MyTechnique{  pass Pass0  {VertexShader = compile vs_3_0 VS();PixelShader = compile ps_3_0 PS();  }}What is the problem?   
  "What is the problem?" - that you don't appear to have done any debugging or analysis and would rather just dump your code and let us do the hard work for you? [smile]HELP US TO HELP YOU - provide us with debugging information, intermediary variables, state information. What patterns have you observed? What different test data have you used? Have you got an independent 3rd party reference that you can compare against? Have you use the reference rasterizer, the debug runtimes or PIX for Windows - if so, what did they say? if not, why haven't you?!At a guess, you're using D3DXCreateTextureFromFile() to load your textures, which most likely aren't in a suitable format for VTF on the hardware you're using. You don't appear to perform any checks to determine this.A blank screen in Direct3D is often a big alarm bell telling you that functions are failing. Your first step is to fire up the debug runtimes and see what they say, but ideally you should also be trapping HRESULT's so you can also identify the source of the debug messages.Cheers,Jack   ;  Also, thought this thread looked familiar... any reason why your previous thread couldn't be continued?!Cheers,Jack   ;  Hi,Sorry for the late reply. I was away.Ok here goes..Quote:Original post by jollyjeffersHELP US TO HELP YOU - provide us with debugging information, intermediary variables, state information. What patterns have you observed? What different test data have you used? Have you got an independent 3rd party reference that you can compare against? Have you use the reference rasterizer, the debug runtimes or PIX for Windows - if so, what did they say? if not, why haven't you?! I get the blank screen whenever I use tex2Dlod. It's fine when I use some in built transforma (like sine). What 3rd party reference do you mean? I'll post my PIX output.Quote: At a guess, you're using D3DXCreateTextureFromFile() to load your textures, which most likely aren't in a suitable format for VTF on the hardware you're using. You don't appear to perform any checks to determine this.D3DXCreateTextureFromFile is NOT the problem because I used the same texture file in another vertex displacement mapping program. I perform the checks in my actual application.Quote:A blank screen in Direct3D is often a big alarm bell telling you that functions are failing. Your first step is to fire up the debug runtimes and see what they say, but ideally you should also be trapping HRESULT's so you can also identify the source of the debug messages.The problem seems to be with the shaders. I could not trap any HRESULT which could help me identify the problem.Thanks,Saranya;  So you've got a working case:  //If I uncomment next line, I get displacement  //Pos.z = Pos.z + sin (Pos.x);And a broken case:  //If I uncomment next 3 lines, I end up with a blank screen  //float4 displaceVal = tex2Dlod(DisplaceSampler,float4(Tex.x,Tex.y,0,0));//float df = displaceVal.r + displaceVal.g + displaceVal.b;  // Pos.z = frac(Pos.x + df);Try modifying the working case to be more and more like the broken case, until it breaks:  float4 displaceVal = tex2Dlod(DisplaceSampler,float4(Tex.x,Tex.y,0,0));    float df = displaceVal.r + displaceVal.g + displaceVal.b;  Pos.z = Pos.z + (df*0);...  float4 displaceVal = tex2Dlod(DisplaceSampler,float4(Tex.x,Tex.y,0,0));    float df = displaceVal.r + displaceVal.g + displaceVal.b;  Pos.z = Pos.z + sin(df);...  float4 displaceVal = tex2Dlod(DisplaceSampler,float4(Tex.x,Tex.y,0,0));    float df = displaceVal.r + displaceVal.g + displaceVal.b;  Pos.z = Pos.z + df;...  float4 displaceVal = tex2Dlod(DisplaceSampler,float4(Tex.x,Tex.y,0,0));    float df = displaceVal.r + displaceVal.g + displaceVal.b;  Pos.z = frac(Pos.z + df);[EDIT]Could your problem be that you're adding the displacement to the X value (instead of the Z value), and storing the result in Z?Pos.z = frac(Pos.x + df);   ;Quote:Original post by HodgmanCould your problem be that you're adding the displacement to the X value (instead of the Z value), and storing the result in Z?Pos.z = frac(Pos.x + df);Even this doesn't work :Pos.z = df;Thanks for all the replies guys! Please let me know if you find the problem.;Quote:Original post by SaranyaEven this doesn't work :Pos.z = df;What about: Pos.z = Pos.z + (df*0.01);;  Nope. That doesn't work either. I got this from PIX :Frame 000001 ............PRE: D3DXCreateEffectFromFileExA(0x02C49158, 0x004188BC, NULL, NULL, NULL, 0x00000000, NULL, 0x0012FC64, 0x0012FDD8)Frame 000001 ............POST: <D3DXERR_INVALIDDATA> D3DXCreateEffectFromFileExA(0x02C49158, 0x004188BC, NULL, NULL, NULL, 0x00000000, NULL, 0x0012FC64, 0x0012FDD8)Frame 000001 ........POST: <D3DXERR_INVALIDDATA> D3DXCreateEffectFromFileA(0x02C49158, 0x004188BC, NULL, NULL, 0x00000000, NULL, 0x0041A1A4, 0x0012FDD8)An unhandled exception occurred.<D3DXERR_INVALIDDATA> ???;  You need to do more error checking, I think. Your program should be able to catch that error without having to use PIX. D3DXERR_INVALIDDATA probably means that it can't find your .fx file.   ;  I've been trying to find out what the error is. If you have the time, please do run the code and see what's happening. Like I said, I've just started with directx and might have missed something.Do run the code and tell me if you find out what's wrong.Thanks in advance,Saranya   