QOpenGLWidget不会绘制三角形 [英] QOpenGLWidget Won't Draw Triangle

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

问题描述

所以我想使用Qt实现我的游戏的编辑器,我似乎无法解读QOpenGLWidget的工作原理。现在,我只想得到一个简单的三角形来渲染,然后我可以担心移动所有剩余的东西。

So I'm trying to implement an editor for my game using Qt, and I can't seem to decipher how the QOpenGLWidget works. Right now, I just want to get a simple triangle to render, then I can worry about moving in all the rest of my stuff.

现在,它将打开窗口,并且在QOpenGLWidget中,将清除我在子类中设置的自定义颜色(所以我没有提升它),但它不会绘制下面类中描述的三角形。我已经尝试遵循Qt OpenGLWindow示例以及QOpenGLWidget和QOpenGLShaderProgram中的示例,我也检查每个函数返回一个bool,以确保他们都正确执行,他们是,但仍然没有三角形。

Right now, it will open the window, and, in the QOpenGLWidget, will clear to the custom color I set in the subclass (so I did promote it), but it won't draw the triangle described in the class below. I've tried following the Qt OpenGLWindow example as well as the examples in QOpenGLWidget and QOpenGLShaderProgram, I also checked every function that returns a bool to make sure they were all being executed properly, and they are, but still no triangle.

我错过了一个重要的函数调用吗?我以绝对错误的方式做事情吗?

Did I miss a vital function call? Am I doing things in the absolute wrong way? Or is it something super subtle and strange with Qt?

这是标题:

#include<qopenglwidget.h>
#include<QtGui/qopenglfunctions.h>

class EditorViewWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
public:
    EditorViewWidget(QWidget *parent);
protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
    QOpenGLShaderProgram* shaderProgram;
    QOpenGLVertexArrayObject vao;
    QOpenGLBuffer VBO;
    int position_attribute;
    int color_uniform;
    float Vertices[9];
    const char* vert="#version 150          \n"
"                                           \n"
"   in vec3 position;                       \n"
"                                           \n"
"   void main()                             \n"
"   {                                       \n"
"       gl_Position = vec4(position, 1.0);  \n"
"   }";
    const char* frag="#version 150          \n"
"                                           \n"
"   uniform vec3 Color;                     \n"
"   out vec4 outColor;                      \n"
"                                           \n"
"   void main()                             \n"
"   {                                       \n"
"       outColor = vec4(Color, 1.0);        \n"
"   }";
};

和来源:

EditorViewWidget::EditorViewWidget(QWidget* parent)
    :QOpenGLWidget(parent)
{
    float v[] = {
        0.0, 0.5, 0.0,
        0.5, -0.5, 0.0,
        -0.5, -0.5, 0.0
    };
    for (int i = 0; i < 9; i++)
        Vertices[i] = v[i];
}

void EditorViewWidget::initializeGL()
{
    initializeOpenGLFunctions();
    vao.create();
    vao.bind();

    //glGenBuffers(1, &VBO);
    glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
    shaderProgram = new QOpenGLShaderProgram(this);
    shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vert);
    shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, frag);
    shaderProgram->link();
    shaderProgram->bind();
    position_attribute = shaderProgram->attributeLocation("position");
    color_uniform = shaderProgram->uniformLocation("Color");
    VBO.create();
    VBO.bind();
    VBO.allocate(&Vertices, 9*sizeof(float));
    shaderProgram->enableAttributeArray(position_attribute);
    shaderProgram->setAttributeArray(position_attribute, Vertices, 3);
    shaderProgram->setUniformValue(color_uniform, 1.0f, 1.0f, 1.0f);
    //glVertexAttribPointer(position_attribute, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
}

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

void EditorViewWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT);
    VBO.bind();
    VBO.allocate(&Vertices, 9 * sizeof(float));
    shaderProgram->enableAttributeArray(position_attribute);
    shaderProgram->setAttributeArray(position_attribute, Vertices, 3, 0);
    shaderProgram->setUniformValue(color_uniform, 1.0f, 1.0f, 1.0f);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    shaderProgram->disableAttributeArray(position_attribute);
}


推荐答案

三角形。默认情况下,OpenGL期望它们按逆时针顺序。切换第二个和第三个顶点,你应该看到你的三角形

You specified the vertices of your triangle in clockwise order. OpenGL expects them in counterclockwise order by default. Switch the 2nd and 3rd vertex and you should see your triangle

这篇关于QOpenGLWidget不会绘制三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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