PDA

View Full Version : QGLWidget renders black



jonasbalmer
20th January 2010, 12:46
I'm fairly new to Qt. I'm trying to make a custom VJ application. For my video-output I want to use OpenGL, which I control by another mainwindow. The problem is, It just renders black. I copypasted what the NeHe tutorial says for drawing a simple white triangle and rectangle, but it's still black. I don't get what's wrong with it. Is there anything I missed?
this is my glscreen.h

#ifndef GLSCREEN_H
#define GLSCREEN_H

#include <QMainWindow>
#include <QGLWidget>

class VJOutput : public QGLWidget{
Q_OBJECT
public:
VJOutput();
protected:
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
public slots:
void Herteken();

};


#endif // GLSCREEN_H

here's the glscreen.cpp code


#include "glscreen.h"
#include "ui_glscreen.h"
#include <iostream>
#include <QTimer>
#include <math.h>

void VJOutput::initializeGL(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
}
VJOutput::VJOutput(){
initializeGL();
QTimer *RedrawTimer = new QTimer(this);
connect(RedrawTimer,SIGNAL(timeout()),this,SLOT(He rteken()));
RedrawTimer->start(20);

}
void VJOutput::resizeGL(int w, int h){

}

void VJOutput::paintGL(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(-1.5f,0.0f,-6.0f);

glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();

glTranslatef(3.0f,0.0f,0.0f);

glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}
void VJOutput::Herteken(){
updateGL();
}

my VJ.pro file has:

# -------------------------------------------------
# Project created by QtCreator 2010-01-15T13:07:06
# -------------------------------------------------
QT += network \
opengl \
sql \
xml \
xmlpatterns \
phonon \
dbus
CONFIG += thread
TARGET = VJ
TEMPLATE = app
SOURCES += main.cpp \
mainwindow.cpp \
lib/Rhythm.cpp \
lib/MidiDevices.cpp \
lib/RtMidi.cpp \
visualLib/glscreen.cpp
HEADERS += mainwindow.h \
lib/Rhythm.h \
lib/MidiDevices.h \
lib/RtMidi.h \
lib/RtError.h \
visualLib/glscreen.h
LIBS += -L/alsa/ \
-lasound \
-lpthread
FORMS += mainwindow.ui


I'm using ubuntu and Qt Creator

wysota
22nd January 2010, 11:15
Are you sure you shouldn't put anything inside resizeGL() or one of other GL methods to setup the projection (or some other, I'm not a GL expert) matrix?

jonasbalmer
24th January 2010, 15:32
you're right.
With the addition of this code it works:

void VJOutput::resizeGL(int w, int h){
if (h==0) // Prevent A Divide By Zero By
{
h=1; // Making Height Equal One
}

glViewport(0, 0, w, h); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

Thanks!