Simplify Gfx matrices

This commit is contained in:
UnknownShadow200 2018-11-24 12:01:32 +11:00
parent cd2d70bd8c
commit 67d4e33fa8
7 changed files with 39 additions and 61 deletions

View File

@ -134,10 +134,8 @@ void EnvRenderer_RenderClouds(double deltaTime) {
time = Game_Accumulator; time = Game_Accumulator;
offset = (float)(time / 2048.0f * 0.6f * Env_CloudsSpeed); offset = (float)(time / 2048.0f * 0.6f * Env_CloudsSpeed);
Gfx_SetMatrixMode(MATRIX_TYPE_TEXTURE);
m = Matrix_Identity; m.Row3.X = offset; /* translate X axis */ m = Matrix_Identity; m.Row3.X = offset; /* translate X axis */
Gfx_LoadMatrix(&m); Gfx_LoadMatrix(MATRIX_TEXTURE, &m);
Gfx_SetMatrixMode(MATRIX_TYPE_VIEW);
Gfx_SetAlphaTest(true); Gfx_SetAlphaTest(true);
Gfx_SetTexturing(true); Gfx_SetTexturing(true);
@ -148,9 +146,7 @@ void EnvRenderer_RenderClouds(double deltaTime) {
Gfx_SetAlphaTest(false); Gfx_SetAlphaTest(false);
Gfx_SetTexturing(false); Gfx_SetTexturing(false);
Gfx_SetMatrixMode(MATRIX_TYPE_TEXTURE); Gfx_LoadIdentityMatrix(MATRIX_TEXTURE);
Gfx_LoadIdentityMatrix();
Gfx_SetMatrixMode(MATRIX_TYPE_VIEW);
} }
static void EnvRenderer_DrawCloudsY(int x1, int z1, int x2, int z2, int y, VertexP3fT2fC4b* vertices) { static void EnvRenderer_DrawCloudsY(int x1, int z1, int x2, int z2, int y, VertexP3fT2fC4b* vertices) {
@ -232,9 +228,9 @@ void EnvRenderer_RenderSky(double deltaTime) {
m.Row3.X += dy * m.Row1.X; m.Row3.Y += dy * m.Row1.Y; m.Row3.X += dy * m.Row1.X; m.Row3.Y += dy * m.Row1.Y;
m.Row3.Z += dy * m.Row1.Z; m.Row3.W += dy * m.Row1.W; m.Row3.Z += dy * m.Row1.Z; m.Row3.W += dy * m.Row1.W;
Gfx_LoadMatrix(&m); Gfx_LoadMatrix(MATRIX_VIEW, &m);
Gfx_DrawVb_IndexedTris(sky_vertices); Gfx_DrawVb_IndexedTris(sky_vertices);
Gfx_LoadMatrix(&Gfx_View); Gfx_LoadMatrix(MATRIX_VIEW, &Gfx_View);
} }
} }
@ -316,12 +312,12 @@ void EnvRenderer_RenderSkybox(double deltaTime) {
Camera_Active->GetView(&view); Matrix_MulBy(&m, &view); Camera_Active->GetView(&view); Matrix_MulBy(&m, &view);
Camera_CurrentPos = pos; Camera_CurrentPos = pos;
Gfx_LoadMatrix(&m); Gfx_LoadMatrix(MATRIX_VIEW, &m);
Gfx_BindVb(skybox_vb); Gfx_BindVb(skybox_vb);
Gfx_DrawVb_IndexedTris(SKYBOX_COUNT); Gfx_DrawVb_IndexedTris(SKYBOX_COUNT);
Gfx_SetTexturing(false); Gfx_SetTexturing(false);
Gfx_LoadMatrix(&Gfx_View); Gfx_LoadMatrix(MATRIX_VIEW, &Gfx_View);
Gfx_SetDepthWrite(true); Gfx_SetDepthWrite(true);
} }

View File

@ -163,9 +163,7 @@ void Game_UpdateProjection(void) {
Game_DefaultFov = Options_GetInt(OPT_FIELD_OF_VIEW, 1, 150, 70); Game_DefaultFov = Options_GetInt(OPT_FIELD_OF_VIEW, 1, 150, 70);
Camera_Active->GetProjection(&Gfx_Projection); Camera_Active->GetProjection(&Gfx_Projection);
Gfx_SetMatrixMode(MATRIX_TYPE_PROJECTION); Gfx_LoadMatrix(MATRIX_PROJECTION, &Gfx_Projection);
Gfx_LoadMatrix(&Gfx_Projection);
Gfx_SetMatrixMode(MATRIX_TYPE_VIEW);
Event_RaiseVoid(&GfxEvents_ProjectionChanged); Event_RaiseVoid(&GfxEvents_ProjectionChanged);
} }
@ -569,9 +567,8 @@ static void Game_LimitFPS(uint64_t frameStart) {
} }
static void Game_UpdateViewMatrix(void) { static void Game_UpdateViewMatrix(void) {
Gfx_SetMatrixMode(MATRIX_TYPE_VIEW);
Camera_Active->GetView(&Gfx_View); Camera_Active->GetView(&Gfx_View);
Gfx_LoadMatrix(&Gfx_View); Gfx_LoadMatrix(MATRIX_VIEW, &Gfx_View);
FrustumCulling_CalcFrustumEquations(&Gfx_Projection, &Gfx_View); FrustumCulling_CalcFrustumEquations(&Gfx_Projection, &Gfx_View);
} }

View File

@ -132,10 +132,8 @@ void Gfx_Mode2D(int width, int height) {
struct Matrix ortho; struct Matrix ortho;
Gfx_CalcOrthoMatrix((float)width, (float)height, &ortho); Gfx_CalcOrthoMatrix((float)width, (float)height, &ortho);
Gfx_SetMatrixMode(MATRIX_TYPE_PROJECTION); Gfx_LoadMatrix(MATRIX_PROJECTION, &ortho);
Gfx_LoadMatrix(&ortho); Gfx_LoadIdentityMatrix(MATRIX_VIEW);
Gfx_SetMatrixMode(MATRIX_TYPE_VIEW);
Gfx_LoadIdentityMatrix();
Gfx_SetDepthTest(false); Gfx_SetDepthTest(false);
Gfx_SetAlphaBlending(true); Gfx_SetAlphaBlending(true);
@ -144,10 +142,8 @@ void Gfx_Mode2D(int width, int height) {
} }
void Gfx_Mode3D(void) { void Gfx_Mode3D(void) {
Gfx_SetMatrixMode(MATRIX_TYPE_PROJECTION); Gfx_LoadMatrix(MATRIX_PROJECTION, &Gfx_Projection);
Gfx_LoadMatrix(&Gfx_Projection); Gfx_LoadMatrix(MATRIX_VIEW, &Gfx_View);
Gfx_SetMatrixMode(MATRIX_TYPE_VIEW);
Gfx_LoadMatrix(&Gfx_View);
Gfx_SetDepthTest(true); Gfx_SetDepthTest(true);
Gfx_SetAlphaBlending(false); Gfx_SetAlphaBlending(false);
@ -274,7 +270,6 @@ static DWORD d3d9_formatMappings[2] = { D3DFVF_XYZ | D3DFVF_DIFFUSE, D3DFVF_XYZ
static IDirect3D9* d3d; static IDirect3D9* d3d;
static IDirect3DDevice9* device; static IDirect3DDevice9* device;
static D3DTRANSFORMSTATETYPE curMatrix;
static DWORD createFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING; static DWORD createFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
static D3DFORMAT d3d9_viewFormat, d3d9_depthFormat; static D3DFORMAT d3d9_viewFormat, d3d9_depthFormat;
@ -815,32 +810,28 @@ void Gfx_DrawIndexedVb_TrisT2fC4b(int verticesCount, int startVertex) {
/*########################################################################################################################* /*########################################################################################################################*
*---------------------------------------------------------Matrices--------------------------------------------------------* *---------------------------------------------------------Matrices--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void Gfx_SetMatrixMode(MatrixType type) { static D3DTRANSFORMSTATETYPE matrix_modes[3] = { D3DTS_PROJECTION, D3DTS_VIEW, D3DTS_TEXTURE0 };
if (type == MATRIX_TYPE_PROJECTION) { curMatrix = D3DTS_PROJECTION; }
else if (type == MATRIX_TYPE_VIEW) { curMatrix = D3DTS_VIEW; }
else if (type == MATRIX_TYPE_TEXTURE) { curMatrix = D3DTS_TEXTURE0; }
}
void Gfx_LoadMatrix(struct Matrix* matrix) { void Gfx_LoadMatrix(MatrixType type, struct Matrix* matrix) {
ReturnCode res; ReturnCode res;
if (curMatrix == D3DTS_TEXTURE0) { if (type == MATRIX_TEXTURE) {
matrix->Row2.X = matrix->Row3.X; /* NOTE: this hack fixes the texture movements. */ matrix->Row2.X = matrix->Row3.X; /* NOTE: this hack fixes the texture movements. */
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2); IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
} }
if (Gfx_LostContext) return; if (Gfx_LostContext) return;
res = IDirect3DDevice9_SetTransform(device, curMatrix, matrix); res = IDirect3DDevice9_SetTransform(device, matrix_modes[type], matrix);
if (res) ErrorHandler_Fail2(res, "D3D9_LoadMatrix"); if (res) ErrorHandler_Fail2(res, "D3D9_LoadMatrix");
} }
void Gfx_LoadIdentityMatrix(void) { void Gfx_LoadIdentityMatrix(MatrixType type) {
ReturnCode res; ReturnCode res;
if (curMatrix == D3DTS_TEXTURE0) { if (type == MATRIX_TEXTURE) {
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
} }
if (Gfx_LostContext) return; if (Gfx_LostContext) return;
res = IDirect3DDevice9_SetTransform(device, curMatrix, &Matrix_Identity); res = IDirect3DDevice9_SetTransform(device, matrix_modes[type], &Matrix_Identity);
if (res) ErrorHandler_Fail2(res, "D3D9_LoadIdentityMatrix"); if (res) ErrorHandler_Fail2(res, "D3D9_LoadIdentityMatrix");
} }
@ -1462,18 +1453,18 @@ void Gfx_DrawIndexedVb_TrisT2fC4b(int verticesCount, int startVertex) {
/*########################################################################################################################* /*########################################################################################################################*
*---------------------------------------------------------Matrices--------------------------------------------------------* *---------------------------------------------------------Matrices--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static int gl_lastMatrixType; static GLenum matrix_modes[3] = { GL_PROJECTION, GL_MODELVIEW, GL_TEXTURE };
static int lastMatrix;
void Gfx_SetMatrixMode(MatrixType type) { void Gfx_LoadMatrix(MatrixType type, struct Matrix* matrix) {
static GLenum modes[3] = { GL_PROJECTION, GL_MODELVIEW, GL_TEXTURE }; if (type != lastMatrix) { lastMatrix = type; glMatrixMode(matrix_modes[type]); }
if (type == gl_lastMatrixType) return; glLoadMatrixf((float*)matrix);
gl_lastMatrixType = type;
glMatrixMode(modes[type]);
} }
void Gfx_LoadMatrix(struct Matrix* matrix) { glLoadMatrixf((float*)matrix); } void Gfx_LoadIdentityMatrix(MatrixType type) {
void Gfx_LoadIdentityMatrix(void) { glLoadIdentity(); } if (type != lastMatrix) { lastMatrix = type; glMatrixMode(matrix_modes[type]); }
glLoadIdentity();
}
void Gfx_CalcOrthoMatrix(float width, float height, struct Matrix* matrix) { void Gfx_CalcOrthoMatrix(float width, float height, struct Matrix* matrix) {
Matrix_OrthographicOffCenter(matrix, 0.0f, width, height, 0.0f, -10000.0f, 10000.0f); Matrix_OrthographicOffCenter(matrix, 0.0f, width, height, 0.0f, -10000.0f, 10000.0f);

View File

@ -28,7 +28,7 @@ typedef enum FogFunc_ {
FOG_LINEAR, FOG_EXP, FOG_EXP2 FOG_LINEAR, FOG_EXP, FOG_EXP2
} FogFunc; } FogFunc;
typedef enum MatrixType_ { typedef enum MatrixType_ {
MATRIX_TYPE_PROJECTION, MATRIX_TYPE_VIEW, MATRIX_TYPE_TEXTURE MATRIX_PROJECTION, MATRIX_VIEW, MATRIX_TEXTURE
} MatrixType; } MatrixType;
void Gfx_Init(void); void Gfx_Init(void);
@ -139,12 +139,10 @@ void Gfx_DrawVb_IndexedTris(int verticesCount);
/* Special case Gfx_DrawVb_IndexedTris_Range for map renderer */ /* Special case Gfx_DrawVb_IndexedTris_Range for map renderer */
void Gfx_DrawIndexedVb_TrisT2fC4b(int verticesCount, int startVertex); void Gfx_DrawIndexedVb_TrisT2fC4b(int verticesCount, int startVertex);
/* Sets the currently active matrix. */
void Gfx_SetMatrixMode(MatrixType type);
/* Loads the given matrix over the currently active matrix. */ /* Loads the given matrix over the currently active matrix. */
void Gfx_LoadMatrix(struct Matrix* matrix); void Gfx_LoadMatrix(MatrixType type, struct Matrix* matrix);
/* Loads the identity matrix over the currently active matrix. */ /* Loads the identity matrix over the currently active matrix. */
void Gfx_LoadIdentityMatrix(void); void Gfx_LoadIdentityMatrix(MatrixType type);
/* Calculates an orthographic matrix suitable with this backend. (usually for 2D) */ /* Calculates an orthographic matrix suitable with this backend. (usually for 2D) */
void Gfx_CalcOrthoMatrix(float width, float height, struct Matrix* matrix); void Gfx_CalcOrthoMatrix(float width, float height, struct Matrix* matrix);
/* Calculates a projection matrix suitable with this backend. (usually for 3D) */ /* Calculates a projection matrix suitable with this backend. (usually for 3D) */

View File

@ -216,9 +216,7 @@ void HeldBlockRenderer_Render(double delta) {
held_block = Inventory_SelectedBlock; held_block = Inventory_SelectedBlock;
view = Gfx_View; view = Gfx_View;
Gfx_SetMatrixMode(MATRIX_TYPE_PROJECTION); Gfx_LoadMatrix(MATRIX_PROJECTION, &held_blockProjection);
Gfx_LoadMatrix(&held_blockProjection);
Gfx_SetMatrixMode(MATRIX_TYPE_VIEW);
HeldBlockRenderer_SetMatrix(); HeldBlockRenderer_SetMatrix();
HeldBlockRenderer_ResetHeldState(); HeldBlockRenderer_ResetHeldState();
@ -227,9 +225,7 @@ void HeldBlockRenderer_Render(double delta) {
if (!Camera_Active->IsThirdPerson) HeldBlockRenderer_RenderModel(); if (!Camera_Active->IsThirdPerson) HeldBlockRenderer_RenderModel();
Gfx_View = view; Gfx_View = view;
Gfx_SetMatrixMode(MATRIX_TYPE_PROJECTION); Gfx_LoadMatrix(MATRIX_PROJECTION, &Gfx_Projection);
Gfx_LoadMatrix(&Gfx_Projection);
Gfx_SetMatrixMode(MATRIX_TYPE_VIEW);
} }
struct EntityVTABLE heldEntity_VTABLE = { struct EntityVTABLE heldEntity_VTABLE = {

View File

@ -134,7 +134,7 @@ void IsometricDrawer_BeginBatch(VertexP3fT2fC4b* vertices, GfxResourceID vb) {
iso_vertices_base = vertices; iso_vertices_base = vertices;
iso_vb = vb; iso_vb = vb;
Gfx_LoadMatrix(&iso_transform); Gfx_LoadMatrix(MATRIX_VIEW, &iso_transform);
} }
void IsometricDrawer_DrawBatch(BlockID block, float size, float x, float y) { void IsometricDrawer_DrawBatch(BlockID block, float size, float x, float y) {
@ -191,5 +191,5 @@ void IsometricDrawer_EndBatch(void) {
} }
iso_lastTexIndex = -1; iso_lastTexIndex = -1;
Gfx_LoadIdentityMatrix(); Gfx_LoadIdentityMatrix(MATRIX_VIEW);
} }

View File

@ -112,9 +112,9 @@ void Model_Render(struct Model* model, struct Entity* entity) {
model->GetTransform(entity, pos, &entity->Transform); model->GetTransform(entity, pos, &entity->Transform);
Matrix_Mul(&m, &entity->Transform, &Gfx_View); Matrix_Mul(&m, &entity->Transform, &Gfx_View);
Gfx_LoadMatrix(&m); Gfx_LoadMatrix(MATRIX_VIEW, &m);
model->DrawModel(entity); model->DrawModel(entity);
Gfx_LoadMatrix(&Gfx_View); Gfx_LoadMatrix(MATRIX_VIEW, &Gfx_View);
} }
void Model_SetupState(struct Model* model, struct Entity* entity) { void Model_SetupState(struct Model* model, struct Entity* entity) {
@ -270,11 +270,11 @@ void Model_RenderArm(struct Model* model, struct Entity* entity) {
Matrix_Mul(&m, &m, &Gfx_View); Matrix_Mul(&m, &m, &Gfx_View);
Matrix_Mul(&m, &translate, &m); Matrix_Mul(&m, &translate, &m);
Gfx_LoadMatrix(&m); Gfx_LoadMatrix(MATRIX_VIEW, &m);
Model_Rotation = ROTATE_ORDER_YZX; Model_Rotation = ROTATE_ORDER_YZX;
model->DrawArm(entity); model->DrawArm(entity);
Model_Rotation = ROTATE_ORDER_ZYX; Model_Rotation = ROTATE_ORDER_ZYX;
Gfx_LoadMatrix(&Gfx_View); Gfx_LoadMatrix(MATRIX_VIEW, &Gfx_View);
} }
void Model_DrawArmPart(struct ModelPart* part) { void Model_DrawArmPart(struct ModelPart* part) {