Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
*.tbl text eol=lf
*.tbm text eol=lf
*.sdr text eol=lf

# Test inputs that are deliberately not UTF-8, kept out of text diffs
test/test_data/graphics/texture_material/data/maps/mat_sharp_s.mat binary
test/test_data/graphics/texture_material/data/maps/mat_truncated.mat binary
4 changes: 4 additions & 0 deletions code/bmpman/bm_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "bmpman/bmpman.h"

struct texture_material;

#include <array>

/**
Expand Down Expand Up @@ -63,6 +65,8 @@ struct bitmap_entry {

int dir_type; //!< which directory this was loaded from (to skip other locations with same name)

const texture_material* material; //!< settings from the texture's .mat file, or nullptr

// compressed bitmap stuff (.dds) - RT please take a look at this and tell me if we really need it
size_t mem_taken; //!< How much memory does this bitmap use? - UnknownPlayer
int num_mipmaps; //!< number of mipmap levels, we need to read all of them
Expand Down
19 changes: 19 additions & 0 deletions code/bmpman/bmpman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "globalincs/systemvars.h"
#include "graphics/2d.h"
#include "graphics/matrix.h"
#include "graphics/texture_material.h"
#include "io/key.h"
#include "io/timer.h"
#include "jpgutils/jpgutils.h"
Expand Down Expand Up @@ -1112,6 +1113,13 @@ int bm_is_valid(int handle) {
return entry->handle == handle;
}

const texture_material* bm_get_texture_material(int handle) {
if (!bm_is_valid(handle))
return nullptr;

return bm_get_entry(handle)->material;
}


// Load an image and validate it while retrieving information for later use
// Input: type = current BM_TYPE_*
Expand Down Expand Up @@ -1272,12 +1280,17 @@ int bm_load(const char *real_filename, int dir_type) {
return -1;
}

const texture_material* material = nullptr;

// Lets find out what type it is
{
// see if it's already loaded (checks for any type with filename)
if (bm_load_sub_fast(filename, &handle, dir_type))
return handle;

// before any file is opened or slot filled, since a bad .mat can throw in unit tests
material = texture_material_find(filename, dir_type);

// if we are still here then we need to fall back to a file-based search
int rval = bm_load_sub_slow(filename, BM_NUM_TYPES, bm_ext_list, &img_cfp, dir_type);

Expand Down Expand Up @@ -1337,6 +1350,7 @@ int bm_load(const char *real_filename, int dir_type) {
entry->mem_taken = (size_t)bm_size;
entry->dir_type = dir_type;
entry->handle = handle;
entry->material = material;

entry->load_count++;

Expand Down Expand Up @@ -1600,6 +1614,8 @@ int bm_load_animation(const char *real_filename, int *nframes, int *fps, int *ke
// used later if EFF type
strcpy_s(clean_name, filename);

const texture_material* material = nullptr;

// Lets find out what type it is
{
int handle = -1;
Expand All @@ -1623,6 +1639,8 @@ int bm_load_animation(const char *real_filename, int *nframes, int *fps, int *ke
return handle;
}

material = texture_material_find(clean_name, dir_type);

// if we are still here then we need to fall back to a file-based search
int rval = bm_load_sub_slow(filename, BM_ANI_NUM_TYPES, bm_ani_ext_list, &img_cfp, dir_type);

Expand Down Expand Up @@ -1832,6 +1850,7 @@ int bm_load_animation(const char *real_filename, int *nframes, int *fps, int *ke
entry->num_mipmaps = mm_lvl;
entry->mem_taken = (size_t)img_size;
entry->dir_type = dir_type;
entry->material = material;

entry->load_count++;

Expand Down
9 changes: 9 additions & 0 deletions code/bmpman/bmpman.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,15 @@ void bm_unlock(int handle);
*/
int bm_is_valid(int handle);

struct texture_material;

/**
* @brief Gets the settings from the bitmap's .mat file
*
* @returns The settings, or nullptr if the handle is invalid or the bitmap has no .mat file
*/
const texture_material* bm_get_texture_material(int handle);

/**
* @brief Gets info on the bitmap indexed by handle.
*
Expand Down
34 changes: 23 additions & 11 deletions code/cfile/cfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "cfilesystem.h"


#include <algorithm>
#include <limits>

char Cfile_root_dir[CFILE_ROOT_DIRECTORY_LEN] = "";
Expand All @@ -52,7 +53,7 @@ cf_pathtype Pathtypes[CF_MAX_PATH_TYPES] = {
// Root must be index 1!!
{ CF_TYPE_ROOT, "", ".mve .ogg", CF_TYPE_ROOT },
{ CF_TYPE_DATA, "data", ".cfg .txt", CF_TYPE_ROOT },
{ CF_TYPE_MAPS, "data" DIR_SEPARATOR_STR "maps", ".pcx .ani .eff .tga .jpg .png .dds .ktx", CF_TYPE_DATA },
{ CF_TYPE_MAPS, "data" DIR_SEPARATOR_STR "maps", ".pcx .ani .eff .tga .jpg .png .dds .ktx .mat", CF_TYPE_DATA },
{ CF_TYPE_TEXT, "data" DIR_SEPARATOR_STR "text", ".txt .net", CF_TYPE_DATA },
{ CF_TYPE_MODELS, "data" DIR_SEPARATOR_STR "models", ".pof", CF_TYPE_DATA },
{ CF_TYPE_TABLES, "data" DIR_SEPARATOR_STR "tables", ".tbl .tbm .lua", CF_TYPE_DATA },
Expand All @@ -68,7 +69,7 @@ cf_pathtype Pathtypes[CF_MAX_PATH_TYPES] = {
{ CF_TYPE_MOVIES, "data" DIR_SEPARATOR_STR "movies", ".mve .msb .ogg .mp4 .srt .webm .png",CF_TYPE_DATA },
{ CF_TYPE_INTERFACE, "data" DIR_SEPARATOR_STR "interface", ".pcx .ani .dds .tga .eff .png .jpg .rml .rcss .ktx", CF_TYPE_DATA },
{ CF_TYPE_FONT, "data" DIR_SEPARATOR_STR "fonts", ".vf .ttf .otf", CF_TYPE_DATA },
{ CF_TYPE_EFFECTS, "data" DIR_SEPARATOR_STR "effects", ".ani .eff .pcx .neb .tga .jpg .png .dds .ktx .sdr .vert .frag .glsl", CF_TYPE_DATA },
{ CF_TYPE_EFFECTS, "data" DIR_SEPARATOR_STR "effects", ".ani .eff .pcx .neb .tga .jpg .png .dds .ktx .sdr .vert .frag .glsl .mat", CF_TYPE_DATA },
{ CF_TYPE_HUD, "data" DIR_SEPARATOR_STR "hud", ".pcx .ani .eff .tga .jpg .png .dds .ktx", CF_TYPE_DATA },
{ CF_TYPE_PLAYERS, "data" DIR_SEPARATOR_STR "players", ".hcf", /* DON'T add pilot files here!! */ CF_TYPE_DATA },
{ CF_TYPE_PLAYER_IMAGES, "data" DIR_SEPARATOR_STR "players" DIR_SEPARATOR_STR "images", ".pcx .png .dds .ktx", CF_TYPE_PLAYERS },
Expand Down Expand Up @@ -1107,26 +1108,37 @@ void cfread_string(char *buf, int n, CFILE *file)

void cfread_string_len(char *buf, int buffer_size, CFILE *file)
{
int len;
len = cfread_int(file);
Assertion( (len <= buffer_size), "String cannot fit in buffer, len: %i, buffer: %i", len, buffer_size);
if (len) {
cfread(buf, len, 1, file);
Assertion(len != buffer_size || buf[len - 1] == 0, "Unterminated string does not fit in buffer");
Assertion(buffer_size > 0, "cfread_string_len needs room for at least the terminator, buffer: %i", buffer_size);

int len = cfread_int(file);
int to_read = std::clamp(len, 0, buffer_size - 1);

if (to_read > 0 && cfread(buf, to_read, 1, file) != 1) {
to_read = 0;
}
buf[to_read] = 0;

buf[MIN(len, buffer_size - 1)] = 0;
if (len > to_read) {
if (len > buffer_size) {
mprintf(("cfread_string_len: a string of %d characters was cut to fit a buffer of %d in %s\n", len, buffer_size, cf_get_filename(file)));
}
// skip what didn't fit so the next read starts after this string
cfseek(file, len - to_read, CF_SEEK_CUR);
}
}

SCP_string cfread_string_len(CFILE *file)
{
int len = cfread_int(file);
// a corrupt length must not make us allocate more than the file can still hold
len = std::clamp(len, 0, std::max(cfilelength(file) - cftell(file), 0));

SCP_string str;
str.resize(len);

if (len)
cfread(&str[0], len, 1, file);
if (len > 0 && cfread(str.data(), len, 1, file) != 1) {
str.clear();
}

return str;
}
Expand Down
8 changes: 6 additions & 2 deletions code/cfile/cfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ void cfread_string(char *buf,int n, CFILE *file);
/**
* @brief Read a fixed length string that is not null-terminated, with the length stored in file.
*
* If it is not null-terminated, the length of the string *must* be less than the size of the buffer (to make room for one)
*
* Reads at most buffer_size - 1 characters and skips the rest of the string in the file, so the next read
* starts after it. A negative length reads nothing.
*
* @param buf Pre-allocated array to store string
* @param buffer_size Size of pre-allocated array
* @param file File to read from
Expand All @@ -332,6 +333,9 @@ void cfread_string_len(char *buf,int buffer_size, CFILE *file);

/**
* @brief Read a string from the file where the length is stored in the file
*
* A negative length reads nothing, and a length past the end of the file reads only what is left.
*
* @param file The file to read the string from
* @return The string that was read
*/
Expand Down
6 changes: 4 additions & 2 deletions code/def_files/data/effects/decal-f.sdr
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ uniform decalInfoData {
int diffuse_blend_mode;

int glow_blend_mode;
float glow_intensity;
float glow_hdr_multiplier;
};

vec3 computeViewPosition(vec2 textureCoord) {
Expand Down Expand Up @@ -180,9 +182,9 @@ void main() {
// We have a valid glow map
vec4 color = texture(glowMap, vec3(tex_coord, float(glow_index)));

color.rgb = srgb_to_linear(color.rgb) * GLOW_MAP_SRGB_MULTIPLIER;
color.rgb = srgb_to_linear(color.rgb) * glow_hdr_multiplier;

color.rgb *= GLOW_MAP_INTENSITY;
color.rgb *= glow_intensity;

if (glow_blend_mode == 0) {
// Normal alpha blending
Expand Down
2 changes: 2 additions & 0 deletions code/def_files/data/effects/decal-v.sdr
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ uniform decalInfoData {
int diffuse_blend_mode;

int glow_blend_mode;
float glow_intensity;
float glow_hdr_multiplier;
};

void main() {
Expand Down
1 change: 1 addition & 0 deletions code/def_files/data/effects/lighting.sdr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const int LT_CONE = 3; // A cone light, like a flood light
const int LT_AMBIENT = 4; // Directionless ambient light

const float SPEC_FACTOR_NO_SPEC_MAP = 0.1;
// no longer used here; kept for mods that ship their own shaders referencing them
const float GLOW_MAP_INTENSITY = 1.5;
const float GLOW_MAP_SRGB_MULTIPLIER = 3.0;
const float PI = 3.14159f;
Expand Down
6 changes: 4 additions & 2 deletions code/def_files/data/effects/main-f.sdr
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ uniform modelData {
int sMiscmapIndex;
float alphaMult;
int flags;
float glowIntensity;
float glowHdrMultiplier;
};

#ifdef VULKAN
Expand Down Expand Up @@ -444,9 +446,9 @@ void main()
#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_TEAMCOLOR
#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_MISC
#prereplace IF_FLAG MODEL_SDR_FLAG_HDR
glowColor = srgb_to_linear(glowColor) * GLOW_MAP_SRGB_MULTIPLIER;
glowColor = srgb_to_linear(glowColor) * glowHdrMultiplier;
#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_HDR
emissiveColor.rgb += glowColor * GLOW_MAP_INTENSITY;
emissiveColor.rgb += glowColor * glowIntensity;
#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_GLOW

#prereplace IF_FLAG MODEL_SDR_FLAG_ALPHA_MULT
Expand Down
2 changes: 2 additions & 0 deletions code/def_files/data/effects/main-g.sdr
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ layout (std140) uniform modelData {
float alphaMult;

int flags;
float glowIntensity;
float glowHdrMultiplier;
};

in VertexOutput {
Expand Down
2 changes: 2 additions & 0 deletions code/def_files/data/effects/main-v.sdr
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ uniform modelData {
float alphaMult;

int flags;
float glowIntensity;
float glowHdrMultiplier;
};

#ifdef VULKAN
Expand Down
6 changes: 6 additions & 0 deletions code/graphics/decal_draw_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "render/3d.h"
#include "tracing/tracing.h"
#include "light.h"
#include "graphics/texture_material.h"

namespace {

Expand Down Expand Up @@ -96,6 +97,11 @@ void decal_draw_list::prepare_global_data() {
bm_get_base_frame(batch_info.normal));
info->diffuse_blend_mode = draw_info.first.material.get_blend_mode(0) == ALPHA_BLEND_ADDITIVE ? 1 : 0;
info->glow_blend_mode = draw_info.first.material.get_blend_mode(2) == ALPHA_BLEND_ADDITIVE ? 1 : 0;
// glow-only decals have no diffuse texture to name the .mat after
const auto mat = bm_get_texture_material(batch_info.diffuse >= 0 ? batch_info.diffuse : batch_info.glow);
const auto glow = mat != nullptr ? mat->glow : texture_glow_settings();
info->glow_intensity = texture_glow_intensity(glow);
info->glow_hdr_multiplier = texture_glow_hdr_multiplier(glow);
}
}

Expand Down
12 changes: 12 additions & 0 deletions code/graphics/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,18 @@ void model_material::reset_alpha_mult() {
Alpha_mult = 1.0f;
}

void model_material::set_glow(const texture_glow_settings& glow) {
Glow = glow;
}

float model_material::get_glow_intensity() const {
return texture_glow_intensity(Glow);
}

float model_material::get_glow_hdr_multiplier() const {
return texture_glow_hdr_multiplier(Glow);
}

uint model_material::get_shader_flags() const
{
uint Shader_flags = 0;
Expand Down
7 changes: 7 additions & 0 deletions code/graphics/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _MATERIAL_H

#include "graphics/grinternal.h"
#include "graphics/texture_material.h"
#include "model/model.h"

#include <array>
Expand Down Expand Up @@ -203,6 +204,8 @@ class model_material : public material
bool Use_alpha_mult = false;
float Alpha_mult = 1.0f;

texture_glow_settings Glow;

public:
model_material();

Expand Down Expand Up @@ -263,6 +266,10 @@ class model_material : public material
void set_alpha_mult(float alpha);
void reset_alpha_mult();

void set_glow(const texture_glow_settings& glow);
float get_glow_intensity() const;
float get_glow_hdr_multiplier() const;

};

class particle_material : public material
Expand Down
Loading
Loading