Files
AVA2/src/eter_render/RenderCommands3D.h
T
shenleiandClaude Opus 5.5 837ccc2f31 Share GPU skinning palette per instance; push it once per change
Render3DDraw/HardwareBatch hold the SDK's shared palette instead of
copying 16 floats per bone per draw, and HardwareScene re-pushes the
uniform only when the palette pointer changes.

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

109 lines
4.8 KiB
C++

#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include <cmath>
#include <memory>
namespace ava2 {
inline void MakeTransformMatrix(float yawDegrees, float tx, float ty, float tz, float scale, float* out) {
float rad = yawDegrees * 3.14159265358979323846f / 180.0f;
float c = std::cos(rad) * scale;
float s = std::sin(rad) * scale;
out[0] = c; out[1] = 0.0f; out[2] = -s; out[3] = 0.0f;
out[4] = 0.0f; out[5] = scale;out[6] = 0.0f; out[7] = 0.0f;
out[8] = s; out[9] = 0.0f; out[10] = c; out[11] = 0.0f;
out[12] = tx; out[13] = ty; out[14] = tz; out[15] = 1.0f;
}
// Conservative homogeneous frustum rejection, including near/far planes.
// Reject only when every AABB corner is outside the same plane.
inline bool IsBoundsVisible(const float lo[3], const float hi[3], const float* view, const float* proj) {
if (!view || !proj) return true;
unsigned outside = 63;
for (unsigned corner = 0; corner < 8; ++corner) {
float p[4] = {corner&1 ? hi[0] : lo[0], corner&2 ? hi[1] : lo[1], corner&4 ? hi[2] : lo[2], 1};
float v[4]{}, c[4]{};
for (int j=0;j<4;++j) for(int k=0;k<4;++k) v[j]+=p[k]*view[k*4+j];
for (int j=0;j<4;++j) for(int k=0;k<4;++k) c[j]+=v[k]*proj[k*4+j];
unsigned bits = (c[0]<-c[3] ? 1:0) | (c[0]>c[3] ? 2:0) | (c[1]<-c[3] ? 4:0) |
(c[1]>c[3] ? 8:0) | (c[2]<0 ? 16:0) | (c[2]>c[3] ? 32:0);
outside &= bits;
if (!outside) return true;
}
return outside == 0;
}
// Intermediate 3D draw command structure, modeled after 40250 cross-platform architecture.
// Decouples game logic/animation (GR2) from the underlying GPU/software renderer.
struct Render3DDraw {
// Per-frame commands reference immutable mesh streams; transforms/material remain local.
std::shared_ptr<const Render3DDraw> geometry;
const Render3DDraw& Mesh() const { return geometry ? *geometry : *this; }
static Render3DDraw ShareGeometry(Render3DDraw source) {
Render3DDraw command = source;
command.positions.clear(); command.positions.shrink_to_fit();
command.normals.clear(); command.normals.shrink_to_fit();
command.uv0.clear(); command.uv0.shrink_to_fit();
command.uv1.clear(); command.uv1.shrink_to_fit();
command.diffuse.clear(); command.diffuse.shrink_to_fit();
command.indices.clear(); command.indices.shrink_to_fit();
command.bone_indices.clear(); command.bone_indices.shrink_to_fit();
command.bone_weights.clear(); command.bone_weights.shrink_to_fit();
command.geometry = std::make_shared<const Render3DDraw>(std::move(source));
return command;
}
bool gpu_skinning = false;
std::uint64_t geometry_key = 0;
std::uint64_t geometry_revision = 0;
// D3D row-vector matrices (translation in elements 12..14)
float world[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
float view[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
float proj[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
float viewport[4] = {0, 0, 1024, 768}; // x, y, width, height
float viewport_z[2] = {0.0f, 1.0f}; // MinZ, MaxZ
// Textures: file path (e.g. "season1/pc/warrior/warrior_4-1.dds") or memory texture
std::string texture0;
std::string texture1;
// Vertex data
std::vector<float> positions; // 3 floats (x, y, z) per vertex
std::vector<float> normals; // 3 floats (nx, ny, nz) per vertex
std::vector<float> uv0; // 2 floats (u, v) per vertex
std::vector<float> uv1; // 2 floats (u, v) per vertex
std::vector<std::uint32_t> diffuse; // 0xAARRGGBB per vertex
std::vector<std::uint32_t> indices; // Triangle list indices (3 per triangle)
// Skeletal skinning streams
std::vector<std::uint8_t> bone_indices; // 4 uint8 per vertex
std::vector<float> bone_weights; // 4 floats per vertex
std::shared_ptr<const std::vector<float>> palette; // GPU skinning, 16 floats per bone; shared per instance
// Render states
bool z_enable = true;
bool z_write = true;
std::uint32_t cull_mode = 2; // 1: None, 2: CW, 3: CCW
bool alpha_blend = false;
std::uint32_t blend_mode = 0; // 0: Alpha (D3DBLEND_INVSRCALPHA), 1: Additive (D3DBLEND_ONE)
bool alpha_test = false;
std::uint32_t alpha_ref = 0;
bool fog = true;
bool terrain = false; // SDL painter layers keep terrain splats ahead of actors.
// Lighting & Material
bool lighting = true;
float material_diffuse[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float material_ambient[4] = {0.85f, 0.85f, 0.85f, 1.0f};
float material_emissive[4] = {0.05f, 0.05f, 0.05f, 0.05f};
float light_direction[3] = {0.577f, 0.577f, -0.577f}; // Normalized key light
float light_diffuse[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float light_ambient[4] = {0.55f, 0.55f, 0.55f, 1.0f};
};
} // namespace ava2