QtOpenGLWidget子类不绘制任何内容
我有一个简单的顶点和片段着色器。我在一个从QtOpenGLWidget派生的类中使用它们。该程序应该绘制一个三角形,但它没有显示任何内容。glClearColor工作正常,我可以更改背景色。当我从命令行运行程序时,程序只输出一行,但不绘制任何内容: QOpenGLShaderProgram::attributeLocation(位置):着色器程序未链接 在gl命令之后插入getGLError时,没有一个命令报告错误。 以下是类定义:QtOpenGLWidget子类不绘制任何内容,qt,opengl,Qt,Opengl,我有一个简单的顶点和片段着色器。我在一个从QtOpenGLWidget派生的类中使用它们。该程序应该绘制一个三角形,但它没有显示任何内容。glClearColor工作正常,我可以更改背景色。当我从命令行运行程序时,程序只输出一行,但不绘制任何内容: QOpenGLShaderProgram::attributeLocation(位置):着色器程序未链接 在gl命令之后插入getGLError时,没有一个命令报告错误。 以下是类定义: class GLCanvas:public QOpenGLWi
class GLCanvas:public QOpenGLWidget , protected QOpenGLFunctions
{
QMatrix4x4 matrix;
QOpenGLShaderProgram* shader;
GLuint posLocation;
GLuint colLocation;
GLuint matLocation;
void cleanup();
public:
GLCanvas(QWidget* parent);
void initializeGL() override;
void paintGL() override;
void resizeGL(int w, int h) override;
QStringList getGLinfo();
};
static const char *vertexShaderSource =
R"(
#version 330
in vec3 position;
void main()
{
gl_Position = vec4(position, 1);
})";
static const char *fragmentShaderSource =
R"(
#version 330
void main()
{
gl_FragColor = vec4(0.4,0.4,0.8,1.0);
})";
GLCanvas::GLCanvas(QWidget* parent): QOpenGLWidget(parent)
{
setFormat(QSurfaceFormat::defaultFormat());
matrix.setToIdentity();
posLocation = -1;
colLocation = -1;
matLocation = -1;
}
void GLCanvas::initializeGL()
{
makeCurrent();
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &GLCanvas::cleanup);
constexpr int glNoError = 0;
GLenum error = -1;
initializeOpenGLFunctions();
glClearColor(.1f, .1f, .1f, 1.0f);
error = glGetError();
if (error != glNoError)
{
qDebug() << "glclearcolor failed";
}
shader = new QOpenGLShaderProgram(this);
shader->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
error = glGetError();
if (error != glNoError)
{
qDebug() << "failed after adding vertex shader.";
}
shader->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
error = glGetError();
if (error != glNoError)
{
qDebug() << "failed after adding fragment shader.";
}
Q_ASSERT(shader->link());
error = glGetError();
if (error != glNoError)
{
qDebug() << "gl error at link.";
}
//colLocation = shader->attributeLocation("color");
//matLocation = shader->uniformLocation("matrix");
//shader->release();
posLocation = shader->attributeLocation("position");
error = glGetError();
if (error != glNoError)
{
qDebug() << "gl error at link.";
}
glEnable(GL_DEPTH_TEST);
//colLocation = shader->attributeLocation("color");
//matLocation = shader->uniformLocation("matrix");
//shader->release();
//glEnable(GL_CULL_FACE);
}
//###################################################################
void GLCanvas::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat vertices[] = { 0.0f, 0.707f, .0f, 0.5f, -0.5f, .0f ,-0.5f, -0.5f, .0f};
Q_ASSERT(shader->bind());
shader->setAttributeArray(posLocation, vertices, 3, 0);
glEnableVertexAttribArray(posLocation);
glDrawArrays(GL_LINES, 0, 3);
/*
GLfloat lvertices[] = { 0.907f, 0.907f, 0.5f, -0.957f, -0.957f, 0.5f };
shader->setAttributeArray(posLocation, lvertices, 0, 3);
glDrawArrays(GL_LINE_STRIP, 0, 2);
glDisableVertexAttribArray(posLocation);
*/
//glDisableVertexAttribArray(colLocation);
shader->release();
// qDebug() << getGLinfo();
}
void GLCanvas::resizeGL(int w, int h)
{
Q_UNUSED(w);
Q_UNUSED(h);
//matrix.setToIdentity();
//matrix.perspective(45.0f, w / float(h), 0.01f, 1000.0f);
glViewport(0, 0, w, h);
}
/*
void GLCanvas::mousePressEvent(QMouseEvent *event)
{
Q_UNUSED(event);
}
void GLCanvas::mouseMoveEvent(QMouseEvent *event)
{
Q_UNUSED(event);
}
*/
void GLCanvas::cleanup()
{
if (shader == nullptr)
return;
makeCurrent();
delete shader;
//shader = nullptr;
doneCurrent();
}
class-GLCanvas:公共QOpenGLWidget,受保护的QopenglFunction
{
QMatrix4x4矩阵;
QOpenGLShaderProgram*着色器;
胶合定位;
胶合搭配;
胶合定位;
空洞清理();
公众:
GLCanvas(QWidget*父项);
void initializeGL()覆盖;
void paintGL()覆盖;
void resizeGL(int w,int h)覆盖;
QStringList getGLinfo();
};
静态常量字符*vertexShaderSource=
R“(
#330版
在vec3位置;
void main()
{
gl_位置=vec4(位置1);
})";
静态常量字符*fragmentShaderSource=
R“(
#330版
void main()
{
gl_FragColor=vec4(0.4,0.4,0.8,1.0);
})";
GLCanvas::GLCanvas(QWidget*父项):QOpenGLWidget(父项)
{
setFormat(QSurfaceFormat::defaultFormat());
矩阵。setToIdentity();
位置=-1;
搭配=-1;
matLocation=-1;
}
void GLCanvas::initializeGL()
{
makeCurrent();
连接(context(),&QOpenGLContext::aboutToBeDestroyed,this,&GLCanvas::cleanup);
constexpr int glNoError=0;
格伦误差=-1;
初始化EnglFunctions();
glClearColor(.1f、.1f、.1f、1.0f);
error=glGetError();
如果(错误!=glNoError)
{
qDebug()addShaderFromSourceCode(QOpenGLShader::Vertex,vertexShaderSource);
error=glGetError();
如果(错误!=glNoError)
{
qDebug()addShaderFromSourceCode(QOpenGLShader::Fragment,fragmentShaderSource);
error=glGetError();
如果(错误!=glNoError)
{
qDebug()链接());
error=glGetError();
如果(错误!=glNoError)
{
qDebug()属性位置(“颜色”);
//matLocation=shader->uniformLocation(“矩阵”);
//着色器->释放();
posLocation=shader->attributeLocation(“位置”);
error=glGetError();
如果(错误!=glNoError)
{
qDebug()属性位置(“颜色”);
//matLocation=shader->uniformLocation(“矩阵”);
//着色器->释放();
//glEnable(GL_CULL_面);
}
//###################################################################
void GLCanvas::paintGL()
{
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
GLfloat顶点[]={0.0f,0.707f,.0f,0.5f,-0.5f,.0f,-0.5f,-0.5f,.0f};
Q_断言(着色器->绑定());
着色器->setAttributeArray(位置,顶点,3,0);
GlenableVertexAttributeArray(posLocation);
gldrawArray(GL_线,0,3);
/*
GLfloat lvertices[]={0.907f,0.907f,0.5f,-0.957f,-0.957f,0.5f};
着色器->setAttributeArray(位置,lvertices,0,3);
gldrawArray(GL_线_带,0,2);
glDisableVertexAttributeArray(posLocation);
*/
//glDisableVertexAttributeArray(并置);
着色器->释放();
//qDebug()我从一开始就仔细地做了每件事,一切都很顺利。可能是一个愚蠢的错误导致了这一切。谢谢大家。那GL_三角形
而不是GL_线
呢?什么都没发生。我把GL_三角形改成GL_线,看看会发生什么。330版
不意味着一个核心
配置文件吗?如果是这样的话,那么这可能是的重复。我将#版本更改为130,默认曲面格式更改为3.0,没有任何更改。但我将尝试学习使用缓冲区对象和数组对象来查看发生了什么。Qt文档指出,在使用setAttributeArray()时
,一旦您调用enableAttributeArray(int location)
,指定的数组将可用。Alghouth很可能此函数只是GlenableVertexAttributeArray()
的包装器,我相信您应该尝试调用它,而不是直接调用OpenGL API函数。