Sign in to follow this  
xketu

Shaders and Textures...!

Recommended Posts

I am a Beginer to the world of DirectX....!! Well this post is just for the Bigger Guns of DirectX as compared to me...!! I have certain Query related to "Shaders". I am trying to load a texture through shaders and not getting success in the same. The thing is I am not even getting any error message so that I can google it out. I am posting my code [That is inherited from various sources one of them being the sample codes of Frank Luna's Book] Kindly Help...!! // The Shader [HLSL Code]
uniform extern float4x4 gWVP;
uniform extern float gHEIGHT;
uniform extern Texture gTex;

sampler TextureSampler = sampler_state
						{
							texture = <gTex>;
							minfilter = LINEAR;
							magfilter = LINEAR;
                            mipfilter = LINEAR;
                            AddressU = mirror; 
                            AddressV = mirror;
						};


struct InputVS 
	{
		float3 pos: POSITION0;
		//float4 col: COLOR0;
		float2 tex: TEXCOORD0;
	};

struct OutputVS
	{
		float4 pos: POSITION0;
		//float4 col: COLOR0;
		float2 tex: TEXCOORD0;
	};

OutputVS mainVS(InputVS i)
	{
		OutputVS output = (OutputVS) 0;
		
		float temp = gHEIGHT * sin (i.pos.z/2.0)+ gHEIGHT * sin(i.pos.x/2.0);
		
		//output.pos = mul(mul(float4(i.pos,1.0),gWVP), (gHEIGHT * sin (i.pos.z/2.0)+ gHEIGHT * sin(i.pos.x/2.0)));
		//output.pos = mul(float4((gHEIGHT * sin (i.pos.z/2.0)+ gHEIGHT * sin(i.pos.x/2.0)),1.0),gWVP);
		
		output.pos = mul(float4(i.pos,1.0),gWVP);

		//output.col = i.col ;
		output.tex = i.tex;
		return output;
	}

float4 mainPS (OutputVS output) : COLOR
	{
		//return output.col;
		//return float4(0,1,0,1);
		//return output.tex;
		return tex2D(TextureSampler, output.tex);
	}


technique effectfirst
{
	pass p0
	{
		// Specified for the compilation if the shaders
		vertexShader = compile vs_3_0 mainVS();
		pixelShader = compile ps_3_0 mainPS();

		// Redering Shit..!!
		FillMode = Wireframe;// Solid
	}

}

// DirectX Code
#include "d3dUtility.h"
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_TEX1)

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z;  // The untransformed position for the vertex
    //DWORD color;    // The vertex color
	FLOAT u, v;		//Texture Coordinate
};


//
// Globals
//
D3DXMATRIX projection;

IDirect3DDevice9* Device = 0; 
IDirect3DVertexBuffer9*  pVB;

//Handlers of the Effect File
D3DXHANDLE hWVP;
D3DXHANDLE hHEIGHT;
D3DXHANDLE hTECH;

ID3DXEffect* fx = 0;
ID3DXBuffer* error = 0;

D3DXVECTOR3 cameraPos(0.0f, 5.0f, 30.0f);
D3DXMATRIX View;
D3DXVECTOR3 cameraLookAt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 upVector(0.0f, 1.0f, 0.0f);
IDirect3DTexture9* Tex;

float height = 1.0f;
//
// Framework Functions
//

bool Setup()
{
// set up projection

D3DXMatrixPerspectiveFovLH(&projection,D3DX_PI*0.5f,1.33f,1.0f,1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &projection);
// set up camera - i.e. view transform
D3DXMatrixLookAtLH(&View, &cameraPos, &cameraLookAt, &upVector);
Device->SetTransform(D3DTS_VIEW, &View);
// we're not using materials or lights, so turn off lighting calculations
Device->SetRenderState(D3DRS_LIGHTING, false);

CUSTOMVERTEX vertices[] =
	{
		//{  -50.0f,  0.0f,  -50.0f, 0xffff0000, }, // x, y, z, rhw, color
		//{  -50.0f,  0.0f,  50.0f,  0xff00ff00, },
		//{   50.0f,  0.0f, 50.0f, 0xff00ffff, },

		//{  -50.0f,  0.0f,  -50.0f, 0xffff0000, }, // x, y, z, rhw, color
		//{  50.0f,  0.0f,  50.0f,  0xff00ff00, },
		//{  50.0f,  0.0f, -50.0f, 0xff00ffff, }

		{  -50.0f,  0.0f,  -50.0f, 0.0f, 1.0f, }, // x, y, z, rhw, color
		{  -50.0f,  0.0f,  50.0f, 0.0f,0.0f,},
		{   50.0f,  0.0f, 50.0f, 1.0f, 0.0f},

		{  -50.0f,  0.0f,  -50.0f, 0.0f,1.0f,}, // x, y, z, rhw, color
		{  50.0f,  0.0f,  50.0f, 1.0f,0.0f,},
		{  50.0f,  0.0f, -50.0f, 1.0f, 1.0f,}
	};

Device->CreateVertexBuffer( 6*sizeof(CUSTOMVERTEX),
                                                  0, 
												  D3DFVF_CUSTOMVERTEX,
												  D3DPOOL_MANAGED,
												  &pVB, NULL );
 
VOID* pVertices;
    pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 );
    memcpy( pVertices, vertices, sizeof(vertices) );
	D3DXCreateTextureFromFile(Device,"sand.jpg",&Tex);
	if (!Tex) MessageBox(NULL, "Can't find image", "Texture read error", MB_OK);
    
	// Compilation of the Effect

	D3DXCreateEffectFromFile(Device,"effect.txt", 0,0, D3DXSHADER_DEBUG, 0, &fx, &error);
	if(error)
	{
		MessageBox(0,(char*) error->GetBufferPointer(),0,0);
		error->Release();
	}

	hHEIGHT = fx->GetParameterByName(0,"gHEIGHT");
	hWVP = fx->GetParameterByName(0,"gWVP");
	hTECH = fx->GetTechniqueByName("effectfirst");
	

	return true;
}
void moveCamera()
{
	bool changed = false;

	if (GetAsyncKeyState(VK_UP))
	{
		changed = true;
		cameraPos.z -= 0.01f;
	}
	if (GetAsyncKeyState(VK_DOWN))
	{
		changed = true;
		cameraPos.z += 0.01f;
	}
	if (GetAsyncKeyState(VK_LEFT))
	{
		changed = true;
		cameraPos.x -= 0.01f;
	}
	if (GetAsyncKeyState(VK_RIGHT))
	{
		changed = true;
		cameraPos.x += 0.01f;
	}

	if (changed)
	{
		D3DXMatrixLookAtLH(&View, &cameraPos, &cameraLookAt, &upVector);
		Device->SetTransform(D3DTS_VIEW, &View);
	}

}
void Cleanup()
{
	Device->Release();
}

void ClearBuffer(DWORD dwColor)
{
	Device->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, dwColor, 1.0f, 0 );
}

bool Display(float timeDelta)
{
	moveCamera();
	
	if( Device )
	{
		ClearBuffer(D3DCOLOR_XRGB(0,0,0));
		// Begin the scene
		Device->BeginScene();

		fx->SetTechnique(hTECH);
		fx->SetFloat(hHEIGHT, height);
		D3DXMATRIX m = View * projection;
		fx->SetMatrix(hWVP, &m);

		UINT passes = 0;

		fx->Begin(&passes, 0);
		for(UINT i = 0; i<passes; i++)
		{
			fx->BeginPass(i);
			Device->SetStreamSource( 0, pVB, 0, sizeof(CUSTOMVERTEX) );
			Device->SetFVF( D3DFVF_CUSTOMVERTEX );
			Device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
			fx->EndPass();
		}
		fx->End();
		Device->EndScene();
		Device->Present(0,0,0,0); 
	}
	return true;
}

//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
	case WM_DESTROY:
		::PostQuitMessage(0);
		break;
		
	case WM_KEYDOWN:
		if( wParam == VK_ESCAPE )
			::DestroyWindow(hwnd);
		break;
	}
	return ::DefWindowProc(hwnd, msg, wParam, lParam);
}

//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
				   HINSTANCE prevInstance, 
				   PSTR cmdLine,
				   int showCmd)
{/*
	bool NotfullScreen;

	NotfullScreen = !(MessageBox(NULL, "Run in fullscreen Mode?", "Display Mode", MB_YESNO)==IDYES);*/

	if(!d3d::InitD3D(hinstance,
		800, 600, true, D3DDEVTYPE_HAL, &Device))
	{
		::MessageBox(0, "InitD3D() - FAILED", 0, 0);
		return 0;
	}
		
	if(!Setup())
	{
		::MessageBox(0, "Setup() - FAILED", 0, 0);
		return 0;
	}

	d3d::EnterMsgLoop( Display );

	Cleanup();

	Device->Release();

	return 0;
}

[Edited by - Yann L on March 13, 2009 3:45:20 PM]

Share this post


Link to post
Share on other sites
It would be easier to read with identation if you put the code (or edited?) in [_source_][_/source_] tags (without _).

So this program compiles and runs wihtout any error, you just dont see anything?

Share this post


Link to post
Share on other sites
Need a bit more information, does the geometry draw and no texture show up, does anything at all show up please explain the problem a bit more.

[EDIT]
Try using fx->SetVertexDecl vs using SetFvF, see if that helps
http://www.gamedev.net/community/forums/topic.asp?topic_id=385054

*note*
As a tip, the first thing I usually try when things don't draw is to set my PS to return 1. This returns bright white, If the geometry is on the screen, then it will show up, if not, its something to do with another section of your pipeline.

[Edited by - 5ocks on March 13, 2009 6:31:35 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this