2016-08-02 44 views
0

我想呈現QGLWidget(Qt5.7)中的三角形,但由於某種原因,我無法在屏幕上顯示三角形,只顯示藍色背景。如何使用glew繪製Qt 5.7中的OpenGL幾何

myapp.pro文件:

QT += core gui opengl 

CONFIG += c++11 

TARGET = myapp 
CONFIG += console 
CONFIG -= app_bundle 

TEMPLATE = app 

INCLUDEPATH += ../../libs/glew/include 

SOURCES += main.cpp \ 
    ../../libs/glew/src/glew.c \ 
    glwindow.cpp 

HEADERS += \ 
    ../../libs/glew/include/GL/glew.h \ 
    glwindow.h 

這是主要的功能:

#include <QApplication> 
#include <glwindow.h> 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    glwindow v; 
    v.show(); 
    return app.exec(); 
} 

的窗楣:

#include <QtOpenGL/QGLWidget> 

class glwindow : public QGLWidget 
{ 
public: 
    glwindow(); 
protected: 
    void initializeGL() override; 
    void paintGL() override; 
}; 

cpp文件:

#include <GL/glew.h> 
#include <glwindow.h> 

GLfloat vertices[] = { 
    +0.0f, +1.0f, 
    -1.0f, -1.0f, 
    +1.0f, -1.0f 
}; 

glwindow::glwindow() 
{} 

void setupGeometry() 
{ 
    GLuint buffer_id; 
    glGenBuffers(1, &buffer_id); 
    glBindBuffer(GL_ARRAY_BUFFER, buffer_id); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 
    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); 
} 

void glwindow::initializeGL() 
{ 
    glewInit(); 
    setupGeometry(); 
} 

void glwindow::paintGL() 
{ 
    glClearColor(0.0, 0.0, 1.0, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glDrawArrays(GL_TRIANGLES, 0, 3); 
} 

更新1: 添加了着色器代碼:

const GLchar *vs = "#version 150 // Specify which version of GLSL we are using." 
       "in vec2 in_Position;" 
       "void main() " 
       "{" 
       " gl_Position = vec4(in_Position.x, in_Position.y, 0.5, 1.0);" 
       "}"; 
const GLchar *fs = "#version 150 // Specify which version of GLSL we are using." 
       "precision highp float; // Video card drivers require this line to function properly" 
       "out vec4 fragColor;" 
       "void main()" 
       "{" 
       " fragColor = vec4(1.0,1.0,1.0,1.0); //Set colour of each fragment to WHITE" 
       "}"; 

即設置着色器的功能是:

void checkShader(GLuint ID) 
{ 
    GLint compile_status = 0; 
    glGetShaderiv(ID, GL_COMPILE_STATUS, &compile_status); 
    if(compile_status != GL_TRUE) 
    { 
     GLint info_length; 
     GLsizei buffer_size; 
     glGetShaderiv(ID, GL_INFO_LOG_LENGTH, &info_length); 
     GLchar *message = new GLchar[info_length]; 
     glGetShaderInfoLog(ID, info_length, &buffer_size, message); 
     delete[] message; 
    } 
} 

void initShader() 
{ 
    GLuint program_id; 
    GLuint vs_id, fs_id; 

    vs_id = glCreateShader(GL_VERTEX_SHADER); 
    fs_id = glCreateShader(GL_FRAGMENT_SHADER); 

    const char *adapter[1]; 
    adapter[0] = vs; 
    glShaderSource(vs_id, 1, adapter, 0); 
    adapter[1] = fs; 
    glShaderSource(fs_id, 1, adapter, 0); 

    glCompileShader(vs_id); 
    checkShader(vs_id); 

    glCompileShader(fs_id); 
    checkShader(fs_id); 

    program_id = glCreateProgram(); 
    glAttachShader(program_id, vs_id); 
    glAttachShader(program_id, fs_id); 

    glLinkProgram(program_id); 
    glUseProgram(program_id); 
} 

所以,init函數改爲

void glview::initializeGL() 
{ 
    glewInit(); 
    initGeometry(); 
    initShader(); 
} 

着色器初始化失敗,錯誤消息: (GLchar *)0x7efe21 \「:1(10):error:GLSL 1 .50不支持。支持的版本爲:1.10,1.20,1.30,1.00 ES和ES 3.00 \ n \」

+0

如果你使用Qt 5.7,你應該避免使用QGLWidget:使用[new Qt OpenGL classes](http://doc.qt.io/qt-5/qtgui-index.html#opengl-and -opengl-es-integration)。你會發現專用對象來封裝你的大部分調用。另請參見[QOpenGLWindow](http://doc.qt.io/qt-5/qopenglwindow.html)。你應該刪除glew你的項目(從最好的情況來說)對於OpenGL函數是多餘的Qt管理 – wasthishelpful

+0

你的着色器在哪裏?你需要使用固定的管道(不推薦使用)或者使用着色器的「現代」OpenGL。 M只是,您使用通用頂點屬性,它只能與着色器一起使用。 –

+0

@wasthishelpful,我不使用Qt OpenGL類的原因是QGLShaderProgram只支持GLSL 1.x – r0ng

回答

0

的Qt 5.7只支持GLSL 1.3.0所以,如果脂肪酶想使用GLSL 4.3,嘗試GLFW或SDL