From 5067013eb8f2e486c4cd1dc4a5283b2ae6eb32e0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Beno=C3=AEt=20Jacob?= Date: Wed, 28 Jun 2006 16:47:22 +0000 Subject: [PATCH] - change so that it's not required to explicitly call VertexArray::select() explicitly anymore before drawing - basic GL text rendering stuff now (sort of) working. It's slow because it's texture-based, but that'll change. M kalzium/src/kalziumglhelperclasses.h M kalzium/src/kalziumglwidget.cpp M kalzium/src/kalziumglhelperclasses.cpp svn path=/trunk/KDE/kdeedu/kalzium/src/kalziumglhelperclasses.cpp; revision=555886 --- kalzium/kalziumglhelperclasses.cpp | 99 ++++++++++++++++++++++++--- kalzium/kalziumglhelperclasses.h | 104 +++-------------------------- kalzium/kalziumglwidget.cpp | 13 ++-- 3 files changed, 108 insertions(+), 108 deletions(-) diff --git a/kalzium/kalziumglhelperclasses.cpp b/kalzium/kalziumglhelperclasses.cpp index 49f9f7d..5756a70 100644 --- a/kalzium/kalziumglhelperclasses.cpp +++ b/kalzium/kalziumglhelperclasses.cpp @@ -72,14 +72,6 @@ VertexArray::~VertexArray() if( m_normalBuffer ) delete [] m_normalBuffer; } -void VertexArray::select() -{ - glEnableClientState( GL_VERTEX_ARRAY ); - glEnableClientState( GL_NORMAL_ARRAY ); - glVertexPointer( 3, GL_FLOAT, 0, m_vertexBuffer ); - glNormalPointer( GL_FLOAT, 0, m_normalBuffer ); -} - bool VertexArray::allocateBuffers() { if( m_vertexCount > 65536 ) return false; @@ -335,3 +327,94 @@ void Cylinder::initialize() m_isInitialized = true; } + +TextPainter::TextPainter() +{ + m_width = 0; + m_height = 0; + m_image = 0; + m_painter = 0; + m_fontMetrics = 0; +} + +TextPainter::~TextPainter() +{ + if( m_image ) delete m_image; + if( m_painter ) delete m_painter; + if( m_fontMetrics ) delete m_fontMetrics; +} + +bool TextPainter::print( QGLWidget *glwidget, int x, int y, const QString &string) +{ + glDisable( GL_LIGHTING ); + glEnable(GL_TEXTURE_2D); + glEnable( GL_BLEND ); + + if( ! m_painter ) + { + m_painter = new QPainter(); + if( ! m_painter ) return false; + } + + if( ! m_fontMetrics ) + { + + m_fontMetrics = new QFontMetrics(glwidget->font()); + if( ! m_fontMetrics ) return false; + } + + int new_width = m_fontMetrics->width( string ); + int new_height = m_fontMetrics->height(); + + if(new_width == 0 || new_height == 0) + { + return false; + } + + if( new_width > m_width || new_height > m_height ) + { + if( m_image ) delete m_image; + m_width = ( new_width > m_width ) ? new_width : m_width; + m_height = ( new_height > m_height ) ? new_height : m_height; + m_image = new QImage( m_width, m_height, QImage::Format_ARGB32 ); + } + + m_painter->begin( m_image ); + m_painter->setFont(glwidget->font()); + m_painter->setRenderHint(QPainter::TextAntialiasing); + //painter.setBackground(Qt::black); + m_painter->setBrush(Qt::white); + m_painter->eraseRect( 0, 0, m_width, m_height ); + + //painter.drawText ( 0, 0, s ); + m_painter->drawText ( 0, m_height, string ); + m_painter->end(); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glwidget->bindTexture( *m_image ); + glMatrixMode( GL_PROJECTION ); + glPushMatrix(); + glLoadIdentity(); + glOrtho( 0, glwidget->width(), 0, glwidget->height(), -1, 1 ); + glMatrixMode( GL_MODELVIEW ); + glPushMatrix(); + glLoadIdentity(); + glBegin(GL_QUADS); + glTexCoord2f( 0, 0); + glVertex2f( x , y ); + glTexCoord2f( 1, 0); + glVertex2f( x+m_width , y ); + glTexCoord2f( 1, 1); + glVertex2f( x+m_width , y+m_height ); + glTexCoord2f( 0, 1); + glVertex2f( x , y+m_height ); + glEnd(); + glDisable( GL_TEXTURE_2D); + glDisable( GL_BLEND ); + glPopMatrix(); + glMatrixMode( GL_PROJECTION ); + glPopMatrix(); + glMatrixMode( GL_MODELVIEW ); + return true; +} diff --git a/kalzium/kalziumglhelperclasses.h b/kalzium/kalziumglhelperclasses.h index b285f41..8d25721 100644 --- a/kalzium/kalziumglhelperclasses.h +++ b/kalzium/kalziumglhelperclasses.h @@ -224,9 +224,14 @@ class VertexArray public: VertexArray(); virtual ~VertexArray(); - virtual void select(); + virtual inline void select() + { + glVertexPointer( 3, GL_FLOAT, 0, m_vertexBuffer ); + glNormalPointer( GL_FLOAT, 0, m_normalBuffer ); + } virtual inline void draw() { + select(); glDrawElements( m_mode, m_indexCount, GL_UNSIGNED_SHORT, m_indexBuffer ); } @@ -276,6 +281,7 @@ class Cylinder : public VertexArray virtual void setup( int detail, GLfloat radius ); virtual inline void draw() { + select(); glDrawArrays( m_mode, 0, m_vertexCount ); } }; @@ -289,99 +295,9 @@ class TextPainter QFontMetrics *m_fontMetrics; public: - TextPainter() - { - m_width = 0; - m_height = 0; - m_image = 0; - m_painter = 0; - m_fontMetrics = 0; - } - ~TextPainter() - { - if( m_image ) delete m_image; - if( m_painter ) delete m_painter; - if( m_fontMetrics ) delete m_fontMetrics; - } - bool print( QGLWidget *glwidget, int x, int y, const QString &string) - { - glDisable( GL_LIGHTING ); - glEnable(GL_TEXTURE_2D); - - /*if( ! m_font ) - { - m_font = new QFont(); - if( ! m_font ) return false; - }*/ - - const QFont &m_font = (glwidget->font()); - - if( ! m_painter ) - { - m_painter = new QPainter(); - if( ! m_painter ) return false; - } - - m_painter->setFont(m_font); - - if( ! m_fontMetrics ) - { - - m_fontMetrics = new QFontMetrics(m_font); - if( ! m_fontMetrics ) return false; - } - - int new_width = m_fontMetrics->width( string ); - int new_height = m_fontMetrics->height(); - - if(new_width == 0 || new_height == 0) - { - return false; - } - - if( new_width > m_width || new_height > m_height ) - { - if( m_image ) delete m_image; - m_width = ( new_width > m_width ) ? new_width : m_width; - m_height = ( new_height > m_height ) ? new_height : m_height; - m_image = new QImage( m_width, m_height, QImage::Format_ARGB32 ); - } - - m_painter->begin( m_image ); - m_painter->setRenderHint(QPainter::TextAntialiasing); - //painter.setBackground(Qt::black); - m_painter->setBrush(Qt::white); - m_painter->eraseRect( 0, 0, m_width, m_height ); - - //painter.drawText ( 0, 0, s ); - m_painter->drawText ( 0, m_height, string ); - m_painter->end(); - - glwidget->bindTexture( *m_image ); - glMatrixMode( GL_PROJECTION ); - glPushMatrix(); - glLoadIdentity(); - glOrtho( 0, glwidget->width(), 0, glwidget->height(), -1, 1 ); - glMatrixMode( GL_MODELVIEW ); - glPushMatrix(); - glLoadIdentity(); - glBegin(GL_QUADS); - glTexCoord2f( 0, 0); - glVertex2f( x , y ); - glTexCoord2f( 1, 0); - glVertex2f( x+m_width , y ); - glTexCoord2f( 1, 1); - glVertex2f( x+m_width , y+m_height ); - glTexCoord2f( 0, 1); - glVertex2f( x , y+m_height ); - glEnd(); - glDisable( GL_TEXTURE_2D); - glPopMatrix(); - glMatrixMode( GL_PROJECTION ); - glPopMatrix(); - glMatrixMode( GL_MODELVIEW ); - return true; - } + TextPainter::TextPainter(); + TextPainter::~TextPainter(); + bool TextPainter::print( QGLWidget *glwidget, int x, int y, const QString &string); }; } // namespace KalziumGL diff --git a/kalzium/kalziumglwidget.cpp b/kalzium/kalziumglwidget.cpp index 5e20605..1ca9145 100644 --- a/kalzium/kalziumglwidget.cpp +++ b/kalzium/kalziumglwidget.cpp @@ -40,6 +40,8 @@ KalziumGLWidget::KalziumGLWidget( QWidget * parent ) m_detail = 0; m_useFog = false; m_textPainter = 0; + m_inZoom = false; + m_inMeasure = false; ChooseStylePreset( PRESET_SPHERES_AND_BICOLOR_BONDS ); @@ -94,6 +96,9 @@ void KalziumGLWidget::initializeGL() glLightModeli( GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SEPARATE_SPECULAR_COLOR_EXT ); + glEnableClientState( GL_VERTEX_ARRAY ); + glEnableClientState( GL_NORMAL_ARRAY ); + setupObjects(); m_textPainter = new TextPainter; @@ -134,7 +139,6 @@ void KalziumGLWidget::paintGL() // prepare for rendering the spheres if( m_atomStyle == ATOM_SPHERE ) { - m_sphere.select(); glEnable( GL_LIGHTING ); } else glDisable( GL_LIGHTING ); @@ -169,7 +173,6 @@ void KalziumGLWidget::paintGL() case BOND_CYLINDER_GRAY: case BOND_CYLINDER_BICOLOR: - m_cylinder.select(); glEnable( GL_LIGHTING ); break; case BOND_DISABLED: break; @@ -232,8 +235,6 @@ void KalziumGLWidget::paintGL() // now, paint a semitransparent sphere around the selected atoms if( m_selectedAtoms.count() > 0 )//there are items selected { - m_sphere.select(); - Color c( 0.4, 0.4, 1.0, 0.7 ); GLFLOAT radius = m_molMinBondLength * 0.35; @@ -283,13 +284,13 @@ void KalziumGLWidget::paintGL() s = QString::number( 1000 * frames / double( new_time - old_time ), 'f', 1 ); - s += " frames per second"; + s += " frames per second" + QString( QChar( 962 ) ); frames = 0; old_time = new_time; } glColor3f( 1.0, 1.0, 0.0 ); - //m_textPainter->print( this, 20, 20, s ); + m_textPainter->print( this, 20, 20, s ); update(); #endif -- 2.47.3