Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Multi-platform OpenGL ES1.1 problem



  • I have trouble in developing a multi-platform application featuring 2d line graphics using QT.
    The codes below show a triangle in Windows and Linux but nothing in Android.

    1. OGL.pro
    QT += core gui widgets
    
    CONFIG += c++11
    
    SOURCES += glwidget.cpp main.cpp
    
    HEADERS += glwidget.h
    
    win32:LIBS += -lopengl32
    android:LIBS += -lGLESv1_CM
    
    
    1. main.cpp
    #include <QApplication>
    #include <QMainWindow>
    #include "glwidget.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QMainWindow w;
        w.resize(512,512);
        GLWidget g(&w);
        g.resize(w.width(),w.height());
        w.show();
        return a.exec();
    }
    
    1. glwidget.h
    #pragma once
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    
    class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
    {
        Q_OBJECT
    
    public:
        GLWidget(QWidget *parent = nullptr);
    
    protected:
        void initializeGL() override;
        void resizeGL(int w, int h) override;
        void paintGL() override;
    
        int m_w, m_h;
    };
    
    1. glwidget.cpp
    #include "glwidget.h"
    
    #if defined(__ANDROID__)
    #   include <GLES/gl.h>
    #   define glOrtho glOrthox
    #elif defined(__linux__)
    #   include <GL/gl.h>
    #else
    #   include <gl/GL.h>
    #endif
    
    GLWidget::GLWidget(QWidget *parent) : QOpenGLWidget(parent)
    {
    }
    
    void GLWidget::initializeGL()
    {
        initializeOpenGLFunctions();
    
        glClearColor(0.f,0.f,.5f,1.f);
    }
    
    void GLWidget::resizeGL(int w, int h)
    {
        glViewport(0, 0, m_w = w, m_h = h);
    }
    
    void GLWidget::paintGL()
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, m_w, m_h, 0, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        glClear(GL_COLOR_BUFFER_BIT);
    
        glColor4f(1.f,1.f,1.f,.1f);
        glLineWidth(1.f);
    
        glEnableClientState(GL_VERTEX_ARRAY);
        float vertex[] = {100.f, 100.f, 300.f, 400.f, 400.f, 300.f, 100.f, 100.f};
        glVertexPointer(2, GL_FLOAT, 0, vertex);
        glDrawArrays(GL_LINE_STRIP, 0, 4);
        glDisableClientState(GL_VERTEX_ARRAY);
    }
    

    Windows/Linux(Ubuntu) build runs properly but Android(Samsung galaxy note 20) build does not draw lines at all as below. My project uses a dedicated native c++ library(based on OpenGL ES 1.1) to deal with some complicated vector data. I found that the codes in paintGL() works also properly when they're compiled as JNI libary and called by Java application using Android Studio.

    Thank you in advance.

    1. Result of Windows build
      alt text

    2. Result of Android build
      alt text


Log in to reply