mirror of
https://github.com/w23/xash3d-fwgs
synced 2024-11-19 08:28:21 +01:00
d8d5019971
thanks to G.I.F
1445 lines
44 KiB
C
1445 lines
44 KiB
C
#include "vk_brush.h"
|
|
|
|
#include "vk_core.h"
|
|
#include "vk_const.h"
|
|
#include "vk_buffer.h"
|
|
#include "vk_pipeline.h"
|
|
#include "vk_framectl.h"
|
|
#include "vk_math.h"
|
|
#include "vk_textures.h"
|
|
#include "vk_lightmap.h"
|
|
#include "vk_scene.h"
|
|
#include "vk_render.h"
|
|
#include "vk_geometry.h"
|
|
#include "vk_light.h"
|
|
#include "vk_mapents.h"
|
|
#include "r_speeds.h"
|
|
#include "vk_logs.h"
|
|
|
|
#include "ref_params.h"
|
|
#include "eiface.h"
|
|
|
|
#include <math.h>
|
|
#include <memory.h>
|
|
|
|
#define MODULE_NAME "brush"
|
|
#define LOG_MODULE LogModule_Brush
|
|
|
|
typedef struct vk_brush_model_s {
|
|
model_t *engine_model;
|
|
|
|
r_geometry_range_t geometry;
|
|
|
|
vk_render_model_t render_model;
|
|
int *surface_to_geometry_index;
|
|
|
|
int *animated_indexes;
|
|
int animated_indexes_count;
|
|
|
|
matrix4x4 prev_transform;
|
|
float prev_time;
|
|
|
|
struct {
|
|
int surfaces_count;
|
|
const int *surfaces_indices;
|
|
|
|
r_geometry_range_t geometry;
|
|
vk_render_model_t render_model;
|
|
} water;
|
|
} vk_brush_model_t;
|
|
|
|
typedef struct {
|
|
int num_surfaces, num_vertices, num_indices;
|
|
int max_texture_id;
|
|
int water_surfaces;
|
|
int animated_count;
|
|
|
|
int water_vertices;
|
|
int water_indices;
|
|
} model_sizes_t;
|
|
|
|
typedef struct conn_edge_s {
|
|
int first_surface;
|
|
int count;
|
|
} conn_edge_t;
|
|
|
|
typedef struct linked_value_s {
|
|
int value, link;
|
|
} linked_value_t;
|
|
|
|
#define MAX_VERTEX_SURFACES 16
|
|
typedef struct conn_vertex_s {
|
|
int count;
|
|
linked_value_t surfs[MAX_VERTEX_SURFACES];
|
|
} conn_vertex_t;
|
|
|
|
static struct {
|
|
struct {
|
|
int total_vertices, total_indices;
|
|
int models_drawn;
|
|
int water_surfaces_drawn;
|
|
int water_polys_drawn;
|
|
} stat;
|
|
|
|
int rtable[MOD_FRAMES][MOD_FRAMES];
|
|
|
|
// Unfortunately the engine only tracks the toplevel worldmodel. *xx submodels, while having their own entities and models, are not lifetime-tracked.
|
|
// I.e. the engine doesn't call Mod_ProcessRenderData() on them, so we don't directly know when to create or destroy them.
|
|
// Therefore, we need to track them manually and destroy them based on some other external event, e.g. Mod_ProcessRenderData(worldmodel)
|
|
vk_brush_model_t *models[MAX_MODELS];
|
|
int models_count;
|
|
|
|
#define MAX_ANIMATED_TEXTURES 256
|
|
int updated_textures[MAX_ANIMATED_TEXTURES];
|
|
|
|
// Smoothed normals comptutation
|
|
// Connectome for edges and vertices
|
|
struct {
|
|
int edges_capacity;
|
|
conn_edge_t *edges;
|
|
|
|
int vertices_capacity;
|
|
conn_vertex_t *vertices;
|
|
} conn;
|
|
} g_brush;
|
|
|
|
void VK_InitRandomTable( void )
|
|
{
|
|
int tu, tv;
|
|
|
|
// make random predictable
|
|
gEngine.COM_SetRandomSeed( 255 );
|
|
|
|
for( tu = 0; tu < MOD_FRAMES; tu++ )
|
|
{
|
|
for( tv = 0; tv < MOD_FRAMES; tv++ )
|
|
{
|
|
g_brush.rtable[tu][tv] = gEngine.COM_RandomLong( 0, 0x7FFF );
|
|
}
|
|
}
|
|
|
|
gEngine.COM_SetRandomSeed( 0 );
|
|
}
|
|
|
|
qboolean VK_BrushInit( void )
|
|
{
|
|
VK_InitRandomTable ();
|
|
|
|
R_SPEEDS_COUNTER(g_brush.stat.models_drawn, "drawn", kSpeedsMetricCount);
|
|
R_SPEEDS_COUNTER(g_brush.stat.water_surfaces_drawn, "water.surfaces", kSpeedsMetricCount);
|
|
R_SPEEDS_COUNTER(g_brush.stat.water_polys_drawn, "water.polys", kSpeedsMetricCount);
|
|
|
|
return true;
|
|
}
|
|
|
|
void VK_BrushShutdown( void ) {
|
|
if (g_brush.conn.edges)
|
|
Mem_Free(g_brush.conn.edges);
|
|
}
|
|
|
|
// speed up sin calculations
|
|
static const float r_turbsin[] =
|
|
{
|
|
#include "warpsin.h"
|
|
};
|
|
|
|
#define SUBDIVIDE_SIZE 64
|
|
#define TURBSCALE ( 256.0f / ( M_PI2 ))
|
|
|
|
static void addWarpVertIndCounts(const msurface_t *warp, int *num_vertices, int *num_indices) {
|
|
for( glpoly_t *p = warp->polys; p; p = p->next ) {
|
|
const int triangles = p->numverts - 2;
|
|
*num_vertices += p->numverts;
|
|
*num_indices += triangles * 3;
|
|
}
|
|
}
|
|
|
|
typedef struct {
|
|
float prev_time;
|
|
float scale;
|
|
const msurface_t *warp;
|
|
qboolean reverse;
|
|
|
|
vk_vertex_t *dst_vertices;
|
|
uint16_t *dst_indices;
|
|
vk_render_geometry_t *dst_geometry;
|
|
|
|
int *out_vertex_count, *out_index_count;
|
|
} compute_water_polys_t;
|
|
|
|
static void brushComputeWaterPolys( compute_water_polys_t args ) {
|
|
const float time = gpGlobals->time;
|
|
|
|
#define MAX_WATER_VERTICES 16
|
|
vk_vertex_t poly_vertices[MAX_WATER_VERTICES];
|
|
|
|
// FIXME unused? const qboolean useQuads = FBitSet( warp->flags, SURF_DRAWTURB_QUADS );
|
|
|
|
ASSERT(args.warp->polys);
|
|
|
|
// set the current waveheight
|
|
// FIXME VK if( warp->polys->verts[0][2] >= RI.vieworg[2] )
|
|
// waveHeight = -ent->curstate.scale;
|
|
// else
|
|
// waveHeight = ent->curstate.scale;
|
|
const float scale = args.scale;
|
|
|
|
// reset fog color for nonlightmapped water
|
|
// FIXME VK GL_ResetFogColor();
|
|
|
|
int vertices = 0;
|
|
int indices = 0;
|
|
|
|
for( glpoly_t *p = args.warp->polys; p; p = p->next ) {
|
|
ASSERT(p->numverts <= MAX_WATER_VERTICES);
|
|
float *v;
|
|
if( args.reverse )
|
|
v = p->verts[0] + ( p->numverts - 1 ) * VERTEXSIZE;
|
|
else v = p->verts[0];
|
|
|
|
for( int i = 0; i < p->numverts; i++ )
|
|
{
|
|
float nv, prev_nv;
|
|
if( scale )
|
|
{
|
|
nv = r_turbsin[(int)(time * 160.0f + v[1] + v[0]) & 255] + 8.0f;
|
|
nv = (r_turbsin[(int)(v[0] * 5.0f + time * 171.0f - v[1]) & 255] + 8.0f ) * 0.8f + nv;
|
|
nv = nv * scale + v[2];
|
|
|
|
prev_nv = r_turbsin[(int)(args.prev_time * 160.0f + v[1] + v[0]) & 255] + 8.0f;
|
|
prev_nv = (r_turbsin[(int)(v[0] * 5.0f + args.prev_time * 171.0f - v[1]) & 255] + 8.0f ) * 0.8f + prev_nv;
|
|
prev_nv = prev_nv * scale + v[2];
|
|
}
|
|
else prev_nv = nv = v[2];
|
|
|
|
const float os = v[3];
|
|
const float ot = v[4];
|
|
|
|
float s = os + r_turbsin[(int)((ot * 0.125f + gpGlobals->time) * TURBSCALE) & 255];
|
|
s *= ( 1.0f / SUBDIVIDE_SIZE );
|
|
|
|
float t = ot + r_turbsin[(int)((os * 0.125f + gpGlobals->time) * TURBSCALE) & 255];
|
|
t *= ( 1.0f / SUBDIVIDE_SIZE );
|
|
|
|
poly_vertices[i].pos[0] = v[0];
|
|
poly_vertices[i].pos[1] = v[1];
|
|
poly_vertices[i].pos[2] = nv;
|
|
|
|
poly_vertices[i].prev_pos[0] = v[0];
|
|
poly_vertices[i].prev_pos[1] = v[1];
|
|
poly_vertices[i].prev_pos[2] = prev_nv;
|
|
|
|
poly_vertices[i].gl_tc[0] = s;
|
|
poly_vertices[i].gl_tc[1] = t;
|
|
|
|
poly_vertices[i].lm_tc[0] = 0;
|
|
poly_vertices[i].lm_tc[1] = 0;
|
|
|
|
Vector4Set(poly_vertices[i].color, 255, 255, 255, 255);
|
|
|
|
poly_vertices[i].normal[0] = 0;
|
|
poly_vertices[i].normal[1] = 0;
|
|
poly_vertices[i].normal[2] = 0;
|
|
|
|
if (i > 1) {
|
|
vec3_t e0, e1, normal;
|
|
VectorSubtract( poly_vertices[i - 1].pos, poly_vertices[0].pos, e0 );
|
|
VectorSubtract( poly_vertices[i].pos, poly_vertices[0].pos, e1 );
|
|
CrossProduct( e1, e0, normal );
|
|
|
|
VectorAdd(normal, poly_vertices[0].normal, poly_vertices[0].normal);
|
|
VectorAdd(normal, poly_vertices[i].normal, poly_vertices[i].normal);
|
|
VectorAdd(normal, poly_vertices[i - 1].normal, poly_vertices[i - 1].normal);
|
|
|
|
args.dst_indices[indices++] = (uint16_t)(vertices);
|
|
args.dst_indices[indices++] = (uint16_t)(vertices + i - 1);
|
|
args.dst_indices[indices++] = (uint16_t)(vertices + i);
|
|
}
|
|
|
|
if( args.reverse )
|
|
v -= VERTEXSIZE;
|
|
else
|
|
v += VERTEXSIZE;
|
|
}
|
|
|
|
for( int i = 0; i < p->numverts; i++ )
|
|
VectorNormalize(poly_vertices[i].normal);
|
|
|
|
memcpy(args.dst_vertices + vertices, poly_vertices, sizeof(vk_vertex_t) * p->numverts);
|
|
vertices += p->numverts;
|
|
}
|
|
|
|
// FIXME VK GL_SetupFogColorForSurfaces();
|
|
|
|
// Render
|
|
const int tex_id = args.warp->texinfo->texture->gl_texturenum;
|
|
*args.dst_geometry = (vk_render_geometry_t){
|
|
.texture = tex_id, // FIXME assert >= 0
|
|
.material = kXVkMaterialRegular,
|
|
.surf_deprecate = args.warp,
|
|
|
|
.max_vertex = vertices,
|
|
.element_count = indices,
|
|
|
|
.emissive = {0,0,0},
|
|
};
|
|
|
|
RT_GetEmissiveForTexture(args.dst_geometry->emissive, tex_id);
|
|
*args.out_vertex_count = vertices;
|
|
*args.out_index_count = indices;
|
|
|
|
g_brush.stat.water_surfaces_drawn++;
|
|
g_brush.stat.water_polys_drawn += indices / 3;
|
|
}
|
|
|
|
static vk_render_type_e brushRenderModeToRenderType( int render_mode ) {
|
|
switch (render_mode) {
|
|
case kRenderNormal: return kVkRenderTypeSolid;
|
|
case kRenderTransColor: return kVkRenderType_A_1mA_RW;
|
|
case kRenderTransTexture: return kVkRenderType_A_1mA_R;
|
|
case kRenderGlow: return kVkRenderType_A_1mA_R;
|
|
case kRenderTransAlpha: return kVkRenderType_AT;
|
|
case kRenderTransAdd: return kVkRenderType_A_1_R;
|
|
default: ASSERT(!"Unxpected render_mode");
|
|
}
|
|
|
|
return kVkRenderTypeSolid;
|
|
}
|
|
|
|
#if 0 // TOO OLD
|
|
static void brushDrawWaterSurfaces( const cl_entity_t *ent, const vec4_t color, const matrix4x4 transform ) {
|
|
const model_t *model = ent->model;
|
|
vec3_t mins, maxs;
|
|
|
|
if( !VectorIsNull( ent->angles ))
|
|
{
|
|
for( int i = 0; i < 3; i++ )
|
|
{
|
|
mins[i] = ent->origin[i] - model->radius;
|
|
maxs[i] = ent->origin[i] + model->radius;
|
|
}
|
|
//rotated = true;
|
|
}
|
|
else
|
|
{
|
|
VectorAdd( ent->origin, model->mins, mins );
|
|
VectorAdd( ent->origin, model->maxs, maxs );
|
|
//rotated = false;
|
|
}
|
|
|
|
// if( R_CullBox( mins, maxs ))
|
|
// return;
|
|
|
|
VK_RenderModelDynamicBegin( brushRenderModeToRenderType(ent->curstate.rendermode), color, transform, "%s water", model->name );
|
|
|
|
// Iterate through all surfaces, find *TURB*
|
|
for( int i = 0; i < model->nummodelsurfaces; i++ )
|
|
{
|
|
const msurface_t *surf = model->surfaces + model->firstmodelsurface + i;
|
|
|
|
if( !FBitSet( surf->flags, SURF_DRAWTURB ) && !FBitSet( surf->flags, SURF_DRAWTURB_QUADS) )
|
|
continue;
|
|
|
|
if( surf->plane->type != PLANE_Z && !FBitSet( ent->curstate.effects, EF_WATERSIDES ))
|
|
continue;
|
|
|
|
if( mins[2] + 1.0f >= surf->plane->dist )
|
|
continue;
|
|
|
|
EmitWaterPolys( ent, surf, false );
|
|
}
|
|
|
|
// submit as dynamic model
|
|
VK_RenderModelDynamicCommit();
|
|
|
|
// TODO:
|
|
// - upload water geometry only once, animate in compute/vertex shader
|
|
}
|
|
#endif
|
|
|
|
static void fillWaterSurfaces( const cl_entity_t *ent, vk_brush_model_t *bmodel, vk_render_geometry_t *geometries ) {
|
|
ASSERT(bmodel->water.surfaces_count > 0);
|
|
|
|
const r_geometry_range_lock_t geom_lock = R_GeometryRangeLock(&bmodel->water.geometry);
|
|
|
|
const float scale = ent ? ent->curstate.scale : 1.f;
|
|
|
|
int vertices_offset = 0;
|
|
int indices_offset = 0;
|
|
for (int i = 0; i < bmodel->water.surfaces_count; ++i) {
|
|
const int surf_index = bmodel->water.surfaces_indices[i];
|
|
|
|
int vertices = 0, indices = 0;
|
|
brushComputeWaterPolys((compute_water_polys_t){
|
|
.prev_time = bmodel->prev_time,
|
|
.scale = scale,
|
|
.reverse = false, // ??? is it ever true?
|
|
.warp = bmodel->engine_model->surfaces + surf_index,
|
|
|
|
.dst_vertices = geom_lock.vertices + vertices_offset,
|
|
.dst_indices = geom_lock.indices + indices_offset,
|
|
.dst_geometry = geometries + i,
|
|
|
|
.out_vertex_count = &vertices,
|
|
.out_index_count = &indices,
|
|
});
|
|
|
|
geometries[i].vertex_offset = bmodel->water.geometry.vertices.unit_offset + vertices_offset;
|
|
geometries[i].index_offset = bmodel->water.geometry.indices.unit_offset + indices_offset;
|
|
|
|
vertices_offset += vertices;
|
|
indices_offset += indices;
|
|
|
|
ASSERT(vertices_offset <= bmodel->water.geometry.vertices.count);
|
|
ASSERT(indices_offset <= bmodel->water.geometry.indices.count);
|
|
}
|
|
|
|
R_GeometryRangeUnlock( &geom_lock );
|
|
}
|
|
|
|
static rt_light_add_polygon_t loadPolyLight(const model_t *mod, const int surface_index, const msurface_t *surf, const vec3_t emissive);
|
|
|
|
static qboolean isSurfaceAnimated( const msurface_t *s, const struct texture_s *base_override ) {
|
|
const texture_t *base = base_override ? base_override : s->texinfo->texture;
|
|
|
|
/* FIXME don't have ent here, need to check both explicitly
|
|
if( ent && ent->curstate.frame ) {
|
|
if( base->alternate_anims )
|
|
base = base->alternate_anims;
|
|
}
|
|
*/
|
|
|
|
if( !base->anim_total )
|
|
return false;
|
|
|
|
if( base->name[0] == '-' )
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
typedef enum {
|
|
BrushSurface_Hidden = 0,
|
|
BrushSurface_Regular,
|
|
BrushSurface_Animated,
|
|
BrushSurface_Water,
|
|
BrushSurface_Sky,
|
|
} brush_surface_type_e;
|
|
|
|
static brush_surface_type_e getSurfaceType( const msurface_t *surf, int i ) {
|
|
// if ( i >= 0 && (surf->flags & ~(SURF_PLANEBACK | SURF_UNDERWATER | SURF_TRANSPARENT)) != 0)
|
|
// {
|
|
// DEBUG("\t%d flags: ", i);
|
|
// #define PRINTFLAGS(X) \
|
|
// X(SURF_PLANEBACK) \
|
|
// X(SURF_DRAWSKY) \
|
|
// X(SURF_DRAWTURB_QUADS) \
|
|
// X(SURF_DRAWTURB) \
|
|
// X(SURF_DRAWTILED) \
|
|
// X(SURF_CONVEYOR) \
|
|
// X(SURF_UNDERWATER) \
|
|
// X(SURF_TRANSPARENT)
|
|
|
|
// #define PRINTFLAG(f) if (FBitSet(surf->flags, f)) DEBUG(" %s", #f);
|
|
// PRINTFLAGS(PRINTFLAG)
|
|
// DEBUG("\n");
|
|
// }
|
|
const xvk_patch_surface_t *patch_surface = R_VkPatchGetSurface(i);
|
|
if (patch_surface && patch_surface->flags & Patch_Surface_Delete)
|
|
return BrushSurface_Hidden;
|
|
|
|
if (surf->flags & (SURF_DRAWTURB | SURF_DRAWTURB_QUADS)) {
|
|
return (!surf->polys) ? BrushSurface_Hidden : BrushSurface_Water;
|
|
}
|
|
|
|
// Explicitly enable SURF_SKY, otherwise they will be skipped by SURF_DRAWTILED
|
|
if( FBitSet( surf->flags, SURF_DRAWSKY ))
|
|
return BrushSurface_Sky;
|
|
|
|
//if( surf->flags & ( SURF_DRAWSKY | SURF_DRAWTURB | SURF_CONVEYOR | SURF_DRAWTURB_QUADS ) ) {
|
|
if( surf->flags & ( SURF_DRAWTURB | SURF_DRAWTURB_QUADS ) ) {
|
|
//if( surf->flags & ( SURF_DRAWSKY | SURF_CONVEYOR ) ) {
|
|
// FIXME don't print this on second sort-by-texture pass
|
|
//DEBUG("Skipping surface %d because of flags %08x", i, surf->flags);
|
|
return BrushSurface_Hidden;
|
|
}
|
|
|
|
if( FBitSet( surf->flags, SURF_DRAWTILED )) {
|
|
//DEBUG("Skipping surface %d because of tiled flag", i);
|
|
return BrushSurface_Hidden;
|
|
}
|
|
|
|
const struct texture_s *texture_override = patch_surface ? patch_surface->tex : NULL;
|
|
if (isSurfaceAnimated(surf, texture_override)) {
|
|
return BrushSurface_Animated;
|
|
}
|
|
|
|
return BrushSurface_Regular;
|
|
}
|
|
|
|
static qboolean brushCreateWaterModel(const model_t *mod, vk_brush_model_t *bmodel, const model_sizes_t sizes) {
|
|
bmodel->water.surfaces_count = sizes.water_surfaces;
|
|
|
|
const r_geometry_range_t geometry = R_GeometryRangeAlloc(sizes.water_vertices, sizes.water_indices);
|
|
if (!geometry.block_handle.size) {
|
|
ERR("Cannot allocate geometry (v=%d, i=%d) for water model %s",
|
|
sizes.water_vertices, sizes.water_indices, mod->name );
|
|
return false;
|
|
}
|
|
|
|
vk_render_geometry_t *const geometries = Mem_Malloc(vk_core.pool, sizeof(vk_render_geometry_t) * sizes.water_surfaces);
|
|
|
|
int* const surfaces_indices = Mem_Malloc(vk_core.pool, sizes.water_surfaces * sizeof(int));
|
|
int index_index = 0;
|
|
for( int i = 0; i < mod->nummodelsurfaces; ++i) {
|
|
const int surface_index = mod->firstmodelsurface + i;
|
|
const msurface_t *surf = mod->surfaces + surface_index;
|
|
|
|
if (getSurfaceType(surf, surface_index) != BrushSurface_Water)
|
|
continue;
|
|
|
|
surfaces_indices[index_index++] = surface_index;
|
|
}
|
|
ASSERT(index_index == sizes.water_surfaces);
|
|
|
|
bmodel->water.surfaces_indices = surfaces_indices;
|
|
bmodel->water.geometry = geometry;
|
|
fillWaterSurfaces(NULL, bmodel, geometries);
|
|
|
|
if (!R_RenderModelCreate(&bmodel->water.render_model, (vk_render_model_init_t){
|
|
.name = mod->name,
|
|
.geometries = geometries,
|
|
.geometries_count = sizes.water_surfaces,
|
|
.dynamic = true,
|
|
})) {
|
|
ERR("Could not create water render model for brush model %s", mod->name);
|
|
return false;
|
|
}
|
|
|
|
bmodel->water.surfaces_indices = surfaces_indices;
|
|
return true;
|
|
}
|
|
|
|
static void brushDrawWater(vk_brush_model_t *bmodel, const cl_entity_t *ent, int render_type, const vec4_t color, const matrix4x4 transform) {
|
|
ASSERT(bmodel->water.surfaces_count > 0);
|
|
|
|
fillWaterSurfaces(NULL, bmodel, bmodel->water.render_model.geometries);
|
|
if (!R_RenderModelUpdate(&bmodel->water.render_model)) {
|
|
ERR("Failed to update brush model \"%s\" water", bmodel->render_model.debug_name);
|
|
}
|
|
|
|
R_RenderModelDraw(&bmodel->water.render_model, (r_model_draw_t){
|
|
.render_type = render_type,
|
|
.color = (const vec4_t*)color,
|
|
.transform = (const matrix4x4*)transform,
|
|
.prev_transform = &bmodel->prev_transform,
|
|
});
|
|
}
|
|
|
|
// FIXME use this
|
|
static void computeConveyorSpeed(const color24 rendercolor, int tex_index, vec2_t speed) {
|
|
float sy, cy;
|
|
float flConveyorSpeed = 0.0f;
|
|
float flRate, flAngle;
|
|
vk_texture_t *texture = findTexture( tex_index );
|
|
//gl_texture_t *texture;
|
|
|
|
// FIXME
|
|
/* if( ENGINE_GET_PARM( PARM_QUAKE_COMPATIBLE ) && RI.currententity == gEngfuncs.GetEntityByIndex( 0 ) ) */
|
|
/* { */
|
|
/* // same as doom speed */
|
|
/* flConveyorSpeed = -35.0f; */
|
|
/* } */
|
|
/* else */
|
|
{
|
|
flConveyorSpeed = (rendercolor.g<<8|rendercolor.b) / 16.0f;
|
|
if( rendercolor.r ) flConveyorSpeed = -flConveyorSpeed;
|
|
}
|
|
//texture = R_GetTexture( glState.currentTextures[glState.activeTMU] );
|
|
|
|
flRate = fabs( flConveyorSpeed ) / (float)texture->width;
|
|
flAngle = ( flConveyorSpeed >= 0 ) ? 180 : 0;
|
|
|
|
SinCos( flAngle * ( M_PI_F / 180.0f ), &sy, &cy );
|
|
speed[0] = cy * flRate;
|
|
speed[1] = sy * flRate;
|
|
}
|
|
|
|
/*
|
|
===============
|
|
R_TextureAnimation
|
|
|
|
Returns the proper texture for a given time and surface
|
|
===============
|
|
*/
|
|
const texture_t *R_TextureAnimation( const cl_entity_t *ent, const msurface_t *s, const struct texture_s *base_override )
|
|
{
|
|
const texture_t *base = base_override ? base_override : s->texinfo->texture;
|
|
int count, reletive;
|
|
|
|
if( ent && ent->curstate.frame )
|
|
{
|
|
if( base->alternate_anims )
|
|
base = base->alternate_anims;
|
|
}
|
|
|
|
if( !base->anim_total )
|
|
return base;
|
|
|
|
if( base->name[0] == '-' )
|
|
{
|
|
int tx = (int)((s->texturemins[0] + (base->width << 16)) / base->width) % MOD_FRAMES;
|
|
int ty = (int)((s->texturemins[1] + (base->height << 16)) / base->height) % MOD_FRAMES;
|
|
|
|
reletive = g_brush.rtable[tx][ty] % base->anim_total;
|
|
}
|
|
else
|
|
{
|
|
int speed;
|
|
|
|
// Quake1 textures uses 10 frames per second
|
|
if( FBitSet( findTexture( base->gl_texturenum )->flags, TF_QUAKEPAL ))
|
|
speed = 10;
|
|
else speed = 20;
|
|
|
|
reletive = (int)(gpGlobals->time * speed) % base->anim_total;
|
|
}
|
|
|
|
count = 0;
|
|
|
|
while( base->anim_min > reletive || base->anim_max <= reletive )
|
|
{
|
|
base = base->anim_next;
|
|
|
|
if( !base || ++count > MOD_FRAMES )
|
|
return s->texinfo->texture;
|
|
}
|
|
|
|
return base;
|
|
}
|
|
|
|
void VK_BrushModelDraw( const cl_entity_t *ent, int render_mode, float blend, const matrix4x4 in_transform ) {
|
|
// Expect all buffers to be bound
|
|
const model_t *mod = ent->model;
|
|
vk_brush_model_t *bmodel = mod->cache.data;
|
|
|
|
if (!bmodel) {
|
|
ERR("Model %s wasn't loaded", mod->name);
|
|
return;
|
|
}
|
|
|
|
matrix4x4 transform;
|
|
if (in_transform)
|
|
Matrix4x4_Copy(transform, in_transform);
|
|
else
|
|
Matrix4x4_LoadIdentity(transform);
|
|
|
|
vec4_t color = {1, 1, 1, 1};
|
|
vk_render_type_e render_type = kVkRenderTypeSolid;
|
|
switch (render_mode) {
|
|
case kRenderNormal:
|
|
Vector4Set(color, 1.f, 1.f, 1.f, 1.f);
|
|
render_type = kVkRenderTypeSolid;
|
|
break;
|
|
case kRenderTransColor:
|
|
render_type = kVkRenderType_A_1mA_RW;
|
|
Vector4Set(color,
|
|
ent->curstate.rendercolor.r / 255.f,
|
|
ent->curstate.rendercolor.g / 255.f,
|
|
ent->curstate.rendercolor.b / 255.f,
|
|
blend);
|
|
break;
|
|
case kRenderTransAdd:
|
|
Vector4Set(color, blend, blend, blend, 1.f);
|
|
render_type = kVkRenderType_A_1_R;
|
|
break;
|
|
case kRenderTransAlpha:
|
|
if( gEngine.EngineGetParm( PARM_QUAKE_COMPATIBLE, 0 ))
|
|
{
|
|
render_type = kVkRenderType_A_1mA_RW;
|
|
Vector4Set(color, 1.f, 1.f, 1.f, blend);
|
|
}
|
|
else
|
|
{
|
|
Vector4Set(color, 1.f, 1.f, 1.f, 1.f);
|
|
render_type = kVkRenderType_AT;
|
|
}
|
|
break;
|
|
case kRenderTransTexture:
|
|
case kRenderGlow:
|
|
render_type = kVkRenderType_A_1mA_R;
|
|
Vector4Set(color, 1.f, 1.f, 1.f, blend);
|
|
break;
|
|
}
|
|
|
|
// Only Normal and TransAlpha have lightmaps
|
|
// TODO: on big maps more than a single lightmap texture is possible
|
|
bmodel->render_model.lightmap = (render_mode == kRenderNormal || render_mode == kRenderTransAlpha) ? 1 : 0;
|
|
|
|
if (bmodel->water.surfaces_count)
|
|
brushDrawWater(bmodel, ent, render_type, color, transform);
|
|
|
|
++g_brush.stat.models_drawn;
|
|
|
|
if (bmodel->render_model.num_geometries == 0)
|
|
return;
|
|
|
|
// TransColor means ignore textures and draw just color
|
|
if (render_mode == kRenderTransColor) {
|
|
// TODO cache previous render_mode.
|
|
// TODO also it will break switching render type from TransColor to anyting else -- textures will be stuck at white
|
|
for (int i = 0; i < bmodel->render_model.num_geometries; ++i) {
|
|
vk_render_geometry_t *geom = bmodel->render_model.geometries + i;
|
|
geom->texture = tglob.whiteTexture;
|
|
}
|
|
} else {
|
|
// Update animated textures
|
|
int updated_textures_count = 0;
|
|
for (int i = 0; i < bmodel->animated_indexes_count; ++i) {
|
|
const int geom_index = bmodel->animated_indexes[i];
|
|
vk_render_geometry_t *geom = bmodel->render_model.geometries + geom_index;
|
|
const int surface_index = geom->surf_deprecate - mod->surfaces;
|
|
const xvk_patch_surface_t *const patch_surface = R_VkPatchGetSurface(surface_index);
|
|
|
|
// Optionally patch by texture_s pointer and run animations
|
|
const struct texture_s *texture_override = patch_surface ? patch_surface->tex : NULL;
|
|
const texture_t *t = R_TextureAnimation(ent, geom->surf_deprecate, texture_override);
|
|
const int new_texture = t->gl_texturenum;
|
|
|
|
if (new_texture >= 0 && new_texture != geom->texture) {
|
|
geom->texture = t->gl_texturenum;
|
|
if (updated_textures_count < MAX_ANIMATED_TEXTURES) {
|
|
g_brush.updated_textures[updated_textures_count++] = bmodel->animated_indexes[i];
|
|
}
|
|
}
|
|
|
|
// Animated textures can be emissive
|
|
// Add them as dynamic lights for now. It would probably be better if they were static lights (for worldmodel),
|
|
// but there's no easy way to do it for now.
|
|
vec3_t *emissive = &bmodel->render_model.geometries[geom_index].emissive;
|
|
if (RT_GetEmissiveForTexture(*emissive, geom->texture)) {
|
|
rt_light_add_polygon_t polylight = loadPolyLight(mod, surface_index, geom->surf_deprecate, *emissive);
|
|
polylight.dynamic = true;
|
|
polylight.transform_row = (const matrix3x4*)&transform;
|
|
RT_LightAddPolygon(&polylight);
|
|
}
|
|
}
|
|
|
|
if (updated_textures_count > 0) {
|
|
R_RenderModelUpdateMaterials(&bmodel->render_model, g_brush.updated_textures, updated_textures_count);
|
|
}
|
|
}
|
|
|
|
R_RenderModelDraw(&bmodel->render_model, (r_model_draw_t){
|
|
.render_type = render_type,
|
|
.color = &color,
|
|
.transform = &transform,
|
|
.prev_transform = &bmodel->prev_transform,
|
|
});
|
|
|
|
Matrix4x4_Copy(bmodel->prev_transform, transform);
|
|
bmodel->prev_time = gpGlobals->time;
|
|
}
|
|
|
|
static model_sizes_t computeSizes( const model_t *mod ) {
|
|
model_sizes_t sizes = {0};
|
|
|
|
for( int i = 0; i < mod->nummodelsurfaces; ++i)
|
|
{
|
|
const int surface_index = mod->firstmodelsurface + i;
|
|
const msurface_t *surf = mod->surfaces + surface_index;
|
|
const int tex_id = surf->texinfo->texture->gl_texturenum;
|
|
|
|
if (tex_id > sizes.max_texture_id)
|
|
sizes.max_texture_id = tex_id;
|
|
|
|
switch (getSurfaceType(surf, surface_index)) {
|
|
case BrushSurface_Water:
|
|
sizes.water_surfaces++;
|
|
addWarpVertIndCounts(surf, &sizes.water_vertices, &sizes.water_indices);
|
|
case BrushSurface_Hidden:
|
|
continue;
|
|
|
|
case BrushSurface_Animated:
|
|
sizes.animated_count++;
|
|
case BrushSurface_Regular:
|
|
case BrushSurface_Sky:
|
|
break;
|
|
}
|
|
|
|
++sizes.num_surfaces;
|
|
sizes.num_vertices += surf->numedges;
|
|
sizes.num_indices += 3 * (surf->numedges - 1);
|
|
}
|
|
|
|
DEBUG("Computed sizes for brush model \"%s\": num_surfaces=%d num_vertices=%d num_indices=%d max_texture_id=%d water_surfaces=%d animated_count=%d water_vertices=%d water_indices=%d", mod->name, sizes.num_surfaces, sizes.num_vertices, sizes.num_indices, sizes.max_texture_id, sizes.water_surfaces, sizes.animated_count, sizes.water_vertices, sizes.water_indices);
|
|
|
|
return sizes;
|
|
}
|
|
|
|
typedef struct {
|
|
const model_t *mod;
|
|
const vk_brush_model_t *bmodel;
|
|
model_sizes_t sizes;
|
|
uint32_t base_vertex_offset;
|
|
uint32_t base_index_offset;
|
|
|
|
vk_render_geometry_t *out_geometries;
|
|
vk_vertex_t *out_vertices;
|
|
uint16_t *out_indices;
|
|
} fill_geometries_args_t;
|
|
|
|
static void getSurfaceNormal( const msurface_t *surf, vec3_t out_normal) {
|
|
if( FBitSet( surf->flags, SURF_PLANEBACK ))
|
|
VectorNegate( surf->plane->normal, out_normal );
|
|
else
|
|
VectorCopy( surf->plane->normal, out_normal );
|
|
|
|
// TODO scale normal by area -- bigger surfaces should have bigger impact
|
|
//VectorScale(normal, surf->plane.
|
|
}
|
|
|
|
static int getSurfaceTexture(const msurface_t *surf, int surface_index) {
|
|
const xvk_patch_surface_t *const psurf = R_VkPatchGetSurface(surface_index);
|
|
if (psurf && psurf->tex_id >= 0)
|
|
return psurf->tex_id;
|
|
return surf->texinfo->texture->gl_texturenum;
|
|
}
|
|
|
|
static qboolean shouldSmoothLinkSurfaces(const model_t* mod, int surf1, int surf2) {
|
|
//return Q_min(surf1, surf2) == 741 && Q_max(surf1, surf2) == 743;
|
|
|
|
// TODO patch filtering
|
|
|
|
// Do not join surfaces with different textures. Assume they belong to different objects.
|
|
const int t1 = getSurfaceTexture(mod->surfaces + surf1, surf1);
|
|
const int t2 = getSurfaceTexture(mod->surfaces + surf2, surf2);
|
|
if (t1 != t2)
|
|
return false;
|
|
|
|
vec3_t n1, n2;
|
|
getSurfaceNormal(mod->surfaces + surf1, n1);
|
|
getSurfaceNormal(mod->surfaces + surf2, n2);
|
|
|
|
const float dot = DotProduct(n1, n2);
|
|
DEBUG("Smoothing: dot(%d, %d) = %f (t=%f)", surf1, surf2, dot, g_map_entities.smoothing.threshold);
|
|
|
|
return dot >= g_map_entities.smoothing.threshold;
|
|
}
|
|
|
|
static int lvFindValue(const linked_value_t *li, int count, int needle) {
|
|
for (int i = 0; i < count; ++i)
|
|
if (li[i].value == needle)
|
|
return i;
|
|
return -1;
|
|
}
|
|
static int lvFindOrAddValue(linked_value_t *li, int *count, int capacity, int needle) {
|
|
const int found = lvFindValue(li, *count, needle);
|
|
if (found >= 0)
|
|
return found;
|
|
if (*count == capacity)
|
|
return -1;
|
|
li[*count].value = needle;
|
|
li[*count].link = *count;
|
|
return (*count)++;
|
|
}
|
|
|
|
static int lvFindBaseIndex(const linked_value_t *li, int index) {
|
|
while (li[index].link != index)
|
|
index = li[index].link;
|
|
return index;
|
|
}
|
|
|
|
static void lvFlatten(linked_value_t *li, int count) {
|
|
for (int i = 0; i < count; ++i) {
|
|
for (int j = i; j < count; ++j) {
|
|
if (lvFindBaseIndex(li, j) == i) {
|
|
li[j].link = i;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void linkSmoothSurfaces(const model_t* mod, int surf1, int surf2, int vertex_index) {
|
|
conn_vertex_t *v = g_brush.conn.vertices + vertex_index;
|
|
|
|
int i1 = lvFindOrAddValue(v->surfs, &v->count, COUNTOF(v->surfs), surf1);
|
|
int i2 = lvFindOrAddValue(v->surfs, &v->count, COUNTOF(v->surfs), surf2);
|
|
|
|
DEBUG("Link %d(%d)<->%d(%d) v=%d", surf1, i1, surf2, i2, vertex_index);
|
|
|
|
if (i1 < 0 || i2 < 0) {
|
|
ERR("Model %s cannot smooth link surf %d<->%d for vertex %d", mod->name, surf1, surf2, vertex_index);
|
|
return;
|
|
}
|
|
|
|
i1 = lvFindBaseIndex(v->surfs, i1);
|
|
i2 = lvFindBaseIndex(v->surfs, i2);
|
|
|
|
// Link them
|
|
v->surfs[Q_max(i1, i2)].link = Q_min(i1, i2);
|
|
}
|
|
|
|
static void connectVertices( const model_t *mod ) {
|
|
if (mod->numedges > g_brush.conn.edges_capacity) {
|
|
if (g_brush.conn.edges)
|
|
Mem_Free(g_brush.conn.edges);
|
|
|
|
g_brush.conn.edges_capacity = mod->numedges;
|
|
g_brush.conn.edges = Mem_Calloc(vk_core.pool, sizeof(*g_brush.conn.edges) * g_brush.conn.edges_capacity);
|
|
}
|
|
|
|
if (mod->numvertexes > g_brush.conn.vertices_capacity) {
|
|
if (g_brush.conn.vertices)
|
|
Mem_Free(g_brush.conn.vertices);
|
|
|
|
g_brush.conn.vertices_capacity = mod->numvertexes;
|
|
g_brush.conn.vertices = Mem_Calloc(vk_core.pool, sizeof(*g_brush.conn.vertices) * g_brush.conn.vertices_capacity);
|
|
}
|
|
|
|
// Find connection edges
|
|
for (int i = 0; i < mod->nummodelsurfaces; ++i) {
|
|
const int surface_index = mod->firstmodelsurface + i;
|
|
const msurface_t *surf = mod->surfaces + surface_index;
|
|
|
|
for(int k = 0; k < surf->numedges; k++) {
|
|
const int iedge_dir = mod->surfedges[surf->firstedge + k];
|
|
const int iedge = iedge_dir >= 0 ? iedge_dir : -iedge_dir;
|
|
|
|
ASSERT(iedge >= 0);
|
|
ASSERT(iedge < mod->numedges);
|
|
|
|
conn_edge_t *cedge = g_brush.conn.edges + iedge;
|
|
if (cedge->count == 0) {
|
|
cedge->first_surface = surface_index;
|
|
} else {
|
|
const medge_t *edge = mod->edges + iedge;
|
|
if (shouldSmoothLinkSurfaces(mod, cedge->first_surface, surface_index)) {
|
|
linkSmoothSurfaces(mod, cedge->first_surface, surface_index, edge->v[0]);
|
|
linkSmoothSurfaces(mod, cedge->first_surface, surface_index, edge->v[1]);
|
|
}
|
|
|
|
if (cedge->count > 1) {
|
|
WARN("Model %s edge %d has %d surfaces", mod->name, i, cedge->count);
|
|
}
|
|
}
|
|
cedge->count++;
|
|
} // for surf->numedges
|
|
} // for mod->nummodelsurfaces
|
|
|
|
int hist[17] = {0};
|
|
for (int i = 0; i < mod->numvertexes; ++i) {
|
|
conn_vertex_t *vtx = g_brush.conn.vertices + i;
|
|
if (vtx->count < 16) {
|
|
hist[vtx->count]++;
|
|
} else {
|
|
hist[16]++;
|
|
}
|
|
|
|
lvFlatten(vtx->surfs, vtx->count);
|
|
|
|
// Too verbose
|
|
#if 0
|
|
if (vtx->count) {
|
|
DEBUG("Vertex %d linked count %d", i, vtx->count);
|
|
for (int j = 0; j < vtx->count; ++j) {
|
|
DEBUG(" %d: l=%d v=%d", j, vtx->surfs[j].link, vtx->surfs[j].value);
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
for (int i = 0; i < COUNTOF(hist); ++i) {
|
|
DEBUG("VTX hist[%d] = %d", i, hist[i]);
|
|
}
|
|
}
|
|
|
|
static qboolean getSmoothedNormalFor(const model_t* mod, int vertex_index, int surface_index, vec3_t out_normal) {
|
|
const conn_vertex_t *v = g_brush.conn.vertices + vertex_index;
|
|
const int index = lvFindValue(v->surfs, v->count, surface_index);
|
|
if (index < 0)
|
|
return false;
|
|
const int base = lvFindBaseIndex(v->surfs, index);
|
|
|
|
vec3_t normal = {0};
|
|
for (int i = 0; i < v->count; ++i) {
|
|
if (v->surfs[i].link == base) {
|
|
const int surface = v->surfs[i].value;
|
|
vec3_t surf_normal = {0};
|
|
getSurfaceNormal(mod->surfaces + surface, surf_normal);
|
|
VectorAdd(normal, surf_normal, normal);
|
|
}
|
|
}
|
|
|
|
VectorNormalize(normal);
|
|
VectorCopy(normal, out_normal);
|
|
return true;
|
|
}
|
|
|
|
static qboolean fillBrushSurfaces(fill_geometries_args_t args) {
|
|
int vertex_offset = 0;
|
|
int num_geometries = 0;
|
|
int animated_count = 0;
|
|
|
|
vk_vertex_t *p_vert = args.out_vertices;
|
|
uint16_t *p_ind = args.out_indices;
|
|
int index_offset = args.base_index_offset;
|
|
|
|
connectVertices(args.mod);
|
|
|
|
// Load sorted by gl_texturenum
|
|
// TODO this does not make that much sense in vulkan (can sort later)
|
|
for (int t = 0; t <= args.sizes.max_texture_id; ++t) {
|
|
for( int i = 0; i < args.mod->nummodelsurfaces; ++i) {
|
|
const int surface_index = args.mod->firstmodelsurface + i;
|
|
msurface_t *surf = args.mod->surfaces + surface_index;
|
|
const mextrasurf_t *info = surf->info;
|
|
vk_render_geometry_t *model_geometry = args.out_geometries + num_geometries;
|
|
const float sample_size = gEngine.Mod_SampleSizeForFace( surf );
|
|
int index_count = 0;
|
|
vec3_t tangent;
|
|
int tex_id = surf->texinfo->texture->gl_texturenum;
|
|
const xvk_patch_surface_t *const psurf = R_VkPatchGetSurface(surface_index);
|
|
|
|
if (t != tex_id)
|
|
continue;
|
|
|
|
if (psurf && psurf->tex_id >= 0)
|
|
tex_id = psurf->tex_id;
|
|
|
|
const brush_surface_type_e type = getSurfaceType(surf, surface_index);
|
|
switch (type) {
|
|
case BrushSurface_Water:
|
|
case BrushSurface_Hidden:
|
|
continue;
|
|
case BrushSurface_Animated:
|
|
args.bmodel->animated_indexes[animated_count++] = num_geometries;
|
|
case BrushSurface_Regular:
|
|
case BrushSurface_Sky:
|
|
break;
|
|
}
|
|
|
|
args.bmodel->surface_to_geometry_index[i] = num_geometries;
|
|
|
|
++num_geometries;
|
|
|
|
//DEBUG( "surface %d: numverts=%d numedges=%d", i, surf->polys ? surf->polys->numverts : -1, surf->numedges );
|
|
|
|
if (vertex_offset + surf->numedges >= UINT16_MAX) {
|
|
// We might be able to handle it by adjusting base_vertex_offset, etc
|
|
ERR("Model %s indices don't fit into 16 bits", args.mod->name);
|
|
return false;
|
|
}
|
|
|
|
VectorClear(model_geometry->emissive);
|
|
|
|
model_geometry->surf_deprecate = surf;
|
|
model_geometry->texture = tex_id;
|
|
|
|
model_geometry->vertex_offset = args.base_vertex_offset;
|
|
model_geometry->max_vertex = vertex_offset + surf->numedges;
|
|
|
|
model_geometry->index_offset = index_offset;
|
|
|
|
if(type == BrushSurface_Sky) {
|
|
model_geometry->material = kXVkMaterialSky;
|
|
} else {
|
|
model_geometry->material = kXVkMaterialRegular;
|
|
ASSERT(!FBitSet( surf->flags, SURF_DRAWTILED ));
|
|
VK_CreateSurfaceLightmap( surf, args.mod );
|
|
}
|
|
|
|
if (FBitSet( surf->flags, SURF_CONVEYOR )) {
|
|
// FIXME make an explicit list of dynamic-uv geometries
|
|
}
|
|
|
|
VectorCopy(surf->texinfo->vecs[0], tangent);
|
|
VectorNormalize(tangent);
|
|
|
|
vec3_t surf_normal;
|
|
getSurfaceNormal(surf, surf_normal);
|
|
|
|
vk_vertex_t *const pvert_begin = p_vert;
|
|
for( int k = 0; k < surf->numedges; k++ )
|
|
{
|
|
const int iedge_dir = args.mod->surfedges[surf->firstedge + k];
|
|
const int iedge = iedge_dir >= 0 ? iedge_dir : -iedge_dir;
|
|
const medge_t *edge = args.mod->edges + iedge;
|
|
const int vertex_index = iedge_dir >= 0 ? edge->v[0] : edge->v[1];
|
|
const mvertex_t *in_vertex = args.mod->vertexes + vertex_index;
|
|
|
|
vk_vertex_t vertex = {
|
|
{in_vertex->position[0], in_vertex->position[1], in_vertex->position[2]},
|
|
};
|
|
|
|
vertex.prev_pos[0] = in_vertex->position[0];
|
|
vertex.prev_pos[1] = in_vertex->position[1];
|
|
vertex.prev_pos[2] = in_vertex->position[2];
|
|
|
|
{
|
|
vec4_t svec, tvec;
|
|
if (psurf && (psurf->flags & Patch_Surface_STvecs)) {
|
|
Vector4Copy(psurf->s_vec, svec);
|
|
Vector4Copy(psurf->t_vec, tvec);
|
|
} else {
|
|
Vector4Copy(surf->texinfo->vecs[0], svec);
|
|
Vector4Copy(surf->texinfo->vecs[1], tvec);
|
|
}
|
|
|
|
float s_off = 0, t_off = 0;
|
|
float s_sc = 1, t_sc = 1;
|
|
|
|
if (psurf && (psurf->flags & Patch_Surface_TexOffset)) {
|
|
s_off = psurf->tex_offset[0];
|
|
t_off = psurf->tex_offset[1];
|
|
}
|
|
|
|
if (psurf && (psurf->flags & Patch_Surface_TexScale)) {
|
|
s_sc = psurf->tex_scale[0];
|
|
t_sc = psurf->tex_scale[1];
|
|
}
|
|
|
|
const float s = s_off + s_sc * DotProduct( in_vertex->position, svec ) + svec[3];
|
|
const float t = t_off + t_sc * DotProduct( in_vertex->position, tvec ) + tvec[3];
|
|
|
|
vertex.gl_tc[0] = s / surf->texinfo->texture->width;
|
|
vertex.gl_tc[1] = t / surf->texinfo->texture->height;
|
|
}
|
|
|
|
// lightmap texture coordinates
|
|
{
|
|
float s = DotProduct( in_vertex->position, info->lmvecs[0] ) + info->lmvecs[0][3];
|
|
s -= info->lightmapmins[0];
|
|
s += surf->light_s * sample_size;
|
|
s += sample_size * 0.5f;
|
|
s /= BLOCK_SIZE * sample_size; //fa->texinfo->texture->width;
|
|
|
|
float t = DotProduct( in_vertex->position, info->lmvecs[1] ) + info->lmvecs[1][3];
|
|
t -= info->lightmapmins[1];
|
|
t += surf->light_t * sample_size;
|
|
t += sample_size * 0.5f;
|
|
t /= BLOCK_SIZE * sample_size; //fa->texinfo->texture->height;
|
|
|
|
vertex.lm_tc[0] = s;
|
|
vertex.lm_tc[1] = t;
|
|
}
|
|
|
|
// Compute smoothed normal if needed
|
|
if (!getSmoothedNormalFor(args.mod, vertex_index, surface_index, vertex.normal)) {
|
|
VectorCopy(surf_normal, vertex.normal);
|
|
}
|
|
|
|
VectorCopy(tangent, vertex.tangent);
|
|
|
|
Vector4Set(vertex.color, 255, 255, 255, 255);
|
|
|
|
*(p_vert++) = vertex;
|
|
|
|
// Ray tracing apparently expects triangle list only (although spec is not very clear about this kekw)
|
|
if (k > 1) {
|
|
*(p_ind++) = (uint16_t)(vertex_offset + 0);
|
|
*(p_ind++) = (uint16_t)(vertex_offset + k - 1);
|
|
*(p_ind++) = (uint16_t)(vertex_offset + k);
|
|
index_count += 3;
|
|
index_offset += 3;
|
|
}
|
|
} // for surf->numedges
|
|
|
|
model_geometry->element_count = index_count;
|
|
vertex_offset += surf->numedges;
|
|
} // for mod->nummodelsurfaces
|
|
}
|
|
|
|
ASSERT(args.sizes.num_surfaces == num_geometries);
|
|
ASSERT(args.sizes.animated_count == animated_count);
|
|
return true;
|
|
}
|
|
|
|
static const xvk_mapent_func_wall_t *getModelFuncWallPatch( const model_t *const mod ) {
|
|
for (int i = 0; i < g_map_entities.func_walls_count; ++i) {
|
|
const xvk_mapent_func_wall_t *const fw = g_map_entities.func_walls + i;
|
|
if (Q_strcmp(mod->name, fw->model) == 0) {
|
|
return fw;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static qboolean createRenderModel( const model_t *mod, vk_brush_model_t *bmodel, const model_sizes_t sizes ) {
|
|
bmodel->geometry = R_GeometryRangeAlloc(sizes.num_vertices, sizes.num_indices);
|
|
if (!bmodel->geometry.block_handle.size) {
|
|
ERR("Cannot allocate geometry for %s", mod->name );
|
|
return false;
|
|
}
|
|
|
|
vk_render_geometry_t *const geometries = Mem_Malloc(vk_core.pool, sizeof(vk_render_geometry_t) * sizes.num_surfaces);
|
|
bmodel->surface_to_geometry_index = Mem_Malloc(vk_core.pool, sizeof(int) * mod->nummodelsurfaces);
|
|
bmodel->animated_indexes = Mem_Malloc(vk_core.pool, sizeof(int) * sizes.animated_count);
|
|
bmodel->animated_indexes_count = sizes.animated_count;
|
|
|
|
if (sizes.animated_count > MAX_ANIMATED_TEXTURES) {
|
|
WARN("Too many animated textures %d for model \"%s\" some surfaces can be static", sizes.animated_count, mod->name);
|
|
}
|
|
|
|
const r_geometry_range_lock_t geom_lock = R_GeometryRangeLock(&bmodel->geometry);
|
|
|
|
const qboolean fill_result = fillBrushSurfaces((fill_geometries_args_t){
|
|
.mod = mod,
|
|
.bmodel = bmodel,
|
|
.sizes = sizes,
|
|
.base_vertex_offset = bmodel->geometry.vertices.unit_offset,
|
|
.base_index_offset = bmodel->geometry.indices.unit_offset,
|
|
.out_geometries = geometries,
|
|
.out_vertices = geom_lock.vertices,
|
|
.out_indices = geom_lock.indices,
|
|
});
|
|
|
|
R_GeometryRangeUnlock( &geom_lock );
|
|
|
|
if (!fill_result) {
|
|
// TODO unlock and free buffers if failed? Currently we can't free geometry range, as it is being implicitly referenced by staging queue. Flush staging and free?
|
|
// This shouldn't really happen btw, kind of unrecoverable for now tbh.
|
|
// Also, we might just handle it, as the only reason it can fail is 16 bit index overflow.
|
|
// I. Split into smaller geometries sets.
|
|
// II. Make indices 32 bit
|
|
return false;
|
|
}
|
|
|
|
if (!R_RenderModelCreate(&bmodel->render_model, (vk_render_model_init_t){
|
|
.name = mod->name,
|
|
.geometries = geometries,
|
|
.geometries_count = sizes.num_surfaces,
|
|
.dynamic = false,
|
|
})) {
|
|
ERR("Could not create render model for brush model %s", mod->name);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
qboolean VK_BrushModelLoad( model_t *mod ) {
|
|
if (mod->cache.data) {
|
|
WARN("Model %s was already loaded", mod->name );
|
|
return true;
|
|
}
|
|
|
|
DEBUG("%s: %s flags=%08x", __FUNCTION__, mod->name, mod->flags);
|
|
|
|
vk_brush_model_t *bmodel = Mem_Calloc(vk_core.pool, sizeof(*bmodel));
|
|
ASSERT(g_brush.models_count < COUNTOF(g_brush.models));
|
|
g_brush.models[g_brush.models_count++] = bmodel;
|
|
|
|
bmodel->engine_model = mod;
|
|
mod->cache.data = bmodel;
|
|
|
|
Matrix4x4_LoadIdentity(bmodel->prev_transform);
|
|
bmodel->prev_time = gpGlobals->time;
|
|
|
|
const model_sizes_t sizes = computeSizes( mod );
|
|
|
|
if (sizes.num_surfaces != 0) {
|
|
if (!createRenderModel(mod, bmodel, sizes)) {
|
|
ERR("Could not load brush model %s", mod->name);
|
|
// FIXME Cannot deallocate bmodel as we might still have staging references to its memory
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (sizes.water_surfaces) {
|
|
if (!brushCreateWaterModel(mod, bmodel, sizes)) {
|
|
ERR("Could not load brush water model %s", mod->name);
|
|
// FIXME Cannot deallocate bmodel as we might still have staging references to its memory
|
|
return false;
|
|
}
|
|
}
|
|
|
|
g_brush.stat.total_vertices += sizes.num_indices + sizes.water_vertices;
|
|
g_brush.stat.total_indices += sizes.num_vertices + sizes.water_indices;
|
|
|
|
DEBUG("Model %s loaded surfaces: %d (of %d); total vertices: %u, total indices: %u",
|
|
mod->name, bmodel->render_model.num_geometries, mod->nummodelsurfaces, g_brush.stat.total_vertices, g_brush.stat.total_indices);
|
|
|
|
return true;
|
|
}
|
|
|
|
static void VK_BrushModelDestroy( vk_brush_model_t *bmodel ) {
|
|
ASSERT(bmodel->engine_model);
|
|
|
|
DEBUG("%s: %s", __FUNCTION__, bmodel->engine_model->name);
|
|
|
|
ASSERT(bmodel->engine_model->cache.data == bmodel);
|
|
ASSERT(bmodel->engine_model->type == mod_brush);
|
|
|
|
if (bmodel->water.surfaces_count) {
|
|
R_RenderModelDestroy(&bmodel->water.render_model);
|
|
Mem_Free((int*)bmodel->water.surfaces_indices);
|
|
Mem_Free(bmodel->water.render_model.geometries);
|
|
R_GeometryRangeFree(&bmodel->water.geometry);
|
|
}
|
|
|
|
R_RenderModelDestroy(&bmodel->render_model);
|
|
|
|
if (bmodel->animated_indexes)
|
|
Mem_Free(bmodel->animated_indexes);
|
|
|
|
if (bmodel->surface_to_geometry_index)
|
|
Mem_Free(bmodel->surface_to_geometry_index);
|
|
|
|
if (bmodel->render_model.geometries) {
|
|
Mem_Free(bmodel->render_model.geometries);
|
|
R_GeometryRangeFree(&bmodel->geometry);
|
|
}
|
|
|
|
bmodel->engine_model->cache.data = NULL;
|
|
Mem_Free(bmodel);
|
|
}
|
|
|
|
void VK_BrushModelDestroyAll( void ) {
|
|
DEBUG("Destroying %d brush models", g_brush.models_count);
|
|
for( int i = 0; i < g_brush.models_count; i++ )
|
|
VK_BrushModelDestroy(g_brush.models[i]);
|
|
|
|
g_brush.stat.total_vertices = 0;
|
|
g_brush.stat.total_indices = 0;
|
|
g_brush.models_count = 0;
|
|
|
|
memset(g_brush.conn.edges, 0, sizeof(*g_brush.conn.edges) * g_brush.conn.edges_capacity);
|
|
|
|
memset(g_brush.conn.vertices, 0, sizeof(*g_brush.conn.vertices) * g_brush.conn.vertices_capacity);
|
|
}
|
|
|
|
static rt_light_add_polygon_t loadPolyLight(const model_t *mod, const int surface_index, const msurface_t *surf, const vec3_t emissive) {
|
|
rt_light_add_polygon_t lpoly = {0};
|
|
lpoly.num_vertices = Q_min(7, surf->numedges);
|
|
|
|
// TODO split, don't clip
|
|
if (surf->numedges > 7)
|
|
WARN("emissive surface %d has %d vertices; clipping to 7", surface_index, surf->numedges);
|
|
|
|
VectorCopy(emissive, lpoly.emissive);
|
|
|
|
for (int i = 0; i < lpoly.num_vertices; ++i) {
|
|
const int iedge = mod->surfedges[surf->firstedge + i];
|
|
const medge_t *edge = mod->edges + (iedge >= 0 ? iedge : -iedge);
|
|
const mvertex_t *vertex = mod->vertexes + (iedge >= 0 ? edge->v[0] : edge->v[1]);
|
|
VectorCopy(vertex->position, lpoly.vertices[i]);
|
|
}
|
|
|
|
lpoly.surface = surf;
|
|
return lpoly;
|
|
}
|
|
|
|
void R_VkBrushModelCollectEmissiveSurfaces( const struct model_s *mod, qboolean is_worldmodel ) {
|
|
vk_brush_model_t *const bmodel = mod->cache.data;
|
|
ASSERT(bmodel);
|
|
|
|
const xvk_mapent_func_wall_t *func_wall = getModelFuncWallPatch(mod);
|
|
const qboolean is_static = is_worldmodel || func_wall;
|
|
|
|
typedef struct {
|
|
int model_surface_index;
|
|
int surface_index;
|
|
const msurface_t *surf;
|
|
vec3_t emissive;
|
|
} emissive_surface_t;
|
|
emissive_surface_t emissive_surfaces[MAX_SURFACE_LIGHTS];
|
|
int geom_indices[MAX_SURFACE_LIGHTS];
|
|
int emissive_surfaces_count = 0;
|
|
|
|
// Load list of all emissive surfaces
|
|
for( int i = 0; i < mod->nummodelsurfaces; ++i) {
|
|
const int surface_index = mod->firstmodelsurface + i;
|
|
const msurface_t *surf = mod->surfaces + surface_index;
|
|
|
|
switch (getSurfaceType(surf, surface_index)) {
|
|
case BrushSurface_Regular:
|
|
case BrushSurface_Animated:
|
|
break;
|
|
default:
|
|
continue;
|
|
}
|
|
|
|
const int tex_id = surf->texinfo->texture->gl_texturenum; // TODO animation?
|
|
|
|
vec3_t emissive;
|
|
const xvk_patch_surface_t *const psurf = R_VkPatchGetSurface(surface_index);
|
|
if (psurf && (psurf->flags & Patch_Surface_Emissive)) {
|
|
VectorCopy(psurf->emissive, emissive);
|
|
} else if (RT_GetEmissiveForTexture(emissive, tex_id)) {
|
|
// emissive
|
|
} else {
|
|
// not emissive, continue to the next
|
|
continue;
|
|
}
|
|
|
|
DEBUG("%d: i=%d surf_index=%d tex_id=%d patch=%d(%#x) => emissive=(%f,%f,%f)", emissive_surfaces_count, i, surface_index, tex_id, !!psurf, psurf?psurf->flags:0, emissive[0], emissive[1], emissive[2]);
|
|
|
|
if (emissive_surfaces_count == MAX_SURFACE_LIGHTS) {
|
|
ERR("Too many emissive surfaces for model %s: max=%d", mod->name, MAX_SURFACE_LIGHTS);
|
|
break;
|
|
}
|
|
|
|
emissive_surface_t* const surface = &emissive_surfaces[emissive_surfaces_count++];
|
|
surface->model_surface_index = i;
|
|
surface->surface_index = surface_index;
|
|
surface->surf = surf;
|
|
VectorCopy(emissive, surface->emissive);
|
|
}
|
|
|
|
// Clear old per-geometry emissive values. The new emissive values will be assigned by the loop below only to the relevant geoms
|
|
for (int i = 0; i < bmodel->render_model.num_geometries; ++i) {
|
|
vk_render_geometry_t *const geom = bmodel->render_model.geometries + i;
|
|
VectorClear(geom->emissive);
|
|
}
|
|
|
|
// Non-static brush models may move around and so must have their emissive surfaces treated as dynamic
|
|
if (!is_static) {
|
|
if (bmodel->render_model.dynamic_polylights)
|
|
Mem_Free(bmodel->render_model.dynamic_polylights);
|
|
bmodel->render_model.dynamic_polylights_count = emissive_surfaces_count;
|
|
bmodel->render_model.dynamic_polylights = Mem_Malloc(vk_core.pool, sizeof(bmodel->render_model.dynamic_polylights[0]) * emissive_surfaces_count);
|
|
}
|
|
|
|
// Apply all emissive surfaces found
|
|
for (int i = 0; i < emissive_surfaces_count; ++i) {
|
|
const emissive_surface_t* const s = emissive_surfaces + i;
|
|
|
|
rt_light_add_polygon_t polylight = loadPolyLight(mod, s->surface_index, s->surf, s->emissive);
|
|
|
|
// func_wall surfaces do not really belong to BSP+PVS system, so they can't be used
|
|
// for lights visibility calculation directly.
|
|
if (func_wall) {
|
|
// TODO this is not really dynamic, but this flag signals using MovingSurface visibility calc
|
|
polylight.dynamic = true;
|
|
matrix3x4 m;
|
|
Matrix3x4_LoadIdentity(m);
|
|
Matrix3x4_SetOrigin(m, func_wall->origin[0], func_wall->origin[1], func_wall->origin[2]);
|
|
polylight.transform_row = &m;
|
|
}
|
|
|
|
// Static emissive surfaces are added immediately, as they are drawn all the time.
|
|
// Non-static ones will be applied later when the model is actually rendered
|
|
if (is_static) {
|
|
RT_LightAddPolygon(&polylight);
|
|
} else {
|
|
bmodel->render_model.dynamic_polylights[i] = polylight;
|
|
}
|
|
|
|
// Assign the emissive value to the right geometry
|
|
const int geom_index = bmodel->surface_to_geometry_index[s->model_surface_index];
|
|
geom_indices[i] = geom_index;
|
|
VectorCopy(polylight.emissive, bmodel->render_model.geometries[geom_index].emissive);
|
|
}
|
|
|
|
if (emissive_surfaces_count > 0) {
|
|
// Update emissive values in kusochki. This is required because initial VK_BrushModelLoad happens before we've read
|
|
// RAD data in vk_light.c, so the emissive values are empty. This is the place and time where we actually get to
|
|
// know them, so let's fixup things.
|
|
// TODO minor optimization: sort geom_indices to have a better chance for them to be sequential
|
|
R_RenderModelUpdateMaterials(&bmodel->render_model, geom_indices, emissive_surfaces_count);
|
|
INFO("Loaded %d polylights for %s model %s", emissive_surfaces_count, is_static ? "static" : "movable", mod->name);
|
|
}
|
|
}
|