From f698f2ad7ecd8d8c7890c3435eab3259ade09470 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 14 May 2011 16:43:43 +0100 Subject: Move light setup code out of paint method and into init. diff --git a/src/glview.cc b/src/glview.cc index 870a1c9..3a4218a 100644 --- a/src/glview.cc +++ b/src/glview.cc @@ -85,6 +85,22 @@ void GLView::initializeGL() glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; + GLfloat light_position0[] = {-1.0, -1.0, +1.0, 0.0}; + GLfloat light_position1[] = {+1.0, +1.0, -1.0, 0.0}; + + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT0, GL_POSITION, light_position0); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT1, GL_POSITION, light_position1); + glEnable(GL_LIGHT1); + glEnable(GL_LIGHTING); + glEnable(GL_NORMALIZE); + + glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); + glEnable(GL_COLOR_MATERIAL); + #ifdef ENABLE_OPENCSG GLenum err = glewInit(); if (GLEW_OK != err) { @@ -234,22 +250,6 @@ void GLView::paintGL() glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; - GLfloat light_position0[] = {-1.0, -1.0, +1.0, 0.0}; - GLfloat light_position1[] = {+1.0, +1.0, -1.0, 0.0}; - - glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); - glLightfv(GL_LIGHT0, GL_POSITION, light_position0); - glEnable(GL_LIGHT0); - glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); - glLightfv(GL_LIGHT1, GL_POSITION, light_position1); - glEnable(GL_LIGHT1); - glEnable(GL_LIGHTING); - glEnable(GL_NORMALIZE); - - glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - glRotated(object_rot_x, 1.0, 0.0, 0.0); glRotated(object_rot_y, 0.0, 1.0, 0.0); glRotated(object_rot_z, 0.0, 0.0, 1.0); -- cgit v0.10.1 From b269b5733b7d8adbb5045568a2f994e35d685b86 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 14 May 2011 17:10:22 +0100 Subject: Move perspective setup into resize function, still needs to be called in paint when displaying axes. diff --git a/src/glview.cc b/src/glview.cc index 3a4218a..f2bc282 100644 --- a/src/glview.cc +++ b/src/glview.cc @@ -228,15 +228,12 @@ void GLView::resizeGL(int w, int h) #endif glViewport(0, 0, w, h); w_h_ratio = sqrt((double)w / (double)h); + + setupPerspective(); } -void GLView::paintGL() +void GLView::setupPerspective() { - const QColor &bgcol = Preferences::inst()->color(Preferences::BACKGROUND_COLOR); - glClearColor(bgcol.redF(), bgcol.greenF(), bgcol.blueF(), 0.0); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (orthomode) @@ -245,6 +242,15 @@ void GLView::paintGL() -FAR_FAR_AWAY, +FAR_FAR_AWAY); else glFrustum(-w_h_ratio, +w_h_ratio, -(1/w_h_ratio), +(1/w_h_ratio), +10.0, +FAR_FAR_AWAY); +} + +void GLView::paintGL() +{ + const QColor &bgcol = Preferences::inst()->color(Preferences::BACKGROUND_COLOR); + glClearColor(bgcol.redF(), bgcol.greenF(), bgcol.blueF(), 0.0); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + gluLookAt(0.0, -viewer_distance, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); @@ -374,6 +380,9 @@ void GLView::paintGL() glVertex3d(zlabel_x-3, zlabel_y+3, 0); glVertex3d(zlabel_x+3, zlabel_y+3, 0); glVertex3d(zlabel_x-3, zlabel_y-3, 0); glVertex3d(zlabel_x+3, zlabel_y+3, 0); glEnd(); + + //Restore perspective for next paint + setupPerspective(); } if (statusLabel) { -- cgit v0.10.1 From b11865d5ce16e327e07a8d9eb6a875114f10a832 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 14 May 2011 19:57:12 +0100 Subject: Some fixes to previous commit, and simplified normalisation. diff --git a/src/GLView.h b/src/GLView.h index 7516894..5ea3907 100644 --- a/src/GLView.h +++ b/src/GLView.h @@ -68,7 +68,10 @@ private: void initializeGL(); void resizeGL(int w, int h); + void setupPerspective(); + void setupOrtho(); void paintGL(); + void normalizeAngle(GLdouble& angle); #ifdef ENABLE_OPENCSG private slots: diff --git a/src/glview.cc b/src/glview.cc index f2bc282..b989cab 100644 --- a/src/glview.cc +++ b/src/glview.cc @@ -236,16 +236,26 @@ void GLView::setupPerspective() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); - if (orthomode) - glOrtho(-w_h_ratio*viewer_distance/10, +w_h_ratio*viewer_distance/10, - -(1/w_h_ratio)*viewer_distance/10, +(1/w_h_ratio)*viewer_distance/10, - -FAR_FAR_AWAY, +FAR_FAR_AWAY); - else - glFrustum(-w_h_ratio, +w_h_ratio, -(1/w_h_ratio), +(1/w_h_ratio), +10.0, +FAR_FAR_AWAY); + glFrustum(-w_h_ratio, +w_h_ratio, -(1/w_h_ratio), +(1/w_h_ratio), +10.0, +FAR_FAR_AWAY); +} + +void GLView::setupOrtho() +{ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-w_h_ratio*viewer_distance/10, +w_h_ratio*viewer_distance/10, + -(1/w_h_ratio)*viewer_distance/10, +(1/w_h_ratio)*viewer_distance/10, + -FAR_FAR_AWAY, +FAR_FAR_AWAY); } void GLView::paintGL() { + if (orthomode) + setupOrtho(); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + const QColor &bgcol = Preferences::inst()->color(Preferences::BACKGROUND_COLOR); glClearColor(bgcol.redF(), bgcol.greenF(), bgcol.blueF(), 0.0); @@ -253,9 +263,6 @@ void GLView::paintGL() gluLookAt(0.0, -viewer_distance, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glRotated(object_rot_x, 1.0, 0.0, 0.0); glRotated(object_rot_y, 0.0, 1.0, 0.0); glRotated(object_rot_z, 0.0, 0.0, 1.0); @@ -382,7 +389,8 @@ void GLView::paintGL() glEnd(); //Restore perspective for next paint - setupPerspective(); + if(!orthomode) + setupPerspective(); } if (statusLabel) { @@ -457,6 +465,14 @@ static void mat_rot(double *trg, double angle, double x, double y, double z) trg[i] = m[i]; } +void GLView::normalizeAngle(GLdouble& angle) +{ + while(angle < 0) + angle += 360; + while(angle > 360) + angle -= 360; +} + void GLView::mouseMoveEvent(QMouseEvent *event) { int this_mouse_x = event->globalX(); @@ -468,18 +484,10 @@ void GLView::mouseMoveEvent(QMouseEvent *event) object_rot_y += (this_mouse_x-last_mouse_x) * 0.7; else object_rot_z += (this_mouse_x-last_mouse_x) * 0.7; - while (object_rot_x < 0) - object_rot_x += 360; - while (object_rot_x >= 360) - object_rot_x -= 360; - while (object_rot_y < 0) - object_rot_y += 360; - while (object_rot_y >= 360) - object_rot_y -= 360; - while (object_rot_z < 0) - object_rot_z += 360; - while (object_rot_z >= 360) - object_rot_z -= 360; + + normalizeAngle(object_rot_x); + normalizeAngle(object_rot_y); + normalizeAngle(object_rot_z); } else { double mx = +(this_mouse_x-last_mouse_x) * viewer_distance/1000; double my = -(this_mouse_y-last_mouse_y) * viewer_distance/1000; -- cgit v0.10.1 From ccaf10dcc61cab770f6185b59703f1998b35618f Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 14 May 2011 20:47:17 +0100 Subject: Reduce multiple divisions by 10, and move similar code into single function. diff --git a/src/GLView.h b/src/GLView.h index 5ea3907..4977abb 100644 --- a/src/GLView.h +++ b/src/GLView.h @@ -69,7 +69,7 @@ private: void initializeGL(); void resizeGL(int w, int h); void setupPerspective(); - void setupOrtho(); + void setupOrtho(double distance,bool offset=false); void paintGL(); void normalizeAngle(GLdouble& angle); diff --git a/src/glview.cc b/src/glview.cc index b989cab..887afea 100644 --- a/src/glview.cc +++ b/src/glview.cc @@ -239,19 +239,22 @@ void GLView::setupPerspective() glFrustum(-w_h_ratio, +w_h_ratio, -(1/w_h_ratio), +(1/w_h_ratio), +10.0, +FAR_FAR_AWAY); } -void GLView::setupOrtho() +void GLView::setupOrtho(double distance, bool offset) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(-w_h_ratio*viewer_distance/10, +w_h_ratio*viewer_distance/10, - -(1/w_h_ratio)*viewer_distance/10, +(1/w_h_ratio)*viewer_distance/10, + if(offset) + glTranslated(-0.8, -0.8, 0); + double l = distance/10; + glOrtho(-w_h_ratio*l, +w_h_ratio*l, + -(1/w_h_ratio)*l, +(1/w_h_ratio)*l, -FAR_FAR_AWAY, +FAR_FAR_AWAY); } void GLView::paintGL() { if (orthomode) - setupOrtho(); + setupOrtho(viewer_distance); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -293,12 +296,13 @@ void GLView::paintGL() glLineWidth(1); glColor3d(0.5, 0.5, 0.5); glBegin(GL_LINES); - glVertex3d(-viewer_distance/10, 0, 0); - glVertex3d(+viewer_distance/10, 0, 0); - glVertex3d(0, -viewer_distance/10, 0); - glVertex3d(0, +viewer_distance/10, 0); - glVertex3d(0, 0, -viewer_distance/10); - glVertex3d(0, 0, +viewer_distance/10); + double l = viewer_distance/10; + glVertex3d(-l, 0, 0); + glVertex3d(+l, 0, 0); + glVertex3d(0, -l, 0); + glVertex3d(0, +l, 0); + glVertex3d(0, 0, -l); + glVertex3d(0, 0, +l); glEnd(); } @@ -317,12 +321,8 @@ void GLView::paintGL() { glDepthFunc(GL_ALWAYS); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glTranslated(-0.8, -0.8, 0); - glOrtho(-w_h_ratio*1000/10, +w_h_ratio*1000/10, - -(1/w_h_ratio)*1000/10, +(1/w_h_ratio)*1000/10, - -FAR_FAR_AWAY, +FAR_FAR_AWAY); + setupOrtho(1000,true); + gluLookAt(0.0, -1000, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); -- cgit v0.10.1 From 9eff0b4bdb22657e77c55fcc4e5435578350408b Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 14 May 2011 21:20:55 +0100 Subject: All this matrix math is very clever, but if you translate before you rotate, I think its redundant. diff --git a/src/glview.cc b/src/glview.cc index 887afea..dd2a3b5 100644 --- a/src/glview.cc +++ b/src/glview.cc @@ -266,6 +266,8 @@ void GLView::paintGL() gluLookAt(0.0, -viewer_distance, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); + glTranslated(object_trans_x, object_trans_y, object_trans_z); + glRotated(object_rot_x, 1.0, 0.0, 0.0); glRotated(object_rot_y, 0.0, 1.0, 0.0); glRotated(object_rot_z, 0.0, 0.0, 1.0); @@ -287,8 +289,6 @@ void GLView::paintGL() glEnd(); } - glTranslated(object_trans_x, object_trans_y, object_trans_z); - // Large gray axis cross inline with the model // FIXME: This is always gray - adjust color to keep contrast with background if (showaxes) @@ -431,39 +431,6 @@ void GLView::mousePressEvent(QMouseEvent *event) setFocus(); } -static void mat_id(double *trg) -{ - for (int i = 0; i < 16; i++) - trg[i] = i%5 == 0; -} - -static void mat_mul(double *trg, const double *m1, const double *m2) -{ - double m[16]; - for (int x = 0; x < 4; x++) - for (int y = 0; y < 4; y++) - { - m[x+y*4] = 0; - for (int i = 0; i < 4; i++) - m[x+y*4] += m1[i+y*4] * m2[x+i*4]; - } - for (int i = 0; i < 16; i++) - trg[i] = m[i]; -} - -static void mat_rot(double *trg, double angle, double x, double y, double z) -{ - double s = sin(M_PI*angle/180), c = cos(M_PI*angle/180); - double cc = 1 - c; - double m[16] = { - x*x*cc+c, x*y*cc-z*s, x*z*cc+y*s, 0, - y*x*cc+z*s, y*y*cc+c, y*z*cc-x*s, 0, - x*z*cc-y*s, y*z*cc+x*s, z*z*cc+c, 0, - 0, 0, 0, 1 - }; - for (int i = 0; i < 16; i++) - trg[i] = m[i]; -} void GLView::normalizeAngle(GLdouble& angle) { @@ -477,43 +444,26 @@ void GLView::mouseMoveEvent(QMouseEvent *event) { int this_mouse_x = event->globalX(); int this_mouse_y = event->globalY(); + int dx = (this_mouse_x-last_mouse_x); + int dy = (this_mouse_y-last_mouse_y); if (mouse_drag_active) { if ((event->buttons() & Qt::LeftButton) != 0) { object_rot_x += (this_mouse_y-last_mouse_y) * 0.7; if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) - object_rot_y += (this_mouse_x-last_mouse_x) * 0.7; + object_rot_y += dx * 0.7; else - object_rot_z += (this_mouse_x-last_mouse_x) * 0.7; + object_rot_z += dx * 0.7; normalizeAngle(object_rot_x); normalizeAngle(object_rot_y); normalizeAngle(object_rot_z); } else { - double mx = +(this_mouse_x-last_mouse_x) * viewer_distance/1000; - double my = -(this_mouse_y-last_mouse_y) * viewer_distance/1000; - double rx[16], ry[16], rz[16], tm[16]; - mat_rot(rx, -object_rot_x, 1.0, 0.0, 0.0); - mat_rot(ry, -object_rot_y, 0.0, 1.0, 0.0); - mat_rot(rz, -object_rot_z, 0.0, 0.0, 1.0); - mat_id(tm); - mat_mul(tm, rx, tm); - mat_mul(tm, ry, tm); - mat_mul(tm, rz, tm); - double vec[16] = { - 0, 0, 0, mx, - 0, 0, 0, 0, - 0, 0, 0, my, - 0, 0, 0, 1 - }; if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) { - vec[3] = 0; - vec[7] = my; - vec[11] = 0; + viewer_distance += (GLdouble)dy; + } else { + object_trans_x += (GLint)dx; + object_trans_z -= (GLint)dy; } - mat_mul(tm, tm, vec); - object_trans_x += tm[3]; - object_trans_y += tm[7]; - object_trans_z += tm[11]; } updateGL(); emit doAnimateUpdate(); -- cgit v0.10.1 From a878f3773d4c2647451ec93a85203ce906e13e2d Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 14 May 2011 22:29:45 +0100 Subject: Use QPoint instead of seperate x and y mouse vars. diff --git a/src/GLView.h b/src/GLView.h index 4977abb..be21578 100644 --- a/src/GLView.h +++ b/src/GLView.h @@ -57,8 +57,7 @@ private: double w_h_ratio; bool mouse_drag_active; - int last_mouse_x; - int last_mouse_y; + QPoint last_mouse; void keyPressEvent(QKeyEvent *event); void wheelEvent(QWheelEvent *event); diff --git a/src/glview.cc b/src/glview.cc index dd2a3b5..d5285dc 100644 --- a/src/glview.cc +++ b/src/glview.cc @@ -48,8 +48,6 @@ GLView::GLView(QWidget *parent) : QGLWidget(parent) object_trans_z = 0; mouse_drag_active = false; - last_mouse_x = 0; - last_mouse_y = 0; orthomode = false; showaxes = false; @@ -425,8 +423,7 @@ void GLView::wheelEvent(QWheelEvent *event) void GLView::mousePressEvent(QMouseEvent *event) { mouse_drag_active = true; - last_mouse_x = event->globalX(); - last_mouse_y = event->globalY(); + last_mouse = event->globalPos(); grabMouse(); setFocus(); } @@ -442,17 +439,16 @@ void GLView::normalizeAngle(GLdouble& angle) void GLView::mouseMoveEvent(QMouseEvent *event) { - int this_mouse_x = event->globalX(); - int this_mouse_y = event->globalY(); - int dx = (this_mouse_x-last_mouse_x); - int dy = (this_mouse_y-last_mouse_y); + QPoint this_mouse = event->globalPos(); + double dx = (this_mouse.x()-last_mouse.x()) * 0.7; + double dy = (this_mouse.y()-last_mouse.y()) * 0.7; if (mouse_drag_active) { if ((event->buttons() & Qt::LeftButton) != 0) { - object_rot_x += (this_mouse_y-last_mouse_y) * 0.7; + object_rot_x += dy; if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) - object_rot_y += dx * 0.7; + object_rot_y += dx; else - object_rot_z += dx * 0.7; + object_rot_z += dx; normalizeAngle(object_rot_x); normalizeAngle(object_rot_y); @@ -461,15 +457,14 @@ void GLView::mouseMoveEvent(QMouseEvent *event) if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) != 0) { viewer_distance += (GLdouble)dy; } else { - object_trans_x += (GLint)dx; - object_trans_z -= (GLint)dy; + object_trans_x += dx; + object_trans_z -= dy; } } updateGL(); emit doAnimateUpdate(); } - last_mouse_x = this_mouse_x; - last_mouse_y = this_mouse_y; + last_mouse = this_mouse; } void GLView::mouseReleaseEvent(QMouseEvent*) -- cgit v0.10.1