Note that there are some explanatory texts on larger screens.

plurals
  1. POFail to generate a triangle by openGL with Qt5.0.2
    primarykey
    data
    text
    <p>OS : mac osx 10.8.3</p> <hr> <p>compiler : clang3.2</p> <hr> <p>I am a beginner of opengl, trying to play opengl with Qt5</p> <p>There are two problems about this simple program(plot a triangle)</p> <ol> <li><p>I can't see the triangle</p></li> <li><p>The program could not exit even I close the window</p></li> </ol> <p>hpp</p> <pre><code>#include &lt;QGLWidget&gt; #include &lt;QtGui/QOpenGLFunctions&gt; #include &lt;QtGui/QOpenGLShaderProgram&gt; 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; }; </code></pre> <p>.cpp</p> <pre><code>#include &lt;locale.h&gt; #include &lt;QWidget&gt; #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::Vertex, "attribute vec4 position;\n" "void main()\n" "{\n" " gl_Position = position;\n" "}\n")) { close(); } // Compile fragment shader if (!program.addShaderFromSourceCode(QOpenGLShader::Fragment, "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, &amp;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() { /* //codes propose by http://stackoverflow.com/questions/13111291/displaying-a-triangle-with-qt-and-opengl?rq=1, can't see the triangle either QSize viewport_size = size(); glViewport(0, 0, viewport_size.width(), viewport_size.height()); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1, 1, -1, 1, 5, 7); // near and far match your triangle Z distance glMatrixMode(GL_MODELVIEW);*/ 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); } </code></pre> <p>main.cpp</p> <pre><code>#include &lt;QApplication&gt; #include "ch1HelloTriangle.hpp" int main(int argc, char *argv[]) { QApplication a(argc, argv); ch1HelloTriangle ch1; ch1.show(); return a.exec(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload