PDA

View Full Version : how to make updateGL() realtime in QT



saman_artorious
18th June 2013, 12:13
I wonder if calling updateGL() in fixed timer intervals may slow down the rendering process. So, I want to try making the render real time. I do not the function to make it execute automatically. Anyone knows that?

wysota
18th June 2013, 13:22
I wonder if calling updateGL() in fixed timer intervals may slow down the rendering process.
No, why would it?


So, I want to try making the render real time.
real time or Real Time?

saman_artorious
18th June 2013, 14:00
i added

this->format().setSwapInterval(1);
in the constructor and

swapbuffers()
at the end of paintGL() function. but it had no effect. how to make it render 60fps?

wysota
18th June 2013, 14:25
i added

this->format().setSwapInterval(1);
in the constructor and swapbuffers() at the end of paintGL() function. but it had no effect.
Why would it have any effect? Did you read the docs for that method?


how to make it render 60fps?
Render less (or more efficiently) or buy faster hardware.

saman_artorious
19th June 2013, 09:32
why would render consume this much CPU ? I thought it was only handled by GPU.
is there any way to optimize it?

wysota
19th June 2013, 09:39
why would render consume this much CPU ? I thought it was only handled by GPU.
Before data can be rendered by the GPU it has to be computed and transfered there. How do you know that rendering consumes much of your CPU power?


is there any way to optimize it?
Probably yes. All depends what your drawing code looks like.

saman_artorious
19th June 2013, 09:50
well, I monitor CPU usage with htop in linux. render rate and CPU usage have a direct relation.
if I render less, it drops. but I don't want to do that. it's a display program, imagine a circle, each degree containing 1000 or 2000 pixels (small rectangles), total 360 * 1000 pixels on the screen.

wysota
19th June 2013, 10:02
well, I monitor CPU usage with htop in linux. render rate and CPU usage have a direct relation.
Such measurements are unreliable.


if I render less, it drops. but I don't want to do that. it's a display program, imagine a circle, each degree containing 1000 or 2000 pixels (small rectangles), total 360 * 1000 pixels on the screen.
Show your rendering code.

saman_artorious
19th June 2013, 10:07
Such measurements are unreliable.
Then how could I monitor a reliable one?



void GlWidget::paintGL()
{
glClearColor(0, 0, 0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

QMatrix4x4 vMatrix;

float sg = pow(2, camera.zoom);

float sx = 256.0 / width();
float sy = 256.0 / height();

vMatrix.setToIdentity();

vMatrix.scale(sx * sg, sy * sg, 1);
vMatrix.translate(-2 * camera.panX + 1, 2 * camera.panY - 1, 0);

shaderProgramPixels.bind();

shaderProgramPixels.setUniformValue("mvpMatrix", vMatrix);

ppi->render(&shaderProgramPixels);


shaderProgramPixels.disableAttributeArray("vertex");
shaderProgramPixels.disableAttributeArray("color" );
shaderProgramPixels.release();

shaderProgram.bind();

shaderProgram.setUniformValue("mvpMatrix", vMatrix);

ppi->RenderRange(&shaderProgram);

RenderLines();

shaderProgram.disableAttributeArray("vertex");
shaderProgram.disableAttributeArray("color" );
shaderProgram.release();
}


Vertices are created at initialization once and then only used. All render functions inside paint do the following:


void PlanPositionIndicator::render(QGLShaderProgram* shaderProgram)
{
// qDebug() << vertices.size();

shaderProgram->setAttributeArray("vertex", vertices.constData());
shaderProgram->enableAttributeArray("vertex");
shaderProgram->setAttributeArray("color", GL_FLOAT,colors,1);
// shaderProgram->setAttributeArray("color", GL_FLOAT,colors.data(),1);
shaderProgram->enableAttributeArray("color");

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glDrawArrays(GL_TRIANGLES, 0, vertices.size());
}

wysota
19th June 2013, 10:19
Then how could I monitor a reliable one?
That's a very good question. Probably using some dedicated instrumentation tool.



Vertices are created at initialization once and then only used. All render functions inside paint do the following:


void PlanPositionIndicator::render(QGLShaderProgram* shaderProgram)
{
// qDebug() << vertices.size();

shaderProgram->setAttributeArray("vertex", vertices.constData());
shaderProgram->enableAttributeArray("vertex");
shaderProgram->setAttributeArray("color", GL_FLOAT,colors,1);
// shaderProgram->setAttributeArray("color", GL_FLOAT,colors.data(),1);
shaderProgram->enableAttributeArray("color");

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glDrawArrays(GL_TRIANGLES, 0, vertices.size());
}


What is "vertices"? Is it a vertex buffer? Did you tell OpenGL that these values are static?

saman_artorious
19th June 2013, 10:48
Yes, it is a vertex buffer.
QVector<QVector3D> vertices;



Did you tell OpenGL that these values are static?

If by this you mean VBO, in which we allocate these vertices and colors inside GPU, No, I haven't yet done it with VBO.
But, I want to try it now, I hope it reduces CPU usage.

by the way, I know how to use VBO with standard C++ opengl and not Qt, do you have any samples in Qt for this?

wysota
19th June 2013, 12:40
Yes, it is a vertex buffer.
Seems more like a QVector.


If by this you mean VBO, in which we allocate these vertices and colors inside GPU, No, I haven't yet done it with VBO.
This implies that you copy the vertices again and again to the GPU upon rendering every frame. If the list of vertices is fixed, that's a pure waste of CPU power.


by the way, I know how to use VBO with standard C++ opengl and not Qt, do you have any samples in Qt for this?
There are examples in the docs. The usage is similar, only that the API is wrapped into Qt calls.

saman_artorious
19th June 2013, 14:12
by the way do you know

how to bind two vertex buffers into different slots (slot0 and slot1),

I want something like this in OpenGL(QT):


QGLBuffer VB0, VB1;

VB0.bind(0);
VB1.bind(1);

wysota
19th June 2013, 14:38
If I understand you correctly then you need to bind the first buffer, then use a call similar to QOpenGLShaderProgram::setAttributeBuffer() and then repeat for the other buffer (bind and assign).

saman_artorious
19th June 2013, 14:49
Does etAttributeBuffer accept the QGLBuffer parameter?!

wysota
19th June 2013, 15:19
No, it uses the currently bound buffer.