Merge branch 'master' of github.com:openscad/openscad

master
Marius Kintel 2015-01-05 13:36:06 -05:00
commit d0cee28898
5 changed files with 57 additions and 54 deletions

View File

@ -3,13 +3,13 @@
#include "printutils.h" #include "printutils.h"
Camera::Camera(enum CameraType camtype) : Camera::Camera(enum CameraType camtype) :
type(camtype), projection(Camera::PERSPECTIVE), fov(45), height(60), viewall(false) type(camtype), projection(Camera::PERSPECTIVE), fov(45), viewall(false), zoom_value(60)
{ {
PRINTD("Camera()"); PRINTD("Camera()");
if (this->type == Camera::GIMBAL) { if (this->type == Camera::GIMBAL) {
object_trans << 0,0,0; object_trans << 0,0,0;
object_rot << 35,0,25; object_rot << 35,0,25;
viewer_distance = 500; zoom_value = 500;
} else if (this->type == Camera::VECTOR) { } else if (this->type == Camera::VECTOR) {
center << 0,0,0; center << 0,0,0;
Eigen::Vector3d cameradir(1, 1, -0.5); Eigen::Vector3d cameradir(1, 1, -0.5);
@ -26,8 +26,7 @@ void Camera::setup(std::vector<double> params)
type = Camera::GIMBAL; type = Camera::GIMBAL;
object_trans << params[0], params[1], params[2]; object_trans << params[0], params[1], params[2];
object_rot << params[3], params[4], params[5]; object_rot << params[3], params[4], params[5];
viewer_distance = params[6]; zoom_value = params[6];
height = params[6];
} else if (params.size() == 6) { } else if (params.size() == 6) {
type = Camera::VECTOR; type = Camera::VECTOR;
eye << params[0], params[1], params[2]; eye << params[0], params[1], params[2];
@ -74,13 +73,13 @@ void Camera::viewAll(const BoundingBox &bbox, float scalefactor)
switch (this->projection) { switch (this->projection) {
case Camera::ORTHOGONAL: case Camera::ORTHOGONAL:
this->height = bbox.diagonal().norm(); this->zoom_value = bbox.diagonal().norm();
break; break;
case Camera::PERSPECTIVE: { case Camera::PERSPECTIVE: {
double radius = bbox.diagonal().norm()/2; double radius = bbox.diagonal().norm()/2;
switch (this->type) { switch (this->type) {
case Camera::GIMBAL: case Camera::GIMBAL:
this->viewer_distance = radius / tan(this->fov*M_PI/360); this->zoom_value = radius / tan(this->fov*M_PI/360);
break; break;
case Camera::VECTOR: { case Camera::VECTOR: {
Vector3d cameradir = (this->center - this->eye).normalized(); Vector3d cameradir = (this->center - this->eye).normalized();
@ -101,25 +100,31 @@ void Camera::viewAll(const BoundingBox &bbox, float scalefactor)
void Camera::zoom(int delta) void Camera::zoom(int delta)
{ {
if (this->projection == PERSPECTIVE) { this->zoom_value *= pow(0.9, delta / 120.0);
this->viewer_distance *= pow(0.9, delta / 120.0);
}
else {
this->height *= pow(0.9, delta / 120.0);
}
} }
void Camera::setProjection(ProjectionType type) void Camera::setProjection(ProjectionType type)
{ {
if (this->projection != type) { this->projection = type;
switch (type) { }
case PERSPECTIVE:
this->viewer_distance = this->height; void Camera::resetView()
break; {
case ORTHOGONAL: object_rot << 35, 0, -25;
this->height = this->viewer_distance; object_trans << 0, 0, 0;
break; zoom_value = 140;
} }
this->projection = type;
} double Camera::zoomValue()
{
return zoom_value;
}
std::string Camera::statusText()
{
boost::format fmt(_("Viewport: translate = [ %.2f %.2f %.2f ], rotate = [ %.2f %.2f %.2f ], distance = %.2f"));
fmt % object_trans.x() % object_trans.y() % object_trans.z()
% object_rot.x() % object_rot.y() % object_rot.z()
% zoom_value;
return fmt.str();
} }

View File

@ -30,7 +30,10 @@ public:
void gimbalDefaultTranslate(); void gimbalDefaultTranslate();
void setProjection(ProjectionType type); void setProjection(ProjectionType type);
void zoom(int delta); void zoom(int delta);
void resetView();
void viewAll(const BoundingBox &bbox, float scalefactor = 1.0f); void viewAll(const BoundingBox &bbox, float scalefactor = 1.0f);
double zoomValue();
std::string statusText();
// Vectorcam // Vectorcam
Eigen::Vector3d eye; Eigen::Vector3d eye;
@ -40,14 +43,10 @@ public:
// Gimbalcam // Gimbalcam
Eigen::Vector3d object_trans; Eigen::Vector3d object_trans;
Eigen::Vector3d object_rot; Eigen::Vector3d object_rot;
double viewer_distance;
// Perspective settings // Perspective settings
double fov; // Field of view double fov; // Field of view
// Orthographic settings
double height; // world-space height of viewport
// true if camera should try to view everything in a given // true if camera should try to view everything in a given
// bounding box. // bounding box.
bool viewall; bool viewall;
@ -58,4 +57,10 @@ public:
unsigned int pixel_width; unsigned int pixel_width;
unsigned int pixel_height; unsigned int pixel_height;
protected:
// This is the viewer-distance in perspective mode and in
// ortographic mode, this is defining the viewport height
// (in world-space)
double zoom_value;
}; };

View File

@ -91,21 +91,22 @@ void GLView::setupCamera()
glLoadIdentity(); glLoadIdentity();
switch (this->cam.type) { switch (this->cam.type) {
case Camera::GIMBAL: case Camera::GIMBAL: {
switch (this->cam.projection) { switch (this->cam.projection) {
case Camera::PERSPECTIVE: { case Camera::PERSPECTIVE: {
double dist = cam.viewer_distance; double dist = cam.zoomValue();
gluPerspective(cam.fov, aspectratio, 0.1*dist, 100*dist); gluPerspective(cam.fov, aspectratio, 0.1 * dist, 100 * dist);
break; break;
} }
case Camera::ORTHOGONAL: { case Camera::ORTHOGONAL: {
glOrtho(-cam.height/2*aspectratio, cam.height*aspectratio/2, double height = cam.zoomValue();
-cam.height/2, cam.height/2, glOrtho(-height/2*aspectratio, height*aspectratio/2,
-height/2, height/2,
-far_far_away, +far_far_away); -far_far_away, +far_far_away);
break; break;
} }
} }
gluLookAt(0.0, -cam.viewer_distance, 0.0, gluLookAt(0.0, -cam.zoomValue(), 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0); 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
@ -114,6 +115,7 @@ void GLView::setupCamera()
glRotated(cam.object_rot.y(), 0.0, 1.0, 0.0); glRotated(cam.object_rot.y(), 0.0, 1.0, 0.0);
glRotated(cam.object_rot.z(), 0.0, 0.0, 1.0); glRotated(cam.object_rot.z(), 0.0, 0.0, 1.0);
break; break;
}
case Camera::VECTOR: { case Camera::VECTOR: {
switch (this->cam.projection) { switch (this->cam.projection) {
case Camera::PERSPECTIVE: { case Camera::PERSPECTIVE: {
@ -122,8 +124,9 @@ void GLView::setupCamera()
break; break;
} }
case Camera::ORTHOGONAL: { case Camera::ORTHOGONAL: {
glOrtho(-cam.height/2*aspectratio, cam.height*aspectratio/2, double height = cam.zoomValue();
-cam.height/2, cam.height/2, glOrtho(-height/2*aspectratio, height*aspectratio/2,
-height/2, height/2,
-far_far_away, +far_far_away); -far_far_away, +far_far_away);
break; break;
} }
@ -450,7 +453,7 @@ void GLView::showSmallaxes(const Color4f &col)
void GLView::showAxes(const Color4f &col) void GLView::showAxes(const Color4f &col)
{ {
double l = cam.projection == Camera::PERSPECTIVE ? cam.viewer_distance : cam.height; double l = cam.zoomValue();
// FIXME: doesn't work under Vector Camera // FIXME: doesn't work under Vector Camera
// Large gray axis cross inline with the model // Large gray axis cross inline with the model
@ -489,7 +492,7 @@ void GLView::showCrosshairs()
glBegin(GL_LINES); glBegin(GL_LINES);
for (double xf = -1; xf <= +1; xf += 2) for (double xf = -1; xf <= +1; xf += 2)
for (double yf = -1; yf <= +1; yf += 2) { for (double yf = -1; yf <= +1; yf += 2) {
double vd = cam.viewer_distance/8; double vd = cam.zoomValue()/8;
glVertex3d(-xf*vd, -yf*vd, -vd); glVertex3d(-xf*vd, -yf*vd, -vd);
glVertex3d(+xf*vd, +yf*vd, +vd); glVertex3d(+xf*vd, +yf*vd, +vd);
} }

View File

@ -84,9 +84,7 @@ void QGLView::init()
void QGLView::resetView() void QGLView::resetView()
{ {
cam.object_rot << 35, 0, -25; cam.resetView();
cam.object_trans << 0, 0, 0;
cam.viewer_distance = 140;
} }
void QGLView::viewAll() void QGLView::viewAll()
@ -164,16 +162,9 @@ void QGLView::paintGL()
GLView::paintGL(); GLView::paintGL();
if (statusLabel) { if (statusLabel) {
QString msg;
Camera nc(cam); Camera nc(cam);
nc.gimbalDefaultTranslate(); nc.gimbalDefaultTranslate();
msg.sprintf(_("Viewport: translate = [ %.2f %.2f %.2f ], rotate = [ %.2f %.2f %.2f ], distance = %.2f"), statusLabel->setText(QString::fromStdString(nc.statusText()));
nc.object_trans.x(), nc.object_trans.y(), nc.object_trans.z(),
nc.object_rot.x(), nc.object_rot.y(), nc.object_rot.z(),
nc.viewer_distance );
statusLabel->setText(msg);
} }
if (running_under_wine) swapBuffers(); if (running_under_wine) swapBuffers();
@ -252,11 +243,10 @@ void QGLView::mouseMoveEvent(QMouseEvent *event)
// Middle button pans in the xy plane // Middle button pans in the xy plane
// Shift-right and Shift-middle zooms // Shift-right and Shift-middle zooms
if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) { if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) {
cam.viewer_distance += (GLdouble)dy; cam.zoom(-12.0 * dy);
} else { } else {
double mx = +(dx) * 3.0 * cam.zoomValue() / QWidget::width();
double mx = +(dx) * cam.viewer_distance/1000; double mz = -(dy) * 3.0 * cam.zoomValue() / QWidget::height();
double mz = -(dy) * cam.viewer_distance/1000;
double my = 0; double my = 0;
#if (QT_VERSION < QT_VERSION_CHECK(4, 7, 0)) #if (QT_VERSION < QT_VERSION_CHECK(4, 7, 0))

View File

@ -1505,7 +1505,7 @@ void MainWindow::updateTemporalVariables()
vpr.push_back(Value(fmodf(360 - qglview->cam.object_rot.z(), 360))); vpr.push_back(Value(fmodf(360 - qglview->cam.object_rot.z(), 360)));
top_ctx.set_variable("$vpr", ValuePtr(vpr)); top_ctx.set_variable("$vpr", ValuePtr(vpr));
top_ctx.set_variable("$vpd", ValuePtr(qglview->cam.viewer_distance)); top_ctx.set_variable("$vpd", ValuePtr(qglview->cam.zoomValue()));
} }
@ -1529,7 +1529,7 @@ void MainWindow::updateCamera()
double rx = cam.object_rot.x(); double rx = cam.object_rot.x();
double ry = cam.object_rot.y(); double ry = cam.object_rot.y();
double rz = cam.object_rot.z(); double rz = cam.object_rot.z();
double d = cam.viewer_distance; double d = cam.zoomValue();
double x, y, z; double x, y, z;
const ValuePtr vpr = root_module->lookup_variable("$vpr"); const ValuePtr vpr = root_module->lookup_variable("$vpr");