QWindow - openGL implementation, painter not paiting?
-
Hey
I'm trying to build up my own "wrapper" for openGL. I keep running in to issues with QOpenGLWidget so I decided to try my luck with QWindow... Anyway, when I try to run this code, the painter does not paint the text :
#include <QtGui/QPainter> #include "testRender.h" #include "QOpenGLPaintDevice" #include "QDebug" testRender::testRender(QWindow *parent) : QWindow(parent), m_animating(false), m_context(0), m_device(0), mInitialize(true) { setSurfaceType(QWindow::OpenGLSurface); qDebug() << "stuff made?"; mFormat.setMajorVersion(4); mFormat.setMinorVersion(5); mFormat.setDepthBufferSize(24); mFormat.setStencilBufferSize(8); setFormat(mFormat); } testRender::~testRender() { } void testRender::renderLater() { requestUpdate(); } void testRender::renderNow() { if (!isExposed()) return; if (!m_context) { m_context = new QOpenGLContext(this); m_context->setFormat(mFormat); m_context->create(); } m_context->makeCurrent(this); if (mInitialize) { initializeOpenGLFunctions(); m_device = new QOpenGLPaintDevice; mInitialize = false; mF = m_context->functions(); } QPainter painter(m_device); painter.beginNativePainting(); mF->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); mF->glClearColor(0, .5, 0.1f, .5); painter.endNativePainting(); QFont fpsFont("Helvetica", 10, QFont::Bold); painter.setPen(Qt::red); painter.setFont(fpsFont); painter.drawText(50, 50, "Lalalalalalalalalal"); painter.end(); qDebug()<<"paint!"; m_context->swapBuffers(this); } bool testRender::event(QEvent *event) { switch (event->type()) { case QEvent::UpdateRequest: renderNow(); return true; default: return QWindow::event(event); } } void testRender::exposeEvent(QExposeEvent *event) { Q_UNUSED(event); //if (isExposed()) // renderNow(); } void testRender::resizeEvent(QResizeEvent *event) { QWindow::resizeEvent(event); renderNow(); }
That did I do wrong? :- )
TIA
-
Hi
seems much like
http://doc.qt.io/qt-5/qtgui-openglwindow-example.htmlonly difference i could spot from fast look over was
m_device->setSize(size());which you dont seem to have.
and that example doesn't use painter.beginNativePainting();also tried your font code in that sample
so the font part does work :)
-
Yup totally missed that.. thanks! I was following that tutorial & slowly playing with functions.
Also... I was trying to draw triangle lately, but I got the same problem... I updated the code with :
painter.beginNativePainting(); //mF->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); //mF->glClearColor(0, .5, 0.1f, .5); char *vertexShaderSource = "attribute highp vec4 posAttr;\n" "attribute lowp vec4 colAttr;\n" "varying lowp vec4 col;\n" "uniform highp mat4 matrix;\n" "void main() {\n" " col = colAttr;\n" " gl_Position = matrix * posAttr;\n" "}\n"; char *fragmentShaderSource = "varying lowp vec4 col;\n" "void main() {\n" " gl_FragColor = col;\n" "}\n"; QOpenGLShaderProgram *m_program = new QOpenGLShaderProgram(this); m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource); m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource); m_program->link(); int m_posAttr = m_program->attributeLocation("posAttr"); int m_colAttr = m_program->attributeLocation("colAttr"); int m_matrixUniform = m_program->uniformLocation("matrix"); mF->glViewport(0, 0, width(), height()); m_program->bind(); QMatrix4x4 matrix; matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f); matrix.translate(0, 0, -2); matrix.rotate(75, 0, 1, 0); /// this seem to not help, triangle is not perfectly on its side "hiding" accidentally. m_program->setUniformValue(m_matrixUniform, matrix); GLfloat vertices[] = { 0.0f, 0.707f, -0.5f, -0.5f, 0.5f, -0.5f }; GLfloat colors[] = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }; mF->glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices); mF->glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors); mF->glEnableVertexAttribArray(0); mF->glEnableVertexAttribArray(1); mF->glDrawArrays(GL_TRIANGLES, 0, 3); mF->glDisableVertexAttribArray(1); mF->glDisableVertexAttribArray(0); m_program->release(); painter.endNativePainting();
I know its a memory leak, but for now I'm just trying to get "something" to render via openGL. Any hints with this one? o.O
The program is linked, not sure what else I'm doing wrong here.... sigh. strange... Something tells me that I will hit like 5 "weird" bugs before I get a hang of this "raw" way of rendering without all the QT help... -
Hi
Hmm, nope. it looks very much like the sample
with some code from TriangleWindow::initialize()
in the render() function.