//////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: mtrlAlpha.cpp
//
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Renders a semi transparent cube using alpha blending alpha blending.
// In this sample, the alpha is taken from the textures alpha channel.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#include "d3dUtility.h"
#include "vertex.h"
#include "cube.h"
#include "camera.h"
//
// Globals
//
IDirect3DDevice9* Device = 0;
const int Width = 800;
const int Height = 600;
IDirect3DTexture9* CrateTex = 0;
Cube *Box[3][3][3];
D3DXMATRIX CubeWorldMatrix;
IDirect3DVertexBuffer9* BackDropVB = 0;
IDirect3DTexture9* BackDropTex = 0;
Camera TheCamera(Camera::LANDOBJECT);
IDirect3DTexture9* Textures[7] = {0, 0, 0, 0, 0, 0, 0};// texture for each subset
//
// Framework Functions
//
bool Setup()
{
Device->CreateVertexBuffer(
6 * sizeof(Vertex),
D3DUSAGE_WRITEONLY,
FVF_VERTEX,
D3DPOOL_MANAGED,
&BackDropVB,
0);
Vertex* v;
BackDropVB->Lock(0, 0, (void**)&v, 0);
// quad built from two triangles, note texture coordinates:
v[0] = Vertex(-10.0f, -17.5f, -5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[1] = Vertex(-10.0f, -2.5f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[2] = Vertex( 10.0f, -2.5f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[3] = Vertex(-10.0f, -17.5f, -5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[4] = Vertex( 10.0f, -2.5f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[5] = Vertex( 10.0f, -17.5f, -5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
BackDropVB->Unlock();
D3DXCreateTextureFromFile(
Device,
"bg.jpg",
&BackDropTex);
D3DXCreateTextureFromFile(
Device,
"a.jpg",
&Textures[0]);
D3DXCreateTextureFromFile(
Device,
"q.jpg",
&Textures[1]);
D3DXCreateTextureFromFile(
Device,
"z.jpg",
&Textures[2]);
D3DXCreateTextureFromFile(
Device,
"w.jpg",
&Textures[3]);
D3DXCreateTextureFromFile(
Device,
"s.jpg",
&Textures[4]);
//D3DXCreateTextureFromFile(
// Device,
// "x.jpg",
// &Textures[5]);
D3DXCreateTextureFromFileEx(Device, "x.jpg", D3DX_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, 0, D3DFMT_R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_POINT,
D3DCOLOR_RGBA(0,0,0,255), NULL, NULL, &Textures[5]);
D3DXCreateTextureFromFile(
Device,
"black.jpg",
&Textures[6]);
Device->SetRenderState( D3DRS_MULTISAMPLEANTIALIAS, true);
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
Device->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 4);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
//// use alpha channel in texture for alpha
//Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
//Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
// set blending factors so that alpha component determines transparency
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
int tex[6]={0,1,2,3,4,5};
D3DXVECTOR3 *position[3][3][3];
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
for(int z=0;z<3;z++)
position[x][y][z] = 0;
}
}
float kk[3]={-2.0,0.0,2.0};
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
for(int z=0;z<3;z++)
position[x][y][z] = new D3DXVECTOR3(kk[x],kk[y],kk[z]);
}
}
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
for(int z=0;z<3;z++)
Box[x][y][z] = new Cube(Device,*position[x][y][z],tex);
}
}
// use alpha channel in texture for alpha
//Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
//Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
// set blending factors so that alpha component determines transparency
//Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
//Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
D3DXVECTOR3 dir(1.0f, -0.0f, 1.0f);
D3DXCOLOR c = d3d::WHITE;
D3DLIGHT9 dirLight = d3d::InitDirectionalLight(&dir, &c);
//
// Set and Enable the light.
//
Device->SetLight(0, &dirLight);
Device->LightEnable(0, true);
//
// disable lighting
//
Device->SetRenderState(D3DRS_LIGHTING, true);
//
// set camera
//
D3DXVECTOR3 pos(0.0f, 9.0f, -9.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(
&V,
&pos,
&target,
&up);
Device->SetTransform(D3DTS_VIEW, &V);
//
// Set projection matrix
//
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
void Cleanup()
{
d3d::Release<IDirect3DTexture9*>(CrateTex);
d3d::Delete<Cube*>(Box[3][3][3]);
//d3d::Release<IDirect3DVertexBuffer9*>(BackDropVB);
//d3d::Release<IDirect3DTexture9*>(BackDropTex);
}
bool Display(float timeDelta)
{
if( Device )
{
//
// Update: Update the camera.
//
if( ::GetAsyncKeyState('W') & 0x8000f )
TheCamera.walk(4.0f * timeDelta);
if( ::GetAsyncKeyState('S') & 0x8000f )
TheCamera.walk(-4.0f * timeDelta);
if( ::GetAsyncKeyState('A') & 0x8000f )
TheCamera.strafe(-4.0f * timeDelta);
if( ::GetAsyncKeyState('D') & 0x8000f )
TheCamera.strafe(4.0f * timeDelta);
if( ::GetAsyncKeyState('R') & 0x8000f )
TheCamera.fly(4.0f * timeDelta);
if( ::GetAsyncKeyState('F') & 0x8000f )
TheCamera.fly(-4.0f * timeDelta);
if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
TheCamera.pitch(1.0f * timeDelta);
if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
TheCamera.pitch(-1.0f * timeDelta);
if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
TheCamera.yaw(-1.0f * timeDelta);
if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
TheCamera.yaw(1.0f * timeDelta);
if( ::GetAsyncKeyState('N') & 0x8000f )
TheCamera.roll(1.0f * timeDelta);
if( ::GetAsyncKeyState('M') & 0x8000f )
TheCamera.roll(-1.0f * timeDelta);
// Update the view matrix representing the cameras
// new position/orientation.
D3DXMATRIX V;
TheCamera.getViewMatrix(&V);
Device->SetTransform(D3DTS_VIEW, &V);
//
D3DMATERIAL9 Mtrl=d3d::WHITE_MTRL;
//
// Update: Rotate the cube.
//
D3DXMATRIX xRot;
D3DXMatrixRotationX(&xRot,0);
D3DXMATRIX yRot;
D3DXMatrixRotationY(&yRot,D3DX_PI * 0.25f);
//y += timeDelta;
//D3DXMATRIX t;
//D3DXMatrixTranslation(&t,0,-3,0);
CubeWorldMatrix = xRot * yRot;
//
// Render
//
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0);
Device->BeginScene();
// draw back drop
D3DXMATRIX I;
D3DXMatrixIdentity(&I);
Device->SetStreamSource(0, BackDropVB, 0, sizeof(Vertex));
Device->SetFVF(FVF_VERTEX);
Device->SetTexture(0, BackDropTex);
Device->SetTransform(D3DTS_WORLD, &I);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
for(int z=0;z<3;z++)
{
if(Box[x][y][z])
Box[x][y][z]->draw(&CubeWorldMatrix, &Mtrl);
}
}
}
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 =