]> git.cworth.org Git - vogl/blob - src/vogleditor/vogleditor_qtextureexplorer.cpp
Initial vogl checkin
[vogl] / src / vogleditor / vogleditor_qtextureexplorer.cpp
1 #include "vogleditor_qtextureexplorer.h"
2 #include "ui_vogleditor_qtextureexplorer.h"
3
4 #include "vogl_gl_object.h"
5 #include "vogl_texture_state.h"
6 #include "vogl_renderbuffer_state.h"
7 #include <QColorDialog>
8
9 Q_DECLARE_METATYPE(vogl_gl_object_state*);
10
11 vogleditor_QTextureExplorer::vogleditor_QTextureExplorer(QWidget *parent) :
12     QWidget(parent),
13     ui(new Ui::vogleditor_QTextureExplorer)
14 {
15     ui->setupUi(this);
16
17     ui->textureScrollArea->setWidget(&m_textureViewer);
18
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);
25
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);
32
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);
39
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()));
43
44     // set default channel so that it doesn't alpha blend
45     ui->channelSelectionBox->setCurrentIndex(VOGL_CSO_RGB);
46
47     ui->zoomSpinBox->setValue(m_textureViewer.getZoomFactor());
48 }
49
50 vogleditor_QTextureExplorer::~vogleditor_QTextureExplorer()
51 {
52     delete ui;
53 }
54
55 void vogleditor_QTextureExplorer::clear()
56 {
57     ui->textureObjectListbox->clear();
58
59     m_textureViewer.clear();
60     m_textureViewer.repaint();
61 }
62
63 void vogleditor_QTextureExplorer::set_zoom_factor(double zoomFactor)
64 {
65     ui->zoomSpinBox->setValue(zoomFactor);
66 }
67
68 unsigned int vogleditor_QTextureExplorer::get_preferred_height() const
69 {
70     // texture viewer height, plus extra space for the explorer UI
71     return m_textureViewer.get_preferred_height() + ui->textureObjectListbox->height() * 2 + 50;
72 }
73
74 void vogleditor_QTextureExplorer::set_texture_objects(vogl_gl_object_state_ptr_vec objects)
75 {
76     clear();
77     m_objects = objects;
78
79     for (vogl_gl_object_state_ptr_vec::iterator iter = objects.begin(); iter != objects.end(); iter++)
80     {
81         if ((*iter)->get_type() == cGLSTTexture)
82         {
83             vogl_texture_state* pTexState = static_cast<vogl_texture_state*>(*iter);
84
85             QString valueStr;
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()));
87
88             ui->textureObjectListbox->addItem(valueStr, QVariant::fromValue(*iter));
89         }
90         else if ((*iter)->get_type() == cGLSTRenderbuffer)
91         {
92             vogl_renderbuffer_state* pRbState = static_cast<vogl_renderbuffer_state*>(*iter);
93
94             QString valueStr;
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)));
96
97             ui->textureObjectListbox->addItem(valueStr, QVariant::fromValue(*iter));
98         }
99         else
100         {
101             VOGL_ASSERT(!"Unhandled object type in TextureExplorer");
102         }
103     }
104 }
105
106 void vogleditor_QTextureExplorer::add_texture_object(vogl_texture_state& textureState, vogl::dynamic_string bufferType)
107 {
108     m_objects.push_back(&textureState);
109
110     QString valueStr;
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()));
112
113     ui->textureObjectListbox->addItem(valueStr, QVariant::fromValue((vogl_gl_object_state*)&textureState));
114 }
115
116 bool vogleditor_QTextureExplorer::set_active_texture(unsigned long long textureHandle)
117 {
118     bool bDisplayedTexture = false;
119     int index = 0;
120     for (vogl_gl_object_state_ptr_vec::iterator iter = m_objects.begin(); iter != m_objects.end(); iter++)
121     {
122         vogl_texture_state* pTexState = static_cast<vogl_texture_state*>(*iter);
123         if (pTexState->get_snapshot_handle() == textureHandle)
124         {
125             ui->textureObjectListbox->setCurrentIndex(index);
126             bDisplayedTexture = true;
127             break;
128         }
129
130         ++index;
131     }
132     return bDisplayedTexture;
133 }
134
135 void vogleditor_QTextureExplorer::selectedTextureIndexChanged(int index)
136 {
137     int count = ui->textureObjectListbox->count();
138     if (index >= 0 && index < count)
139     {
140         vogl_gl_object_state* pObjState = ui->textureObjectListbox->itemData(index).value<vogl_gl_object_state*>();
141         if (pObjState == NULL)
142         {
143             VOGL_ASSERT(!"NULL object type in TextureExplorer");
144             return;
145         }
146
147         vogl_texture_state* pTexState = NULL;
148
149         if (pObjState->get_type() == cGLSTTexture)
150         {
151             pTexState = static_cast<vogl_texture_state*>(pObjState);
152         }
153         else if (pObjState->get_type() == cGLSTRenderbuffer)
154         {
155             vogl_renderbuffer_state* pRbState = static_cast<vogl_renderbuffer_state*>(pObjState);
156             if (pRbState != NULL)
157             {
158                 pTexState = &(pRbState->get_texture());
159             }
160         }
161         else
162         {
163             VOGL_ASSERT(!"Unhandled object type in TextureExplorer");
164         }
165
166         if (pTexState != NULL)
167         {
168             uint maxSample = pTexState->get_num_samples();
169             if (maxSample <= 1)
170             {
171                 ui->sampleSpinBox->setEnabled(false);
172                 ui->sampleSpinBox->setMaximum(0);
173
174                 // simulate that the value has changed to select the first (only) sample
175                 on_sampleSpinBox_valueChanged(0);
176             }
177             else
178             {
179                 ui->sampleSpinBox->setEnabled(true);
180
181                 int sample = ui->sampleSpinBox->value();
182                 ui->sampleSpinBox->setMaximum(maxSample - 1);
183
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)
188                 {
189                     on_sampleSpinBox_valueChanged(sample);
190                 }
191             }
192         }
193     }
194 }
195
196 void vogleditor_QTextureExplorer::channelSelectionChanged(int index)
197 {
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 );
203
204     ui->alphaBlendColorButton->setEnabled(bAlphaBlending);
205 }
206
207 void vogleditor_QTextureExplorer::alphaBlendButtonClicked()
208 {
209     QColor newColor = QColorDialog::getColor(m_textureViewer.getBackgroundColor(), this, "Select an alpha blend color");
210     m_textureViewer.setBackgroundColor(QBrush(newColor));
211 }
212
213 void vogleditor_QTextureExplorer::on_zoomSpinBox_valueChanged(double zoomFactor)
214 {
215     m_textureViewer.setZoomFactor(zoomFactor);
216     emit zoomFactorChanged(zoomFactor);
217 }
218
219 void vogleditor_QTextureExplorer::on_pushButton_toggled(bool checked)
220 {
221     m_textureViewer.setInverted(checked);
222 }
223
224 void vogleditor_QTextureExplorer::on_sampleSpinBox_valueChanged(int sample)
225 {
226     vogl_gl_object_state* pObjState = ui->textureObjectListbox->itemData(ui->textureObjectListbox->currentIndex()).value<vogl_gl_object_state*>();
227     if (pObjState == NULL)
228     {
229         VOGL_ASSERT(!"NULL object type in TextureExplorer");
230         return;
231     }
232
233     vogl_texture_state* pTexState = NULL;
234
235     if (pObjState->get_type() == cGLSTTexture)
236     {
237         pTexState = static_cast<vogl_texture_state*>(pObjState);
238     }
239     else if (pObjState->get_type() == cGLSTRenderbuffer)
240     {
241         vogl_renderbuffer_state* pRbState = static_cast<vogl_renderbuffer_state*>(pObjState);
242         if (pRbState != NULL)
243         {
244             pTexState = &(pRbState->get_texture());
245         }
246     }
247     else
248     {
249         VOGL_ASSERT(!"Unhandled object type in TextureExplorer");
250     }
251
252     if (pTexState != NULL)
253     {
254         const vogl::ktx_texture* pKTXTexture = &(pTexState->get_texture(sample));
255
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();
260     }
261 }