1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#pragma once
#include <QVector>
#include <QGLWidget>
#include <QMainWindow>
#include <QPoint>
#include <QList>
#include "SyntopiaCore/Math/Vector3.h"
#include "SyntopiaCore/Math/Matrix4.h"
#include "Object3D.h"
namespace SyntopiaCore {
namespace GLEngine {
struct Command {
Command() {};
Command(QString command, QString arg) : command(command), arg(arg) {};
QString command;
QString arg;
};
/// Settings for the GLEngine
class Settings {
public:
// Default constructor
Settings() {
perspectiveAngle = 22.0;
nearClipping = 5.0;
farClipping = 60.0;
}
// Projection settings
double perspectiveAngle;
double nearClipping;
double farClipping;
};
class ProgressBox : public QWidget {
Q_OBJECT
public:
ProgressBox(QWidget* parent) : QWidget(parent) {
QHBoxLayout *layout = new QHBoxLayout(this);
cancelButton = new QPushButton(this);
bar = new QProgressBar(this);
layout->addWidget(cancelButton);
layout->addWidget(bar);
canceled = false;
connect(cancelButton, SIGNAL(clicked()), this, SLOT(setCanceled()));
dismiss();
started = false;
}
bool wasCanceled() { return canceled; }
void setValue(int v) { bar->setValue(v); }
void start() { cancelButton->setEnabled(true); started = true; setValue(0); bar->setVisible(true); setEnabled(true); cancelButton->setText("Cancel"); canceled = false; }
void dismiss() { cancelButton->setEnabled(true); started = false; setValue(0); bar->setVisible(false); cancelButton->setText("Raytrace (in Window)"); }
public slots:
void setCanceled() {
if (started) {
canceled = true;
cancelButton->setEnabled(false);
} else {
emit startPressed();
}
}
signals:
void startPressed();
private:
QProgressBar* bar;
QPushButton* cancelButton;
bool canceled;
bool started;
};
/// Widget for the mini OpenGL engine.
class EngineWidget : public QGLWidget {
public:
/// Constructor
EngineWidget(QMainWindow* mainWindow, QWidget* parent);
/// Destructor
~EngineWidget();
/// Use this whenever the an redraw is required.
/// Calling this function multiple times will still only result in one redraw
void requireRedraw();
QImage getScreenShot();
void toggleShowDepth() { showDepth = !showDepth; };
void clearWorld();
void reset();
void addObject(Object3D* object);
QList<Object3D*> getObjects() { return objects; };
int objectCount() const { return objects.size(); }
SyntopiaCore::Math::Vector3f getPivot() { return pivot; }
SyntopiaCore::Math::Matrix4f getRotation() { return rotation; }
SyntopiaCore::Math::Vector3f getTranslation() { return translation; }
SyntopiaCore::Math::Vector3f getCameraPosition();
SyntopiaCore::Math::Vector3f getCameraUp();
SyntopiaCore::Math::Vector3f getCameraTarget();
double getScale() { return scale; }
void setPivot(SyntopiaCore::Math::Vector3f pivot) { this->pivot = pivot; }
void setRotation(SyntopiaCore::Math::Matrix4f rotation) { this->rotation = rotation; }
void setTranslation(SyntopiaCore::Math::Vector3f translation) { this->translation = translation; }
void setScale(double scale) { this->scale = scale; }
void setPerspectiveAngle(double angle) { this->settings.perspectiveAngle = angle; updatePerspective(); }
/// RGB in [0;1]
void setBackgroundColor(double r, double g, double b) {
backgroundColor = QColor((int)(r*255.0), (int)(g*255.0), (int)(b*255.0));
};
SyntopiaCore::Math::Vector3f getBackgroundColor() {
return SyntopiaCore::Math::Vector3f(backgroundColor.red()/255.0f,backgroundColor.green()/255.0f,backgroundColor.blue()/255.0f);
}
void setContextMenu(QMenu* contextMenu) { this->contextMenu = contextMenu; }
double getFOV();
double getDepthAt(int x,int y);
QColor getVisibleForegroundColor();
void setDisabled(bool disabled) { this->disabled = disabled; }
void setFastRotate(bool enabled);
GLdouble* getModelViewCache() { return modelViewCache; };
GLdouble* getProjectionCache() { return projectionCache; };
GLint* getViewPortCache() { return viewPortCache; };
void getBoundingBox(SyntopiaCore::Math::Vector3f& from, SyntopiaCore::Math::Vector3f& to) const;
void setRaytracerCommands(QVector<GLEngine::Command> raytracerCommands) { this->raytracerCommands = raytracerCommands; }
QVector<GLEngine::Command> getRaytracerCommands() { return raytracerCommands; }
void setupFragmentShader(); // For experimenting with shader effects.
void setImage(QImage im);
void setShowCoordinateSystem(bool val) { showCoordinateSystem = val; }
protected:
void mouseMoveEvent(QMouseEvent* ev) ;
void contextMenuEvent (QContextMenuEvent* ev);
void mouseReleaseEvent ( QMouseEvent * ev);
void initializeGL();
void timerEvent( QTimerEvent * );
void paintEvent(QPaintEvent * ev);
/// Actual drawing is implemented here
void paintGL();
/// Triggers a perspective update and a redraw
void resizeGL(int w, int h);
void wheelEvent(QWheelEvent* e);
void rotateWorldXY(double x, double y);
void rotateWorldZ(double z);
void translateWorld(double x, double y, double z);
private:
bool showCoordinateSystem;
bool fragmentShader;
QGLShaderProgram* shaderProgram;
QVector<GLEngine::Command> raytracerCommands;
// Creates the appropriate GL_PROJECTION matrix
void updatePerspective();
SyntopiaCore::Math::Vector3f screenTo3D(int sx, int sy, int sz);
int pendingRedraws; // the number of times we must redraw
// (when a redraw is requested we must draw two times, when double buffering)
int requiredRedraws;
Settings settings;
double scale;
double mouseSpeed;
double mouseTranslationSpeed;
double minimumScale;
QPoint oldPos;
QColor backgroundColor;
SyntopiaCore::Math::Vector3f translation;
SyntopiaCore::Math::Vector3f pivot;
SyntopiaCore::Math::Matrix4f rotation;
QList<Object3D*> objects;
QString infoText;
QMenu* contextMenu;
bool rmbDragging;
SyntopiaCore::Math::Vector3f cameraPosition;
SyntopiaCore::Math::Vector3f cameraUp;
SyntopiaCore::Math::Vector3f cameraTarget;
GLdouble modelViewCache[16];
GLdouble projectionCache[16];
GLint viewPortCache[16];
QTime textTimer;
bool disabled;
bool fastRotate;
bool doingRotate;
bool showDepth;
QMainWindow* mainWindow;
QImage staticImage;
};
};
};
|