Skip to content

Instantly share code, notes, and snippets.

Created February 15, 2013 15:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/f9507cf69d93d52e97b6 to your computer and use it in GitHub Desktop.
#include "pch.h"
#include "TransformMatrix.h"
using namespace DirectX;
TransformMatrix::TransformMatrix(void)
{
Identity();
}
void TransformMatrix::Identity()
{
scale = rotate = translate = XMMatrixIdentity();
}
void TransformMatrix::Scale( float x, float y, float z )
{
scale *= XMMatrixScaling( x, y, z );
}
void TransformMatrix::Translate( float x, float y, float z )
{
translate *= XMMatrixTranslation( x, y, z );
}
void TransformMatrix::Rotate( float x, float y, float z, float ang )
{
if ( x * ang != 0.0f ) rotate *= XMMatrixRotationX( x * ang );
if ( y * ang != 0.0f ) rotate *= XMMatrixRotationY( y * ang );
if ( z * ang != 0.0f ) rotate *= XMMatrixRotationZ( z * ang );
}
#pragma once
class TransformMatrix
{
public:
TransformMatrix( );
void Identity();
void Scale( float x, float y, float z );
void Translate( float x, float y, float z );
void Rotate( float x, float y, float z, float ang );
operator DirectX::XMMATRIX()
{
DirectX::XMMATRIX transformed = scale * rotate * translate;
return transformed;
}
protected:
DirectX::XMMATRIX scale, rotate, translate;
};
#include "pch.h"
#include "Universe.h"
#include "DirectXSample.h"
using namespace DirectX;
Universe::Universe( DirectX_3d *d3d )
: direct3d( d3d )
, projection( 65.0f, DirectX::XMFLOAT2(800,600) )
, camera()
{
}
void Universe::Create( void )
{
CD3D11_BUFFER_DESC desc(sizeof(ConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
DX::ThrowIfFailed( direct3d->device->CreateBuffer( &desc, nullptr, buffer.GetAddressOf() ) );
model.Identity();
ReviseProjectionData();
ReviseCameraData();
ReviseModelData();
}
void Universe::ReviseProjectionData( unsigned int eye )
{
#ifdef USE_STEREO_PROJECTION
XMStoreFloat4x4( &bufferData.projection, XMMatrixTranspose( projection[eye] ) );
#else
XMStoreFloat4x4( &bufferData.projection, XMMatrixTranspose( projection.Matrix() ) );
#endif
update = true;
}
void Universe::ReviseCameraData( void )
{
XMStoreFloat4x4( &bufferData.view, XMMatrixTranspose( camera ) );
update = true;
}
void Universe::ReviseModelData( void )
{
XMStoreFloat4x4( &bufferData.model, XMMatrixTranspose( model ) );
update = true;
}
ID3D11Buffer *Universe::AquireBuffer( void )
{
UpdateBuffers( );
return buffer.Get();
}
ID3D11Buffer **Universe::AquireBufferAddress( void )
{
UpdateBuffers( );
return buffer.GetAddressOf();
}
ID3D11Buffer **Universe::AquireBufferAddressRef( void )
{
UpdateBuffers( );
return &buffer;
}
void Universe::UpdateBuffers( void )
{
if ( update )
{
direct3d->context->UpdateSubresource( buffer.Get(), 0, nullptr, &bufferData, 0, 0 );
update = false;
}
}
#pragma once
#include "TransformMatrix.h"
#include "Camera.h"
#include "ProjectionMatrix.h"
#include "StereoProjectionMatrix.h"
#define USE_STEREO_PROJECTION 1
#ifdef PROJECTION_TYPE
#undef PROJECTION_TYPE
#endif
#ifdef USE_STEREO_PROJECTION
# define PROJECTION_TYPE StereoProjectionMatrix
#else
# define PROJECTION_TYPE ProjectionMatrix
#endif
struct Universe
{
public:
Universe( DirectX_3d *d3d );
void Create( void );
void ReviseProjectionData( unsigned int eye = 0 );
void ReviseCameraData( void );
void ReviseModelData( void );
ID3D11Buffer *AquireBuffer( void );
ID3D11Buffer **AquireBufferAddress( void );
ID3D11Buffer **AquireBufferAddressRef( void );
TransformMatrix model;
Camera camera;
PROJECTION_TYPE projection;
protected:
DirectX_3d *direct3d;
ConstantBuffer bufferData;
Microsoft::WRL::ComPtr<ID3D11Buffer> buffer;
bool update;
private:
void UpdateBuffers( void );
};
cbuffer ProjectionBuffer : register(b0)
{
matrix p;
matrix m;
matrix v;
};
struct sVSInput
{
float3 pos : POSITION;
float3 norm : NORMAL;
float2 tex : TEXCOORD0;
};
struct pixelShader
{
float4 pos : SV_POSITION;
float3 norm : NORMAL;
float2 tex : TEXCOORD0;
};
pixelShader SimpleVertexShader(sVSInput input)
{
pixelShader output;
float4 temp = float4(input.pos, 1.0f);
temp = mul(temp, m);
temp = mul(temp, v);
temp = mul(temp, p);
output.pos = temp;
output.tex = input.tex;
output.norm = mul(float4(input.norm, 1.0f), m).xyz;
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment