secret
anonymous / TransformMatrix.cpp
Created

  • Download Gist
TransformMatrix.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#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 );
}
TransformMatrix.hpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#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;
};
Universe.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#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;
}
}
Universe.hpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#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 );
};
VertexShader.hlsl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.