diff --git a/src/console/Console.cpp b/src/console/Console.cpp new file mode 100644 index 0000000..1609c64 --- /dev/null +++ b/src/console/Console.cpp @@ -0,0 +1,29 @@ +#include "console/Console.hpp" + +static int32_t s_active; +static int32_t s_consoleAccessEnabled; +static KEY s_consoleKey = KEY_TILDE; + +int32_t ConsoleAccessGetEnabled() { + return s_consoleAccessEnabled; +} + +void ConsoleAccessSetEnabled(int32_t enable) { + s_consoleAccessEnabled = enable; +} + +int32_t ConsoleGetActive() { + return s_active; +} + +KEY ConsoleGetHotKey() { + return s_consoleKey; +} + +void ConsoleSetActive(int32_t active) { + s_active = active; +} + +void ConsoleSetHotKey(KEY hotkey) { + s_consoleKey = hotkey; +} diff --git a/src/console/Console.hpp b/src/console/Console.hpp new file mode 100644 index 0000000..e9bfac8 --- /dev/null +++ b/src/console/Console.hpp @@ -0,0 +1,19 @@ +#ifndef CONSOLE_CONSOLE_HPP +#define CONSOLE_CONSOLE_HPP + +#include "event/Types.hpp" +#include + +int32_t ConsoleAccessGetEnabled(); + +void ConsoleAccessSetEnabled(int32_t enable); + +int32_t ConsoleGetActive(); + +KEY ConsoleGetHotKey(); + +void ConsoleSetActive(int32_t active); + +void ConsoleSetHotKey(KEY hotkey); + +#endif // ifndef CONSOLE_CONSOLE_HPP diff --git a/src/console/Device.cpp b/src/console/Device.cpp index 7d59bc3..ebb169a 100644 --- a/src/console/Device.cpp +++ b/src/console/Device.cpp @@ -1,5 +1,6 @@ #include "console/Device.hpp" #include "client/Gui.hpp" +#include "console/Console.hpp" #include "console/CVar.hpp" #include "event/Input.hpp" #include "gx/Device.hpp" @@ -147,6 +148,9 @@ void ConsoleDeviceInitialize(const char* title) { // TODO proper logic s_hwDetect = true; + // TODO ConsoleAccessSetEnabled(CmdLineGetBool(35)); + ConsoleAccessSetEnabled(1); + // TODO RegisterGxCVars(); diff --git a/src/console/Handlers.cpp b/src/console/Handlers.cpp new file mode 100644 index 0000000..4f5a3bc --- /dev/null +++ b/src/console/Handlers.cpp @@ -0,0 +1,58 @@ +#include "console/Handlers.hpp" +#include "event/Event.hpp" +#include + +namespace { + +int32_t OnChar(const EVENT_DATA_CHAR* data, void* param) { + // TODO + return 1; +} + +int32_t OnIdle(const EVENT_DATA_IDLE* data, void* param) { + // TODO + return 1; +} + +int32_t OnKeyDown(const EVENT_DATA_KEY* data, void* param) { + // TODO + return 1; +} + +int32_t OnKeyDownRepeat(const EVENT_DATA_KEY* data, void* param) { + // TODO + return 1; +} + +int32_t OnKeyUp(const EVENT_DATA_KEY* data, void* param) { + // TODO + return 1; +} + +int32_t OnMouseDown(const EVENT_DATA_MOUSE* data, void* param) { + // TODO + return 1; +} + +int32_t OnMouseMove(const EVENT_DATA_MOUSE* data, void* param) { + // TODO + return 1; +} + +int32_t OnMouseUp(const EVENT_DATA_MOUSE* data, void* param) { + // TODO + return 1; +} + +} + +void RegisterHandlers() { + EventRegisterEx(EVENT_ID_CHAR, reinterpret_cast(OnChar), nullptr, 7.0f); + EventRegisterEx(EVENT_ID_IDLE, reinterpret_cast(OnIdle), nullptr, 7.0f); + EventRegisterEx(EVENT_ID_KEYDOWN, reinterpret_cast(OnKeyDown), nullptr, 7.0f); + EventRegisterEx(EVENT_ID_KEYUP, reinterpret_cast(OnKeyUp), nullptr, 7.0f); + EventRegisterEx(EVENT_ID_KEYDOWN_REPEATING, reinterpret_cast(OnKeyDownRepeat), nullptr, 7.0f); + EventRegisterEx(EVENT_ID_MOUSEDOWN, reinterpret_cast(OnMouseDown), nullptr, 7.0f); + EventRegisterEx(EVENT_ID_MOUSEUP, reinterpret_cast(OnMouseUp), nullptr, 7.0f); + EventRegisterEx(EVENT_ID_MOUSEMOVE, reinterpret_cast(OnMouseMove), nullptr, 7.0f); +} diff --git a/src/console/Handlers.hpp b/src/console/Handlers.hpp new file mode 100644 index 0000000..092cec4 --- /dev/null +++ b/src/console/Handlers.hpp @@ -0,0 +1,6 @@ +#ifndef CONSOLE_HANDLERS_HPP +#define CONSOLE_HANDLERS_HPP + +void RegisterHandlers(); + +#endif diff --git a/src/console/Screen.cpp b/src/console/Screen.cpp index dd93622..2e2844c 100644 --- a/src/console/Screen.cpp +++ b/src/console/Screen.cpp @@ -1,7 +1,13 @@ #include "console/Screen.hpp" +#include "console/Handlers.hpp" +#include "console/Types.hpp" +#include "gx/Buffer.hpp" #include "gx/Coordinate.hpp" +#include "gx/Device.hpp" +#include "gx/Draw.hpp" #include "gx/Font.hpp" #include "gx/Gx.hpp" +#include "gx/RenderState.hpp" #include "gx/Screen.hpp" #include #include @@ -11,23 +17,72 @@ static float s_caretpixwidth; static float s_caretpixheight; static float s_fontHeight = 0.02f; static char s_fontName[STORM_MAX_PATH]; +static int32_t s_highlightState; static HLAYER s_layerBackground; static HLAYER s_layerText; static RECTF s_rect = { 0.0f, 1.0f, 1.0f, 1.0f }; static HTEXTFONT s_textFont; -void PaintBackground(void* param, const RECTF* rect, const RECTF* visible, float elapsedSec) { +static CImVector s_colorArray[] = { + { 0xFF, 0xFF, 0xFF, 0xFF }, // DEFAULT_COLOR + { 0xFF, 0xFF, 0xFF, 0xFF }, // INPUT_COLOR + { 0x80, 0x80, 0x80, 0xFF }, // ECHO_COLOR + { 0x00, 0x00, 0xFF, 0xFF }, // ERROR_COLOR + { 0x00, 0xFF, 0xFF, 0xFF }, // WARNING_COLOR + { 0xFF, 0xFF, 0xFF, 0xFF }, // GLOBAL_COLOR + { 0xFF, 0xFF, 0xFF, 0xFF }, // ADMIN_COLOR + { 0xFF, 0xFF, 0xFF, 0x80 }, // HIGHLIGHT_COLOR + { 0x00, 0x00, 0x00, 0xC0 }, // BACKGROUND_COLOR +}; + +void DrawBackground() { + uint16_t indices[] = { + 0, 1, 2, 3 + }; + + C3Vector position[] = { + { s_rect.left, s_rect.bottom, 0.0f }, + { s_rect.right, s_rect.bottom, 0.0f }, + { s_rect.left, s_rect.top, 0.0f }, + { s_rect.right, s_rect.top, 0.0f } + }; + + GxRsPush(); + + GxRsSet(GxRs_Lighting, 0); + GxRsSet(GxRs_Fog, 0); + GxRsSet(GxRs_DepthTest, 0); + GxRsSet(GxRs_DepthWrite, 0); + GxRsSet(GxRs_Culling, 0); + GxRsSet(GxRs_PolygonOffset, 0.0f); + GxRsSet(GxRs_BlendingMode, GxBlend_Alpha); + GxRsSet(GxRs_AlphaRef, CGxDevice::s_alphaRef[GxBlend_Alpha]); + + GxPrimLockVertexPtrs(4, position, sizeof(C3Vector), nullptr, 0, &s_colorArray[BACKGROUND_COLOR], 0, nullptr, 0, nullptr, 0, nullptr, 0); + GxDrawLockedElements(GxPrim_TriangleStrip, 4, indices); + GxPrimUnlockVertexPtrs(); + + GxRsPop(); +} + +void DrawHighLight() { // TODO } +void PaintBackground(void* param, const RECTF* rect, const RECTF* visible, float elapsedSec) { + if (s_rect.bottom < 1.0f) { + DrawBackground(); + + if (s_highlightState) { + DrawHighLight(); + } + } +} + void PaintText(void* param, const RECTF* rect, const RECTF* visible, float elapsedSec) { // TODO } -void RegisterHandlers() { - // TODO -} - void ConsoleScreenInitialize(const char* title) { CRect windowSize; GxCapsWindowSize(windowSize); diff --git a/src/console/Types.hpp b/src/console/Types.hpp new file mode 100644 index 0000000..0099be2 --- /dev/null +++ b/src/console/Types.hpp @@ -0,0 +1,17 @@ +#ifndef CONSOLE_TYPES_HPP +#define CONSOLE_TYPES_HPP + +enum COLOR_T { + DEFAULT_COLOR, + INPUT_COLOR, + ECHO_COLOR, + ERROR_COLOR, + WARNING_COLOR, + GLOBAL_COLOR, + ADMIN_COLOR, + HIGHLIGHT_COLOR, + BACKGROUND_COLOR, + NUM_COLORTYPES, +}; + +#endif diff --git a/src/gx/CGxDevice.cpp b/src/gx/CGxDevice.cpp index 1a354b7..16387a9 100644 --- a/src/gx/CGxDevice.cpp +++ b/src/gx/CGxDevice.cpp @@ -432,7 +432,7 @@ void CGxDevice::IRsSync(int32_t force) { this->IRsForceUpdate(); } - for (int32_t i = 0; i < this->m_dirtyStates.Count(); i++) { + for (int32_t i = this->m_dirtyStates.Count() - 1; i >= 0; i--) { auto ds = this->m_dirtyStates[i]; auto rs = &this->m_appRenderStates[ds]; auto hs = &this->m_hwRenderStates[ds]; @@ -734,8 +734,11 @@ void CGxDevice::RsPop() { auto topOfStack = this->m_stackOffsets[this->m_stackOffsets.Count() - 1]; if (this->m_pushedStates.Count() > topOfStack) { - for (int32_t i = this->m_pushedStates.Count() - 1; i > topOfStack; i--) { - auto ps = &this->m_pushedStates[i]; + auto bottomOfStack = this->m_pushedStates.Count() - 1; + auto stackSize = this->m_pushedStates.Count() - topOfStack; + + for (uint32_t stackOffset = 0; stackOffset < stackSize; stackOffset++) { + auto ps = &this->m_pushedStates[bottomOfStack - stackOffset]; auto rs = &this->m_appRenderStates[ps->m_which]; if (!rs->m_dirty) { @@ -1049,8 +1052,7 @@ void CGxDevice::XformSetViewport(float minX, float maxX, float minY, float maxY, return; } - // TODO - // this->unk4[4] = 1; + this->intF6C = 1; this->m_viewport.x.l = minX; this->m_viewport.x.h = maxX; diff --git a/src/gx/CGxMatrixStack.cpp b/src/gx/CGxMatrixStack.cpp index 804bf1e..1efb83b 100644 --- a/src/gx/CGxMatrixStack.cpp +++ b/src/gx/CGxMatrixStack.cpp @@ -1,7 +1,7 @@ #include "gx/CGxMatrixStack.hpp" CGxMatrixStack::CGxMatrixStack() { - this->m_flags[0] = 0x1; + this->m_flags[0] = F_Identity; } void CGxMatrixStack::Pop() { @@ -25,6 +25,10 @@ void CGxMatrixStack::Push() { C44Matrix& CGxMatrixStack::Top() { this->m_dirty = 1; - this->m_flags[this->m_level] &= 0xFFFFFFFE; + this->m_flags[this->m_level] &= ~F_Identity; + return this->m_mtx[this->m_level]; +} + +const C44Matrix& CGxMatrixStack::TopConst() { return this->m_mtx[this->m_level]; } diff --git a/src/gx/CGxMatrixStack.hpp b/src/gx/CGxMatrixStack.hpp index a0eef62..fd54109 100644 --- a/src/gx/CGxMatrixStack.hpp +++ b/src/gx/CGxMatrixStack.hpp @@ -6,6 +6,11 @@ class CGxMatrixStack { public: + // Types + enum EMatrixFlags { + F_Identity = 0x1, + }; + // Member variables uint32_t m_level = 0; int8_t m_dirty = 0; @@ -14,9 +19,10 @@ class CGxMatrixStack { // Member functions CGxMatrixStack(); - void Pop(void); - void Push(void); - C44Matrix& Top(void); + void Pop(); + void Push(); + C44Matrix& Top(); + const C44Matrix& TopConst(); }; #endif diff --git a/src/gx/CGxStateBom.cpp b/src/gx/CGxStateBom.cpp index fc9d4cf..0527190 100644 --- a/src/gx/CGxStateBom.cpp +++ b/src/gx/CGxStateBom.cpp @@ -33,20 +33,42 @@ const CGxStateBom& CGxStateBom::operator=(C3Vector& value) { return *this; } -bool CGxStateBom::operator!=(int32_t value) { - return this->m_data.i[0] != value; +bool CGxStateBom::operator==(float value) { + return this->m_data.f[0] == value; } -bool CGxStateBom::operator!=(uint32_t value) { - return this->m_data.i[0] != value; +bool CGxStateBom::operator==(int32_t value) { + return this->m_data.i[0] == value; +} + +bool CGxStateBom::operator==(uint32_t value) { + return this->m_data.u[0] == value; +} + +bool CGxStateBom::operator==(void* value) { + return this->m_data.p == value; +} + +bool CGxStateBom::operator==(C3Vector& value) { + return this->m_data.f[0] == value.x + || this->m_data.f[1] == value.y + || this->m_data.f[2] == value.z; } bool CGxStateBom::operator!=(float value) { - return this->m_data.f[0] != value; + return !(*this == value); +} + +bool CGxStateBom::operator!=(int32_t value) { + return !(*this == value); +} + +bool CGxStateBom::operator!=(uint32_t value) { + return !(*this == value); } bool CGxStateBom::operator!=(void* value) { - return this->m_data.p != value; + return !(*this == value); } bool CGxStateBom::operator!=(C3Vector& value) { diff --git a/src/gx/CGxStateBom.hpp b/src/gx/CGxStateBom.hpp index bff7163..4a3d749 100644 --- a/src/gx/CGxStateBom.hpp +++ b/src/gx/CGxStateBom.hpp @@ -11,6 +11,7 @@ class CGxStateBom { // Member variables union { int32_t i[3]; + uint32_t u[3]; float f[3]; void* p; } m_data; @@ -23,6 +24,11 @@ class CGxStateBom { const CGxStateBom& operator=(uint32_t); const CGxStateBom& operator=(void*); const CGxStateBom& operator=(C3Vector&); + bool operator==(float); + bool operator==(int32_t); + bool operator==(uint32_t); + bool operator==(void*); + bool operator==(C3Vector&); bool operator!=(float); bool operator!=(int32_t); bool operator!=(uint32_t); diff --git a/src/gx/Transform.cpp b/src/gx/Transform.cpp index ca94049..6ee7b3e 100644 --- a/src/gx/Transform.cpp +++ b/src/gx/Transform.cpp @@ -141,9 +141,9 @@ void GxuXformCreateOrtho(float minX, float maxX, float minY, float maxY, float m dst.c2 = 2.0 / v12; dst.c3 = 0.0f; - dst.d0 = 0.0f; - dst.d1 = 0.0f; - dst.d2 = 0.0f; + dst.d0 = -((minX + maxX) / v10); + dst.d1 = -((minY + maxY) / v11); + dst.d2 = -((minZ + maxZ) / v12); dst.d3 = 1.0f; } diff --git a/src/gx/d3d/CGxDeviceD3d.cpp b/src/gx/d3d/CGxDeviceD3d.cpp index d0fb4ed..58b103e 100644 --- a/src/gx/d3d/CGxDeviceD3d.cpp +++ b/src/gx/d3d/CGxDeviceD3d.cpp @@ -1580,7 +1580,11 @@ void CGxDeviceD3d::IStateSync() { this->IShaderConstantsFlush(); this->IRsSync(0); - // TODO ffp if vertex shader is disabled + if (this->m_hwRenderStates[GxRs_VertexShader] == nullptr && this->m_appRenderStates[GxRs_VertexShader].m_value == nullptr) { + this->IStateSyncLights(); + this->IStateSyncMaterial(); + this->IStateSyncXforms(); + } this->IStateSyncEnables(); @@ -1590,6 +1594,10 @@ void CGxDeviceD3d::IStateSync() { this->IStateSyncIndexPtr(); // TODO + + if (this->intF6C) { + this->IXformSetViewport(); + } } void CGxDeviceD3d::IStateSyncEnables() { @@ -1611,6 +1619,14 @@ void CGxDeviceD3d::IStateSyncIndexPtr() { } } +void CGxDeviceD3d::IStateSyncLights() { + // TODO +} + +void CGxDeviceD3d::IStateSyncMaterial() { + // TODO +} + void CGxDeviceD3d::IStateSyncVertexPtrs() { if (this->m_primVertexFormat < GxVertexBufferFormats_Last && this->m_d3dVertexDecl[this->m_primVertexFormat]) { auto d3dVertexDecl = this->m_d3dVertexDecl[this->m_primVertexFormat]; @@ -1691,6 +1707,22 @@ void CGxDeviceD3d::IStateSyncVertexPtrs() { } } +void CGxDeviceD3d::IStateSyncXforms() { + if (this->m_xforms[GxXform_Projection].m_dirty) { + this->m_d3dDevice->SetTransform(D3DTS_PROJECTION, reinterpret_cast(&this->m_projNative)); + this->m_xforms[GxXform_Projection].m_dirty = 0; + } + + if (this->m_xforms[GxXform_View].m_dirty) { + this->m_d3dDevice->SetTransform(D3DTS_VIEW, reinterpret_cast(&this->m_xforms[GxXform_View].TopConst())); + this->m_xforms[GxXform_View].m_dirty = 0; + } + + // TODO world + + // TODO tex +} + void CGxDeviceD3d::ITexCreate(CGxTex* texId) { uint32_t width, height, startLevel, endLevel; this->ITexWHDStartEnd(texId, width, height, startLevel, endLevel); @@ -1918,6 +1950,30 @@ void CGxDeviceD3d::IXformSetProjection(const C44Matrix& matrix) { memcpy(&this->m_projNative, &projNative, sizeof(this->m_projNative)); } +void CGxDeviceD3d::IXformSetViewport() { + const auto& gxViewport = this->m_viewport; + auto windowRect = this->DeviceCurWindow(); + + D3DVIEWPORT9 d3dViewport; + + d3dViewport.X = (gxViewport.x.l * windowRect.maxX) + 0.5; + d3dViewport.Y = ((1.0 - gxViewport.y.h) * windowRect.maxY) + 0.5; + + // TODO account for negative X value + + d3dViewport.Width = (gxViewport.x.h * windowRect.maxX) - d3dViewport.X + 0.5; + d3dViewport.Height = ((1.0 - gxViewport.y.l) * windowRect.maxY) - d3dViewport.Y + 0.5; + + d3dViewport.MinZ = gxViewport.z.l; + d3dViewport.MaxZ = gxViewport.z.h; + + // TODO conditionally adjust Y value + + this->m_d3dDevice->SetViewport(&d3dViewport); + + this->intF6C = 0; +} + void CGxDeviceD3d::PoolSizeSet(CGxPool* pool, uint32_t size) { // TODO } @@ -1938,7 +1994,7 @@ void CGxDeviceD3d::SceneClear(uint32_t mask, CImVector color) { } if (this->intF6C) { - // TODO + this->IXformSetViewport(); } D3DCOLOR d3dColor = color.b | (color.g | (color.r << 8) << 8); diff --git a/src/gx/d3d/CGxDeviceD3d.hpp b/src/gx/d3d/CGxDeviceD3d.hpp index 18437d9..9823083 100644 --- a/src/gx/d3d/CGxDeviceD3d.hpp +++ b/src/gx/d3d/CGxDeviceD3d.hpp @@ -294,10 +294,14 @@ class CGxDeviceD3d : public CGxDevice { void IStateSync(); void IStateSyncEnables(); void IStateSyncIndexPtr(); + void IStateSyncLights(); + void IStateSyncMaterial(); void IStateSyncVertexPtrs(); + void IStateSyncXforms(); void ITexCreate(CGxTex* texId); void ITexUpload(CGxTex* texId); void IXformSetProjection(const C44Matrix& matrix); + void IXformSetViewport(); }; #endif diff --git a/src/gx/gll/GLDevice.cpp b/src/gx/gll/GLDevice.cpp index 3a74e0c..facc73c 100644 --- a/src/gx/gll/GLDevice.cpp +++ b/src/gx/gll/GLDevice.cpp @@ -628,8 +628,59 @@ void GLDevice::ApplyGLStates(const GLStates& states, bool force) { { glMatrixMode(GL_PROJECTION); - // TODO - // - some interesting logic to manipulate the projection matrix before loading + GLTransform projection = { + true, + { + 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, + }, + true + }; + + if (!states.fixedFunc.transforms.projection.isIdentity) { + projection.isIdentity = false; + + memcpy(projection.m, states.fixedFunc.transforms.projection.m, sizeof(projection.m)); + projection.isDirty = true; + } + + if (projection.isIdentity) { + projection.SetIdentity(); + } + + projection.a1 *= -1.0f; + projection.b1 *= -1.0f; + projection.c1 *= -1.0f; + projection.d1 *= -1.0f; + + auto isIdentity = projection.a0 == 1.0f + && projection.a1 == 0.0f + && projection.a2 == 0.0f + && projection.a3 == 0.0f + && projection.b0 == 0.0f + && projection.b1 == 1.0f + && projection.b2 == 0.0f + && projection.b3 == 0.0f + && projection.c0 == 0.0f + && projection.c1 == 0.0f + && projection.c2 == 1.0f + && projection.c3 == 0.0f + && projection.d0 == 0.0f + && projection.d1 == 0.0f + && projection.d2 == 0.0f + && projection.d3 == 1.0f; + + projection.isDirty = true; + + if (isIdentity) { + glLoadIdentity(); + } else { + glLoadMatrixf(projection.m); + } + + projection.isDirty = false; } glMatrixMode(states.fixedFunc.transforms.matrixMode); @@ -892,7 +943,53 @@ void GLDevice::ApplyShaderConstants() { } void GLDevice::ApplyTransforms() { - // TODO + this->SetModelView(GL_MODELVIEW); + + auto& projection = this->m_States.fixedFunc.transforms.projection; + if (projection.isDirty) { + if (projection.isIdentity) { + projection.SetIdentity(); + } + + projection.a1 *= -1.0f; + projection.b1 *= -1.0f; + projection.c1 *= -1.0f; + projection.d1 *= -1.0f; + + projection.isIdentity = projection.a0 == 1.0f + && projection.a1 == 0.0f + && projection.a2 == 0.0f + && projection.a3 == 0.0f + && projection.b0 == 0.0f + && projection.b1 == 1.0f + && projection.b2 == 0.0f + && projection.b3 == 0.0f + && projection.c0 == 0.0f + && projection.c1 == 0.0f + && projection.c2 == 1.0f + && projection.c3 == 0.0f + && projection.d0 == 0.0f + && projection.d1 == 0.0f + && projection.d2 == 0.0f + && projection.d3 == 1.0f; + + projection.isDirty = true; + + if (this->m_States.fixedFunc.transforms.matrixMode != GL_PROJECTION) { + glMatrixMode(GL_PROJECTION); + this->m_States.fixedFunc.transforms.matrixMode = GL_PROJECTION; + } + + if (projection.isIdentity) { + glLoadIdentity(); + } else { + glLoadMatrixf(projection.m); + } + + projection.isDirty = false; + } + + // TODO texture transforms } void GLDevice::BindBuffer(GLBuffer* buffer, GLEnum target) { @@ -2103,29 +2200,29 @@ void GLDevice::SetAlphaTestEnable(bool enable) { } } -void GLDevice::SetClearColor(const GLColor4f& a2) { +void GLDevice::SetClearColor(const GLColor4f& clearColor) { if ( - this->m_States.clear.clearColor.r != a2.r - || this->m_States.clear.clearColor.g != a2.g - || this->m_States.clear.clearColor.b != a2.b - || this->m_States.clear.clearColor.a != a2.a + this->m_States.clear.clearColor.r != clearColor.r + || this->m_States.clear.clearColor.g != clearColor.g + || this->m_States.clear.clearColor.b != clearColor.b + || this->m_States.clear.clearColor.a != clearColor.a ) { - glClearColor(a2.r, a2.g, a2.b, a2.a); - this->m_States.clear.clearColor = { a2.r, a2.g, a2.b, a2.a }; + glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); + this->m_States.clear.clearColor = { clearColor.r, clearColor.g, clearColor.b, clearColor.a }; } } -void GLDevice::SetClearDepth(double depth) { - if (this->m_States.clear.clearDepth != depth) { - glClearDepth(depth); - this->m_States.clear.clearDepth = depth; +void GLDevice::SetClearDepth(double clearDepth) { + if (this->m_States.clear.clearDepth != clearDepth) { + glClearDepth(clearDepth); + this->m_States.clear.clearDepth = clearDepth; } } -void GLDevice::SetClearStencil(int32_t s) { - if (this->m_States.clear.clearStencil != s) { - glClearStencil(s); - this->m_States.clear.clearStencil = s; +void GLDevice::SetClearStencil(int32_t clearStencil) { + if (this->m_States.clear.clearStencil != clearStencil) { + glClearStencil(clearStencil); + this->m_States.clear.clearStencil = clearStencil; } } @@ -2315,6 +2412,57 @@ void GLDevice::SetLightingEnable(bool enable) { } } +void GLDevice::SetModelView(GLEnum transform) { + if (transform == 'VIEW') { + // TODO + return; + } + + if (transform == 'WRLD') { + // TODO + return; + } + + if (transform != GL_MODELVIEW) { + BLIZZARD_ASSERT(false); + } + + auto& world = this->m_States.fixedFunc.transforms.world; + auto& view = this->m_States.fixedFunc.transforms.view; + auto& modelView = this->m_States.fixedFunc.transforms.modelView; + + if (this->m_States.fixedFunc.transforms.modelviewStatus != transform || modelView.isDirty) { + if (world.isIdentity && view.isIdentity) { + modelView.isIdentity = true; + modelView.isDirty = true; + } else if (world.isIdentity) { + modelView = view; + modelView.isIdentity = false; + modelView.isDirty = true; + } else if (view.isIdentity) { + modelView = world; + modelView.isIdentity = false; + modelView.isDirty = true; + } else { + // TODO assign model * view to modelView + BLIZZARD_ASSERT(!"Unimplemented"); + } + + if (this->m_States.fixedFunc.transforms.matrixMode != GL_MODELVIEW) { + glMatrixMode(GL_MODELVIEW); + this->m_States.fixedFunc.transforms.matrixMode = GL_MODELVIEW; + } + + if (modelView.isIdentity) { + glLoadIdentity(); + } else { + glLoadMatrixf(modelView.m); + } + + this->m_States.fixedFunc.transforms.modelviewStatus = transform; + } +} + void GLDevice::SetScissor(bool a2, const GLRect& a3) { // TODO } @@ -2469,15 +2617,13 @@ void GLDevice::SetTransform(GLEnum transform, const float* a3) { BLIZZARD_ASSERT(false); } - // TODO - // int32_t needsUpdate = !(t == a3); // GLTransform::operator==() - // if (needsUpdate) { - // t.Set(a3); - // } + if (*t != a3) { + t->Set(a3); + } if (t->isDirty) { if (transform == 'VIEW' || transform == 'WRLD') { - this->m_States.fixedFunc.transforms.modelView.isDirty = 1; + this->m_States.fixedFunc.transforms.modelView.isDirty = true; } } } diff --git a/src/gx/gll/GLDevice.h b/src/gx/gll/GLDevice.h index df09376..d755acc 100644 --- a/src/gx/gll/GLDevice.h +++ b/src/gx/gll/GLDevice.h @@ -63,13 +63,13 @@ class GLDevice { static GLFramebuffer* m_F8330C; // Static functions - static GLDevice* Get(void); - static void Set(GLDevice*); - static void InitPools(void); - static RendererInfo GetRendererInfo(void); - static void InitRendererInfo(void); - static void SetOption(GLDeviceOption, bool); - static void StaticInit(void); + static GLDevice* Get(); + static void Set(GLDevice* device); + static void InitPools(); + static RendererInfo GetRendererInfo(); + static void InitRendererInfo(); + static void SetOption(GLDeviceOption option, bool enable); + static void StaticInit(); // Member variables std::basic_string, std::allocator> m_DebugName; @@ -121,71 +121,72 @@ class GLDevice { // Member functions GLDevice(); - void ApplyGLBindings(const GLStates&, bool); - void ApplyGLStates(const GLStates&, bool); - void ApplyShaderConstants(void); - void ApplyTransforms(void); - void BindBuffer(GLBuffer*, GLEnum); - void BindFramebuffer(GLFramebuffer*); - void BindGLSLProgram(GLGLSLProgram*); - void BindShader(GLShader*); - void BindTexture(GLEnum, GLTexture*); - void BindVertexArray(GLVertexArray*); - void BlitFramebuffer(GLMipmap*, const GLRect*, GLMipmap*, const GLRect*, GLEnum, GLEnum); - void CheckDepthTarget(void); - void Clear(uint32_t, const GLColor4f&, double, int32_t); - void CopyTex(uint32_t, uint32_t, GLMipmap*, const GLRect*); - GLBuffer* CreateBuffer(GLEnum, uint32_t, const void*, GLEnum, GLEnum); - GLShader* CreateShader(GLShader::ShaderType, const void*, int32_t, const char*); - GLTexture* CreateTexture2D(uint32_t, uint32_t, uint32_t, GLTextureFormat, uint32_t); - GLTexture* CreateTextureCubeMap(uint32_t, uint32_t, GLTextureFormat, uint32_t); - void Draw(GLEnum, uint32_t, uint32_t); - void DrawIndexed(GLEnum, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t); - void DrawRect(void); - GLFramebuffer* GetCurrentTarget(void); // invented name - uint32_t GetID(void); - GLShader* GetShader(GLShader::ShaderType); + void ApplyGLBindings(const GLStates& states, bool a3); + void ApplyGLStates(const GLStates& states, bool force); + void ApplyShaderConstants(); + void ApplyTransforms(); + void BindBuffer(GLBuffer* buffer, GLEnum target); + void BindFramebuffer(GLFramebuffer* framebuffer); + void BindGLSLProgram(GLGLSLProgram* a2); + void BindShader(GLShader* shader); + void BindTexture(GLEnum textureType, GLTexture* texture); + void BindVertexArray(GLVertexArray* a2); + void BlitFramebuffer(GLMipmap* src, const GLRect* srcRect, GLMipmap* dst, const GLRect* dstRect, GLEnum mask, GLEnum filter); + void CheckDepthTarget(); + void Clear(uint32_t clearMask, const GLColor4f& clearColor, double clearDepth, int32_t clearStencil); + void CopyTex(uint32_t a2, uint32_t a3, GLMipmap* dst, const GLRect* framebufferRect); + GLBuffer* CreateBuffer(GLEnum type, uint32_t a3, const void* a4, GLEnum usage, GLEnum format); + GLShader* CreateShader(GLShader::ShaderType type, const void* buf, int32_t codeLen, const char* name); + GLTexture* CreateTexture2D(uint32_t width, uint32_t height, uint32_t numMipMap, GLTextureFormat format, uint32_t flags); + GLTexture* CreateTextureCubeMap(uint32_t size, uint32_t numMipMap, GLTextureFormat format, uint32_t flags); + void Draw(GLEnum primitive, uint32_t a3, uint32_t a4); + void DrawIndexed(GLEnum primitive, uint32_t a3, uint32_t a4, uint32_t a5, uint32_t a6, uint32_t count); + void DrawRect(); + GLFramebuffer* GetCurrentTarget(); // invented name + uint32_t GetID(); + GLShader* GetShader(GLShader::ShaderType shaderType); const GLStates::VertexArrayObject& GetVertexArrayStates(); - void GLLDraw(GLEnum, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t); - void Init(GLAbstractWindow*, const char*, uint32_t, GLTextureFormat); - void LoadDefaultStates(void); + void GLLDraw(GLEnum mode, uint32_t start, uint32_t end, uint32_t a5, uint32_t a6, uint32_t count); + void Init(GLAbstractWindow* a2, const char* a3, uint32_t a4, GLTextureFormat a5); + void LoadDefaultStates(); void ResetBackbuffer(uint32_t width, uint32_t height, GLTextureFormat colorFormat, GLTextureFormat depthFormat, uint32_t sampleCount); void Resize(uint32_t width, uint32_t height); - void RestoreTextures(void); - void SetActiveTexture(uint32_t); - void SetAlphaBlend(GLEnum, GLEnum, GLEnum); - void SetAlphaBlendEnable(bool); - void SetAlphaTest(GLEnum, float); - void SetAlphaTestEnable(bool); - void SetClearColor(const GLColor4f&); - void SetClearDepth(double); - void SetClearStencil(int32_t); + void RestoreTextures(); + void SetActiveTexture(uint32_t a2); + void SetAlphaBlend(GLEnum srcBlend, GLEnum dstBlend, GLEnum blendOp); + void SetAlphaBlendEnable(bool enable); + void SetAlphaTest(GLEnum func, float ref); + void SetAlphaTestEnable(bool enable); + void SetClearColor(const GLColor4f& clearColor); + void SetClearDepth(double clearDepth); + void SetClearStencil(int32_t clearStencil); void SetColorWriteMask(bool red, bool green, bool blue, bool alpha, uint32_t index); - void SetCullMode(GLEnum); + void SetCullMode(GLEnum cullMode); void SetDepthBias(float constantBias, float slopeScaledBias); - void SetDepthTestEnable(bool); - void SetDepthTestFunc(GLEnum); - void SetDepthWriteMask(bool); - void SetDisplay(uint32_t, uint32_t, GLTextureFormat, GLTextureFormat, uint32_t, bool, bool, uint32_t); + void SetDepthTestEnable(bool enable); + void SetDepthTestFunc(GLEnum func); + void SetDepthWriteMask(bool enable); + void SetDisplay(uint32_t width, uint32_t height, GLTextureFormat a4, GLTextureFormat a5, uint32_t a6, bool a7, bool a8, uint32_t a9); void SetFogColor(float r, float g, float b, float a); void SetFogEnable(bool enable); void SetFogParam(GLEnum param, float value); - void SetIndexBuffer(GLBuffer*); + void SetIndexBuffer(GLBuffer* buffer); void SetLightingEnable(bool enable); - void SetScissor(bool, const GLRect&); - void SetShader(GLShader::ShaderType, GLShader*); - void SetShaderConstants(GLShader::ShaderType, uint32_t, const float*, uint32_t); - void SetShaderConstantsInternal(GLShader::ShaderType, uint32_t, const float*, uint32_t); - void SetTexture(uint32_t, GLTexture*); - void SetTransform(GLEnum, const float*); - void SetUnpackClientStorage(bool); - void SetVertexBuffer(uint32_t, GLBuffer*, uint32_t, uint32_t); - void SetVertexFormat(GLVertexFormat*); - void SetViewport(const GLRect&, double, double); + void SetModelView(GLEnum transform); + void SetScissor(bool a2, const GLRect& a3); + void SetShader(GLShader::ShaderType shaderType, GLShader* shader); + void SetShaderConstants(GLShader::ShaderType shaderType, uint32_t index, const float* constants, uint32_t count); + void SetShaderConstantsInternal(GLShader::ShaderType shaderType, uint32_t index, const float* constants, uint32_t count); + void SetTexture(uint32_t stage, GLTexture* texture); + void SetTransform(GLEnum transform, const float* a3); + void SetUnpackClientStorage(bool enable); + void SetVertexBuffer(uint32_t index, GLBuffer* buffer, uint32_t offset, uint32_t stride); + void SetVertexFormat(GLVertexFormat* format); + void SetViewport(const GLRect& viewport, double zNear, double zFar); void Sub34BB0(GLEnum a2, GLMipmap* a3, uint32_t index); - void Sub38460(bool); - void Swap(void); - void UpdateFFPTexturing(void); + void Sub38460(bool a2); + void Swap(); + void UpdateFFPTexturing(); }; #endif diff --git a/src/gx/gll/GLTypes.cpp b/src/gx/gll/GLTypes.cpp index 6875344..226ae4f 100644 --- a/src/gx/gll/GLTypes.cpp +++ b/src/gx/gll/GLTypes.cpp @@ -1,5 +1,58 @@ #include "gx/gll/GLTypes.h" +#include GLColor4f GLColor4f::ZERO = { 0.0, 0.0, 0.0, 0.0 }; GLColor4f GLColor4f::WHITE = { 1.0, 1.0, 1.0, 1.0 }; GLColor4f GLColor4f::BLACK = { 0.0, 0.0, 0.0, 1.0 }; + +bool GLTransform::operator==(const float m[16]) const { + return this->m[0] == m[0] + && this->m[1] == m[1] + && this->m[2] == m[2] + && this->m[3] == m[3] + && this->m[4] == m[4] + && this->m[5] == m[5] + && this->m[6] == m[6] + && this->m[7] == m[7] + && this->m[8] == m[8] + && this->m[9] == m[9] + && this->m[10] == m[10] + && this->m[11] == m[11] + && this->m[12] == m[12] + && this->m[13] == m[13] + && this->m[14] == m[14] + && this->m[15] == m[15]; +} + +bool GLTransform::operator!=(const float m[16]) const { + return !(*this == m); +} + +void GLTransform::Set(const float m[16]) { + memcpy(this->m, m, sizeof(this->m)); + this->isDirty = true; + this->isIdentity = this->a0 == 1.0f + && this->a1 == 0.0f + && this->a2 == 0.0f + && this->a3 == 0.0f + && this->b0 == 0.0f + && this->b1 == 1.0f + && this->b2 == 0.0f + && this->b3 == 0.0f + && this->c0 == 0.0f + && this->c1 == 0.0f + && this->c2 == 1.0f + && this->c3 == 0.0f + && this->d0 == 0.0f + && this->d1 == 0.0f + && this->d2 == 0.0f + && this->d3 == 1.0f; +} + +void GLTransform::SetIdentity() { + memset(this->m, 0, sizeof(this->m)); + this->a0 = 1.0f; + this->b1 = 1.0f; + this->c2 = 1.0f; + this->d3 = 1.0f; +} diff --git a/src/gx/gll/GLTypes.h b/src/gx/gll/GLTypes.h index 6b01706..9c147fb 100644 --- a/src/gx/gll/GLTypes.h +++ b/src/gx/gll/GLTypes.h @@ -138,6 +138,11 @@ struct GLTransform { }; bool isIdentity; + + bool operator==(const float m[16]) const; + bool operator!=(const float m[16]) const; + void Set(const float m[16]); + void SetIdentity(); }; struct GLStates { diff --git a/src/gx/gll/GLVertexArray.cpp b/src/gx/gll/GLVertexArray.cpp index e4cc825..d3af1b0 100644 --- a/src/gx/gll/GLVertexArray.cpp +++ b/src/gx/gll/GLVertexArray.cpp @@ -124,7 +124,7 @@ void GLVertexArray::ApplyVertexFormat(GLDevice* device) { auto vertexBuffer = this->GetProperties().m_VertexBuffer[attrib.stream]; - if (useVertexShader || attrib.slot - 1 > 1) { + if (useVertexShader || static_cast(attrib.slot - 1) > 1) { if (this->m_GLStates.buffers[0] != vertexBuffer->m_BufferID) { glBindBuffer(vertexBuffer->m_Type, vertexBuffer->m_BufferID); this->m_GLStates.buffers[0] = vertexBuffer->m_BufferID; @@ -147,38 +147,164 @@ void GLVertexArray::ApplyVertexFormat(GLDevice* device) { reinterpret_cast(offset) ); } else { - // TODO + switch (attrib.slot) { + case 0: { + glVertexPointer( + k_VertexTypeInfo[attrib.type].m_Size, + k_VertexTypeInfo[attrib.type].m_Type, + stride, + reinterpret_cast(offset) + ); + + break; + } + + case 3: { + glNormalPointer( + k_VertexTypeInfo[attrib.type].m_Type, + stride, + reinterpret_cast(offset) + ); + + break; + } + + case 4: { + glColorPointer( + k_VertexTypeInfo[attrib.type].m_Size, + k_VertexTypeInfo[attrib.type].m_Type, + stride, + reinterpret_cast(offset) + ); + + break; + } + + case 5: { + glSecondaryColorPointer( + k_VertexTypeInfo[attrib.type].m_Size, + k_VertexTypeInfo[attrib.type].m_Type, + stride, + reinterpret_cast(offset) + ); + + break; + } + + case 6: + case 7: + case 8: + case 9: + case 10: + case 11: + case 12: + case 13: { + auto tmu = attrib.slot - 6; + auto texCoordIndex = device->m_States.fixedFunc.texCoordIndex[tmu]; + glClientActiveTextureARB(GL_TEXTURE0 + texCoordIndex); + glTexCoordPointer( + k_VertexTypeInfo[attrib.type].m_Size, + k_VertexTypeInfo[attrib.type].m_Type, + stride, + reinterpret_cast(offset) + ); + + break; + } + } } } } for (int32_t s = 0; s < 16; s++) { - bool* prevAttribEnable; - + // Shader if (useVertexShader) { - prevAttribEnable = &this->m_GLStates.vertexAttribs[s].enable; - } else { - // TODO - } + auto prevAttribEnable = &this->m_GLStates.vertexAttribs[s].enable; - if (*prevAttribEnable != attribEnable[s]) { - if (attribEnable[s]) { - if (useVertexShader) { - glEnableVertexAttribArrayARB(s); + if (*prevAttribEnable != attribEnable[s]) { + if (attribEnable[s]) { + glEnableVertexAttribArrayARB(s); } else { - // TODO - } - } else { - if (useVertexShader) { glDisableVertexAttribArrayARB(s); - } else { - // TODO } } *prevAttribEnable = attribEnable[s]; + + // FFP + } else { + bool* prevAttribEnable = nullptr; + GLenum glArray = GL_NONE; + + switch (s) { + case 0: { + prevAttribEnable = &this->m_GLStates.position.enable; + glArray = GL_VERTEX_ARRAY; + + break; + } + + case 3: { + prevAttribEnable = &this->m_GLStates.normal.enable; + glArray = GL_NORMAL_ARRAY; + + break; + } + + case 4: { + prevAttribEnable = &this->m_GLStates.color0.enable; + glArray = GL_COLOR_ARRAY; + + break; + } + + case 5: { + prevAttribEnable = &this->m_GLStates.color1.enable; + glArray = GL_SECONDARY_COLOR_ARRAY; + + break; + } + + case 6: + case 7: + case 8: + case 9: + case 10: + case 11: + case 12: + case 13: { + auto tmu = s - 6; + auto texCoordIndex = device->m_States.fixedFunc.texCoordIndex[tmu]; + + prevAttribEnable = &this->m_GLStates.texCoord[texCoordIndex].enable; + glArray = GL_TEXTURE_COORD_ARRAY; + + glClientActiveTextureARB(GL_TEXTURE0 + texCoordIndex); + + break; + } + + default: + break; + } + + if (prevAttribEnable) { + if (*prevAttribEnable != attribEnable[s]) { + if (attribEnable[s]) { + glEnableClientState(glArray); + } else { + glDisableClientState(glArray); + } + } + + *prevAttribEnable = attribEnable[s]; + } } } + + if (!useVertexShader) { + // TODO device->SetColorMaterial(this->m_GLStates.color0.enable); + } } GLVertexArray::Properties& GLVertexArray::GetProperties() {