1 #include "vogleditor_qtextureexplorer.h"
2 #include "ui_vogleditor_qtextureexplorer.h"
4 #include "vogl_gl_object.h"
5 #include "vogl_texture_state.h"
6 #include "vogl_renderbuffer_state.h"
7 #include <QColorDialog>
9 Q_DECLARE_METATYPE(vogl_gl_object_state*);
11 vogleditor_QTextureExplorer::vogleditor_QTextureExplorer(QWidget *parent) :
13 ui(new Ui::vogleditor_QTextureExplorer)
17 ui->textureScrollArea->setWidget(&m_textureViewer);
19 ui->channelSelectionBox->addItem("RGBA", VOGL_CSO_RGBA);
20 ui->channelSelectionBox->addItem("RGB", VOGL_CSO_RGB);
21 ui->channelSelectionBox->addItem("R", VOGL_CSO_R);
22 ui->channelSelectionBox->addItem("G", VOGL_CSO_G);
23 ui->channelSelectionBox->addItem("B", VOGL_CSO_B);
24 ui->channelSelectionBox->addItem("A", VOGL_CSO_A);
26 ui->channelSelectionBox->addItem("1 - RGBA", VOGL_CSO_ONE_MINUS_RGBA);
27 ui->channelSelectionBox->addItem("1 - RGB", VOGL_CSO_ONE_MINUS_RGB);
28 ui->channelSelectionBox->addItem("1 - R", VOGL_CSO_ONE_MINUS_R);
29 ui->channelSelectionBox->addItem("1 - G", VOGL_CSO_ONE_MINUS_G);
30 ui->channelSelectionBox->addItem("1 - B", VOGL_CSO_ONE_MINUS_B);
31 ui->channelSelectionBox->addItem("1 - A", VOGL_CSO_ONE_MINUS_A);
33 ui->channelSelectionBox->addItem("1 / RGBA", VOGL_CSO_ONE_OVER_RGBA);
34 ui->channelSelectionBox->addItem("1 / RGB", VOGL_CSO_ONE_OVER_RGB);
35 ui->channelSelectionBox->addItem("1 / R", VOGL_CSO_ONE_OVER_R);
36 ui->channelSelectionBox->addItem("1 / G", VOGL_CSO_ONE_OVER_G);
37 ui->channelSelectionBox->addItem("1 / B", VOGL_CSO_ONE_OVER_B);
38 ui->channelSelectionBox->addItem("1 / A", VOGL_CSO_ONE_OVER_A);
40 connect(ui->textureObjectListbox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectedTextureIndexChanged(int)));
41 connect(ui->channelSelectionBox, SIGNAL(currentIndexChanged(int)), this, SLOT(channelSelectionChanged(int)));
42 connect(ui->alphaBlendColorButton, SIGNAL(clicked()), this, SLOT(alphaBlendButtonClicked()));
44 // set default channel so that it doesn't alpha blend
45 ui->channelSelectionBox->setCurrentIndex(VOGL_CSO_RGB);
47 ui->zoomSpinBox->setValue(m_textureViewer.getZoomFactor());
50 vogleditor_QTextureExplorer::~vogleditor_QTextureExplorer()
55 void vogleditor_QTextureExplorer::clear()
57 ui->textureObjectListbox->clear();
59 m_textureViewer.clear();
60 m_textureViewer.repaint();
63 void vogleditor_QTextureExplorer::set_zoom_factor(double zoomFactor)
65 ui->zoomSpinBox->setValue(zoomFactor);
68 unsigned int vogleditor_QTextureExplorer::get_preferred_height() const
70 // texture viewer height, plus extra space for the explorer UI
71 return m_textureViewer.get_preferred_height() + ui->textureObjectListbox->height() * 2 + 50;
74 void vogleditor_QTextureExplorer::set_texture_objects(vogl_gl_object_state_ptr_vec objects)
79 for (vogl_gl_object_state_ptr_vec::iterator iter = objects.begin(); iter != objects.end(); iter++)
81 if ((*iter)->get_type() == cGLSTTexture)
83 vogl_texture_state* pTexState = static_cast<vogl_texture_state*>(*iter);
86 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()));
88 ui->textureObjectListbox->addItem(valueStr, QVariant::fromValue(*iter));
90 else if ((*iter)->get_type() == cGLSTRenderbuffer)
92 vogl_renderbuffer_state* pRbState = static_cast<vogl_renderbuffer_state*>(*iter);
95 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)));
97 ui->textureObjectListbox->addItem(valueStr, QVariant::fromValue(*iter));
101 VOGL_ASSERT(!"Unhandled object type in TextureExplorer");
106 void vogleditor_QTextureExplorer::add_texture_object(vogl_texture_state& textureState, vogl::dynamic_string bufferType)
108 m_objects.push_back(&textureState);
111 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()));
113 ui->textureObjectListbox->addItem(valueStr, QVariant::fromValue((vogl_gl_object_state*)&textureState));
116 bool vogleditor_QTextureExplorer::set_active_texture(unsigned long long textureHandle)
118 bool bDisplayedTexture = false;
120 for (vogl_gl_object_state_ptr_vec::iterator iter = m_objects.begin(); iter != m_objects.end(); iter++)
122 vogl_texture_state* pTexState = static_cast<vogl_texture_state*>(*iter);
123 if (pTexState->get_snapshot_handle() == textureHandle)
125 ui->textureObjectListbox->setCurrentIndex(index);
126 bDisplayedTexture = true;
132 return bDisplayedTexture;
135 void vogleditor_QTextureExplorer::selectedTextureIndexChanged(int index)
137 int count = ui->textureObjectListbox->count();
138 if (index >= 0 && index < count)
140 vogl_gl_object_state* pObjState = ui->textureObjectListbox->itemData(index).value<vogl_gl_object_state*>();
141 if (pObjState == NULL)
143 VOGL_ASSERT(!"NULL object type in TextureExplorer");
147 vogl_texture_state* pTexState = NULL;
149 if (pObjState->get_type() == cGLSTTexture)
151 pTexState = static_cast<vogl_texture_state*>(pObjState);
153 else if (pObjState->get_type() == cGLSTRenderbuffer)
155 vogl_renderbuffer_state* pRbState = static_cast<vogl_renderbuffer_state*>(pObjState);
156 if (pRbState != NULL)
158 pTexState = &(pRbState->get_texture());
163 VOGL_ASSERT(!"Unhandled object type in TextureExplorer");
166 if (pTexState != NULL)
168 uint maxSample = pTexState->get_num_samples();
171 ui->sampleSpinBox->setEnabled(false);
172 ui->sampleSpinBox->setMaximum(0);
174 // simulate that the value has changed to select the first (only) sample
175 on_sampleSpinBox_valueChanged(0);
179 ui->sampleSpinBox->setEnabled(true);
181 int sample = ui->sampleSpinBox->value();
182 ui->sampleSpinBox->setMaximum(maxSample - 1);
184 // if the value is still at the same sample after setting the max, then the
185 // valueChanged signal will not be emitted, so we'll need to simulate that
186 // in order to update and display the new texture.
187 if (ui->sampleSpinBox->value() == (int)sample)
189 on_sampleSpinBox_valueChanged(sample);
196 void vogleditor_QTextureExplorer::channelSelectionChanged(int index)
198 ChannelSelectionOption channelSelection = (ChannelSelectionOption)ui->channelSelectionBox->itemData(index).toInt();
199 m_textureViewer.setChannelSelectionOption(channelSelection);
200 bool bAlphaBlending = (channelSelection == VOGL_CSO_RGBA ||
201 channelSelection == VOGL_CSO_ONE_MINUS_RGBA ||
202 channelSelection == VOGL_CSO_ONE_OVER_RGBA );
204 ui->alphaBlendColorButton->setEnabled(bAlphaBlending);
207 void vogleditor_QTextureExplorer::alphaBlendButtonClicked()
209 QColor newColor = QColorDialog::getColor(m_textureViewer.getBackgroundColor(), this, "Select an alpha blend color");
210 m_textureViewer.setBackgroundColor(QBrush(newColor));
213 void vogleditor_QTextureExplorer::on_zoomSpinBox_valueChanged(double zoomFactor)
215 m_textureViewer.setZoomFactor(zoomFactor);
216 emit zoomFactorChanged(zoomFactor);
219 void vogleditor_QTextureExplorer::on_pushButton_toggled(bool checked)
221 m_textureViewer.setInverted(checked);
224 void vogleditor_QTextureExplorer::on_sampleSpinBox_valueChanged(int sample)
226 vogl_gl_object_state* pObjState = ui->textureObjectListbox->itemData(ui->textureObjectListbox->currentIndex()).value<vogl_gl_object_state*>();
227 if (pObjState == NULL)
229 VOGL_ASSERT(!"NULL object type in TextureExplorer");
233 vogl_texture_state* pTexState = NULL;
235 if (pObjState->get_type() == cGLSTTexture)
237 pTexState = static_cast<vogl_texture_state*>(pObjState);
239 else if (pObjState->get_type() == cGLSTRenderbuffer)
241 vogl_renderbuffer_state* pRbState = static_cast<vogl_renderbuffer_state*>(pObjState);
242 if (pRbState != NULL)
244 pTexState = &(pRbState->get_texture());
249 VOGL_ASSERT(!"Unhandled object type in TextureExplorer");
252 if (pTexState != NULL)
254 const vogl::ktx_texture* pKTXTexture = &(pTexState->get_texture(sample));
256 uint baseLevel = pTexState->get_params().get_value<uint>(GL_TEXTURE_BASE_LEVEL);
257 uint maxLevel = pTexState->get_params().get_value<uint>(GL_TEXTURE_MAX_LEVEL);
258 m_textureViewer.setTexture(pKTXTexture, baseLevel, maxLevel);
259 m_textureViewer.repaint();