From bbe4b9515a9b5bebd08bf33eb892d5b22b85aaa4 Mon Sep 17 00:00:00 2001 From: Carsten Niehaus Date: Sun, 11 Jun 2006 12:08:01 +0000 Subject: [PATCH 1/1] As I also want to make the Crystalview OpenGL Benoit proposed to create a KalziumGL-widget, as both will use them svn path=/trunk/KDE/kdeedu/kalzium/src/kalziumglwidget.h; revision=550283 --- kalzium/kalziumglwidget.cpp | 168 ++++++++++++++++++++++++++++++++++++ kalzium/kalziumglwidget.h | 90 +++++++++++++++++++ 2 files changed, 258 insertions(+) create mode 100644 kalzium/kalziumglwidget.cpp create mode 100644 kalzium/kalziumglwidget.h diff --git a/kalzium/kalziumglwidget.cpp b/kalzium/kalziumglwidget.cpp new file mode 100644 index 0000000..129f065 --- /dev/null +++ b/kalzium/kalziumglwidget.cpp @@ -0,0 +1,168 @@ +/*************************************************************************** + copyright : (C) 2006 by Carsten Niehaus + email : cniehaus@kde.org + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +#include "kalziumglwidget.h" + +#include + +#include +#include + +KalziumGLWidget::KalziumGLWidget( QWidget * parent ) + : QGLWidget( parent ) +{ + sphereDisplayList = 0; + isDragging = false; + + setMinimumSize( 100,100 ); +} + +KalziumGLWidget::~KalziumGLWidget() +{ + if( sphereDisplayList ) + glDeleteLists( sphereDisplayList, 1 ); +} + +void KalziumGLWidget::initializeGL() +{ + glClearColor( 0.0, 0.0, 0.0, 1.0); + glShadeModel( GL_SMOOTH ); + glEnable( GL_DEPTH_TEST ); + glEnable( GL_CULL_FACE ); + glEnable( GL_NORMALIZE ); + glDisable( GL_BLEND ); + + glMatrixMode( GL_MODELVIEW ); + glPushMatrix(); + glLoadIdentity(); + glGetDoublev( GL_MODELVIEW_MATRIX, RotationMatrix ); + glPopMatrix(); + + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + + GLfloat ambientLight[] = { 0.4, 0.4, 0.4, 1.0 }; + GLfloat diffuseLight[] = { 0.8, 0.8, 0.8, 1.0 }; + GLfloat specularLight[] = { 1.0, 1.0, 1.0, 1.0 }; + GLfloat position[] = { 0.6, 0.5, 1.0, 0.0 }; + + glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); + glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); + glLightfv(GL_LIGHT0, GL_POSITION, position); + + glEnable(GL_FOG); + GLfloat fogColor[] = { 0.0, 0.0, 0.0, 1.0 }; + glFogfv(GL_FOG_COLOR, fogColor); + glFogi(GL_FOG_MODE, GL_LINEAR); + glFogf(GL_FOG_DENSITY, 0.4); + glFogf(GL_FOG_START, 11.0); + glFogf(GL_FOG_END, 17.0); + + glEnable (GL_COLOR_SUM_EXT); + glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SEPARATE_SPECULAR_COLOR_EXT); +} + +void KalziumGLWidget::paintGL() +{ + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + glLoadIdentity(); + glTranslated( 0.0, 0.0, -12.0); + glMultMatrixd( RotationMatrix ); + drawSphere(1.0, -1.0, -2.0, 1.5, 1.0, 0.0, 0.0); + drawSphere(-2.0, 1.0, 1.0, 2.5, 0.0, 1.0, 0.0); + drawSphere(2.0, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0); +} + +void KalziumGLWidget::resizeGL( int width, int height ) +{ + glViewport( 0, 0, width, height ); + glMatrixMode( GL_PROJECTION ); + glLoadIdentity(); + gluPerspective( 60.0, float(width) / height, 0.5, 50.0 ); + glMatrixMode( GL_MODELVIEW ); +} + +void KalziumGLWidget::mousePressEvent( QMouseEvent * event ) +{ + if( event->buttons () & Qt::LeftButton ) + { + isDragging = true; + lastDraggingPosition = event->pos (); + } +} + +void KalziumGLWidget::mouseReleaseEvent( QMouseEvent * event ) +{ + if( !( event->buttons () & Qt::LeftButton ) ) + { + isDragging = false; + } +} + +void KalziumGLWidget::mouseMoveEvent( QMouseEvent * event ) +{ + if( isDragging ) + { + deltaDragging = event->pos() - lastDraggingPosition; + lastDraggingPosition = event->pos(); + + glPushMatrix(); + glLoadIdentity(); + glRotated( deltaDragging.x(), 0.0, 1.0, 0.0 ); + glRotated( deltaDragging.y(), 1.0, 0.0, 0.0 ); + glMultMatrixd( RotationMatrix ); + glGetDoublev( GL_MODELVIEW_MATRIX, RotationMatrix ); + glPopMatrix(); + updateGL(); + } +} + +void KalziumGLWidget::drawGenericSphere() +{ + if( 0 == sphereDisplayList ) + { + sphereDisplayList = glGenLists( 1 ); + if( 0 == sphereDisplayList ) return; + GLUquadricObj *q = gluNewQuadric(); + if( 0 == q) return; + glNewList( sphereDisplayList, GL_COMPILE ); + gluSphere( q, 1.0, SPHERE_TESSELATE_SLICES, SPHERE_TESSELATE_STACKS ); + glEndList(); + gluDeleteQuadric( q ); + } + else + { + glCallList( sphereDisplayList ); + } +} + +void KalziumGLWidget::drawSphere( GLdouble x, GLdouble y, GLdouble z, GLdouble radius, + GLfloat red, GLfloat green, GLfloat blue ) +{ + GLfloat ambientColor [] = { red / 2, green / 2, blue / 2, 1.0 }; + GLfloat diffuseColor [] = { red, green, blue, 1.0 }; + GLfloat specularColor [] = { (2.0 + red) / 3, (2.0 + green) / 3, (2.0 + blue) / 3, 1.0 }; + glMaterialfv(GL_FRONT, GL_AMBIENT, ambientColor); + glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseColor); + glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor); + glMaterialf(GL_FRONT, GL_SHININESS, 50.0); + + glPushMatrix(); + glTranslated( x, y, z ); + glScaled( radius, radius, radius); + drawGenericSphere(); + glPopMatrix(); +} + +#include "kalziumglwidget.moc" diff --git a/kalzium/kalziumglwidget.h b/kalzium/kalziumglwidget.h new file mode 100644 index 0000000..86bfadc --- /dev/null +++ b/kalzium/kalziumglwidget.h @@ -0,0 +1,90 @@ +#ifndef KALZIUMGLWIDGET_H +#define KALZIUMGLWIDGET_H +/*************************************************************************** + copyright : (C) 2006 by Carsten Niehaus + email : cniehaus@kde.org + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include + +#define SPHERE_TESSELATE_SLICES 30 +#define SPHERE_TESSELATE_STACKS 30 + +/** + * This class displays the 3D-view of a molecule + * + * @autor Benoit Jacobs + */ +class KalziumGLWidget : public QGLWidget +{ + Q_OBJECT + + protected: + GLuint sphereDisplayList; + +/* struct VertexArrayEntry { + float nx, ny, nz, vx, vy, vz; + }; +*/ + + bool isDragging; + QPoint lastDraggingPosition; + QPoint deltaDragging; + GLdouble RotationMatrix[16]; + + public: + /** + * Constructor + */ + KalziumGLWidget( QWidget *parent = 0 ); + + /** + * Destructor + */ + virtual ~KalziumGLWidget(); + + protected: + /** + * This method initilized OpenGL + */ + virtual void initializeGL(); + virtual void paintGL(); + virtual void resizeGL( int width, int height ); + virtual void mousePressEvent( QMouseEvent * event ); + virtual void mouseReleaseEvent( QMouseEvent * event ); + virtual void mouseMoveEvent( QMouseEvent * event ); + + /** + * This method... + */ + virtual void drawGenericSphere(); + + /** + * This method... + * @param x + * @param y + * @param z + * @param radius + * @param red + * @param green + * @param blue + */ + virtual void drawSphere( + GLdouble x, + GLdouble y, + GLdouble z, + GLdouble radius, + GLfloat red, + GLfloat green, + GLfloat blue ); +}; +#endif // KALZIUMGLWIDGET_H -- 2.47.3