PDA

View Full Version : Qt Widget Bug.



litedrive
21st January 2012, 02:26
I've been having a strange bug happen in my program, lately. It's been this way for about an hour now, and I'm not quite sure what to do about it.

First off, I'll post the code which is relevant to what may (or may not) be causing this:


#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "GLWidget.h"

MainWindow::MainWindow( QWidget *parent ) :
QMainWindow( parent ),
ui( new Ui::MainWindow ),
display( new GLWidget )
{
ui->setupUi( this );
//ui->gridLayout->addWidget( display );
//resize( 640, 480 );
installEventFilter( display );
display->setWindowTitle( "OpenGL Cube Rendering Test" );
display->glDraw();
setCentralWidget( display );
}

MainWindow::~MainWindow( void )
{
delete display;
delete ui;
}

The bug is strange, for it resembles the following image:

http://img41.imageshack.us/img41/1156/bullshitd.th.png (http://imageshack.us/photo/my-images/41/bullshitd.png/)

Uploaded with ImageShack.us (http://imageshack.us)

I have no idea what could be causing this; I've tried restarting my pc to see if that would help, but unfortunately it doesn't. Does anyone have any idea as to what the problem might be?

I can post more code too, if necessary.

Edit:

Realized I posted the wrong image. Sorry.

Description

The bug basically, for some reason, captures whatever lies behind the window and projects it onto the window itself. Is this common for any reason?

ChrisW67
21st January 2012, 02:57
I cannot see what you mean from the screen shot. However, calling glDraw() inside the parent window constructor, before the parent window is shown, may explain odd behaviour.

litedrive
21st January 2012, 03:18
That makes sense. Should I just call it in main after w.show()?

Added after 8 minutes:

Update: just tried that. I'm thinking that I may have mis configured my openGL, so I post the functions which use it incase anyone has any idea:


void GLWidget::initializeGL( void )
{
mQtGreen = QColor::fromCmyk( 0.40, 0.0, 1.0, 0.0 );
mQtPurple = QColor::fromCmyk( 0.39, 0.39, 0.0, 0.0 );

qglClearColor( mQtPurple );
glClearDepth( 1.0f );

mCube->SetColor( mQtGreen.dark() );

glEnable( GL_VERTEX_PROGRAM_ARB );
glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
glShadeModel( GL_SMOOTH );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_MULTISAMPLE );
static GLfloat lightPosition[ 4 ] = { 0.5, 5.0, 7.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, lightPosition );

qDebug() << "GL Initialized" << '\n';
}


void GLWidget::paintGL( void )
{
const GLfloat ANGLE_DIVISOR = 16.0;

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

glMatrixMode( GL_MODELVIEW );
glTranslatef( 0.0, 0.0, -10.0 );

glRotatef( mXRot / ANGLE_DIVISOR, 1.0, 0.0, 0.0 );
glRotatef( mYRot / ANGLE_DIVISOR, 0.0, 1.0, 0.0 );
glRotatef( mZRot / ANGLE_DIVISOR, 0.0, 0.0, 1.0 );

glMatrixMode( GL_MODELVIEW );

mCube->Draw();

qDebug() << "GL Painted" << '\n';
}


void GLWidget::resizeGL( int width, int height )
{
const GLdouble NEAR = 1;
const GLdouble FAR = 200;
const GLdouble FOVY = 45;

int side = qMin( width, height );

glMatrixMode( GL_PROJECTION );

gluPerspective(
FOVY,
( float ) width / ( float ) height,
NEAR,
FAR
);

glMatrixMode( GL_MODELVIEW );

glViewport( ( width - side ) / VIEWPORT_DIVISOR, ( height - side ) / VIEWPORT_DIVISOR, side, side );

glMatrixMode( GL_PROJECTION );

glLoadIdentity();

#ifdef QT_OPENGL_ES_1
glOrthof( -0.5, +0.5, -0.5, +0.5, 4.0, 15.0 );
#else
glOrtho( -0.5, +0.5, -0.5, +0.5, 4.0, 15.0 );
#endif*/

glMatrixMode( GL_MODELVIEW );

qDebug() << "GL Resized" << '\n';
}


void Patch::Draw( void ) const
{
const bool PUSH_AND_POP_MATRIX = CheckMinMatrixStack( GL_MODELVIEW );

if ( PUSH_AND_POP_MATRIX )
glPushMatrix();

float *faceColor = new float[ 4 ];

faceColor[ 0 ] = 0.0f;
faceColor[ 1 ] = 0.0f;
faceColor[ 2 ] = 0.0f;
faceColor[ 3 ] = 1.0f;

//qMultMatrix( mMatrix44 );
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, static_cast< GLfloat* >( faceColor ) );

delete[] faceColor;

//GLushort *indicies = new GLushort;

//*indicies = ( *mGeometry->mFaces )[ 0 ];

GLenum element;

switch ( mFocus )
{
case GF_Triangles:
element = GL_TRIANGLES;
break;
case GF_Quads:
element = GL_QUADS;
break;
default:
element = GL_LINES;
break;
}

//glDrawElements( element, mCount, GL_UNSIGNED_SHORT, indicies + mStart );

glBegin( element );

qreal x = 0, y = 0, z = 0;

for ( int i = 0; i < mCount; ++i )
{
x = ( *mGeometry->mVerticies )[ i ].x();
y = ( *mGeometry->mVerticies )[ i ].y();
z = ( *mGeometry->mVerticies )[ i ].z();

glVertex3f( x, y, z );
}

glEnd();

if ( PUSH_AND_POP_MATRIX )
glPopMatrix();
}



void Cube::BuildGeometry( int divisions, qreal scale )
{
if ( !mGeometry->mFinalized )
mGeometry->Finalize();
mGeometry->LoadArrays();

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );

for ( size_t i = 0; i < mParts->size(); i++ )
( *mParts )[ i ]->Draw();
}


void Cube::RenderCube( const unsigned short type, const QPoint3 &position, const int size )
{
( *mCubeage )[ "cubes" + '\0' ] += 1;

SetColor( type );

int x1 = 0, x2 = 0;
int y1 = 0, y2 = 0;
int z1 = 0, z2 = 0;

x1 = position.x() - size;
x2 = position.x() + size;

y1 = position.y() - size;
y2 = position.y() + size;

z1 = ( position.z() - size ) + 1;
z2 = position.z() + size + 1;

qreal camZ = mCamera->z();
qreal camY = mCamera->y();
qreal camX = mCamera->x();

glMatrixMode( GL_PROJECTION );

glTranslatef( 0.0, 0.0, -10.0 );

glOrtho( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 );

glMatrixMode( GL_MODELVIEW );

/*
* if axis > mCamera.axis; then normal3f -1.0 on the axis.
* axis point is axis1
* else if axis < mCamera.axis; then nomral3f 1.0 on the axis.
* axis point is axis2
*/

/* Pattern: one axis is written twice, with the other variant of the same axis written twice as well, then the
* order at which those axes are written is flipped, yet still following the same pattern.
* Pattern2: one type of axis is still written twice, and the other group of the same axis written twice as well,
* the only difference being that the first group is written once, with the next group written twice, and then that
* first group written once again. The third axis type is written using one type, depending on the camera comparisons.
*/

const bool PUSH_AND_POP_MATRIX = ( CheckMinMatrixStack( GL_MODELVIEW ) );

if ( PUSH_AND_POP_MATRIX )
glPushMatrix;

glBegin( GL_QUADS );

if ( z1 > camZ )
{
glNormal3f( 0.0, 0.0, -1.0 );

glVertex3i( x1, y1, z1 ); //1
glVertex3i( x1, y2, z1 ); //1
glVertex3i( x2, y2, z1 ); //1
glVertex3i( x2, y1, z1 ); //1
}

if ( z2 < camZ )
{
glNormal3f( 0.0, 0.0, 1.0 );

glVertex3i( x2, y1, z2 ); //2
glVertex3i( x2, y2, z2 ); //2
glVertex3i( x1, y2, z2 ); //2
glVertex3i( x1, y1, z2 ); //2
}

if ( y1 > camY )
{
glNormal3f( 0.0, -1.0, 0.0 );

glVertex3i( x2, y1, z1 );
glVertex3i( x2, y1, z2 );
glVertex3i( x1, y1, z2 );
glVertex3i( x1, y1, z1 );
}

if ( y2 < camY )
{
glNormal3f( 0.0, 1.0, 0.0 );

glVertex3i( x1, y2, z1 );
glVertex3i( x1, y2, z2 );
glVertex3i( x2, y2, z2 );
glVertex3i( x2, y2, z1 );
}

if ( x1 > camX )
{
glNormal3f( -1.0, 0.0, 0.0 );

glVertex3i( x1, y1, z1 );
glVertex3i( x1, y1, z2 );
glVertex3i( x1, y2, z2 );
glVertex3i( x1, y2, z1 );
}

if ( x2 < camX )
{
glNormal3f( 1.0, 0.0, 0.0 );

glVertex3i( x2, y2, z1 );
glVertex3i( x2, y2, z2 );
glVertex3i( x2, y1, z2 );
glVertex3i( x2, y1, z1 );
}

glEnd();

if ( PUSH_AND_POP_MATRIX )
glPopMatrix();

}


void Cube::RenderVB( unsigned short type, const QPoint3F &position, int size )
{
static const GLuint NUM_COORDS_PER_VERTEX = 3;
static const GLuint NUM_ARR_TO_DRAW = 24;
static const GLuint FIRST_INDEX_TO_DRAW = 0;

( *mCubeage )[ "cubes" ] += 1;

SetColor( type );

glMatrixMode( GL_PROJECTION );

const bool pushAndPop = CheckMinMatrixStack( GL_PROJECTION );

if ( pushAndPop )
glPushMatrix();

glTranslatef( position.x(), position.y(), position.z() );
glScalef( size, size, size );

glVertexPointer( NUM_COORDS_PER_VERTEX, GL_FLOAT, sizeof( QPoint3F ), ( void* ) ( sizeof( QPoint3F ) * 24 ) );
glNormalPointer( GL_FLOAT, sizeof( QPoint3F ), 0 );

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );

glDrawArrays( GL_QUADS, FIRST_INDEX_TO_DRAW, NUM_ARR_TO_DRAW);

glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );

if ( pushAndPop )
glPopMatrix();

}

And...that's that. Please, if anyone has any idea what the reason for this is, I'd appreciate an answer. Thanks.

wysota
21st January 2012, 08:46
Should I just call it in main after w.show()?
You shouldn't need to call it at all. How are you calling it from an external widget, by the way, as it is a protected method?