Skip to content

Commit

Permalink
#5773: TextureMatrix no longer exposes the 6 doubles it holds
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 9, 2021
1 parent 1c308bf commit 6e0af0e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 55 deletions.
13 changes: 2 additions & 11 deletions radiantcore/brush/Face.cpp
Expand Up @@ -476,23 +476,14 @@ void Face::setTexdef(const TexDef& texDef)
projection.setFromTexDef(texDef);

// The bprimitive texdef needs to be scaled using our current texture dims
auto width = static_cast<double>(_shader.getWidth());
auto height = static_cast<double>(_shader.getHeight());

projection.getTextureMatrix().coords[0][0] /= width;
projection.getTextureMatrix().coords[0][1] /= width;
projection.getTextureMatrix().coords[0][2] /= width;
projection.getTextureMatrix().coords[1][0] /= height;
projection.getTextureMatrix().coords[1][1] /= height;
projection.getTextureMatrix().coords[1][2] /= height;
projection.getTextureMatrix().addScale(_shader.getWidth(), _shader.getHeight());

SetTexdef(projection);
}

ShiftScaleRotation Face::getShiftScaleRotation() const
{
auto texdef = _texdef.getTextureMatrix().getFakeTexCoords();
auto ssr = texdef.toShiftScaleRotation();
auto ssr = _texdef.getTextureMatrix().getShiftScaleRotation();

// These values are going to show up in the Surface Inspector, so
// we need to make some adjustments:
Expand Down
1 change: 0 additions & 1 deletion radiantcore/brush/TexDef.h
Expand Up @@ -54,7 +54,6 @@ class TexDef
static TexDef CreateFromShiftScaleRotation(const ShiftScaleRotation& scr);

friend std::ostream& operator<<(std::ostream& st, const TexDef& texdef);
friend struct TextureMatrix; // for TextureMatrix::getFakeTexCoords
};

inline std::ostream& operator<<(std::ostream& st, const TexDef& texdef)
Expand Down
37 changes: 12 additions & 25 deletions radiantcore/brush/TextureMatrix.cpp
Expand Up @@ -16,16 +16,6 @@ TextureMatrix::TextureMatrix()
coords[1][2] = 0.f;
}

TextureMatrix::TextureMatrix(const Matrix4& transform)
{
coords[0][0] = transform.xx();
coords[0][1] = transform.yx();
coords[0][2] = transform.tx();
coords[1][0] = transform.xy();
coords[1][1] = transform.yy();
coords[1][2] = transform.ty();
}

TextureMatrix::TextureMatrix(const Matrix3& transform)
{
coords[0][0] = transform.xx();
Expand Down Expand Up @@ -96,20 +86,17 @@ void TextureMatrix::addScale(std::size_t width, std::size_t height) {
coords[1][2] /= height;
}

// compute a fake shift scale rot representation from the texture matrix
// these shift scale rot values are to be understood in the local axis base
// Note: this code looks similar to Texdef_fromTransform, but the algorithm is slightly different.
TexDef TextureMatrix::getFakeTexCoords() const
ShiftScaleRotation TextureMatrix::getShiftScaleRotation() const
{
TexDef texdef;
ShiftScaleRotation ssr;

texdef._scale[0] = 1.0 / Vector2(coords[0][0], coords[1][0]).getLength();
texdef._scale[1] = 1.0 / Vector2(coords[0][1], coords[1][1]).getLength();
ssr.scale[0] = 1.0 / Vector2(coords[0][0], coords[1][0]).getLength();
ssr.scale[1] = 1.0 / Vector2(coords[0][1], coords[1][1]).getLength();

texdef._rotate = -radians_to_degrees(arctangent_yx(coords[1][0], coords[0][0]));
ssr.rotate = -radians_to_degrees(arctangent_yx(coords[1][0], coords[0][0]));

texdef._shift[0] = -coords[0][2];
texdef._shift[1] = coords[1][2];
ssr.shift[0] = -coords[0][2];
ssr.shift[1] = coords[1][2];

// determine whether or not an axis is flipped using a 2d cross-product
auto cross = Vector2(coords[0][0], coords[0][1]).crossProduct(Vector2(coords[1][0], coords[1][1]));
Expand All @@ -120,18 +107,18 @@ TexDef TextureMatrix::getFakeTexCoords() const
// we pick one (rather arbitrarily) using the following convention: If the X-axis is between
// 0 and 180, we assume it's the Y-axis that flipped, otherwise we assume it's the X-axis and
// subtract out 180 degrees to compensate.
if (texdef._rotate >= 180.0)
if (ssr.rotate >= 180.0)
{
texdef._rotate -= 180.0;
texdef._scale[0] = -texdef._scale[0];
ssr.rotate -= 180.0;
ssr.scale[0] = -ssr.scale[0];
}
else
{
texdef._scale[1] = -texdef._scale[1];
ssr.scale[1] = -ssr.scale[1];
}
}

return texdef;
return ssr;
}

// All texture-projection translation (shift) values are congruent modulo the dimensions of the texture.
Expand Down
17 changes: 8 additions & 9 deletions radiantcore/brush/TextureMatrix.h
Expand Up @@ -21,8 +21,8 @@ class Matrix3;
// the W coordinate and is set to 1, to make it translatable by tx and ty):
//
// | xx yx tx | | x | | u |
// | xy yy ty | X | y | | v |
// | 0 0 1 | | 1 | = | 1 |
// | xy yy ty | X | y | = | v |
// | 0 0 1 | | 1 | | 1 |
//
// Therefore only 6 compontents are really needed to define the matrix.
// The trick to rotate the world space such that it is "oriented" along a plane changed
Expand All @@ -33,17 +33,14 @@ class Matrix3;
// texdef defined in a Q3 map file will never look the same in idTech4 engines when placed on
// angled brush faces - there will always be a stretch in some direction, which the D3 engine
// completely works around by using the actual face normal - no stretching.
struct TextureMatrix
class TextureMatrix
{
private:
double coords[2][3];

// Constructor
public:
TextureMatrix();

// Construct the BP Definition out of the transformation matrix
// Basically copies over the values from the corresponding components
TextureMatrix(const Matrix4& transform);

// Copy-construct from the relevant components from the given transform
// (which is everything except the last row: xz() and yz() are 0, zz() is 1)
TextureMatrix(const Matrix3& transform);
Expand Down Expand Up @@ -72,7 +69,7 @@ struct TextureMatrix
// compute a fake shift scale rot representation from the texture matrix
// these shift scale rot values are to be understood in the local axis base
// Note: this code looks similar to Texdef_fromTransform, but the algorithm is slightly different.
TexDef getFakeTexCoords() const;
ShiftScaleRotation getShiftScaleRotation() const;

// All texture-projection translation (shift) values are congruent modulo the dimensions of the texture.
// This function normalises shift values to the smallest positive congruent values.
Expand All @@ -89,6 +86,8 @@ struct TextureMatrix

// Checks if any of the matrix components are NaN or INF (in which case the matrix is not sane)
bool isSane() const;

friend std::ostream& operator<<(std::ostream& st, const TextureMatrix& texdef);
};

inline std::ostream& operator<<(std::ostream& st, const TextureMatrix& texdef)
Expand Down
10 changes: 1 addition & 9 deletions radiantcore/brush/TextureProjection.cpp
Expand Up @@ -63,15 +63,7 @@ void TextureProjection::setTransform(const Matrix3& transform)

void TextureProjection::setTransformFromMatrix4(const Matrix4& transform)
{
// Check the matrix for validity
if ((transform[0] != 0 || transform[4] != 0) && (transform[1] != 0 || transform[5] != 0))
{
_matrix = TextureMatrix(transform);
}
else
{
rError() << "invalid texture matrix" << std::endl;
}
setTransform(getTextureMatrixFromMatrix4(transform));
}

void TextureProjection::setFromTexDef(const TexDef& texDef)
Expand Down

0 comments on commit 6e0af0e

Please sign in to comment.