简体   繁体   English

QML下的OpenGL

[英]OpenGL under QML

So I've been toying around with OpenGL under QML and have been looking at the supplied example file of the same name. 因此,我一直在研究QML下的OpenGL,并一直在查看提供的同名示例文件。 I kind of understand how it's working but here's the thing: I tried to replace the OpenGL Shader Program that was in the paint() function of the example with my own very basic Open GL stuff. 我有点理解它是如何工作的,但事情是这样的:我试图用我自己的非常基本的Open GL内容替换示例中paint()函数中的OpenGL Shader程序。 However I was unable to get anything visible on the screen. 但是我无法在屏幕上看到任何东西。 The only thing I was able to change was the color of the background. 我唯一能够更改的是背景颜色。 So I'm wondering how do I set up the viewport, the camera, and whatever is needed to have something on the screen. 因此,我想知道如何设置视口,相机以及在屏幕上显示内容所需的一切。 I have some (very rusty) experience on OpenGL but in the past there's always been things like freeglut that makes life a bit easier. 我在OpenGL上有一些(非常生锈)的经验,但是在过去,总有一些像freeglut这样的东西使生活变得更轻松。 Any pointers or examples (something I can put in the paint() method to observe and learn from) to the right direction would be much appreciated... 任何指向正确方向的指针或示例(我可以在paint()方法中进行观察和学习的东西)都将不胜感激...

Edit: So here's what I have in the paint() method: 编辑:所以这是我在paint()方法中所拥有的:

void QtOpenGLViewRenderer::paint()
{
    // The following two lines fixed the problem
    QOpenGLFunctions glFuncs(QOpenGLContext::currentContext());
    glFuncs.glUseProgram(0);

    glViewport(0, 0, m_viewportSize.width(), m_viewportSize.height());

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClearColor(0.2, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    GLfloat triangle[] = {
        0.25f, 0.25f, 0.0f,
        0.75f, 0.25f, 0.0f,
        0.25f, 0.75f, 0.0f
    };

    GLfloat colors[] = {
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f
    };

    glVertexPointer(3, GL_FLOAT, 0, triangle);
    glColorPointer(4, GL_FLOAT, 0, colors);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glShadeModel(GL_SMOOTH);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glFlush();
}

All I see is the dark reddish background but no sign of the triangle. 我所看到的只是深红色背景,但没有三角形的迹象。 Why is this? 为什么是这样?

Answering my own question for the record. 记录我自己的问题。 The problem was that Qt by default is expecting new OpenGL Shading Language (GLSL) style instructions. 问题在于Qt默认情况下期望新的OpenGL阴影语言(GLSL)样式说明。 To get old style OpenGL instructions to work you need to tell Qt that you're going to use them instead of a program defined by shaders. 要使旧式OpenGL指令起作用,您需要告诉Qt您将使用它们而不是着色器定义的程序。 This is done by issuing the following commands: 通过发出以下命令来完成此操作:

QOpenGLFunctions glFuncs(QOpenGLContext::currentContext());
glFuncs.glUseProgram(0);

For these to work you also need to include the following headers: 为了使它们起作用,您还需要包括以下标题:

#include <QOpenGLFunctions>
#include <QOpenGLContext>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM