PDA

View Full Version : Fail to generate a triangle by openGL(Qt5.0.2)



stereoMatching
7th May 2013, 06:18
OS : mac osx 10.8.3
compiler : clang3.2

I am a beginner of opengl, trying to play opengl with Qt5

There are two problems about this simple program(plot a triangle)
1 : I can't see the triangle
2 : The program could not exit even I close the window

hpp


#ifndef CH1HELLOTRIANGLE_HPP
#define CH1HELLOTRIANGLE_HPP

#include <QGLWidget>

#include <QtGui/QOpenGLFunctions>
#include <QtGui/QOpenGLShaderProgram>

class QWidget;

class ch1HelloTriangle : public QGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit ch1HelloTriangle(QWidget *parent = 0);

protected:
virtual void initializeGL();
void initShaders();
void InitializeVertexBuffer();

virtual void resizeGL(int w, int h);
virtual void paintGL();

private:
QOpenGLShaderProgram program;

GLuint positionBufferObject;
};

#endif // CH1HELLOTRIANGLE_HPP



.cpp


#include <locale.h>

#include <QWidget>

#include "ch1HelloTriangle.hpp"

namespace
{

float const vertexPositions[] = {
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,
};

}

ch1HelloTriangle::ch1HelloTriangle(QWidget *parent) :
QGLWidget(parent)
{
}

void ch1HelloTriangle::initializeGL()
{
initializeOpenGLFunctions();
InitializeVertexBuffer();
initShaders();
}

void ch1HelloTriangle::initShaders()
{
// Override system locale until shaders are compiled
setlocale(LC_NUMERIC, "C");

// Compile vertex shader
if (!program.addShaderFromSourceCode(QOpenGLShader::V ertex,
"attribute vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n"))
{
close();
}

// Compile fragment shader
if (!program.addShaderFromSourceCode(QOpenGLShader::F ragment,
"out vec4 outputColor;\n"
"void main()\n"
"{\n"
" outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
"}\n"))
{
close();

}

// Link shader pipeline
if (!program.link())
close();

// Bind shader pipeline for use
if (!program.bind())
close();

// Restore system locale
setlocale(LC_ALL, "");
}

void ch1HelloTriangle::InitializeVertexBuffer()
{
glGenBuffers(1, &positionBufferObject);

glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void ch1HelloTriangle::resizeGL(int w, int h)
{
// Set OpenGL viewport to cover whole widget
glViewport(0, 0, w, h);
}

void ch1HelloTriangle::paintGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);

int vertexLocation = program.attributeLocation("position");
program.enableAttributeArray(vertexLocation);

glVertexAttribPointer(vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 3);
}



main.cpp


#include <QApplication>

#include "ch1HelloTriangle.hpp"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

ch1HelloTriangle ch1;
ch1.show();

return a.exec(&#41;;
}

stereoMatching
8th May 2013, 04:21
After a lot of trial and error, I solved the problem.

Change two things :
1 : read the shader by files


// Compile vertex shader
if (!program.addShaderFromSourceFile(QOpenGLShader::V ertex, "modernOpenGLShader/ch1/vertex")){
QMessageBox::warning(this, "QOpenGLShader::Vertex", "QOpenGLShader::Vertex" + program.log());
close();
}

// Compile fragment shader
if (!program.addShaderFromSourceFile(QOpenGLShader::F ragment, "modernOpenGLShader/ch1/frag")){
QMessageBox::warning(this, "QOpenGLShader::Fragment", "QOpenGLShader::Fragment" + program.log());
close();
}


2 : change the fragment shader, remove the output variable


void main() {
//set every drawn pixel to white
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

From the websites, "modern opengl":http://www.arcsynthesis.org/gltut/
and "another site":tomdalling.com/blog/modern-opengl/01-getting-started-in-xcode-and-visual-cpp/

The output qualify should exist(atleast the codes of the second website will work on my pc)
But Qt will throw error message

QOpenGLShader::FragmentERROR: 0:4: Invalid qualifiers 'out' in global variable context

Do anyone know why?Thanks

wysota
8th May 2013, 06:51
Whether you get this error or not depends on the OpenGL version used by the shader engine. Probably the version Qt requests by default (or the one you use if you requested a different version explicitly) doesn't allow renaming the fragment shader output variable.

stereoMatching
8th May 2013, 08:44
Whether you get this error or not depends on the OpenGL version used by the shader engine. Probably the version Qt requests by default (or the one you use if you requested a different version explicitly) doesn't allow renaming the fragment shader output variable.

Maybe you are right, since Qt need to portable, it may not support some features of the GLSL.

wysota
8th May 2013, 12:44
It's not about Qt as it is not Qt that spits out the error but rather the GLSL compiler.

saman_artorious
23rd June 2013, 12:46
QOpenGLShader::FragmentERROR: 0:4: Invalid qualifiers 'out' in global variable context

Do anyone know why?Thanks

Yes, I know. You need to change the in & out identifiers inside your shader program to varying & attribute.