1 #include "vogleditor_qtextureexplorer.h"
2 #include "ui_vogleditor_qtextureexplorer.h"
4 #include "vogl_gl_object.h"
5 #include "vogl_gl_state_snapshot.h"
6 #include "vogl_texture_state.h"
7 #include "vogl_renderbuffer_state.h"
8 #include <QColorDialog>
10 Q_DECLARE_METATYPE(vogl_gl_object_state*);
12 vogleditor_QTextureExplorer::vogleditor_QTextureExplorer(QWidget *parent) :
14 ui(new Ui::vogleditor_QTextureExplorer)
18 ui->textureScrollArea->setWidget(&m_textureViewer);
20 ui->channelSelectionBox->addItem("RGBA", VOGL_CSO_RGBA);
21 ui->channelSelectionBox->addItem("RGB", VOGL_CSO_RGB);
22 ui->channelSelectionBox->addItem("R", VOGL_CSO_R);
23 ui->channelSelectionBox->addItem("G", VOGL_CSO_G);
24 ui->channelSelectionBox->addItem("B", VOGL_CSO_B);
25 ui->channelSelectionBox->addItem("A", VOGL_CSO_A);
27 ui->channelSelectionBox->addItem("1 - RGBA", VOGL_CSO_ONE_MINUS_RGBA);
28 ui->channelSelectionBox->addItem("1 - RGB", VOGL_CSO_ONE_MINUS_RGB);
29 ui->channelSelectionBox->addItem("1 - R", VOGL_CSO_ONE_MINUS_R);
30 ui->channelSelectionBox->addItem("1 - G", VOGL_CSO_ONE_MINUS_G);
31 ui->channelSelectionBox->addItem("1 - B", VOGL_CSO_ONE_MINUS_B);
32 ui->channelSelectionBox->addItem("1 - A", VOGL_CSO_ONE_MINUS_A);
34 ui->channelSelectionBox->addItem("1 / RGBA", VOGL_CSO_ONE_OVER_RGBA);
35 ui->channelSelectionBox->addItem("1 / RGB", VOGL_CSO_ONE_OVER_RGB);
36 ui->channelSelectionBox->addItem("1 / R", VOGL_CSO_ONE_OVER_R);
37 ui->channelSelectionBox->addItem("1 / G", VOGL_CSO_ONE_OVER_G);
38 ui->channelSelectionBox->addItem("1 / B", VOGL_CSO_ONE_OVER_B);
39 ui->channelSelectionBox->addItem("1 / A", VOGL_CSO_ONE_OVER_A);
41 connect(ui->textureObjectListbox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectedTextureIndexChanged(int)));
42 connect(ui->channelSelectionBox, SIGNAL(currentIndexChanged(int)), this, SLOT(channelSelectionChanged(int)));
43 connect(ui->alphaBlendColorButton, SIGNAL(clicked()), this, SLOT(alphaBlendButtonClicked()));
45 // set default channel so that it doesn't alpha blend
46 ui->channelSelectionBox->setCurrentIndex(VOGL_CSO_RGB);
48 ui->zoomSpinBox->setValue(m_textureViewer.getZoomFactor());
51 vogleditor_QTextureExplorer::~vogleditor_QTextureExplorer()
57 void vogleditor_QTextureExplorer::clear()
61 ui->textureObjectListbox->clear();
63 m_textureViewer.clear();
64 m_textureViewer.repaint();
67 void vogleditor_QTextureExplorer::set_zoom_factor(double zoomFactor)
69 ui->zoomSpinBox->setValue(zoomFactor);
72 unsigned int vogleditor_QTextureExplorer::get_preferred_height() const
74 // texture viewer height, plus extra space for the explorer UI
75 return m_textureViewer.get_preferred_height() + ui->textureObjectListbox->height() * 2 + 50;
78 uint vogleditor_QTextureExplorer::set_texture_objects(vogl::vector<vogl_context_snapshot*> sharingContexts)
82 uint textureCount = 0;
84 for (uint c = 0; c < sharingContexts.size(); c++)
86 vogl_gl_object_state_ptr_vec textureObjects;
87 sharingContexts[c]->get_all_objects_of_category(cGLSTTexture, textureObjects);
89 textureCount += add_texture_objects(textureObjects);
95 uint vogleditor_QTextureExplorer::set_renderbuffer_objects(vogl::vector<vogl_context_snapshot*> sharingContexts)
99 uint textureCount = 0;
101 for (uint c = 0; c < sharingContexts.size(); c++)
103 vogl_gl_object_state_ptr_vec renderbufferObjects;
104 sharingContexts[c]->get_all_objects_of_category(cGLSTRenderbuffer, renderbufferObjects);
106 textureCount += add_texture_objects(renderbufferObjects);
112 uint vogleditor_QTextureExplorer::set_texture_objects(vogl_gl_object_state_ptr_vec textureObjects)
116 return add_texture_objects(textureObjects);
119 uint vogleditor_QTextureExplorer::add_texture_objects(vogl_gl_object_state_ptr_vec textureObjects)
121 uint textureCount = 0;
123 for (vogl_gl_object_state_ptr_vec::iterator iter = textureObjects.begin(); iter != textureObjects.end(); iter++)
126 m_objects.push_back(*iter);
128 if ((*iter)->get_type() == cGLSTTexture)
130 vogl_texture_state* pTexState = static_cast<vogl_texture_state*>(*iter);
133 valueStr = valueStr.sprintf("Texture %" PRIu64 " - %s (%u samples) (%u x %u x %u) %s", pTexState->get_snapshot_handle(), g_gl_enums.find_name(pTexState->get_target()), pTexState->get_num_samples(), pTexState->get_texture().get_width(), pTexState->get_texture().get_height(), pTexState->get_texture().get_depth(), g_gl_enums.find_name(pTexState->get_texture().get_ogl_internal_fmt()));
135 ui->textureObjectListbox->addItem(valueStr, QVariant::fromValue(*iter));
137 else if ((*iter)->get_type() == cGLSTRenderbuffer)
139 vogl_renderbuffer_state* pRbState = static_cast<vogl_renderbuffer_state*>(*iter);
142 valueStr = valueStr.sprintf("Renderbuffer %" PRIu64 " - %s (%u samples) (%u x %u) %s", pRbState->get_snapshot_handle(), "GL_RENDERBUFFER", pRbState->get_texture().get_num_samples(), pRbState->get_desc().get_int_or_default(GL_RENDERBUFFER_WIDTH), pRbState->get_desc().get_int_or_default(GL_RENDERBUFFER_HEIGHT), g_gl_enums.find_name(pRbState->get_desc().get_int_or_default(GL_RENDERBUFFER_INTERNAL_FORMAT)));
144 ui->textureObjectListbox->addItem(valueStr, QVariant::fromValue(*iter));
148 VOGL_ASSERT(!"Unhandled object type in TextureExplorer");
155 uint vogleditor_QTextureExplorer::add_texture_object(vogl_texture_state& textureState, vogl::dynamic_string bufferType)
157 m_objects.push_back(&textureState);
160 valueStr = valueStr.sprintf("%s (%u x %u) %s", bufferType.c_str(), textureState.get_texture().get_width(), textureState.get_texture().get_height(), g_gl_enums.find_name(textureState.get_texture().get_ogl_internal_fmt()));
162 ui->textureObjectListbox->addItem(valueStr, QVariant::fromValue((vogl_gl_object_state*)&textureState));
166 bool vogleditor_QTextureExplorer::set_active_texture(unsigned long long textureHandle)
168 bool bDisplayedTexture = false;
170 for (vogl_gl_object_state_ptr_vec::iterator iter = m_objects.begin(); iter != m_objects.end(); iter++)
172 vogl_texture_state* pTexState = static_cast<vogl_texture_state*>(*iter);
173 if (pTexState->get_snapshot_handle() == textureHandle)
175 ui->textureObjectListbox->setCurrentIndex(index);
176 bDisplayedTexture = true;
182 return bDisplayedTexture;
185 void vogleditor_QTextureExplorer::selectedTextureIndexChanged(int index)
187 int count = ui->textureObjectListbox->count();
188 if (index >= 0 && index < count)
190 vogl_gl_object_state* pObjState = ui->textureObjectListbox->itemData(index).value<vogl_gl_object_state*>();
191 if (pObjState == NULL)
193 VOGL_ASSERT(!"NULL object type in TextureExplorer");
197 vogl_texture_state* pTexState = NULL;
199 if (pObjState->get_type() == cGLSTTexture)
201 pTexState = static_cast<vogl_texture_state*>(pObjState);
203 else if (pObjState->get_type() == cGLSTRenderbuffer)
205 vogl_renderbuffer_state* pRbState = static_cast<vogl_renderbuffer_state*>(pObjState);
206 if (pRbState != NULL)
208 pTexState = &(pRbState->get_texture());
213 VOGL_ASSERT(!"Unhandled object type in TextureExplorer");
216 if (pTexState != NULL)
218 uint maxSample = pTexState->get_num_samples();
221 ui->sampleSpinBox->setEnabled(false);
222 ui->sampleSpinBox->setMaximum(0);
224 // simulate that the value has changed to select the first (only) sample
225 on_sampleSpinBox_valueChanged(0);
229 ui->sampleSpinBox->setEnabled(true);
231 int sample = ui->sampleSpinBox->value();
232 ui->sampleSpinBox->setMaximum(maxSample - 1);
234 // if the value is still at the same sample after setting the max, then the
235 // valueChanged signal will not be emitted, so we'll need to simulate that
236 // in order to update and display the new texture.
237 if (ui->sampleSpinBox->value() == (int)sample)
239 on_sampleSpinBox_valueChanged(sample);
246 void vogleditor_QTextureExplorer::channelSelectionChanged(int index)
248 ChannelSelectionOption channelSelection = (ChannelSelectionOption)ui->channelSelectionBox->itemData(index).toInt();
249 m_textureViewer.setChannelSelectionOption(channelSelection);
250 bool bAlphaBlending = (channelSelection == VOGL_CSO_RGBA ||
251 channelSelection == VOGL_CSO_ONE_MINUS_RGBA ||
252 channelSelection == VOGL_CSO_ONE_OVER_RGBA );
254 ui->alphaBlendColorButton->setEnabled(bAlphaBlending);
257 void vogleditor_QTextureExplorer::alphaBlendButtonClicked()
259 QColor newColor = QColorDialog::getColor(m_textureViewer.getBackgroundColor(), this, "Select an alpha blend color");
260 m_textureViewer.setBackgroundColor(QBrush(newColor));
263 void vogleditor_QTextureExplorer::on_zoomSpinBox_valueChanged(double zoomFactor)
265 m_textureViewer.setZoomFactor(zoomFactor);
266 emit zoomFactorChanged(zoomFactor);
269 void vogleditor_QTextureExplorer::on_pushButton_toggled(bool checked)
271 m_textureViewer.setInverted(checked);
274 void vogleditor_QTextureExplorer::on_sampleSpinBox_valueChanged(int sample)
276 vogl_gl_object_state* pObjState = ui->textureObjectListbox->itemData(ui->textureObjectListbox->currentIndex()).value<vogl_gl_object_state*>();
277 if (pObjState == NULL)
279 VOGL_ASSERT(!"NULL object type in TextureExplorer");
283 vogl_texture_state* pTexState = NULL;
285 if (pObjState->get_type() == cGLSTTexture)
287 pTexState = static_cast<vogl_texture_state*>(pObjState);
289 else if (pObjState->get_type() == cGLSTRenderbuffer)
291 vogl_renderbuffer_state* pRbState = static_cast<vogl_renderbuffer_state*>(pObjState);
292 if (pRbState != NULL)
294 pTexState = &(pRbState->get_texture());
299 VOGL_ASSERT(!"Unhandled object type in TextureExplorer");
302 if (pTexState != NULL)
304 const vogl::ktx_texture* pKTXTexture = &(pTexState->get_texture(sample));
306 uint baseLevel = pTexState->get_params().get_value<uint>(GL_TEXTURE_BASE_LEVEL);
307 uint maxLevel = pTexState->get_params().get_value<uint>(GL_TEXTURE_MAX_LEVEL);
308 m_textureViewer.setTexture(pKTXTexture, baseLevel, maxLevel);
309 m_textureViewer.repaint();