src/gl/partrenderer.cpp

changeset 47
cd6704009eb9
parent 46
98645c8e7704
child 48
3c10f0e2fbe0
equal deleted inserted replaced
46:98645c8e7704 47:cd6704009eb9
19 #include <GL/glut.h> 19 #include <GL/glut.h>
20 #include <glm/ext/matrix_transform.hpp> 20 #include <glm/ext/matrix_transform.hpp>
21 #include <glm/ext/matrix_clip_space.hpp> 21 #include <glm/ext/matrix_clip_space.hpp>
22 #include <QMouseEvent> 22 #include <QMouseEvent>
23 #include <QMessageBox> 23 #include <QMessageBox>
24 #include <QAbstractButton>
24 #include "partrenderer.h" 25 #include "partrenderer.h"
25 26
26 PartRenderer::PartRenderer( 27 PartRenderer::PartRenderer(
27 Model* model, 28 Model* model,
28 DocumentManager* documents, 29 DocumentManager* documents,
83 throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; 84 throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
84 } 85 }
85 86
86 void PartRenderer::paintGL() 87 void PartRenderer::paintGL()
87 { 88 {
88 /* 89 glEnable(GL_DEPTH_TEST);
89 glEnable (GL_BLEND); 90 glShadeModel(GL_SMOOTH);
90 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 91 glEnable(GL_MULTISAMPLE);
91 */ 92 this->renderScene();
92 glEnable (GL_DEPTH_TEST); 93 }
93 glShadeModel (GL_SMOOTH); 94
94 glEnable (GL_MULTISAMPLE); 95 void PartRenderer::renderScene()
96 {
95 if (this->renderPreferences.lineAntiAliasing && this->renderPreferences.style != gl::RenderStyle::PickScene) 97 if (this->renderPreferences.lineAntiAliasing && this->renderPreferences.style != gl::RenderStyle::PickScene)
96 { 98 {
97 glEnable(GL_LINE_SMOOTH); 99 glEnable(GL_LINE_SMOOTH);
98 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); 100 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
99 } 101 }
100 else { 102 else {
101 glDisable(GL_LINE_SMOOTH); 103 glDisable(GL_LINE_SMOOTH);
102 } 104 }
103 this->renderScene();
104 }
105
106 void PartRenderer::renderScene()
107 {
108 const QColor& backgroundColor = this->renderPreferences.backgroundColor;
109 if (this->renderPreferences.style != gl::RenderStyle::PickScene) 105 if (this->renderPreferences.style != gl::RenderStyle::PickScene)
110 { 106 {
107 const QColor& backgroundColor = this->renderPreferences.backgroundColor;
111 glClearColor( 108 glClearColor(
112 static_cast<float>(backgroundColor.redF()), 109 static_cast<float>(backgroundColor.redF()),
113 static_cast<float>(backgroundColor.greenF()), 110 static_cast<float>(backgroundColor.greenF()),
114 static_cast<float>(backgroundColor.blueF()), 111 static_cast<float>(backgroundColor.blueF()),
115 1.0f); 112 1.0f);
197 const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); 194 const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError)));
198 errors.append(glErrorString); 195 errors.append(glErrorString);
199 } 196 }
200 if (not errors.isEmpty()) 197 if (not errors.isEmpty())
201 { 198 {
202 QMessageBox::critical( 199 QMessageBox box{this};
203 this, 200 box.setIcon(QMessageBox::Critical);
204 tr("Rendering error"), 201 box.setText(tr("Failed to render: %1").arg(errors.join("\n")));
205 QString{"Failed to render.\n%1"}.arg(errors.join("\n"))); 202 box.setWindowTitle(tr("Rendering error"));
203 box.setStandardButtons(QMessageBox::Close);
204 box.button(QMessageBox::Close)->setText(tr("Damn it"));
205 box.exec();
206 } 206 }
207 } 207 }
208 208
209 void PartRenderer::mouseMoveEvent(QMouseEvent* event) 209 void PartRenderer::mouseMoveEvent(QMouseEvent* event)
210 { 210 {
234 this->zoom = std::clamp(this->zoom + move, MIN_ZOOM, MAX_ZOOM); 234 this->zoom = std::clamp(this->zoom + move, MIN_ZOOM, MAX_ZOOM);
235 this->updateViewMatrix(); 235 this->updateViewMatrix();
236 this->update(); 236 this->update();
237 } 237 }
238 238
239 ldraw::Id PartRenderer::pick(const QPoint& where)
240 {
241 const gl::RenderStyle oldRenderStyle = this->renderPreferences.style;
242 this->renderPreferences.style = gl::RenderStyle::PickScene;
243 this->makeCurrent();
244 this->renderScene();
245 std::array<GLbyte, 3> data;
246 glReadPixels(where.x(), this->height() - where.y(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &data[0]);
247 this->renderPreferences.style = oldRenderStyle;
248 this->update();
249 return gl::Compiler::idFromColor(data);
250 }
251
239 /** 252 /**
240 * @brief Changes the color of rendered fragments 253 * @brief Changes the color of rendered fragments
241 * @param newFragmentStyle new fragment style to use 254 * @param newFragmentStyle new fragment style to use
242 */ 255 */
243 void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle) 256 void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle)

mercurial