【发布时间】:2021-07-20 04:30:00
【问题描述】:
在 GLFW 中,我们通过此命令定义 Opengl 帧缓冲区的大小。
GLFWwindow* window = glfwCreateWindow(1920, 1080, "Test Window", NULL, NULL);
这会创建一个大小为 1920 X 1080 的 opengl 窗口,并且 Opengl 的默认帧缓冲区也是相同大小。
我们如何在 QT 中做同样的事情
这就是我目前为 opengl 渲染设置参数的方式。
QGLFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setSampleBuffers(true);
format.setSamples(4);
format.setSwapInterval(1);
QGLFormat::setDefaultFormat(format);
///////////////////////////////GlWidget 类 /////// /////////
class GLWidget : public QOpenGLWidget
{
Q_OBJECT;
public:
explicit GLWidget(QWidget *parent = 0);
// ~GLWidget();
void initializeGL() override;
void paintGL() override;
void resizeGL(int w, int h) override;
QTimer timer;
QElapsedTimer elapsedtimer;
private:
int width;
int height;
};
///////////////////////////////////////////////////////////////////////////////////
GLWidget::GLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
}
//////////////////////////////////////////////////////////////////////////////////////////
void GLWidget::initializeGL()
{
GLenum GlewInitResult;
glewExperimental = GL_TRUE;
GlewInitResult = glewInit();
if (GLEW_OK != GlewInitResult) // Check if glew is initialized properly
{
QMessageBox msgBox;
msgBox.setText("Not able to Initialize Glew");
msgBox.exec();
}
}
/////////////////////////////////////// ///////////////////////////////////////p>
void GLWidget::paintGL()
{
// Painting commands here which are opengl commands using GLEW.
glBindFramebuffer(GL_FRAMEBUFFER, 0); // Render in default framebuffer
glClear(GL_COLOR_BUFFER_BIT);
RenderCube();
}
///////////////////////////////////////////////////////////////////////////////////////////
IN the Main function
QApplication application(argc, argv);
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setVersion(3, 2);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
return application.exec();
【问题讨论】:
-
默认的帧缓冲总是窗口大小。
-
@NicolBolas 在这种情况下,它将是被提升为 opegl 类的 Qwidget 的大小?
-
您实际上是如何创建底层上下文的?
QOpenGLWidget? -
@G.M.我已经更新了代码。
-
我仍然不确定我是否理解这个问题,但是......底层帧缓冲区大小通常与
QWidget本身相同,并且会随着小部件的大小调整而相应调整。如果您希望小部件具有特定大小,请尝试调用QWidget::setFixedSize。