Debian Codegraph @

radiant/ui/materials/editor/TexturePreview.h

raw
1 #pragma once
2
3 #include "igl.h"
4 #include "ishaderlayer.h"
5 #include "wxutil/GLWidget.h"
6
7 namespace ui
8 {
9
10 /**
11 * An openGL preview widget rendering the map of a single material stage,
12 * e.g. the bumpmap only.
13 */
14 class TexturePreview :
15 public wxutil::GLWidget
16 {
17 public:
18 enum class ImageType
19 {
20 EditorImage,
21 Diffuse,
22 Bump,
23 Specular
24 };
25
26 private:
27 ImageType _type;
28 MaterialPtr _material;
29
30 public:
31 TexturePreview(wxWindow* parent, ImageType type = ImageType::EditorImage) :
32 GLWidget(parent, std::bind(&TexturePreview::onRender, this), "TexturePreview"),
33 _type(type)
34 {}
35
36 void SetImageType(ImageType type)
37 {
38 _type = type;
39 QueueDraw();
40 }
41
42 void SetMaterial(const MaterialPtr& material)
43 {
44 _material = material;
45 QueueDraw();
46 }
47
48 void QueueDraw()
49 {
50 Refresh(false);
51
52 #if defined(__WXGTK__) && !wxCHECK_VERSION(3, 1, 3)
53 // Just calling Refresh doesn't cut it before wxGTK 3.1.3
54 // the GLWidget::OnPaint event is never invoked unless we call Update()
55 Update();
56 #endif
57 }
58
59 private:
60
61 bool onRender()
62 {
63 // Get the viewport size from the GL widget
64 wxSize req = GetClientSize();
65
66 if (req.GetWidth() == 0 || req.GetHeight() == 0) return false;
67
68 glPushAttrib(GL_ALL_ATTRIB_BITS);
69
70 glViewport(0, 0, req.GetWidth(), req.GetHeight());
71
72 // Initialise
73 glClearColor(0.9f, 0.9f, 0.9f, 0);
74 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
75 glDisable(GL_DEPTH_TEST);
76 glMatrixMode(GL_TEXTURE);
77 glLoadIdentity();
78 glMatrixMode(GL_MODELVIEW);
79 glLoadIdentity();
80 glMatrixMode(GL_PROJECTION);
81 glLoadIdentity();
82 glOrtho(0, req.GetWidth(), 0, req.GetHeight(), -100, 100);
83 glEnable(GL_TEXTURE_2D);
84
85 // This is an "ordinary" texture, take the editor image
86 auto texture = getLayerTexture();
87
88 if (texture)
89 {
90 glBindTexture(GL_TEXTURE_2D, texture->getGLTexNum());
91
92 // Calculate the correct aspect ratio for preview
93 float aspect = float(texture->getWidth()) / float(texture->getHeight());
94 float hfWidth, hfHeight;
95
96 if (aspect > 1.0f)
97 {
98 hfWidth = 0.5 * req.GetWidth();
99 hfHeight = 0.5 * req.GetHeight() / aspect;
100 }
101 else
102 {
103 hfHeight = 0.5 * req.GetWidth();
104 hfWidth = 0.5 * req.GetHeight() * aspect;
105 }
106
107 // Draw a quad to put the texture on
108 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
109 glColor3f(1, 1, 1);
110
111 glBegin(GL_QUADS);
112 glTexCoord2i(0, 1);
113 glVertex2f(0.5 * req.GetWidth() - hfWidth, 0.5 * req.GetHeight() - hfHeight);
114 glTexCoord2i(1, 1);
115 glVertex2f(0.5 * req.GetWidth() + hfWidth, 0.5 * req.GetHeight() - hfHeight);
116 glTexCoord2i(1, 0);
117 glVertex2f(0.5 * req.GetWidth() + hfWidth, 0.5 * req.GetHeight() + hfHeight);
118 glTexCoord2i(0, 0);
119 glVertex2f(0.5 * req.GetWidth() - hfWidth, 0.5 * req.GetHeight() + hfHeight);
120 glEnd();
121 }
122
123 glPopAttrib();
124
125 return true;
126 }
127
128 TexturePtr getLayerTexture()
129 {
130 if (!_material)
131 {
132 return TexturePtr();
133 }
134
135 if (_type == ImageType::EditorImage)
136 {
137 return _material->getEditorImage();
138 }
139
140 auto layerToFind = IShaderLayer::DIFFUSE;
141
142 switch (_type)
143 {
144 case ImageType::Diffuse: layerToFind = IShaderLayer::DIFFUSE; break;
145 case ImageType::Bump: layerToFind = IShaderLayer::BUMP; break;
146 case ImageType::Specular: layerToFind = IShaderLayer::SPECULAR; break;
147 }
148
149 TexturePtr result;
150
151 _material->foreachLayer([&](const IShaderLayer::Ptr& layer)
152 {
153 if (layer->getType() == layerToFind)
154 {
155 result = layer->getTexture();
156 return false;
157 }
158
159 return true;
160 });
161
162 return result;
163 }
164 };
165
166 }