1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TrenchBroom_UVViewHelper
21 #define TrenchBroom_UVViewHelper
22 
23 #include "TrenchBroom.h"
24 #include "VecMath.h"
25 #include "Model/Hit.h"
26 
27 namespace TrenchBroom {
28     namespace Assets {
29         class Texture;
30     }
31 
32     namespace Model {
33         class BrushFace;
34         class PickResult;
35     }
36 
37     namespace Renderer {
38         class ActiveShader;
39         class Camera;
40         class OrthographicCamera;
41         class RenderContext;
42     }
43 
44     namespace View {
45         class UVViewHelper {
46         private:
47             Renderer::OrthographicCamera& m_camera;
48             bool m_zoomValid;
49 
50             Model::BrushFace* m_face;
51 
52             Vec2i m_subDivisions;
53 
54             /**
55              The position of the scaling origin / rotation center handle in texture coordinates (without offset and scaling applied).
56              */
57             Vec2f m_origin;
58         public:
59             UVViewHelper(Renderer::OrthographicCamera& camera);
60 
61             bool valid() const;
62             Model::BrushFace* face() const;
63             const Assets::Texture* texture() const;
64             void setFace(Model::BrushFace* face);
65             void cameraViewportChanged();
66 
67             const Vec2i& subDivisions() const;
68             Vec2 stripeSize() const;
69             void setSubDivisions(const Vec2i& subDivisions);
70 
71             const Vec3 origin() const;
72             const Vec2f originInFaceCoords() const;
73             const Vec2f originInTexCoords() const;
74             void setOrigin(const Vec2f& originInFaceCoords);
75 
76             const Renderer::Camera& camera() const;
77             float cameraZoom() const;
78 
79             void pickTextureGrid(const Ray3& ray, const Model::Hit::HitType hitTypes[2], Model::PickResult& pickResult) const;
80 
81             Vec2f snapDelta(const Vec2f& delta, const Vec2f& distance) const;
82             Vec2f computeDistanceFromTextureGrid(const Vec3& position) const;
83 
84             void computeOriginHandleVertices(Vec3& x1, Vec3& x2, Vec3& y1, Vec3& y2) const;
85             void computeScaleHandleVertices(const Vec2& pos, Vec3& x1, Vec3& x2, Vec3& y1, Vec3& y2) const;
86             void computeLineVertices(const Vec2& pos, Vec3& x1, Vec3& x2, Vec3& y1, Vec3& y2, const Mat4x4& toTex, const Mat4x4& toWorld) const;
87         private:
88             void resetOrigin();
89             void resetCamera();
90             void resetZoom();
91 
92             BBox3 computeFaceBoundsInCameraCoords() const;
93             Vec3 transformToCamera(const Vec3& point) const;
94             Vec3 transformFromCamera(const Vec3& point) const;
95         };
96     }
97 }
98 
99 #endif /* defined(TrenchBroom_UVViewHelper) */
100