Togglling effects

Started by
2 comments, last by iedoc 12 years, 3 months ago
Hi guys
i hope your holidays were fine and your new year is so far going well.
I have been working on a project coutsey of iedoc's tutorials and required some assisstance
on implementing advanced keyboard input.
i want o be able to give the user the ability to togglle various features of my scene.
(some togglling features: B-blending, R-rotate, T-textures etc)
so far i have implemented the rotation however the R key must be held down
or the pyramid will stop rotating. i require the user to just tap the relevant key
to togglle the effects on and off.

here is the the specific code for rotation(find it in the detect input function)

///////control rotation
if(keyboardState[DIK_R] & 0x80)
{
//rot = true;
rot += .001f;
}
/////>>>>>>>>>>>.


and here is the whole source file

#include <Windows.h>
#include <d3d10.h>
#include <d3dx10.h>
#include <string>
#include <vector>
#include <dinput.h>
#include <fstream>
#include <istream>
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")
#pragma comment(lib, "D3D10.lib")
#pragma comment(lib, "d3dx10d.lib")
using namespace std;
LPCTSTR WndClassName = L"firstwindow";
HWND hwnd = NULL;
const int Width = 800;
const int Height = 600;
bool InitializeWindow(HINSTANCE hInstance,
int ShowWnd,
int width, int height,
bool windowed);
HRESULT hr;
ID3D10Device* d3dDevice;
IDXGISwapChain* SwapChain;
ID3D10RenderTargetView* RenderTargetView;
ID3D10Effect* FX;
ID3D10InputLayout* VertexLayout;
////////////sphere
ID3D10Buffer* VertexBuffer;
ID3D10Buffer* IndexBuffer;
/////sphere
///////////////blending////////
ID3D10BlendState* Transparency;
ID3D10RasterizerState* CCWcullMode;
ID3D10RasterizerState* CWcullMode;
//////////////blending////////////
ID3D10EffectTechnique* Technique;
ID3D10DepthStencilView* DepthStencilView;
ID3D10Texture2D* DepthStencilBuffer;
D3DXMATRIX Rotation;
///////pyramid
ID3D10Buffer* pyramidVertBuff;
ID3D10Buffer* pyramidIndexBuff;
float rot = 0.05f;
/////////////pyramid
ID3D10EffectShaderResourceVariable* fxDiffuseMapVar;
ID3D10EffectMatrixVariable* fxWVPVar;
D3DXMATRIX WVP;
D3DXMATRIX World;
D3DXMATRIX View;
D3DXMATRIX Projection;
ID3D10EffectVariable* fxLightVar;
IDirectInputDevice8* DIKeyboard;
IDirectInputDevice8* DIMouse;
DIMOUSESTATE mouseLastState;
LPDIRECTINPUT8 DirectInput;
vector<ID3DX10Mesh*> meshes;
int meshCount;
bool LoadMesh(wstring filename);
int meshTextures = 0;
vector<ID3D10ShaderResourceView*> TextureResourceViews;
vector<UINT> meshSubsets;
DWORD NumVertices;
DWORD NumFaces;
/////////////////////////new/////////////////////////////////////////////////
ID3D10EffectShaderResourceVariable* fxSkyMapVar;
ID3D10ShaderResourceView* smrv = 0;
ID3D10EffectTechnique* SkyMapTechnique;
void CreateSphere(int LatLines, int LongLines);
/////////////////////////new/////////////////////////////////////////////////
float moveLeftRight = 0.0f;
float moveBackForward = 0.0f;
D3DXVECTOR3 Position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXVECTOR3 Target = D3DXVECTOR3( 0.0f, 0.0f, 1.0f);
D3DXVECTOR3 Up = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXVECTOR3 DefaultForward = D3DXVECTOR3(0.0f,0.0f,1.0f);
D3DXVECTOR3 DefaultRight = D3DXVECTOR3(1.0f,0.0f,0.0f);
D3DXVECTOR3 Forward = D3DXVECTOR3(0.0f,0.0f,1.0f);
D3DXVECTOR3 Right = D3DXVECTOR3(1.0f,0.0f,0.0f);
D3DXMATRIX rotationMatrix;
float yaw = 0.0f;
float pitch = 0.0f;
void UpdateCamera();
D3DXMATRIX Scale;
D3DXMATRIX Translation;
D3DXMATRIX Transformations;
bool InitializeDirect3dApp(HINSTANCE hInstance);
bool InitDirectInput(HINSTANCE hInstance);
void DetectInput();
bool InitScene();
void DrawScene();
bool ReleaseObjects();
int messageloop();
LRESULT CALLBACK WndProc(HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam);
struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z,
float u, float v, float nx, float ny, float nz)
: pos(x,y,z), texCoord(u,v), normal(nx,ny,nz){}
D3DXVECTOR3 pos;
D3DXVECTOR2 texCoord;
D3DXVECTOR3 normal;
};
D3D10_INPUT_ELEMENT_DESC layout[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 20, D3D10_INPUT_PER_VERTEX_DATA, 0}
};
struct Light
{
Light()
{
ZeroMemory(this, sizeof(Light));
}
D3DXVECTOR3 dir;
float pad;
D3DXCOLOR ambient;
D3DXCOLOR diffuse;
};
Light light;

int WINAPI WinMain(HINSTANCE hInstance, //Main windows function
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
if(!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
{
MessageBox(0, L"Window Initialization - Failed",
L"Error", MB_OK);
return 0;
}
if(!InitializeDirect3dApp(hInstance))
{
MessageBox(0, L"Direct3D Initialization - Failed",
L"Error", MB_OK);
return 0;
}
if(!InitDirectInput(hInstance))
{
MessageBox(0, L"Direct Input Initialization - Failed",
L"Error", MB_OK);
return 0;
}
if(!InitScene())
{
MessageBox(0, L"Scene Initialization - Failed",
L"Error", MB_OK);
return 0;
}
messageloop();
if(!ReleaseObjects())
{
MessageBox(0, L"Object Releasing - Failed",
L"Error", MB_OK);
return 0;
}
return 0;
}
bool InitializeWindow(HINSTANCE hInstance,
int ShowWnd,
int width, int height,
bool windowed)
{
typedef struct _WNDCLASS {
UINT cbSize;
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HANDLE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
} WNDCLASS;
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
wc.lpszMenuName = NULL;
wc.lpszClassName = WndClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Error registering class",
L"Error", MB_OK | MB_ICONERROR);
return 1;
}
hwnd = CreateWindowEx(
NULL,
WndClassName,
L"Window Title",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
width, height,
NULL,
NULL,
hInstance,
NULL
);
if (!hwnd)
{
MessageBox(NULL, L"Error creating window",
L"Error", MB_OK | MB_ICONERROR);
return 1;
}
ShowWindow(hwnd, ShowWnd);
UpdateWindow(hwnd);

return true;
}
bool InitializeDirect3dApp(HINSTANCE hInstance)
{
UINT createDeviceFlags = 0;
D3D10_DRIVER_TYPE driverTypes[] =
{
D3D10_DRIVER_TYPE_HARDWARE,
D3D10_DRIVER_TYPE_REFERENCE,
};
UINT numDriverTypes = sizeof( driverTypes ) / sizeof( driverTypes[0] );
DXGI_SWAP_CHAIN_DESC scd;
scd.BufferDesc.Width = Width;
scd.BufferDesc.Height = Height;
scd.BufferDesc.RefreshRate.Numerator = 60;
scd.BufferDesc.RefreshRate.Denominator = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
//no multisampling
scd.SampleDesc.Count = 1;
scd.SampleDesc.Quality = 0;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.BufferCount = 1;
scd.OutputWindow = hwnd;
scd.Windowed = true;
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
scd.Flags = 0;
D3D10CreateDeviceAndSwapChain(0, D3D10_DRIVER_TYPE_HARDWARE, 0, 0, D3D10_SDK_VERSION, &scd, &SwapChain, &d3dDevice);
ID3D10Texture2D* backBuffer;
SwapChain->GetBuffer(0, _uuidof(ID3D10Texture2D), reinterpret_cast<void**>(&backBuffer));
d3dDevice->CreateRenderTargetView(backBuffer, 0, &RenderTargetView);
backBuffer->Release();
D3D10_TEXTURE2D_DESC depthStencilDesc;
depthStencilDesc.Width = Width;
depthStencilDesc.Height = Height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 1;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage = D3D10_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
d3dDevice->CreateTexture2D(&depthStencilDesc, NULL, &DepthStencilBuffer);
d3dDevice->CreateDepthStencilView(DepthStencilBuffer, NULL, &DepthStencilView);
d3dDevice->OMSetRenderTargets(1, &RenderTargetView, DepthStencilView);
// Setup the viewport
D3D10_VIEWPORT vp;
vp.Width = Width;
vp.Height = Height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
d3dDevice->RSSetViewports( 1, &vp );
D3DXMatrixIdentity( &World );
Position = D3DXVECTOR3( 0.0f, 4.0f, -10.0f );
Target = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
Up = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &View, &Position, &Target, &Up );
return true;
}
bool InitDirectInput(HINSTANCE hInstance)
{
DirectInput8Create(hInstance,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&DirectInput,
NULL);
DirectInput->CreateDevice(GUID_SysKeyboard,
&DIKeyboard,
NULL);
DirectInput->CreateDevice(GUID_SysMouse,
&DIMouse,
NULL);
DIKeyboard->SetDataFormat(&c_dfDIKeyboard);
DIKeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
DIMouse->SetDataFormat(&c_dfDIMouse);
DIMouse->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND);
return true;
}
void UpdateCamera()
{
D3DXMatrixRotationYawPitchRoll( &rotationMatrix, yaw, pitch, 0 );
D3DXVec3TransformCoord( &Target, &DefaultForward, &rotationMatrix );
D3DXVec3Normalize( &Target, &Target );
D3DXMATRIX RotateYTempMatrix;
D3DXMatrixRotationY(&RotateYTempMatrix, yaw);
D3DXVec3TransformNormal(&Right, &DefaultRight, &RotateYTempMatrix);
D3DXVec3TransformNormal(&Up, &Up, &RotateYTempMatrix);
D3DXVec3TransformNormal(&Forward, &DefaultForward, &RotateYTempMatrix);

Position += moveLeftRight*Right;
Position += moveBackForward*Forward;
moveLeftRight = 0.0f;
moveBackForward = 0.0f;
Target = Position + Target;
D3DXMatrixLookAtLH( &View, &Position, &Target, &Up );
}

void DetectInput()
{
float speed = 0.010f;

DIMOUSESTATE mouseCurrState;
BYTE keyboardState[256];
DIKeyboard->Acquire();
DIMouse->Acquire();
DIMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseCurrState);
DIKeyboard->GetDeviceState(sizeof(keyboardState),(LPVOID)&keyboardState);
if(keyboardState[DIK_ESCAPE] & 0x80)
PostMessage(hwnd, WM_DESTROY, 0, 0);
if(keyboardState[DIK_A] & 0x80)
{
moveLeftRight -= speed;
}
if(keyboardState[DIK_D] & 0x80)
{
moveLeftRight += speed;
}
if(keyboardState[DIK_W] & 0x80)
{
moveBackForward += speed;
}
if(keyboardState[DIK_S] & 0x80)
{
moveBackForward -= speed;
}
///////control rotation
if(keyboardState[DIK_R] & 0x80)
{
//rot = true;
rot += .001f;
}
/////>>>>>>>>>>>.
if((mouseCurrState.lX != mouseLastState.lX) || (mouseCurrState.lY != mouseLastState.lY))
{
yaw += mouseLastState.lX * 0.001f;
pitch += mouseCurrState.lY * 0.001f;
mouseLastState = mouseCurrState;
}
UpdateCamera();
return;
}

bool LoadMesh(wstring filename)
{
HRESULT hr = 0;
ID3DX10Mesh* tempMesh;
UINT tempMeshSubsets;
wifstream fileIn (filename.c_str());
wstring skipString;
UINT meshVertices = 0;
UINT meshTriangles = 0;
if (fileIn)
{
fileIn >> skipString; // #Subsets
fileIn >> tempMeshSubsets;
fileIn >> skipString; // #Vertices
fileIn >> meshVertices;
fileIn >> skipString; // #Faces (Triangles)
fileIn >> meshTriangles;
meshSubsets.push_back(tempMeshSubsets);
hr = D3DX10CreateMesh(d3dDevice,
layout,
3,
layout[0].SemanticName,
meshVertices,
meshTriangles,
D3DX10_MESH_32_BIT,
&tempMesh);
if(FAILED(hr))
{
MessageBox(0, L"Mesh Creation - Failed",
L"Error", MB_OK);
return false;
}
fileIn >> skipString; //#Subset_info
for(UINT i = 0; i < tempMeshSubsets; ++i)
{
std::wstring diffuseMapFilename;
fileIn >> diffuseMapFilename;
ID3D10ShaderResourceView* DiffuseMapResourceView;
D3DX10CreateShaderResourceViewFromFile(d3dDevice,
diffuseMapFilename.c_str(), 0, 0, &DiffuseMapResourceView, 0 );
TextureResourceViews.push_back(DiffuseMapResourceView);
meshTextures++;
}
Vertex* verts = new Vertex[meshVertices];
fileIn >> skipString; //#Vertex_info
for(UINT i = 0; i < meshVertices; ++i)
{
fileIn >> skipString; //Vertex Position
fileIn >> verts.pos.x;
fileIn >> verts.pos.y;
fileIn >> verts.pos.z;
fileIn >> skipString; //Vertex Normal
fileIn >> verts.normal.x;
fileIn >> verts.normal.y;
fileIn >> verts.normal.z;
fileIn >> skipString; //Vertex Texture Coordinates
fileIn >> verts.texCoord.x;
fileIn >> verts.texCoord.y;
}
tempMesh->SetVertexData(0, verts);
delete[] verts;
DWORD* indices = new DWORD[meshTriangles*3];
UINT* attributeIndex = new UINT[meshTriangles];
fileIn >> skipString; //#Face_Index
for(UINT i = 0; i < meshTriangles; ++i)
{
fileIn >> indices[i*3+0];
fileIn >> indices[i*3+1];
fileIn >> indices[i*3+2];
fileIn >> attributeIndex; //Current Subset
}
tempMesh->SetIndexData(indices, meshTriangles*3);
tempMesh->SetAttributeData(attributeIndex);
delete[] indices;
delete[] attributeIndex;
tempMesh->GenerateAdjacencyAndPointReps(0.001f);
tempMesh->Optimize(D3DX10_MESHOPT_ATTR_SORT|D3DX10_MESHOPT_VERTEX_CACHE,0,0);
tempMesh->CommitToDevice();
meshCount++;
meshes.push_back(tempMesh);
}
else
{
MessageBox(0, L"Load Mesh File - Failed",
L"Error", MB_OK);
return false;
}
return true;
}
/////////////////////////new/////////////////////////////////////////////////
void CreateSphere(int LatLines, int LongLines)
{
NumVertices = LatLines * LongLines;
NumFaces = (LatLines-1)*(LongLines-1)*2;
float sphereYaw = 0.0f;
float spherePitch = 0.0f;
std::vector<Vertex> vertices(NumVertices);
D3DXVECTOR3 currVertPos = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
for(DWORD i = 0; i < LatLines; ++i)
{
sphereYaw = i * (3.14/LatLines);
for(DWORD j = 0; j < LongLines; ++j)
{
spherePitch = j * (3.14/LongLines);
D3DXMatrixRotationYawPitchRoll( &rotationMatrix, sphereYaw, spherePitch, 0 );
D3DXVec3TransformCoord( &currVertPos, &DefaultForward, &rotationMatrix );
D3DXVec3Normalize( &currVertPos, &currVertPos );
vertices[i*LongLines+j].pos = currVertPos;
}
}
D3D10_BUFFER_DESC bd;
bd.Usage = D3D10_USAGE_IMMUTABLE;
bd.ByteWidth = sizeof( Vertex ) * NumVertices;
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA InitData;
InitData.pSysMem = &vertices[0];
d3dDevice->CreateBuffer( &bd, &InitData, &VertexBuffer );
std::vector<DWORD> indices(NumFaces * 3);
int k = 0;
for(DWORD i = 0; i < LatLines-1; ++i)
{
for(DWORD j = 0; j < LongLines-1; ++j)
{
indices[k] = i*LongLines+j;
indices[k+1] = i*LongLines+j+1;
indices[k+2] = (i+1)*LongLines+j;
indices[k+3] = (i+1)*LongLines+j;
indices[k+4] = i*LongLines+j+1;
indices[k+5] = (i+1)*LongLines+j+1;
k += 6; // next quad
}
}
D3D10_BUFFER_DESC ibd;
ibd.Usage = D3D10_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(DWORD) * NumFaces * 3;
ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = &indices[0];
d3dDevice->CreateBuffer(&ibd, &iinitData, &IndexBuffer);
UINT stride = sizeof( Vertex );
UINT offset = 0;
d3dDevice->IASetVertexBuffers( 0, 1, &VertexBuffer, &stride, &offset );
d3dDevice->IASetIndexBuffer(IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
}
/////////////////////////new/////////////////////////////////////////////////
bool InitScene()
{
HRESULT hr = 0;
ID3D10Blob* compilationErrors = 0;
/////////////////////////new/////////////////////////////////////////////////
LoadMesh(L"plane.dat"); //meshes[1]
LoadMesh(L"3DSphere.dat"); //meshes[0]
CreateSphere(10, 10);
//////////////////////////pyramid vertices
Vertex v[5];
v[0] = Vertex( 0.0f, 1.0f, 0.0f, 2.0f, 2.0f, 0.5f, 0.0f, 0.0f);
v[1] = Vertex( 1.0f, -1.0f, 1.0f, 2.0f, 2.0f, 0.5f, 0.0f, 0.0f);
v[2] = Vertex( -1.0f, -1.0f, 1.0f, 2.0f, 2.0f, 0.5f, 0.0f, 0.0f);
v[3] = Vertex( 1.0f, -1.0f,-1.1f, 2.0f, 2.0f, 0.5f, 0.0f, 0.0f);
v[4] = Vertex( -1.0f, -1.0f,-1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f);
D3D10_BUFFER_DESC vbd;
vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(Vertex) * 5;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = v;

// Now storing in the pyramidVertBuff buffer
d3dDevice->CreateBuffer(&vbd, &vinitData, &pyramidVertBuff);
//////////////////////////pyramid vertices
DWORD i[18];

// Front Face
i[0] = 0; i[1] = 2; i[2] = 1;

//Back
i[3] = 0; i[4] = 3; i[5] = 4;

// Left Face
i[6] = 0; i[7] = 1; i[8] = 3;

//Right
i[9] = 0; i[10] = 4; i[11] = 2;

// Bottom Face
i[12] = 4; i[13] = 1; i[14] = 2;
i[15] = 3; i[16] = 1; i[17] = 4;

/////pyramid vertices

D3D10_BUFFER_DESC ibd;
ibd.Usage = D3D10_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(DWORD) * 6 * 3;
ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = &i[0];
d3dDevice->CreateBuffer(&ibd, &iinitData, &pyramidIndexBuff);
///////pyramid vertices

D3DX10_IMAGE_LOAD_INFO loadSMInfo;
loadSMInfo.MiscFlags = D3D10_RESOURCE_MISC_TEXTURECUBE;
ID3D10Texture2D* SMTexture = 0;
hr = D3DX10CreateTextureFromFile(d3dDevice, L"skymap.dds",
&loadSMInfo, 0, (ID3D10Resource**)&SMTexture, 0);
if(FAILED(hr))
{
MessageBox(0, L"Load pyramid texture - Failed",
L"Error", MB_OK);
return false;
}
D3D10_TEXTURE2D_DESC SMTextureDesc;
SMTexture->GetDesc(&SMTextureDesc);
D3D10_SHADER_RESOURCE_VIEW_DESC SMViewDesc;
SMViewDesc.Format = SMTextureDesc.Format;
SMViewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURECUBE;
SMViewDesc.TextureCube.MipLevels = SMTextureDesc.MipLevels;
SMViewDesc.TextureCube.MostDetailedMip = 0;
hr = d3dDevice->CreateShaderResourceView(SMTexture, &SMViewDesc, &smrv);
if(FAILED(hr))
{
MessageBox(0, L"Create Cube Texture RV - Failed",
L"Error", MB_OK);
return false;
}
SMTexture->Release();
/////////////////////////new/////////////////////////////////////////////////
light.dir = D3DXVECTOR3(0.25f, 0.5f, -1.0f);
light.ambient = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
light.diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
hr = D3DX10CreateEffectFromFile( L"vertex.fx", NULL, NULL, "fx_4_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
d3dDevice, NULL, NULL, &FX, &compilationErrors, NULL );
if(FAILED(hr))
{
MessageBoxA(0, (char*)compilationErrors->GetBufferPointer(), 0, 0);
compilationErrors->Release();
return false;
}
Technique = FX->GetTechniqueByName( "Tech" );
/////////////////////////new/////////////////////////////////////////////////
SkyMapTechnique = FX->GetTechniqueByName("SkyMapTech");
/////////////////////////new/////////////////////////////////////////////////
fxWVPVar = FX->GetVariableByName("WVP")->AsMatrix();
fxDiffuseMapVar = FX->GetVariableByName("DiffuseMap")->AsShaderResource();
fxLightVar = FX->GetVariableByName("light");
fxSkyMapVar = FX->GetVariableByName("SkyMap")->AsShaderResource();
D3D10_PASS_DESC PassDesc;
Technique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
d3dDevice->CreateInputLayout( layout, 3, PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize, &VertexLayout );
d3dDevice->IASetInputLayout( VertexLayout );
d3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
////////////////////blending///////////////////////
D3D10_BLEND_DESC blendDesc = {0};
blendDesc.AlphaToCoverageEnable = false;
blendDesc.BlendEnable[0] = true;
blendDesc.SrcBlend = D3D10_BLEND_SRC_COLOR;
blendDesc.DestBlend = D3D10_BLEND_BLEND_FACTOR;
blendDesc.BlendOp = D3D10_BLEND_OP_ADD;
blendDesc.SrcBlendAlpha = D3D10_BLEND_ONE;
blendDesc.DestBlendAlpha = D3D10_BLEND_ZERO;
//blendDesc.SrcBlend = D3D10_BLEND_SRC_ALPHA_SAT; //mine
blendDesc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
blendDesc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_BLUE| D3D10_COLOR_WRITE_ENABLE_GREEN;
d3dDevice->CreateBlendState(&blendDesc, &Transparency);
D3D10_RASTERIZER_DESC cmdesc;
ZeroMemory(&cmdesc, sizeof(D3D10_RASTERIZER_DESC));
cmdesc.FillMode = D3D10_FILL_SOLID;
cmdesc.CullMode = D3D10_CULL_BACK;
cmdesc.FrontCounterClockwise = true;
hr = d3dDevice->CreateRasterizerState(&cmdesc, &CCWcullMode);
if(FAILED(hr))
{
MessageBox(0, L"RS STATE Creation - Failed",
L"Error", MB_OK);
return false;
}
cmdesc.FrontCounterClockwise = false;
hr = d3dDevice->CreateRasterizerState(&cmdesc, &CWcullMode);
if(FAILED(hr))
{
MessageBox(0, L"RS STATE Creation - Failed",
L"Error", MB_OK);
return false;
}
////////////////////blending/////////////////////////////////
return true;
}
bool ReleaseObjects()
{
if( d3dDevice ) d3dDevice->ClearState();
if( VertexBuffer ) VertexBuffer->Release();
if( IndexBuffer ) IndexBuffer->Release();
if( VertexLayout ) VertexLayout->Release();
if( FX ) FX->Release();
if( RenderTargetView ) RenderTargetView->Release();
if( SwapChain ) SwapChain->Release();
if( d3dDevice ) d3dDevice->Release();
////////////////////blending/////////////////////
if( Transparency ) Transparency->Release();
////////////////////blending///////////////
DIKeyboard->Unacquire();
DIMouse->Unacquire();
if( DirectInput ) DirectInput->Release();
for(int i = 0; i < meshCount; i++)
if( meshes ) meshes->Release();
return true;
}
//Draw Scene Here
void DrawScene()
{
////background colour
D3DXCOLOR bgColor( 0.0f, 0.0f, 0.0f, 1.0f);
////////////////////blending///////////////////////////
float blendFactor[] = {0.75f, 0.75f, 0.75f, 1.0f};
////////////////////blending////////////////////////////
d3dDevice->ClearRenderTargetView( RenderTargetView, bgColor );
d3dDevice->ClearDepthStencilView(DepthStencilView, D3D10_CLEAR_DEPTH|D3D10_CLEAR_STENCIL, 1.0f, 0);
D3DXMatrixPerspectiveFovLH(&Projection, 0.4f*3.14f, Width/Height, 1.0f, 1000.0f);
fxLightVar->SetRawValue(&light, 0, sizeof(Light));
/////////////////////////skymap/////////////////////////////////////////////////
D3D10_TECHNIQUE_DESC skymaptechDesc;
SkyMapTechnique->GetDesc( &skymaptechDesc );
/////////////////////////skymap/////////////////////////////////////////////////
D3D10_TECHNIQUE_DESC techDesc;
Technique->GetDesc( &techDesc );
////////////////////blending////////////////////////////////////////////////////
d3dDevice->OMSetBlendState(0, 0, 0xffffffff);
//Draw opaque objects here
////////////////////blending////////////////////////////////////////////////////////
D3DXMatrixScaling( &Scale, 1.0f, 1.0f, 1.0f );
D3DXMatrixTranslation( &Translation, 0.0f, -3.0f, 0.0f );
Transformations = Scale * Translation;
WVP = World * Transformations * View * Projection;
fxWVPVar->SetMatrix((float*)&WVP);

//draw plane
for( UINT p = 0; p < techDesc.Passes; ++p )
{
for(UINT subsetID = 0; subsetID < meshSubsets[0]; ++subsetID)
{
fxDiffuseMapVar->SetResource(TextureResourceViews[subsetID]);
Technique->GetPassByIndex( p )->Apply( 0 );
meshes[0]->DrawSubset(subsetID);
}
}

//scale sphere
D3DXMatrixScaling( &Scale, 5.0f, 5.0f, 5.0f );
//translate sphere
D3DXMatrixTranslation( &Translation, 5.0f, 2.0f, 10.0f);
Transformations = Scale * Translation;
WVP = World * Transformations * View * Projection;
fxWVPVar->SetMatrix((float*)&WVP);
////////////////////////////////////////////////plane

//draw sphere
for( UINT p = 0; p < techDesc.Passes; ++p )
{
for(UINT subsetID = 0; subsetID < meshSubsets[1]; ++subsetID)
{
fxDiffuseMapVar->SetResource(TextureResourceViews[subsetID + meshSubsets[0]]);
Technique->GetPassByIndex( p )->Apply( 0 );
meshes[1]->DrawSubset(subsetID);
}
}
D3DXMatrixScaling( &Scale, 100.0f, 100.0f,100.0f );
D3DXMatrixTranslation( &Translation, Position.x, Position.y, Position.z );
Transformations = Scale * Translation;
WVP = World * Transformations * View * Projection;
fxWVPVar->SetMatrix((float*)&WVP);
//+++++++++++++++++++++++++++
// Set sky sphere's index and vertex buffers
UINT stride = sizeof( Vertex );
UINT offset = 0;
d3dDevice->IASetVertexBuffers( 0, 1, &VertexBuffer, &stride, &offset );
d3dDevice->IASetIndexBuffer(IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
//++++++++++++++++++++++++++++

//draw skymap based off loaded mesh sphere
fxSkyMapVar->SetResource(smrv);
for( UINT p = 0; p < skymaptechDesc.Passes; ++p )
{
for (UINT subsetID = 0; subsetID < meshSubsets[1]; ++subsetID)
{
SkyMapTechnique->GetPassByIndex( p )->Apply( 0 );

meshes[1]->DrawSubset(subsetID);
}
}


// Create the World matrix (and put in the WVP matrix)
D3DXVECTOR3 rotaxis(0.0f, 1.0f, 0.0f);

D3DXMatrixRotationAxis(&Rotation, &rotaxis, rot);
D3DXMatrixTranslation( &Translation, 0.0f, 0.0f, 4.0f );
// stop rotation
//rot = false;

//rot += .001f;
////rot try
//scale pyramid
D3DXMatrixScaling( &Scale, 20.0f, 20.0f, 20.0f );
//translate pyramid
D3DXMatrixTranslation( &Translation, 0.0f, 30.0f, 30.0f );

Transformations = Rotation * Scale * Translation;

WVP = World * Transformations * View * Projection;
fxWVPVar->SetMatrix((float*)&WVP);
////////////////////blending//////////////////////////////////////////
d3dDevice->OMSetBlendState(Transparency, blendFactor, 0xffffffff);

// Bind the pyramids vertex and index buffer
d3dDevice->IASetVertexBuffers( 0, 1, &pyramidVertBuff, &stride, &offset );
d3dDevice->IASetIndexBuffer(pyramidIndexBuff, DXGI_FORMAT_R32_UINT, 0);

//draw pyramid
for( UINT p = 0; p < techDesc.Passes; ++p )
{
// Send a texture to the shader since it's expecting one (otherwise it will use the last texture sent to the shaders
fxDiffuseMapVar->SetResource(TextureResourceViews[0 + meshSubsets[0]]);

Technique->GetPassByIndex( p )->Apply( 0 );
d3dDevice->DrawIndexed(18, 0, 0);
}
////////////////////////////
SwapChain->Present( 0, 0 );
}
int messageloop(){
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while(true)
{
BOOL PeekMessageL(
LPMSG lpMsg,
HWND hWnd,
UINT wMsgFilterMin,
UINT wMsgFilterMax,
UINT wRemoveMsg
);
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
// run game code
DetectInput();
DrawScene();
}
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,
msg,
wParam,
lParam);
}


Any assisstance is greatly appreciated.
Advertisement
i can help with this ;)

you can create two a global boolean variables called toggleRotate and rDown. You check to see if R is being pressed. if it is, then you check to make sure that the variable rDown is false. if rDown is false and R is being pressed, you change toggleRotate to not equal toggleRotate. then you check to see if R is not being pressed. if R is not being pressed, you set rDown to false. right after you check for input, you can check to see if toggleRotate is true. if it's true, then do your rotation. hope this helps!

// Global variables
bool rDown = false;
bool toggleRotate = false;

if(keyboardState[DIK_R] & 0x80){ // "R" key is being pressed
if(!rDown)
{
toggleRotate = -toggleRotate;
rDown = true;
}
}

if(!keyboardState[DIK_R] & 0x80){ // "R" key is not being pressed
rDown = false;
}
if(toggleRotate)
{
//rot = true;
rot += .001f;
}
Thanks for the solution iedoc it works. although i had to change this part

if(!rDown)
{
toggleRotate = -toggleRotate;
rDown = true;
}


to this in order for it to work


if(!rDown)
{
toggleRotate = !toggleRotate;
rDown = true;
}

it also does not stop when you press R again.
I will try tweaking it see if i can get it right. Thanks again.
oh yeah, sorry, that was my bad, i should have said !toggleRotate in the first place...

also, try using else instead of !keyboardState[DIK_R], so here, this should work for you, sorry:


// Global variables
bool rDown = false;
bool toggleRotate = false;

if(keyboardState[DIK_R] & 0x80){ // "R" key is being pressed
if(!rDown)
{
toggleRotate = !toggleRotate;
rDown = true;
}
}
else{ // "R" key is not being pressed
rDown = false;
}
if(toggleRotate)
{
//rot = true;
rot += .001f;
}

This topic is closed to new replies.

Advertisement