Files
metin2-client/src/port/UserInterface/CRenderTarget.cpp
T
shenleiandClaude Opus 5.5 84404cf1f1 Model preview: AVA2 render-target window on a Vulkan colour target
Port of AVA2's CRenderTarget / GrpRenderTargetTexture / renderTarget module.
The host gains an RGBA8 offscreen colour pass for D3D colour render targets;
mt_preview shows wearable items on every allowed race and monsters with
motions, reached by X+click in the inventory and shop and from the wiki.
Player races are created as TYPE_PC so their parts exist.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 04:48:07 +09:00

325 lines
10 KiB
C++

#include "StdAfx.h"
#include "CRenderTarget.h"
#include "InstanceBase.h"
#include "../EterLib/Camera.h"
#include "../EterLib/GrpImageInstance.h"
#include "../EterLib/ResourceManager.h"
#include "../EterLib/StateManager.h"
#include "../EterPythonLib/PythonGraphic.h"
CRenderTarget::~CRenderTarget() {}
CRenderTarget::CRenderTarget(const DWORD width, const DWORD height) :
m_pModel(nullptr),
m_background(nullptr),
m_modelRotation(0),
m_pmodelRotationActive(true),
m_visible(false),
m_bShowModel(true),
m_bScale(1.0),
m_v3AngularVelocity(0.0f, 0.0f, 0.0f),
m_v3Eye(0.0f, 0.0f, 0.0f),
m_v3Target(0.0f, 0.0f, 0.0f),
m_v3Up(0.0f, 0.0f, 1.0f)
{
auto pTex = std::make_unique<CGraphicRenderTargetTexture>();
if (!pTex->Create(width, height, D3DFMT_X8R8G8B8, D3DFMT_D16))
{
TraceError("CRenderTarget::CRenderTarget: Could not create CGraphicRenderTargetTexture %dx%d", width, height);
throw std::runtime_error("CRenderTarget::CRenderTarget: Could not create CGraphicRenderTargetTexture");
}
m_renderTargetTexture = std::move(pTex);
}
void CRenderTarget::RenderTexture() const
{
m_renderTargetTexture->Render();
}
void CRenderTarget::SetRenderingRect(RECT* rect) const
{
m_renderTargetTexture->SetRenderingRect(rect);
}
void CRenderTarget::CreateTextures() const
{
m_renderTargetTexture->CreateTextures();
}
void CRenderTarget::ReleaseTextures() const
{
m_renderTargetTexture->ReleaseTextures();
}
void CRenderTarget::ResetModel()
{
// PORT: AVA2 also cleared its wiki affects here; the port has none.
m_pModel.reset();
}
void CRenderTarget::SetScale(float fScale)
{
m_bScale = fScale;
if (IsReady())
SelectModel(m_pModel->GetRace(), true);
}
void CRenderTarget::SelectModel(const int index, bool isFromScale)
{
if (m_pModel && !isFromScale && m_pModel->GetRace() == index)
return;
if (isFromScale && m_pModel)
m_pModel.reset();
CInstanceBase::SCreateData kCreateData{};
// PORT: AVA2 created every model as TYPE_NPC. Here CActorInstance reserves the armor/weapon/hair parts for
// TYPE_PC only, so a player race created as an NPC crashes on the first ChangeArmor.
kCreateData.m_bType = index < NRaceData::JOB_MAX_NUM * 2 ? CActorInstance::TYPE_PC : CActorInstance::TYPE_NPC;
kCreateData.m_dwRace = index;
auto model = std::make_unique<CInstanceBase>();
if (!model->Create(kCreateData))
{
ResetModel();
return;
}
m_pModel = std::move(model);
// PORT: the preview has no name; the empty text tail Create attaches would follow the world origin.
m_pModel->DetachTextTail();
m_pModel->NEW_SetPixelPosition(TPixelPosition(0, 0, 0));
m_pModel->SetRotation(0.0f);
float fTargetHeight = 110.0f;
m_v3Eye.x = 0.0f;
m_v3Eye.y = -1000.0f;
m_v3Eye.z = 600.0f;
m_v3Target.x = 0.0f;
m_v3Target.y = 0.0f;
m_v3Target.z = 95.0f;
if (index > (NRaceData::JOB_MAX_NUM * 2))
{
const auto ptrGraphicInstance = m_pModel->GetGraphicThingInstancePtr();
if (ptrGraphicInstance) {
const float fRaceHeight = (ptrGraphicInstance->GetHeight()/m_bScale);
fTargetHeight = fRaceHeight / 2.0f;
m_v3Eye.y = -(fRaceHeight * 8.9f);
m_v3Eye.z = 0.0f;
m_v3Target.z = fRaceHeight / 2.0f;
}
}
auto& camera_manager = CCameraManager::instance();
camera_manager.SetCurrentCamera(CCameraManager::DEFAULT_ORTHO_CAMERA);
camera_manager.GetCurrentCamera()->SetTargetHeight(fTargetHeight);
camera_manager.ResetToPreviousCamera();
m_modelRotation = 0.0f;
m_v3AngularVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}
bool CRenderTarget::CreateBackground(const char* imgPath, const DWORD width, const DWORD height)
{
if (m_background)
return false;
m_background = std::make_unique<CGraphicImageInstance>();
const auto graphic_image = dynamic_cast<CGraphicImage*>(CResourceManager::instance().GetResourcePointer(imgPath));
if (!graphic_image)
{
m_background.reset();
return false;
}
m_background->SetImagePointer(graphic_image);
return true;
}
// PORT: AVA2 drew its background in a pass of its own through CGraphicImageInstance::Render. The port's image
// instance goes to the UI command list, not the device, so RenderModel draws the background into the target
// itself: one textured quad that fills it under a target-sized ortho.
void CRenderTarget::RenderBackground() const
{
if (!m_background || m_background->IsEmpty())
return;
CGraphicTexture* texture = m_background->GetTexturePointer();
if (!texture)
return;
const auto& rectRender = *m_renderTargetTexture->GetRenderingRect();
const auto width = static_cast<float>(rectRender.right - rectRender.left);
const auto height = static_cast<float>(rectRender.bottom - rectRender.top);
auto& python_graphic = CPythonGraphic::Instance();
python_graphic.PushState();
STATEMANAGER.SaveTransform(D3DTS_WORLD, &CGraphicBase::GetIdentityMatrix());
STATEMANAGER.SetTransform(D3DTS_VIEW, &CGraphicBase::GetIdentityMatrix());
python_graphic.SetOrtho2D(width, height, 1.0f);
STATEMANAGER.SaveRenderState(D3DRS_ZENABLE, FALSE);
STATEMANAGER.SaveRenderState(D3DRS_ZWRITEENABLE, FALSE);
STATEMANAGER.SaveRenderState(D3DRS_LIGHTING, FALSE);
STATEMANAGER.SaveRenderState(D3DRS_FOGENABLE, FALSE);
STATEMANAGER.SaveRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
STATEMANAGER.SaveRenderState(D3DRS_ALPHATESTENABLE, FALSE);
STATEMANAGER.SaveRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
STATEMANAGER.SaveTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
STATEMANAGER.SaveTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
STATEMANAGER.SaveTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
STATEMANAGER.SaveTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
STATEMANAGER.SaveTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
STATEMANAGER.SaveTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_NONE);
STATEMANAGER.SaveTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
TPDTVertex vertices[4];
const float x[4] = { 0.0f, width, 0.0f, width };
const float y[4] = { 0.0f, 0.0f, height, height };
for (int i = 0; i < 4; ++i)
{
vertices[i].position = TPosition(x[i], y[i], 0.0f);
vertices[i].texCoord = TTextureCoordinate(x[i] / width, y[i] / height);
vertices[i].diffuse = 0xffffffff;
}
STATEMANAGER.SetTexture(0, texture->GetD3DTexture());
STATEMANAGER.SetTexture(1, nullptr);
STATEMANAGER.SetVertexShader(D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE);
STATEMANAGER.DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vertices, sizeof(TPDTVertex));
STATEMANAGER.RestoreTextureStageState(1, D3DTSS_COLOROP);
STATEMANAGER.RestoreTextureStageState(0, D3DTSS_MIPFILTER);
STATEMANAGER.RestoreTextureStageState(0, D3DTSS_MAGFILTER);
STATEMANAGER.RestoreTextureStageState(0, D3DTSS_MINFILTER);
STATEMANAGER.RestoreTextureStageState(0, D3DTSS_ALPHAOP);
STATEMANAGER.RestoreTextureStageState(0, D3DTSS_COLOROP);
STATEMANAGER.RestoreTextureStageState(0, D3DTSS_COLORARG1);
STATEMANAGER.RestoreRenderState(D3DRS_CULLMODE);
STATEMANAGER.RestoreRenderState(D3DRS_ALPHATESTENABLE);
STATEMANAGER.RestoreRenderState(D3DRS_ALPHABLENDENABLE);
STATEMANAGER.RestoreRenderState(D3DRS_FOGENABLE);
STATEMANAGER.RestoreRenderState(D3DRS_LIGHTING);
STATEMANAGER.RestoreRenderState(D3DRS_ZWRITEENABLE);
STATEMANAGER.RestoreRenderState(D3DRS_ZENABLE);
STATEMANAGER.RestoreTransform(D3DTS_WORLD);
python_graphic.PopState();
}
void CRenderTarget::UpdateModel()
{
if (!IsReady())
return;
if (m_pmodelRotationActive && m_bShowModel)
{
if (m_modelRotation < 360.0f)
m_modelRotation += 1.0f / 60 * 60.0f;
else
m_modelRotation = 0.0f / 60 * 60.0f;
m_pModel->SetRotation(m_modelRotation);
}
else
m_pModel->SetRotation(0.0f);
m_pModel->Transform();
if(m_pmodelRotationActive)
m_pModel->GetGraphicThingInstanceRef().RotationProcess();
}
void CRenderTarget::DeformModel()
{
if (!IsReady())
return;
// PORT: stands in for AVA2's SetAlwaysRender. The model sits at the world origin, so the culling pass hides it.
m_pModel->GetGraphicThingInstanceRef().Show();
m_pModel->Deform();
}
void CRenderTarget::RenderModel()
{
if (!IsReady())
return;
auto& python_graphic = CPythonGraphic::Instance();
auto& camera_manager = CCameraManager::instance();
auto& state_manager = CStateManager::Instance();
auto& rectRender = *m_renderTargetTexture->GetRenderingRect();
const auto width = static_cast<float>(rectRender.right - rectRender.left);
const auto height = static_cast<float>(rectRender.bottom - rectRender.top);
// PORT: nothing to draw before the window has given its rect.
if (width <= 0.0f || height <= 0.0f)
return;
m_renderTargetTexture->SetRenderTarget();
m_renderTargetTexture->Clear();
python_graphic.SetViewport(0.0f, 0.0f, width, height);
RenderBackground();
python_graphic.ClearDepthBuffer();
const auto fov = python_graphic.GetFOV();
const auto aspect = python_graphic.GetAspect();
float near_y, far_y;
python_graphic.GetClipPlane(&near_y, &far_y);
state_manager.SetRenderState(D3DRS_FOGENABLE, FALSE);
python_graphic.PushState();
camera_manager.SetCurrentCamera(CCameraManager::DEFAULT_ORTHO_CAMERA);
camera_manager.GetCurrentCamera()->SetViewParams(m_v3Eye, m_v3Target, m_v3Up);
camera_manager.GetCurrentCamera()->RotateEyeAroundTarget(m_v3AngularVelocity.z, m_v3AngularVelocity.x);
python_graphic.UpdateViewMatrix();
python_graphic.SetPerspective(10.0f, width / height, 100.0f, 15000.0f);
if (m_bShowModel)
{
m_pModel->GetGraphicThingInstanceRef().Show();
m_pModel->Update();
m_pModel->Render();
}
// PORT: AVA2's RenderAllAttachingEffectWiki (attached effects) is not ported.
camera_manager.ResetToPreviousCamera();
python_graphic.RestoreViewport();
python_graphic.PopState();
python_graphic.SetPerspective(fov, aspect, near_y, far_y);
m_renderTargetTexture->ResetRenderTarget();
state_manager.SetRenderState(D3DRS_FOGENABLE, TRUE);
}
void CRenderTarget::SetModelV3Eye(float x, float y, float z)
{
m_v3Eye.x = x;
m_v3Eye.y = y;
m_v3Eye.z = z;
}
void CRenderTarget::SetModelV3Target(float x, float y, float z)
{
m_v3Target.x = x;
m_v3Target.y = y;
m_v3Target.z = z;
}
void CRenderTarget::RotateEyeAroundTarget(float fPitchDegree, float fRollDegree)
{
m_v3AngularVelocity.x = fRollDegree;
m_v3AngularVelocity.z = fPitchDegree;
}
void CRenderTarget::Zoom(float fRatio)
{
if (fRatio == 1.0f)
return;
D3DXVECTOR3 v3Temp = m_v3Eye - m_v3Target;
v3Temp *= fRatio;
m_v3Eye = v3Temp + m_v3Target;
}