Skip to content
Merged
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
11 changes: 10 additions & 1 deletion code/graphics/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ int material::get_texture_map(int tex_type) const
return Texture_maps[tex_type];
}

int material::get_specular_map(bool* is_spec_gloss) const
{
const auto reflect_map = get_texture_map(TM_SPEC_GLOSS_TYPE);
if (is_spec_gloss != nullptr) {
*is_spec_gloss = reflect_map >= 0;
}
return reflect_map >= 0 ? reflect_map : get_texture_map(TM_SPECULAR_TYPE);
}

bool material::is_textured() const
{
return
Expand Down Expand Up @@ -787,7 +796,7 @@ int model_material::get_shader_runtime_flags() const {
flags |= MODEL_SDR_FLAG_DIFFUSE;
if (get_texture_map(TM_GLOW_TYPE) >= 0)
flags |= MODEL_SDR_FLAG_GLOW;
if (get_texture_map(TM_SPECULAR_TYPE) >= 0 || get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0)
if (get_specular_map() >= 0)
flags |= MODEL_SDR_FLAG_SPEC;
if (get_texture_map(TM_NORMAL_TYPE) >= 0)
flags |= MODEL_SDR_FLAG_NORMAL;
Expand Down
5 changes: 5 additions & 0 deletions code/graphics/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ class material

void set_texture_map(int tex_type, int texture_num);
int get_texture_map(int tex_type) const;

// Returns the -reflect map if present; otherwise returns the -shine slot value (-1 when absent).
// If supplied, is_spec_gloss reports whether the chosen texture uses specular/gloss encoding.
int get_specular_map(bool* is_spec_gloss = nullptr) const;

bool is_textured() const;

void set_texture_type(texture_type t_type);
Expand Down
25 changes: 8 additions & 17 deletions code/graphics/opengl/gropengltnl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1004,23 +1004,14 @@ void opengl_tnl_set_model_material(model_material *material_info)
1);
}

if (material_info->get_texture_map(TM_SPECULAR_TYPE) >= 0 ||
material_info->get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) {
if (material_info->get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) {
gr_opengl_tcache_set(material_info->get_texture_map(TM_SPEC_GLOSS_TYPE),
TCACHE_TYPE_NORMAL,
&u_scale,
&v_scale,
&array_index,
2);
} else {
gr_opengl_tcache_set(material_info->get_texture_map(TM_SPECULAR_TYPE),
TCACHE_TYPE_NORMAL,
&u_scale,
&v_scale,
&array_index,
2);
}
const auto specular_map = material_info->get_specular_map();
if (specular_map >= 0) {
gr_opengl_tcache_set(specular_map,
TCACHE_TYPE_NORMAL,
&u_scale,
&v_scale,
&array_index,
2);
}

if (material_info->get_texture_map(TM_NORMAL_TYPE) >= 0) {
Expand Down
19 changes: 6 additions & 13 deletions code/graphics/uniforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,12 @@ void convert_model_material(model_uniform_data* data_out,
}


if (material.get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) {
data_out->sSpecmapIndex = bm_get_array_index(material.get_texture_map(TM_SPEC_GLOSS_TYPE));

data_out->gammaSpec = 1;
data_out->alphaGloss = 1;

}

if (material.get_texture_map(TM_SPECULAR_TYPE) >= 0) {
data_out->sSpecmapIndex = bm_get_array_index(material.get_texture_map(TM_SPECULAR_TYPE));

data_out->gammaSpec = 0;
data_out->alphaGloss = 0;
bool is_spec_gloss;
const auto specular_map = material.get_specular_map(&is_spec_gloss);
if (specular_map >= 0) {
data_out->sSpecmapIndex = bm_get_array_index(specular_map);
data_out->gammaSpec = is_spec_gloss ? 1 : 0;
data_out->alphaGloss = is_spec_gloss ? 1 : 0;
}

if (material.get_texture_map(TM_NORMAL_TYPE) >= 0) {
Expand Down
13 changes: 2 additions & 11 deletions code/graphics/vulkan/VulkanDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1304,12 +1304,7 @@ bool VulkanDrawManager::bindMaterialTextures(material* mat, DescriptorWriter* wr
setTexture(mat->get_texture_map(TM_BASE_TYPE), TextureSlot::BaseMap, true);
setTexture(mat->get_texture_map(TM_GLOW_TYPE), TextureSlot::GlowMap);

// Specular - prefer spec_gloss if available
int specMap = mat->get_texture_map(TM_SPEC_GLOSS_TYPE);
if (specMap < 0) {
specMap = mat->get_texture_map(TM_SPECULAR_TYPE);
}
setTexture(specMap, TextureSlot::SpecMap);
setTexture(mat->get_specular_map(), TextureSlot::SpecMap);

setTexture(mat->get_texture_map(TM_NORMAL_TYPE), TextureSlot::NormalMap);
setTexture(mat->get_texture_map(TM_HEIGHT_TYPE), TextureSlot::HeightMap);
Expand Down Expand Up @@ -1453,11 +1448,7 @@ bool VulkanDrawManager::applyMaterial(material* mat, primitive_type prim_type, v
MaterialSetInputs matInputs;
matInputs.texHandles[0] = mat->get_texture_map(TM_BASE_TYPE);
matInputs.texHandles[1] = mat->get_texture_map(TM_GLOW_TYPE);
int specMap = mat->get_texture_map(TM_SPEC_GLOSS_TYPE);
if (specMap < 0) {
specMap = mat->get_texture_map(TM_SPECULAR_TYPE);
}
matInputs.texHandles[2] = specMap;
matInputs.texHandles[2] = mat->get_specular_map();
matInputs.texHandles[3] = mat->get_texture_map(TM_NORMAL_TYPE);
matInputs.texHandles[4] = mat->get_texture_map(TM_HEIGHT_TYPE);
matInputs.texHandles[5] = mat->get_texture_map(TM_AMBIENT_TYPE);
Expand Down
26 changes: 26 additions & 0 deletions test/src/graphics/test_material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,29 @@ TEST(DecalMaterial, SceneNormalFlagSetOnlyWithoutDecalNormalMap)
with_normal.set_texture_map(TM_NORMAL_TYPE, 1);
EXPECT_EQ(with_normal.get_shader_flags(), 0u);
}

TEST(ModelMaterial, SpecularSelectionPrefersReflectAndReportsEncoding)
{
model_material mat;
bool is_spec_gloss = true;
EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), -1);
EXPECT_FALSE(is_spec_gloss);

mat.set_texture_map(TM_SPECULAR_TYPE, 0);
EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), 0);
EXPECT_FALSE(is_spec_gloss);

mat.set_texture_map(TM_SPEC_GLOSS_TYPE, 1);
EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), 1);
EXPECT_TRUE(is_spec_gloss);
EXPECT_EQ(mat.get_specular_map(), 1);

mat.set_texture_map(TM_SPECULAR_TYPE, -1);
mat.set_texture_map(TM_SPEC_GLOSS_TYPE, 0);
EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), 0);
EXPECT_TRUE(is_spec_gloss);

mat.set_texture_map(TM_SPEC_GLOSS_TYPE, -1);
EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), -1);
EXPECT_FALSE(is_spec_gloss);
}
Loading