I am working on converting a pure GLUT/OpenGL/OpenCL code into Qt/OpenGL/OpenCL. For this, I use QGLWidget subclass named "GLWidget" for displaying the animation. My issue is that I would like to keep the GLUT displayfunc working of the first code : in main GLUT loop, I used `glutDisplayFunc(displayfunc);`
So, in the Qt/OpenGL, I tried to do this :
void GLWidget::initializeGL()
{
LoadGLTextures(); // load the textures.
glClearColor(0.0 ,0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
m_program = _compileProgram(vertexShader);
glClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
// memsize of GPU data
unsigned int memSize = sizeof(cl_double4) * 4 * Galaxy->getNumParticles();
createVBO(memSize);
connect(m_timer, SIGNAL(timeout()), this, SLOT(displayfunc()));
m_timer->start(30);
}
void GLWidget::initializeGL()
{
LoadGLTextures(); // load the textures.
glClearColor(0.0 ,0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
m_program = _compileProgram(vertexShader);
glClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
// memsize of GPU data
unsigned int memSize = sizeof(cl_double4) * 4 * Galaxy->getNumParticles();
createVBO(memSize);
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(displayfunc()));
m_timer->start(30);
}
To copy to clipboard, switch view to plain text mode
with displayfunc :
void GLWidget::displayfunc()
{
if (Galaxy->isFirstLaunch)
{
gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(0.03f, 0.03f, 0.03f);
drawPoints();
//Calling kernel for calculatig subsequent positions
Galaxy->runCLKernels();
Galaxy->isFirstLaunch = false;
glFlush();
swapBuffers();
return;
}
else {
clWaitForEvents(1, &Galaxy->glEvent);
drawPoints();
//Calling kernel for calculatig subsequent positions
Galaxy->runCLKernels();
glFlush();
swapBuffers();
}
update();
}
void GLWidget::displayfunc()
{
if (Galaxy->isFirstLaunch)
{
gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(0.03f, 0.03f, 0.03f);
drawPoints();
//Calling kernel for calculatig subsequent positions
Galaxy->runCLKernels();
Galaxy->isFirstLaunch = false;
glFlush();
swapBuffers();
return;
}
else {
clWaitForEvents(1, &Galaxy->glEvent);
drawPoints();
//Calling kernel for calculatig subsequent positions
Galaxy->runCLKernels();
glFlush();
swapBuffers();
}
update();
}
To copy to clipboard, switch view to plain text mode
and drawPoints :
void GLWidget::drawPoints()
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
glUseProgram(m_program);
glUniform1f( glGetUniformLocation(m_program, "pointRadius"), m_particleRadius );
glUniform1f( glGetUniformLocation(m_program, "pointScale"),m_pointScale);
GLuint vbo_disk;
glBindBuffer(GL_ARRAY_BUFFER, vbo_disk);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), Galaxy->pos);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0f, 1.0f, 1.0f, 0.1f);
glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_disk());
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
GLuint vbo_halo;
glBindBuffer(GL_ARRAY_BUFFER, vbo_halo);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), &Galaxy->pos[Galaxy->getNumParticles_disk()]);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(0.0f, 0.0f, 1.0f, 0.1f);
glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_halo());
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_BLEND);
glDisable(GL_POINT_SPRITE);
}
void GLWidget::drawPoints()
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
glUseProgram(m_program);
glUniform1f( glGetUniformLocation(m_program, "pointRadius"), m_particleRadius );
glUniform1f( glGetUniformLocation(m_program, "pointScale"),m_pointScale);
GLuint vbo_disk;
glBindBuffer(GL_ARRAY_BUFFER, vbo_disk);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), Galaxy->pos);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0f, 1.0f, 1.0f, 0.1f);
glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_disk());
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
GLuint vbo_halo;
glBindBuffer(GL_ARRAY_BUFFER, vbo_halo);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), &Galaxy->pos[Galaxy->getNumParticles_disk()]);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(0.0f, 0.0f, 1.0f, 0.1f);
glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_halo());
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_BLEND);
glDisable(GL_POINT_SPRITE);
}
To copy to clipboard, switch view to plain text mode
Can I do without paintGL() function ?
I tried to add makeCurrent() into all the functions containing OpenGL functions but nothing displays in GLWidget, just black screen because of `glClearColor(0.0 ,0.0, 0.0, 0.0)`
Could you see what's wrong ?
Bookmarks