How can I change the following code so that it actually draws the triangle?
First is the shader, then the implementation of the glwiedget class which is derived from QOpenglWidget
.
// shaders here static const char* vertexShaderSource = "#version 330 core\n" "in vec3 posAttr;\n" //"attribute lowp vec3 colAttr;\n" //"varying lowp vec4 col;\n" //"uniform highp mat4 matrix;\n" "void main() {\n" //" col = colAttr;\n" " gl_Position = vec4(posAttr, 1) ;\n" "}\n"; // fragment shader static const char* fragmentShaderSource = "#version 330 core\n" //"varying lowp vec4 col;\n" "void main() {\n" "gl_FragColor = vec4(1.0, 0.0, 1.0, 0.0);\n" "}\n"; Glwidget::Glwidget(QWidget* parent):QOpenGLWidget(parent) { // } Glwidget::~Glwidget() { cleanup(); } void Glwidget::initializeGL() { connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &Glwidget::cleanup); initializeOpenGLFunctions(); glClearColor(.0f, .0f, .0f, 1.0f); shader = new QOpenGLShaderProgram(this); shader->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource); shader->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource); // posAttribute = shader->attributeLocation("posAttr"); //colAttribute = shader->attributeLocation("colAttr"); //matrixAttribute = shader->uniformLocation("matrix"); Q_ASSERT(shader->link()); Q_ASSERT(shader->bind()); //shader->release(); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); } void Glwidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT); makeCurrent(); matrix.perspective(60.0, 4.0f/3.0f, 0.1f, 10.0f); matrix.translate(0, 0, -2); matrix.rotate(100.0f, 0, 1, 0); //shader->setUniformValue(matrixAttribute, matrix); // shader->bind(); GLfloat vertices[] = { 0.0f, 0.707f, 1.0f, -0.5f, -0.5f, 1.0f, 0.5f, -0.5f, 1.0f }; shader->setAttributeArray(posAttribute,vertices, 3); GLfloat colors[] = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }; glVertexAttribPointer(posAttribute, 3, GL_FLOAT, GL_FALSE, 0, vertices); //glVertexAttribPointer(colAttribute, 3, GL_FLOAT, GL_FALSE, 0, colors); glEnableVertexAttribArray(posAttribute); //glEnableVertexAttribArray(colAttribute); glDrawArrays(GL_TRIANGLES, 0, 1); glDisableVertexAttribArray(posAttribute); //glDisableVertexAttribArray(colAttribute); //shader->release(); } void Glwidget::resizeGL(int w, int h) { matrix.setToIdentity(); matrix.perspective(45.0f, w / float(h), 0.01f, 1000.0f); //glViewport(0, 0, w, h); } void Glwidget::mousePressEvent(QMouseEvent *event) { Q_UNUSED(event); } void Glwidget::mouseMoveEvent(QMouseEvent *event) { Q_UNUSED(event); } void Glwidget::cleanup() { if (shader == nullptr) return; makeCurrent(); delete shader; shader = 0; doneCurrent(); }