Qt
Graphical User Interface (GUI) toolkit
Qt
interaction follows the basic
Qt OpenGL
tutorials (e.g., the
Hello GL
example).
OpenGL
is the graphics
library that we use to display graphics primitives, in this case
GL_POINTS
which are just 3D points in space
(the 3D world).
Qt
window that "listens"
to user events can also process OpenGL
commands.
OpenGL
user events:
initializeGL()
glClearColor(0.0, 0.0, 0.0, 0.0);
resizeGL(int w, int h)
// set up the OpenGL view port
glViewport(0,0,w,h);
// set up the view transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,5.12/3.84,0.1,100.0);
// set up the model transformation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Note that the parameters provided to
gluPerspective
match those in our
usual model.txt
scene file.
paintGL()
updateGL()
;
DO NOT CALL paintGL()
DIRECTLY!
// draw to the back buffer
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(0.0,0.0,0.0,0.0);
// model/view transformation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.56,2.0,5.0, // camera position
2.56,2.0,0.0, // view direction
0.0,1.0,0.0); // view up vector (tilt)
// render photons
glColor4f(0.8,0.8,0.8,0.8);
for(int i=0; i<(int)photons.size(); i++) {
glPointSize(1.0);
glBegin(GL_POINTS);
glVertex3f((*photons[i])[0],
(*photons[i])[1],
(*photons[i])[2]);
glEnd();
}
Notice that the parameters provided to
gluLookAt
match those in our
usual model.txt
scene file.
Notice also how the photon coordinates are
accessed—remember that photons
is a std::vector<photon_t* >
,
an STL vector
holding pointers to
photon_t
objects.
Qt
user events (public slots:
):
mouseMoveEvent(QMouseEvent* e)
e->accept();
updateGL();
update()
updateGL();
imgSaveAs()
OpenGL
back buffer, then use
one of Qt's very convenient features, the
file browser to select the file name where we
want to write the screen captured image:
glReadBuffer(GL_BACK);
QImage img = QGLWidget::grabFrameBuffer(true);
QString qfilename = QFileDialog::getSaveFileName(this,
tr("Save Image As..."),
"./",
tr("Images (*.png)"));
// get QImage to write itself
if(!qfilename.isEmpty()) img.save(qfilename,"PNG");
fileOpen()
imgSaveAs()
except that we read in a file full of photons—this
is why you need to have operator>>
defined for the photon_t
class:
QString qfilename = QFileDialog::getOpenFileName(this,
tr("Open File"),
"./",
tr("Point Files (*.pts)"));
std::ifstream photons_ifs; // input file
photon_t *ph; // ptr to photon
if(!qfilename.isEmpty()) {
// open file if file dialog returned something
photons_ifs.open(qfilename.toStdString().c_str());
// read one photon per line
while(!photons_ifs.eof()) {
ph = new photon_t(); // allocate new photon
photons_ifs >> ph; // read it in
photons.push_back(ph); // add to photon array
}
photons_ifs.close();
}
updateGL();
Notice the call to updateGL()
to refresh
the drawable.
Qt
photon viewer.
Qt
app, your best bet is to follow the
Hello GL
tutorial and create the following files:
glwinobj.h
, glwinobj.cpp
:
these hold the interface and implementation of the
main window (I called mine GLObjectWindow
which is derived from QWidget
) which
contains the menubar as well as the GL
window.
gltexobj.h
, gltexobj.cpp
:
these hold the interface and implementation of just the
GL
window (I called mine GLTexobj
following the Qt
tutorial, which is derived
from QGLWidget
). This object handles all the
user events as well, the GLObjectWindow
connects connects the user event signals to
GLTexobj
's slots.
Qt
is set up properly, the following
main()
is one way to start the program
(Qt
documentation suggests an alternate approach
using something like the QMainWindow
but the
following will work just as well, but feel free to use either):
int main(int argc, char **argv)
{
QApplication::setColorSpec(QApplication::CustomColor);
QApplication app(argc,argv);
if(!QGLFormat::hasOpenGL()) {
qWarning( "This system has no OpenGL support. Exiting." );
return -1;
}
// Create OpenGL format
QGLFormat f;
f.setDoubleBuffer(TRUE); f.setRgba(TRUE); f.setDepth(TRUE);
QGLFormat::setDefaultFormat(f);
GLObjectWindow* w = new GLObjectWindow;
// set size...
w->resize( 640, 480 );
w->show();
// ... or go full screen
//w->showFullScreen();
int result = app.exec();
delete w;
return result;
}
tar.gz
archive of your lab##/ directory, including:
README
file containing
Makefile
.h
headers and .cpp
source)
make clean
before tar
)
sendlab
notes