QtGui segmentation fault - glDrawArrays



  • I modified example from website (http://doc.qt.io/qt-5/qtgui-openglwindow-example.html) to work with 3D models and noticed that when I execute my program from terminal (without debugger) my program crushes. When i execute it from qt Creator in debug mode it never crushed, in release it crushes sometimes at the beginning. I'm having bad time figuring this out. Object i'm trying to render is about 4k vertices.

    Code (there is some additional code about arduino readings, but it's not error's cause for sure):

    #include "h/modelwindow.h"
    
    ModelWindow::ModelWindow(QString modelDestination) : m_program(Q_NULLPTR), angle(0.0f,0.0f,0.0f),
    													 distance(0.0f,0.0f,0.0f)
    {
    	objectModel.LoadObject(modelDestination);
    }
    
    ModelWindow::~ModelWindow()
    {
    	delete m_program;
    }
    
    void ModelWindow::initialize()
    {
    	m_program = new QOpenGLShaderProgram(this);
    	if(m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, QDir::currentPath()+"/shaders/basicshader.vert"))
    	{
    		qDebug() << "Vertex shader loaded";
    	}
    	else
    	{
    		close();
    		qDebug() << "Unable to load vertex shader";
    	}
    	if(m_program->addShaderFromSourceFile(QOpenGLShader::Fragment,QDir::currentPath()+"/shaders/basicshader.frag"))
    	{
    		qDebug() << "Fragment shader loaded";
    	}
    	else
    	{
    		close();
    		qDebug() << "Unable to load vertex fragment";
    	}
    
    	m_program->link();
    	m_program->bind();
    
    	m_positionAttribute = m_program->attributeLocation("posAttr");
    	m_colorAttribute=m_program->attributeLocation("colAttr");
    	m_matrixUniform = m_program->uniformLocation("matrix");
    }
    
    void ModelWindow::render()
    {
    	arduino.readLine();
    
    	//arduino delay in seconds
    	float aDelay = 0.1f;
    	float threshold = 0.1f;
    	float threshold2 = 0.001f;
    
    	QVector3D temp = arduino.gyro()* aDelay;
    	QVector3D temp2 = arduino.acc()* aDelay *aDelay * 10;
    
    	if(temp.x() > threshold || temp.x() < -threshold)
    	{
    		angle.setX(angle.x()+temp.x());
    	}
    	if(temp.y() > threshold || temp.y() < -threshold)
    	{
    		angle.setY(angle.y()+temp.y());
    	}
    	if(temp.z() > threshold || temp.z() < -threshold)
    	{
    		angle.setZ(angle.z()+temp.z());
    	}
    	if(temp2.x() > threshold2 || temp2.x() < -threshold2)
    	{
    		distance.setX(distance.x()+temp2.x());
    	}
    	if(temp2.y() > threshold2 || temp2.y() < -threshold2)
    	{
    		distance.setY(distance.y()+temp2.y());
    	}
    	if(temp2.z() > threshold2 || temp2.z() < -threshold2)
    	{
    		distance.setZ(distance.z()+temp2.z());
    	}
    
    	qDebug() << "Distance: " << distance.y() << " m " << distance.z() << " m " << -distance.x() << " m";
    	qDebug() << "Angle: " << angle.y() << " deg " << angle.z() << " deg " << -angle.x() << " deg";
    
    	glViewport(0, 0, width(), height());
    	glClear(GL_COLOR_BUFFER_BIT);
    	m_program->bind();
    
    	QMatrix4x4 matrix;
    	matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
    
    	//axes
    	//board | OpenGL
    	//	x	|	-z
    	//	y	|	x
    	//	z	|	y
    
    	//translations
    	matrix.translate(0.0f,0.0f,-4.0f);
    
    	matrix.translate(-distance.y(),0.0f,distance.x());
    
    	//rotations
    	matrix.rotate(angle.x(), 0, 0, -1);
    	matrix.rotate(angle.y(), 1, 0, 0);
    	matrix.rotate(angle.z(), 0, -1, 0);
    
    	m_program->setUniformValue(m_matrixUniform, matrix);
    
    	QVector <QVector3D> colors;
    
    	for(int i=0; i<objectModel.vertices().size(); ++i)
    	{
    		QVector3D temp(0.75f, 0.75f, 0.75f);
    		colors.push_back(temp);
    	}
    
    	glVertexAttribPointer(m_positionAttribute, 3, GL_FLOAT, GL_TRUE, sizeof(QVector3D), &objectModel.vertices()[0]);
    	glVertexAttribPointer(m_colorAttribute,3, GL_FLOAT, GL_FALSE, sizeof(QVector3D), &colors[0]);
    
    	glEnableVertexAttribArray(0);
    	glEnableVertexAttribArray(1);
    
    	glDrawArrays(GL_TRIANGLES, 0, objectModel.vertices().size());
    
    	glDisableVertexAttribArray(1);
    	glDisableVertexAttribArray(0);
    
    	m_program->release();
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.