summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authorJakob Odersky <jodersky@gmail.com>2011-04-06 13:07:59 +0000
committerJakob Odersky <jodersky@gmail.com>2011-04-06 13:07:59 +0000
commitae19966fd47370e6c769beb9c403abd6697fe361 (patch)
tree04195cca468f6259f2e556c5ffa1ce457ba102ae /src/gui
parent6fadcf8a8c8c597b8686dcc5cbfb8a89e76c08cf (diff)
downloadvhc-ae19966fd47370e6c769beb9c403abd6697fe361.tar.gz
vhc-ae19966fd47370e6c769beb9c403abd6697fe361.tar.bz2
vhc-ae19966fd47370e6c769beb9c403abd6697fe361.zip
Une camera qui fonctionne!
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/Camera.h61
-rw-r--r--src/gui/ElementRenderer.h2
-rw-r--r--src/gui/GLWidget.cc163
-rw-r--r--src/gui/GLWidget.h8
-rw-r--r--src/gui/Makefile9
-rw-r--r--src/gui/gui.pro2
-rw-r--r--src/gui/moc_GLWidget.cpp2
7 files changed, 108 insertions, 139 deletions
diff --git a/src/gui/Camera.h b/src/gui/Camera.h
index 75e07b2..fccd3f9 100644
--- a/src/gui/Camera.h
+++ b/src/gui/Camera.h
@@ -7,58 +7,53 @@
#ifndef CAMERA_H_
#define CAMERA_H_
-#include <math.h>
+#include <cmath>
#include <QtOpenGL>
#include "exception.h"
+#include "Vector3D.h"
+
+namespace vhc {
class Camera {
private:
+ Vector3D position; //from position
+ Vector3D direction; //to position + direction
+ Vector3D up;
-public:
+ double heading; //left/right
+ double pitch; //up/down
- double r;
- double theta;
- double phi;
- Camera(double r, double theta, double phi): r(r), theta(theta), phi(phi) {};
- virtual ~Camera() {};
+public:
+ Camera(): position(1, 1, 1), direction(1, 0, 0), up(0,0,1), heading(M_PI_4), pitch(-M_PI_4) {};
+ virtual ~Camera() {};
void setView() {
- double eyeX, eyeY, eyeZ;
- toCarthesian(r, theta, phi, eyeX, eyeY, eyeZ);
- gluLookAt(eyeX,eyeY,eyeZ,
- 0,0,0,
- 0,1,0);
- }
+ Vector3D td = direction.rotate(Vector3D::j, pitch).rotate(Vector3D::k, heading);; //tranformed direction
+ Vector3D at = position + td;
+ gluLookAt(position.getX(), position.getY(), position.getZ(),
+ at.getX(), at.getY(), at.getZ(),
+ up.getX(), up.getY(), up.getZ());
+ }
- static double sign(double value) {return (value > 0) - (value < 0);}
-
- static void toSpherical(double x, double y, double z, double& r, double&theta, double& phi) {
- r = sqrt(x * x + y * y + z * z);
- if (r == 0) {
- theta = 0;
- phi = 0;
- return;
- }
+ void addHeading(double h) {
+ heading += h;
+ }
- theta = acos(z / r);
+ void addPitch(double h) {
+ pitch += h;
+ }
- if (x > 0) phi = atan(y / x);
- else if (x == 0) phi = sign(y) * M_PI / 2;
- else if (x < 0 && y >= 0) phi = atan(y / x) + M_PI;
- else if (x < 0 && y < 0) phi = atan(y / x) - M_PI;
- else throw vhc::Exception("This should not happen!");
+ void move(const Vector3D& moveVector) {
+ Vector3D mv = moveVector.rotate(Vector3D::j, pitch).rotate(Vector3D::k, heading);
+ position = position + mv;
}
- static void toCarthesian(double r, double theta, double phi, double& x, double& y, double& z) {
- x = r * sin(theta) * cos(phi);
- y = r * sin(theta) * sin(phi);
- z = r * cos(theta);
- }
};
+}
#endif /* CAMERA_H_ */
diff --git a/src/gui/ElementRenderer.h b/src/gui/ElementRenderer.h
index dd55471..4d50a9f 100644
--- a/src/gui/ElementRenderer.h
+++ b/src/gui/ElementRenderer.h
@@ -17,7 +17,7 @@ namespace vhc {
class ElementRenderer: public ElementVisitor {
private:
- static const int SLICES = 12;
+ static const int SLICES = 100;
static const int STACKS_PER_LENGTH = 20;
public:
diff --git a/src/gui/GLWidget.cc b/src/gui/GLWidget.cc
index 720dd9e..a9c5e86 100644
--- a/src/gui/GLWidget.cc
+++ b/src/gui/GLWidget.cc
@@ -9,57 +9,49 @@
#include "util.h"
#include "Vector3D.h"
-void torus(int numc, int numt, double fraction = 1 , double R = 1, double r = 0.1) {
- int i, j, k;
- double s, t, x, y, z, twopi;
-
- twopi = 2 * M_PI;
- for (i = 0; i < numc; i++) {
- glBegin(GL_QUAD_STRIP);
- for (j = 0; j <= numt / fraction ; j++) {
- for (k = 1; k >= 0; k--) {
- s = (i + k) % numc + 0.5;
- t = j % numt;
-
- x = (R+r*cos(s*twopi/numc))*cos(t*twopi/numt);
- y = (R+r*cos(s*twopi/numc))*sin(t*twopi/numt);
- z = r * sin(s * twopi / numc);
- glColor3d(0, s/numc, t/numc);
- glVertex3f(x, y, z);
- }
- }
- glEnd();
- }
-}
-
-
+using namespace vhc;
void axes() {
glBegin(GL_LINES);
glColor3d(1, 0, 0);
glVertex3d(0, 0, 0); // origin of the line
- glVertex3d(100, 0, 0); // ending point of the line
+ glVertex3d(1, 0, 0); // ending point of the line
glEnd();
glBegin(GL_LINES);
glColor3d(0, 1, 0);
glVertex3d(0, 0, 0); // origin of the line
- glVertex3d(0, 100, 0); // ending point of the line
+ glVertex3d(0, 1, 0); // ending point of the line
glEnd();
glBegin(GL_LINES);
glColor3d(0, 0, 1);
glVertex3d(0, 0, 0); // origin of the line
- glVertex3d(0, 0, 100); // ending point of the line
+ glVertex3d(0, 0, 1); // ending point of the line
+ glEnd();
+}
+
+void grid() {
+ int size = 10;
+ glColor3d(0.5, 0.5, 0.5);
+ glBegin(GL_LINES);
+ for (int i = 0; i <= 2 * size; ++i) {
+ glVertex3d(1.0 * (i - size) / size, -1, 0);
+ glVertex3d(1.0 * (i - size) / size, 1, 0);
+ };
+ glEnd();
+ glBegin(GL_LINES);
+ for (int i = 0; i <= 2 * size; ++i) {
+ glVertex3d(-1, 1.0 * (i - size) / size, 0);
+ glVertex3d(1, 1.0 * (i - size) / size, 0);
+ };
glEnd();
}
GLWidget::GLWidget (QWidget* parent)
- : QGLWidget (parent), camera(10,0,0), wireframe(false)
+ : QGLWidget (parent), wireframe(false), camera(), center()
{
- eyeX = 0;
- eyeY = 0;
- eyeZ = 10;
+ setMouseTracking(true);
resize (sizeHint ());
}
@@ -70,30 +62,40 @@ QSize GLWidget::minimumSizeHint () const {
}
QSize GLWidget::sizeHint () const {
- return QSize (800, 600);
+ return QSize (1000, 800);
}
void GLWidget::initializeGL () {
glClearColor (0, 0, 0, 1.0);
glEnable (GL_DEPTH_TEST);
- gluPerspective(65.0, 4.0/3, 1.0, 1000.0);
+ gluPerspective(65.0, 4.0/3.0, 1.0, 10000.0);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
}
+void GLWidget::resizeGL (int width, int height) {
+ center = QPoint(width / 2, height / 2);
+ glViewport (0, 0, width, height);
+ glMatrixMode (GL_PROJECTION);
+ glLoadIdentity();
+ gluPerspective(65.0, 1, 1.0, 10000.0);
+ glMatrixMode (GL_MODELVIEW);
+}
+
void GLWidget::paintGL () {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
- //gluLookAt(eyeX,eyeY,eyeZ,
- //0,0,0,
- //0,1,0);
- //glTranslated (0.0, 0.0, -10.0);
- //glColor3d (0.0, 0.0, 1.0);
camera.setView();
-
+ glPushMatrix();
+ glScaled(100, 100, 100);
+ grid();
axes();
+ glPopMatrix();
+
+
+ QCursor::setPos(center);
- glScaled (300.0, 300.0, 300.0);
+ glScaled (100.0, 100.0, 100.0);
if (wireframe) {
@@ -121,44 +123,9 @@ void GLWidget::paintGL () {
delete ce; ce = NULL;
-/*
- glBegin (GL_POLYGON);
- glColor3d(1,0,0);
- glVertex3d (-300, -300, 0.0);
- glColor3d(0,1,0);
- glVertex3d (300 + 10, -300, 0.0);
- glColor3d(0,0,1);
- glVertex3d (0, 300, 0.0);
- glColor3d(1,1,1);
- glVertex3d (0, 0, 100);
- glColor3d(0,1,0);
- glVertex3d (300 + 10, -300, 0.0);
- glEnd ();
-
-
- axes();
-
- glColor3d(0,0,1);
- glScaled(300,300,300);
-
- glPolygonMode(GL_FRONT, GL_LINE);
- glPolygonMode(GL_BACK, GL_LINE);
- torus(12, 20, 4, 1, 0.1);
-
- vhc::util::cylinder(0.1, 0, 2, 12, 20);
-
- glPolygonMode(GL_FRONT, GL_FILL);
- glPolygonMode(GL_BACK, GL_FILL);*/
-
}
-void GLWidget::resizeGL (int width, int height) {
- glViewport (0, 0, width, height);
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity ();
- glOrtho (-width, width, -height, height, -1000.0, 1000.0);
- glMatrixMode (GL_MODELVIEW);
-}
+
void GLWidget::mousePressEvent (QMouseEvent* event) {
}
@@ -167,33 +134,36 @@ void GLWidget::mouseReleaseEvent (QMouseEvent* event) {
}
void GLWidget::keyPressEvent (QKeyEvent* event) {
+ Vector3D mv = Vector3D::Null;
switch (event->key()) {
case Qt::Key_Escape:
qApp->quit();
break;
+ case Qt::Key_A:
+ mv = mv - Vector3D::j;
+ break;
+ case Qt::Key_W:
+ mv = mv + Vector3D::i;
+ break;
+ case Qt::Key_D:
+ mv = mv + Vector3D::j;
+ break;
+ case Qt::Key_S:
+ mv = mv - Vector3D::i;
+ break;
case Qt::Key_Up:
- //eyeZ += 10;
- camera.theta += 2 * M_PI / 100;
+ camera.addPitch(2 * M_PI / 100);
break;
case Qt::Key_Down:
- //eyeZ -= 10;
- camera.theta -= 2 * M_PI / 100;
+ camera.addPitch(-2 * M_PI / 100);
break;
case Qt::Key_Right:
- //eyeX += 10;
- camera.phi += 2 * M_PI / 100;
+ camera.addHeading(-2 * M_PI / 100);
break;
case Qt::Key_Left:
- //eyeX -= 10;
- camera.phi -= 2 * M_PI / 100;
+ camera.addHeading(2 * M_PI / 100);
break;
case Qt::Key_R:
- //eyeX = 0;
- //eyeY = 0;
- //eyeZ = 10;
- camera.r = 10;
- camera.phi = 0;
- camera.theta = 0;
break;
case Qt::Key_Space:
wireframe = !wireframe;
@@ -201,9 +171,20 @@ void GLWidget::keyPressEvent (QKeyEvent* event) {
default:
break;
}
- repaint();
+ camera.move(mv);
+ updateGL();
+ //repaint();
}
void GLWidget::keyReleaseEvent (QKeyEvent* event) {
}
+
+void GLWidget::mouseMoveEvent(QMouseEvent* event) {
+ int dheading = -QCursor::pos().x() + center.x();
+ int dpitch = QCursor::pos().y() - center.y();
+ camera.addHeading(1.0 * dheading / 20);
+ camera.addPitch(1.0 * dpitch / 20);
+ updateGL();
+
+}
diff --git a/src/gui/GLWidget.h b/src/gui/GLWidget.h
index 2cbbb68..a5408c3 100644
--- a/src/gui/GLWidget.h
+++ b/src/gui/GLWidget.h
@@ -15,12 +15,9 @@ public:
QSize minimumSizeHint () const;
QSize sizeHint () const;
- double eyeX;
- double eyeY;
- double eyeZ;
-
- Camera camera;
bool wireframe;
+ vhc::Camera camera;
+ QPoint center;
protected:
@@ -32,6 +29,7 @@ protected:
void mouseReleaseEvent (QMouseEvent* event);
void keyPressEvent (QKeyEvent* event);
void keyReleaseEvent (QKeyEvent* event);
+ void mouseMoveEvent(QMouseEvent* event);
};
#endif
diff --git a/src/gui/Makefile b/src/gui/Makefile
index a6b4f8c..9a05f2c 100644
--- a/src/gui/Makefile
+++ b/src/gui/Makefile
@@ -1,6 +1,6 @@
#############################################################################
# Makefile for building: $(BINDIR)/gui/gui
-# Generated by qmake (2.01a) (Qt 4.7.0) on: Tue Apr 5 21:04:56 2011
+# Generated by qmake (2.01a) (Qt 4.7.0) on: Wed Apr 6 14:53:06 2011
# Project: gui.pro
# Template: app
# Command: /usr/bin/qmake -o Makefile gui.pro
@@ -45,12 +45,10 @@ OBJECTS_DIR = $(BINDIR)/gui/
SOURCES = Main.cc \
GLWidget.cc \
- Camera.cc \
ElementRenderer.cc \
util.cc moc_GLWidget.cpp
OBJECTS = $(BINDIR)/gui/Main.o \
$(BINDIR)/gui/GLWidget.o \
- $(BINDIR)/gui/Camera.o \
$(BINDIR)/gui/ElementRenderer.o \
$(BINDIR)/gui/util.o \
$(BINDIR)/gui/moc_GLWidget.o
@@ -162,7 +160,7 @@ qmake: FORCE
dist:
@$(CHK_DIR_EXISTS) $(BINDIR)/gui/gui1.0.0 || $(MKDIR) $(BINDIR)/gui/gui1.0.0
- $(COPY_FILE) --parents $(SOURCES) $(DIST) $(BINDIR)/gui/gui1.0.0/ && $(COPY_FILE) --parents GLWidget.h Camera.h ElementRenderer.h util.h $(BINDIR)/gui/gui1.0.0/ && $(COPY_FILE) --parents Main.cc GLWidget.cc Camera.cc ElementRenderer.cc util.cc $(BINDIR)/gui/gui1.0.0/ && (cd `dirname $(BINDIR)/gui/gui1.0.0` && $(TAR) gui1.0.0.tar gui1.0.0 && $(COMPRESS) gui1.0.0.tar) && $(MOVE) `dirname $(BINDIR)/gui/gui1.0.0`/gui1.0.0.tar.gz . && $(DEL_FILE) -r $(BINDIR)/gui/gui1.0.0
+ $(COPY_FILE) --parents $(SOURCES) $(DIST) $(BINDIR)/gui/gui1.0.0/ && $(COPY_FILE) --parents GLWidget.h Camera.h ElementRenderer.h util.h $(BINDIR)/gui/gui1.0.0/ && $(COPY_FILE) --parents Main.cc GLWidget.cc ElementRenderer.cc util.cc $(BINDIR)/gui/gui1.0.0/ && (cd `dirname $(BINDIR)/gui/gui1.0.0` && $(TAR) gui1.0.0.tar gui1.0.0 && $(COMPRESS) gui1.0.0.tar) && $(MOVE) `dirname $(BINDIR)/gui/gui1.0.0`/gui1.0.0.tar.gz . && $(DEL_FILE) -r $(BINDIR)/gui/gui1.0.0
clean:compiler_clean
@@ -219,9 +217,6 @@ $(BINDIR)/gui/GLWidget.o: GLWidget.cc GLWidget.h \
util.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/GLWidget.o GLWidget.cc
-$(BINDIR)/gui/Camera.o: Camera.cc Camera.h
- $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/Camera.o Camera.cc
-
$(BINDIR)/gui/ElementRenderer.o: ElementRenderer.cc ElementRenderer.h \
util.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(BINDIR)/gui/ElementRenderer.o ElementRenderer.cc
diff --git a/src/gui/gui.pro b/src/gui/gui.pro
index ee15909..b56e8b3 100644
--- a/src/gui/gui.pro
+++ b/src/gui/gui.pro
@@ -11,4 +11,4 @@ QT += opengl
# Input
HEADERS += GLWidget.h Camera.h ElementRenderer.h util.h
-SOURCES += Main.cc GLWidget.cc Camera.cc ElementRenderer.cc util.cc
+SOURCES += Main.cc GLWidget.cc Camera.h ElementRenderer.cc util.cc
diff --git a/src/gui/moc_GLWidget.cpp b/src/gui/moc_GLWidget.cpp
index 0e6bd03..e9961f2 100644
--- a/src/gui/moc_GLWidget.cpp
+++ b/src/gui/moc_GLWidget.cpp
@@ -1,7 +1,7 @@
/****************************************************************************
** Meta object code from reading C++ file 'GLWidget.h'
**
-** Created: Tue Apr 5 13:32:33 2011
+** Created: Wed Apr 6 14:53:15 2011
** by: The Qt Meta Object Compiler version 62 (Qt 4.7.0)
**
** WARNING! All changes made in this file will be lost!