Sign in to follow this  
Swatter555

2D Map Question- Lighting

Recommended Posts

I am creating a 2D hexagonal map with textured quads. Alot of the advice I see and questions being answered are not aimed directly at what Im doing, so hopefully I can get some advice tailored for my situation. I have the position,color, and texture issues down. I also have materials and lights set up, everything works fine as far as those basics go. The problems that I run into have to do with normals... vertex normals, face normals, normals this, normals that. Ok, let me define what Im trying to do more clearly. I am looking for a map with changing elevations, with the changing elevations casting shadows. Im assuming that changing a vertex's z position will cause a change in elevation on a 2D map, but the specifics of how its done a fuzzy to me. I guess this is where the normals come in. Could someone please break down for me what needs to be done? Any advice would be appreciated. Ill include some code Ive been playing with to give you an idea of where I am. Thanks


#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1)

struct VERTEX
{
    FLOAT x, y, z;
    FLOAT nx,ny,nz;
    D3DCOLOR color;
    FLOAT u, v;
};


LPDIRECT3DVERTEXBUFFER9 VB = NULL;
IDirect3DTexture9* current_texture = 0;

//Colors
D3DCOLOR Trans  = D3DCOLOR_ARGB(0,255,255,255);
D3DCOLOR Black  = D3DCOLOR_ARGB(255,0,0,0);
D3DCOLOR White  = D3DCOLOR_ARGB(255,255,255,255);
D3DCOLOR Desert = D3DCOLOR_ARGB(255,200,200,0);
D3DCOLOR Desert2 = D3DCOLOR_ARGB(255,220,220,0);
D3DCOLOR Desert3 = D3DCOLOR_ARGB(255,220,220,220);




//Prototypes
int ExampleCleanUp();
void RenderMap();
void Setup2DMap();
void SetupCamera();


//Implementation

//Example clean up
int ExampleCleanUp()
{
	//Clean up vertex buffer
	VB->Release();
	VB = NULL;

	//Release textures
	current_texture->Release();
	current_texture = NULL;

	return(1);
}


//SetupCamera

void SetupCamera()
{
	/*
	//Setup 3D camera
	D3DXMATRIX  matProj;
	D3DXMATRIX  identity;
	D3DXVECTOR3 vecCameraSource(5.0f,3.0f,-10.0f);
	D3DXVECTOR3 vecCameraTarget(0.0f,0.0f,0.0f);
	D3DXVECTOR3 vecUp(0.0f,1.0f,0.0f);

	D3DXMatrixIdentity(&identity);

	D3DXMatrixLookAtLH(&identity,
		           &vecCameraSource,
			   &vecCameraTarget,
			   &vecUp);

	D3DXMatrixPerspectiveFovLH(&matProj,
		                   D3DX_PI/.5f,
				   1024/768,
				   1.0f,
				   100.0f);

	D3DXMatrixOrthoLH(&matProj,1024,768, 0.0f, 1.0f);

	Device->SetTransform(D3DTS_PROJECTION, &matProj);
        Device->SetTransform(D3DTS_WORLD,&identity);
	Device->SetTransform(D3DTS_VIEW, &identity);
	*/


    //Setup 2D camera
	D3DXMATRIX Ortho2D;	
	D3DXMATRIX Identity;
	
	D3DXMatrixOrthoLH(&Ortho2D, 1024, 768, 0.0f, 4.0f);
	D3DXMatrixIdentity(&Identity);

	Device->SetTransform(D3DTS_PROJECTION, &Ortho2D);
	Device->SetTransform(D3DTS_WORLD, &Identity);
	Device->SetTransform(D3DTS_VIEW, &Identity);
  
    //Load needed textures
    IDirect3DTexture9* desert_tile = LoadTexture (Device,"graphics/desert_tile.bmp");
	current_texture = desert_tile;

	Device->SetRenderState(D3DRS_LIGHTING,true);
	Device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);

	//Set filter
	Device->SetSamplerState (0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
	Device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);

	//Set mipmap filter
	Device->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);

	//Set material
	OW_LIGHTING Light;
	Device->SetMaterial(&Light.desert_mtrl);

	//Set light
	Device->SetLight(0,&Light.directional);
	Device->LightEnable(0,true);
	
	//Set the viewport
	D3DVIEWPORT9 view_port;

	view_port.Width = 1024;
	view_port.Height = 768;
	view_port.X = 0;
	view_port.Y = 0;
	view_port.MinZ = 0;
	view_port.MaxZ = 100;

	Device->SetViewport(&view_port);


}



//Setup 2D map
void Setup2DMap()
{

	///////////////////////
	//Setup vertex buffer//
	///////////////////////

	Device->CreateVertexBuffer(1350 * sizeof(VERTEX),
		                   D3DUSAGE_WRITEONLY,
                                   D3DFVF_VERTEX,
				   D3DPOOL_MANAGED,
	        		   &VB,0);

	
	VERTEX* Vertices = NULL;

	//Lock the vertex buffer
	VB->Lock(0, 1350 * sizeof(VERTEX),(void**)&Vertices, 0);

    int offset_x = 0;
	int offset_y = 0;
	int row_offset = 0;

	for(int y = 0; y < 1350; y+= 90)
	{
		for(int x = 0; x < 90; x+= 6)
		{
			//Set vertice positions, x and y
			//Vertice 0
			Vertices[y + x + 0].x =  32 + offset_x - 512;
			Vertices[y + x + 0].y = -64 + offset_y + 384;
			Vertices[y + x + 0].z = 0;

			//Vertice 1
			Vertices[y + x + 1].x =   0 + offset_x - 512;
			Vertices[y + x + 1].y = -48 + offset_y + 384;
			Vertices[y + x + 1].z = 0;

			//Vertice 2
			Vertices[y + x + 2].x =   0 + offset_x - 512;
			Vertices[y + x + 2].y = -16 + offset_y + 384;
			Vertices[y + x + 2].z = 0;

			//Vertice 3
			Vertices[y + x + 3].x =  32 + offset_x - 512;
			Vertices[y + x + 3].y =   0 + offset_y + 384;
			Vertices[y + x + 3].z = 0;

			//Vertice 4
			Vertices[y + x + 4].x =  64 + offset_x - 512;
			Vertices[y + x + 4].y = -16 + offset_y + 384;
			Vertices[y + x + 4].z = 0;

			//Vertice 5
			Vertices[y + x + 5].x =  64 + offset_x - 512;
			Vertices[y + x + 5].y = -48 + offset_y + 384;
			Vertices[y + x + 5].z = 0;

			//Set vertex normals
			//Vertice 0
			Vertices[y + x + 0].nx = 0;
			Vertices[y + x + 0].ny = .707f;
			Vertices[y + x + 0].nz = -.707f;

			//Vertice 1
			Vertices[y + x + 1].nx = 0;
			Vertices[y + x + 1].ny = .707f;
			Vertices[y + x + 1].nz = -.707f;

			//Vertice 2
			Vertices[y + x + 2].nx = 0;
			Vertices[y + x + 2].ny = .707f;
			Vertices[y + x + 2].nz = -.707f;

			//Vertice 3
			Vertices[y + x + 3].nx = 0;
			Vertices[y + x + 3].ny = .707f;
			Vertices[y + x + 3].nz = -.707f;

			//Vertice 4
			Vertices[y + x + 4].nx = 0;
			Vertices[y + x + 4].ny = .707f;
			Vertices[y + x + 4].nz = -.707f;

			//Vertice 5
			Vertices[y + x + 5].nx = 0;
			Vertices[y + x + 5].ny = .707f;
			Vertices[y + x + 5].nz = -.707f;

			//Set color
			Vertices[y + x + 0].color = Trans;
			Vertices[y + x + 1].color = Trans;
			Vertices[y + x + 2].color = Trans;
			Vertices[y + x + 3].color = Trans;
			Vertices[y + x + 4].color = Trans;
			Vertices[y + x + 5].color = Trans;

			//Texture position
			//Vertice 0
			Vertices[y + x + 0].u = 0.50f;
			Vertices[y + x + 0].v = 1.00f;

            //Vertice 1
			Vertices[y + x + 1].u = 0.00f;
			Vertices[y + x + 1].v = 0.75f;


			//Vertice 2
			Vertices[y + x + 2].u = 0.00f;
		    Vertices[y + x + 2].v = 0.25f;

			//Vertice 3
			Vertices[y + x + 3].u = 0.50f;
			Vertices[y + x + 3].v = 0.00f;

			//Vertice 4
			Vertices[y + x + 4].u = 1.00f;
			Vertices[y + x + 4].v = 0.25f;

			//Vertice 5
			Vertices[y + x + 5].u = 1.00f;
			Vertices[y + x + 5].v = 0.75f;

			
			//Update loop variables
			offset_x += 64;
			
		}//End x for

        //Update loop variables
        if(row_offset == 0)
		{
		   offset_x  = 32;
		   offset_y -= 48;

           //Enter other condition next time
		   row_offset = 1;

		}//end if
		else
		{
		   offset_x  =  0;
		   offset_y -= 48;
           
		   //Enter other condition next time
		   row_offset = 0;

		}//end else

	}//End y for


	VB->Unlock();
}



//Second fan example run
void RenderMap()
{
	//Set current texture
	Device->SetTexture(0,current_texture);
	Device->SetFVF(D3DFVF_VERTEX);
	Device->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);

		for(int x = 0; x < 1350; x += 6)
		{
			Device->SetStreamSource(0,VB,x * sizeof(VERTEX),sizeof(VERTEX));

            Device->DrawPrimitive(D3DPT_TRIANGLEFAN,0,4);

		}//end x for
}




As you can see, Im not even sure of how to use the camera in a situation like this. Thanks in advance :)

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