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

how to use opengl native function in my function ,not in initializeGL(),paintGL(),resizeGL(int w, int h)



  • I write a program to read 3D obj file and show it in QopenglWidget. When I read the file in initializeGL() and set the VAO in it,everything is ok.Also I try to change the path, the result is fine.But when I do these things in my function mySlotOpenFile() ,I get wrong result,why?
    frist picture is the sphere1.obj contain a plane and a sphere
    ![5804d1e6-9df7-4f57-8937-a30470fdc980-N$Z$4KP8]WPWF4V8]FUQ}CR.png](https://ddgobkiprc33d.cloudfront.net/216346b9-fc56-4038-81fb-ae83f720091e.png)
    second picture is sphere.obj contain a small sphere
    bb2b362a-7717-47cc-a608-8e79679e8850-SCHO35Q2$_BB@2FT@U9)75G.png
    And in third pciure ,I open sphere1.obj in nitializeGL() ,and then open sphere.obj in my function mySlotOpenFile() .which I get is this ,why?
    6507c61c-a9fe-4142-a145-6ea1ee944959-5I}(AOW651F2XHI2GULHJI7.png

    #include "glWidget.h"
    #include <QMessageBox>
    #include<Qmath.h>
    #include<Qdebug>
    #include<FileSystem>
    glWidget::glWidget(QWidget *parent)
    	: QOpenGLWidget(parent)
    {
    	myrender = NULL;
    }
    
    glWidget::~glWidget()
    {
    }
    
    void glWidget::initializeGL()
    {
    	core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_4_3_Core>();
    	myrender = new MyRender(core);
    	//myrender->openFile("easymodel/sphere.obj");
    	drawFlag = true;
    }
    
    void glWidget::paintGL()
    {
    	if (drawFlag==false)
    	{
    		return;
    	}
    	myrender->Draw();
    	update();
    }
    
    void glWidget::resizeGL(int w, int h)
    {
    	
    }
    
    
    void glWidget::mySlot2(int front, int back,int left,int right)
    {
    	myrender->setCameraPos(front, back, left, right);
    }
    
    void glWidget::mySlotMosue(QPointF firstPoint, QPointF nextPoint)
    {
    	myrender->setCameraFront(firstPoint, nextPoint);
    }
    
    void glWidget::mySlotOpenFile()
    {
    	this->makeCurrent();
    	//auto *ctx = QOpenGLContext::currentContext();
    	drawFlag = false;
    	//myrender = new MyRender(core);
    	QString fileName = QFileDialog::getOpenFileName(NULL, "标题", ".");
    	if (fileName!="")
    	{
    		myrender->openFile(fileName);
    	}
    	core->glClearColor(0.6f, 0.2f, 0.2f, 1.0f);
    
    	drawFlag = true;
    	this->doneCurrent();
    	update();
    }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    I would start with using QOpenGLWidget::context to use the one associated with your widget.

    Are you sure your MyRender class does not do anything unusual with regard to the OpenGL state ?


Log in to reply