Create Matrix function headers in C

This commit is contained in:
UnknownShadow200 2017-05-11 19:54:20 +10:00
parent 9ccba753c5
commit 36863b0cec
6 changed files with 86 additions and 14 deletions

View File

@ -2,6 +2,7 @@
#include "GraphicsAPI.h"
#include "D3D9Api.h"
#include "ErrorHandler.h"
#include "GraphicsEnums.h"
#define USE_DX true
#ifdef USE_DX
@ -166,18 +167,44 @@ void Gfx_SetDepthWrite(bool enabled) {
}
*Sets the matrix type that load / push / pop operations should be applied to. * /
void Gfx_SetMatrixMode(Int32 matrixType);
void Gfx_SetMatrixMode(Int32 matrixType) {
if (matrixType == MatrixType_Projection) {
curStack = &projStack;
} else if (matrixType == MatrixType_Modelview) {
curStack = &viewStack;
} else if (matrixType == MatrixType_Texture) {
curStack = &texStack;
}
}
/* Sets the current matrix to the given matrix.*/
void Gfx_LoadMatrix(Matrix* matrix);
void Gfx_LoadMatrix(Matrix* matrix) {
if (curStack == &texStack) {
matrix->Row2.X = matrix->Row3.X; // NOTE: this hack fixes the texture movements.
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
}
/* Sets the current matrix to the identity matrix. */
void Gfx_LoadIdentityMatrix();
Int32 idx = curStack->Index;
curStack->Stack[idx] = *matrix;
ReturnCode hresult = IDirect3DDevice9_SetTransform(device, curStack->Type, &curStack->Stack[idx]);
ErrorHandler_CheckOrFail(hresult, "D3D9_LoadMatrix");
}
/* Multiplies the current matrix by the given matrix, then
sets the current matrix to the result of the multiplication. */
void Gfx_MultiplyMatrix(Matrix* matrix);
void Gfx_LoadIdentityMatrix() {
if (curStack == &texStack) {
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
}
Int32 idx = curStack->Index;
curStack->Stack[idx] = *matrix;
ReturnCode hresult = IDirect3DDevice9_SetTransform(device, curStack->Type, &curStack->Stack[idx]);
ErrorHandler_CheckOrFail(hresult, "D3D9_LoadIdentityMatrix");
}
void Gfx_MultiplyMatrix(Matrix* matrix) {
curStack.MultiplyTop(ref matrix);
}
void Gfx_PushMatrix() {
Int32 idx = curStack->Index;
@ -195,6 +222,7 @@ void Gfx_PopMatrix() {
ErrorHandler_Fail("Unable to pop matrix, at 0 already");
}
D3DMATRIX m;
curStack->Index--; idx--;
ReturnCode hresult = IDirect3DDevice9_SetTransform(device, curStack->Type, &curStack->Stack[idx]);
ErrorHandler_CheckOrFail(hresult, "D3D9_PopMatrix");

View File

@ -7,7 +7,7 @@ void EventHandler_Register(void** handlers, Int32* count, void* handler) {
}
if (*count == EventHandler_Size) {
ErrorHandler_Fail("Unable to add another event handler"));
ErrorHandler_Fail("Unable to add another event handler");
}
handlers[*count] = handler;
*count++;

View File

@ -19,7 +19,7 @@ void GfxCommon_LoseContext(String reason) {
Platform_Log(String_FromConstant("Lost graphics context:"));
Platform_Log(reason);
EventHandler_Call_Void(Gfx_ContextLost, Gfx_ContextLostCount);
EventHandler_Raise_Void(Gfx_ContextLost, Gfx_ContextLostCount);
GfxCommon_Free();
}
@ -27,7 +27,7 @@ void GfxCommon_RecreateContext() {
Gfx_LostContext = false;
Platform_Log(String_FromConstant("Recreating graphics context"));
EventHandler_Call_Void(Gfx_ContextRecreated, Gfx_ContextRecreatedCount);
EventHandler_Raise_Void(Gfx_ContextRecreated, Gfx_ContextRecreatedCount);
GfxCommon_Init();
}

View File

@ -2,4 +2,4 @@
Vector4 Vector4_Create4(Real32 x, Real32 y, Real32 z, Real32 w) {
Vector4 v; v.X = x; v.Y = y; v.Z = z; v.W = w; return v;
}
}

View File

@ -1,6 +1,7 @@
#ifndef CS_MATRIX_H
#define CS_MATRIX_H
#include "Typedefs.h"
#include "Vectors.h"
/* Represents a 4 by 4 matrix.
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/
@ -30,4 +31,47 @@ typedef struct Matrix {
/* Bottom row of the matrix */
Vector4 Row3;
} Matrix;
/* Identity matrix. */
Matrix Matrix_Identity = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
/* Transformation matrix representing rotation angle radians around X axis. */
void Matrix_RotateX(Real32 angle, Matrix* result);
/* Transformation matrix representing rotation angle radians around Y axis. */
void Matrix_RotateY(Real32 angle, Matrix* result);
/* Transformation matrix representing rotation angle radians around Z axis. */
void Matrix_RotateZ(Real32 angle, Matrix* result);
/* Transformation matrix representing translation of given coordinates. */
void Matrix_Translate(Real32 x, Real32 y, Real32 z, Matrix* result);
/* Transformation matrix representing scaling of given axes. */
void Matrix_Scale(Real32 x, Real32 y, Real32 z, Matrix* result);
/* Multiplies two matrices.*/
void Matrix_Mul(Matrix* left, Matrix* right, Matrix* result);
/* Transformation matrix representing orthographic projection. */
void Matrix_Orthographic(Real32 width, Real32 height, Real32 zNear, Real32 zFar, Matrix* result);
/* Transformation matrix representing orthographic projection. */
void Matrix_OrthographicOffCenter(Real32 left, Real32 right, Real32 bottom, Real32 top, Real32 zNear, Real32 zFar, Matrix* result);
/* Transformation matrix representing perspective projection. */
void Matrix_PerspectiveFieldOfView(Real32 fovy, Real32 aspect, Real32 zNear, Real32 zFar, Matrix* result);
/* Transformation matrix representing perspective projection. */
void Matrix_PerspectiveOffCenter(Real32 left, Real32 right, Real32 bottom, Real32 top, Real32 zNear, Real32 zFar, Matrix* result);
/* Transformation matrix representing camera look at. */
void Matrix_LookAt(Vector3 eye, Vector3 target, Vector3 up, Matrix* result);
#endif

View File

@ -17,7 +17,7 @@ void World_SetNewMap(BlockID* blocks, Int32 blocksSize, Int32 width, Int32 heigh
if (World_BlocksSize == 0) World_Blocks = NULL;
if (blocksSize != (width * height * length)) {
ErrorHandler_Fail(String_FromConstant("Blocks array size does not match volume of map."));
ErrorHandler_Fail("Blocks array size does not match volume of map");
}
if (WorldEnv_EdgeHeight == -1) {