QOpenGLWidget不拉丝阵列 [英] QOpenGLWidget not drawing array

查看:287
本文介绍了QOpenGLWidget不拉丝阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

you're我对这个问题,因为林的想法最后手段。
所以首先我已经做了一些openGL的东西,但不与Qt库。

you´re my last resort for this question because Im out of ideas. So first of all I have already done some openGL stuff but not with the Qt Libraries.

由于我有,因为Qt的Andr​​oid的编译器的Qt库做到这一点,围绕那里有没有办法对我来说。

And since I HAVE to do it with Qt Libraries because of the Qt Android compiler, theres no way around for me.

所以我试图得出一些顶点坐标像正常的openGL。
保存float数组等..点
所以我hoppyfully做出了正确的VAO,VBO ..着色器..但它不需额外画什么。
paintGL()只画的东西,如果它很难$ C $光盘在glBegin和结束。

So I was trying to draw some vertex coordinates like in normal openGL. Save points in float array etc.. So I hoppyfully made the right vao, vbo.. shader.. but it won´t draw anything. paintGL() only draws something if its hardcoded with glBegin and end.

请告诉我,从来就出了问题,并从我的痛苦释放我^^
啊,是的已经有像我一个问题,这里但没有答案,所以我想再次尝试。

Please tell me where I´ve gone wrong and release me from my suffering^^ Ah and yes there is already a questions like mine Here but there was no answer so I want to try it again.

- MainWidget头:

--MainWidget Header:

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QVector2D>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QVector3D>
#include <QOpenGLVertexArrayObject>


class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
     MainWidget(QWidget *parent = 0);
    ~MainWidget();


protected:
    void initializeGL() Q_DECL_OVERRIDE;
    void resizeGL(int w, int h) Q_DECL_OVERRIDE;
    void paintGL() Q_DECL_OVERRIDE;

    GLfloat vertices[9];



private:
    QOpenGLShaderProgram program;
    QOpenGLBuffer arrayBuf;
    QOpenGLVertexArrayObject vertexArrayID;

};

#endif // MAINWIDGET_H

- MainWidget .cpp的:

-- MainWidget Cpp:

#include "mainwidget.h"


MainWidget::MainWidget(QWidget *parent): QOpenGLWidget(parent){

}
MainWidget::~MainWidget(){

}

void MainWidget::initializeGL(){
    initializeOpenGLFunctions();
    glClearColor(0, 0, 0.4f, 0);
    glEnable(GL_DEPTH_TEST);
    //glEnable(GL_CULL_FACE);

   static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f,  1.0f, 0.0f
    };
    for(int i = 0; i < 9; i++){
        vertices[i] = g_vertex_buffer_data[i];
    }

    vertexArrayID.create();
    vertexArrayID.bind();
    program.addShaderFromSourceFile(QOpenGLShader::Vertex, "vertex.vert");
    program.addShaderFromSourceFile(QOpenGLShader::Fragment, "fragment.frag");
    program.link();
    program.bind();
    arrayBuf.create();
    arrayBuf.bind();
    arrayBuf.allocate(&vertices, 9 * sizeof(GLfloat));
   /* glGenBuffers(1, &arrayBuf);
    glBindBuffer(GL_ARRAY_BUFFER, arrayBuf);*/
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);


}
void MainWidget::resizeGL(int w, int h){

}

void MainWidget::paintGL(){
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    arrayBuf.bind();
    arrayBuf.allocate(&vertices, 9*sizeof(GLfloat));



    int vertexLocation = program.attributeLocation("vertex");
    program.enableAttributeArray("vertexLocation");
    program.setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(GLfloat) );

    //glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);

}

- 主.cpp的:

--Main Cpp:

#include <QApplication>
#include <QLabel>
#include <QSurfaceFormat>

#ifndef QT_NO_OPENGL
#include "mainwidget.h"
#endif

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

    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    QSurfaceFormat::setDefaultFormat(format);

    app.setApplicationName("GLtest");

#ifndef QT_NO_OPENGL
    MainWidget widget;
    widget.show();
#else
    QLabel note("OpenGL Support required");
    note.show();
#endif
    return app.exec();
}

- 顶点着色引擎尽可能地简单:

--Vertex Shader as easy as possible:

    #version 330

layout(location = 0) in vec3 vertex;

void main(){
    gl_Position = vec4(vertex, 1.0);
}

- 片段着色器:

--Fragment Shader:

#version 330
out vec3 color;
void main(){
    color = vec3(1,0,0);
}

在此先感谢..任何形式的帮助是值得欢迎的!
干杯

Thanks in advance.. any kind of help is welcome! Cheers

推荐答案

您正在使错了顶点属性:

You are enabling the wrong vertex attribute:

program.enableAttributeArray("vertexLocation");

这应该要么(不是失踪)

It should either be (not the missing ")

program.enableAttributeArray(vertexLocation);

或(正确名称)

program.enableAttributeArray("vertex");

这篇关于QOpenGLWidget不拉丝阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆