Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to include freeglut in a QWidget?
QtWS25 Last Chance

How to include freeglut in a QWidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qwidgetfreeglutopengl
1 Posts 1 Posters 326 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    timob256
    wrote on 22 Sept 2022, 11:06 last edited by timob256
    #1

    How to include freeglut in a QWidget?

    I know that there is a QOpenGLQWidget that can do more than freeglut. But I would like to be able to embed a freeglut window into a QWidget.

    All code

    main.cpp

    #include "widgetglut.h"
    
    #include <QApplication>
    #include <QScreen>
    #include <QDebug>
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
         WidgetGlut w;
        //выставляем подстройку к размеру экрана
        QSize screenSize = qApp->screens().at(0)->availableSize();
        const int width = screenSize.width() / 2.5;
        const int height = screenSize.height() * 0.5;
        w.setGeometry((screenSize.width() - width) / 2.0,
                      (screenSize.height() - height) / 2.0,
                      width,
                      height);
        w.setWindowTitle("куте + бесплатный глут");
        
        w.setScale((float)width, (float)height);
    
        w.show();
        return a.exec();
    }
    

    widgetglut.h

    #ifndef WIDGETGLUT_H
    #define WIDGETGLUT_H
    
    #include <QWidget>
    #include "myfreeglut.h"
    
    class WidgetGlut : public QWidget
    {
        Q_OBJECT
    public:
        WidgetGlut(QWidget *parent = 0);
        ~WidgetGlut();
    
    protected:
    
    private:
    
    
    protected:
      QGridLayout* gl_layaout;
    };
    
    #endif // WIDGETGLUT_H
    

    widgetglut.cpp

    #include "widgetglut.h"
    
    WidgetGlut::WidgetGlut(QWidget *parent)
        : QWidget(parent)
    {
        gl_layaout = new QGridLayout(parent);
        MyFreeGlut * glutWindow = new MyFreeGlut ();
        glutWindow->Size(this->width,this->height);
        glutWindow->Position(this->x,this->y);
        
        QWidget *container = QWidget::createWindowContainer(glutWindow);
        gl_layaout->addWidget(container);
    
        this->setLayout(gl_layaout);
    
    }
    
    WidgetGlut::~WidgetGlut()
    {
    
    }
    

    myfreeglut.h

    #ifndef MYFREEGLUT_H
    #define MYFREEGLUT_H
    
    #include <GL/glew.h>
    #include <GLFW/glfw3.h>
    #include <GL/freeglut.h>
    
    class	MyFreeGlut
    {
    	public:
    	    MyFreeGlut() ;
    	    ~MyFreeGlut();
    	
    	int Size(int x, int y);
    	int Plaise(int x, int y);
        
        void Init(void);
        void LineSegment(void);
    	    
    	private:
    	int    _size_x                      {0};
        int    _size_y                      {0};
        int    _position_x               {0};
        int    _position_y               {0};
    	   
    };
    #endif // MYFREEGLUT_H
    

    myfreeglut.cpp

    #include "myfreeglut.h"
    
    MyFreeGlut::MyFreeGlut() 
    {
    	  glutInit(&argc, argv);  // How to call it correctly! ? 
    
    //   glutInitContextVersion(4, 3);
    //   glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
    
       glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
       glutInitWindowSize(_size_x, _size_y);
       glutInitWindowPosition( _position_x, _position_y) ;
       glutCreateWindow("line");
       MyFreeGlut::Init();
      glutDisplayFunc(MyFreeGlut::LineSegment());
      glutMainLoop();
    
    }
    
    int MyFreeGlut::Size(int x, int y) 
    {
    	_size_x =x;
    	_size_y =y;
    }
    
    int MyFreeGlut::Position(int x, int y) 
    {
    	_position_x =x;
    	_position_y =y;
    }
    
    void MyFreeGlut::Init(void)
    {
      glClearColor(1.0, 1.0, 1.0, 0.0);
      glMatrixMode(GL_PROJECTION);
      gluOrtho2D(0.0, 200.0, 0.0, 160.0);
    }
    void MyFreeGlut::LineSegment(void)
    {
      glClear(GL_COLOR_BUFFER_BIT);
      glColor3f(1.0, 0.0, 0.0);
      glBegin(GL_LINES);
      glVertex2i (180, 15);
      glVertex2i (10, 145);
      glEnd();
      glFlush();
    }
    
    1 Reply Last reply
    0

    1/1

    22 Sept 2022, 11:06

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved