C++ 我可以使用qt中的OpenGL功能而不使用任何类型的小部件吗?
我正在尝试使用openGL计算着色器在gpu上计算一些东西。 但我想把这个程序保存在终端中,所以我不想要任何类型的小部件 现在看来,如果没有分段错误,我将无法调用InitializePenglFunctions或任何其他ogl相关函数 标题C++ 我可以使用qt中的OpenGL功能而不使用任何类型的小部件吗?,c++,qt,opengl,C++,Qt,Opengl,我正在尝试使用openGL计算着色器在gpu上计算一些东西。 但我想把这个程序保存在终端中,所以我不想要任何类型的小部件 现在看来,如果没有分段错误,我将无法调用InitializePenglFunctions或任何其他ogl相关函数 标题 #include <QOpenGLShaderProgram> #include <QOpenGLFunctions_4_5_Core> class SolverGPU : public QObject,
#include <QOpenGLShaderProgram>
#include <QOpenGLFunctions_4_5_Core>
class SolverGPU : public QObject,
protected QOpenGLFunctions_4_5_Core
{
Q_OBJECT
public:
SolverGPU();
private:
QOpenGLShaderProgram* program;
};
我已经尝试过使用QOpenGLFunctions而不是QOpenGLFunctions\u 4\u 5\u Core,但没有成功。下面的代码适合我。我现在使用的盒子不支持OpenGL4.5,因此“降级”到3.3。原理是一样的
#include <cstdlib>
#include <iostream>
#include <QApplication>
#include <QOffscreenSurface>
#include <QOpenGLContext>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
int main (int argc, char **argv)
{
try {
QApplication app(argc, argv);
QSurfaceFormat format;
format.setMajorVersion(3);
format.setMinorVersion(3);
format.setProfile(QSurfaceFormat::CoreProfile);
QOpenGLContext gl_ctx;
gl_ctx.setFormat(format);
if (!gl_ctx.create())
throw std::runtime_error("context creation failed");
QOffscreenSurface surface;
surface.create();
gl_ctx.makeCurrent(&surface);
QOpenGLFunctions_3_3_Core opengl_functions;
if (!opengl_functions.initializeOpenGLFunctions())
throw std::runtime_error("initialization failed");
QOpenGLShader vertex_shader(QOpenGLShader::Vertex);
if (!vertex_shader.compileSourceCode(
"#version 330 core\n"
"\n"
"void main ()\n"
"{\n"
" gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n"
"}\n"
)) {
throw std::runtime_error("vertex shader compilaton failed");
}
QOpenGLShader fragment_shader(QOpenGLShader::Fragment);
if (!fragment_shader.compileSourceCode(
"#version 330 core\n"
"\n"
"layout(location = 0) out vec4 colour;\n"
"\n"
"void main ()\n"
"{\n"
" colour = vec4(0.0, 0.0, 0.0, 1.0);\n"
"}\n"
)) {
throw std::runtime_error("fragment shader compilaton failed");
}
QOpenGLShaderProgram program;
if (!program.addShader(&vertex_shader))
throw std::runtime_error("failed to add vertex shader to program");
if (!program.addShader(&fragment_shader))
throw std::runtime_error("failed to add fragment shader to program");
if (!program.link())
throw std::runtime_error("failed to link failed");
if (!program.bind())
throw std::runtime_error("glUseProgram failed");
app.exec();
}
catch (std::exception &ex) {
std::clog << "\n" << ex.what();
}
catch (...) {
std::clog << "\nunrecognized exception";
}
exit(0);
}
我只执行了最低限度的测试,但显示的代码创建了一个合适的上下文,初始化了3.30核心函数集,并成功编译和链接了着色器程序。调用QOpenGLFunctions_4_5_core::InitializePenglFunctions时,您可能需要一个有效的电流。或者,使用所需规范创建QOpenGLContext并使用.QOpenGLContext::makeCurrent需要QSurface作为参数。调用QOpenGLContext::函数需要上下文是最新的。好的,我几乎可以肯定的是,使用Qt包装器是没有办法做到这一点的。谢谢你回答我的第一个问题!是的,就这样!多谢各位
#include <cstdlib>
#include <iostream>
#include <QApplication>
#include <QOffscreenSurface>
#include <QOpenGLContext>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
int main (int argc, char **argv)
{
try {
QApplication app(argc, argv);
QSurfaceFormat format;
format.setMajorVersion(3);
format.setMinorVersion(3);
format.setProfile(QSurfaceFormat::CoreProfile);
QOpenGLContext gl_ctx;
gl_ctx.setFormat(format);
if (!gl_ctx.create())
throw std::runtime_error("context creation failed");
QOffscreenSurface surface;
surface.create();
gl_ctx.makeCurrent(&surface);
QOpenGLFunctions_3_3_Core opengl_functions;
if (!opengl_functions.initializeOpenGLFunctions())
throw std::runtime_error("initialization failed");
QOpenGLShader vertex_shader(QOpenGLShader::Vertex);
if (!vertex_shader.compileSourceCode(
"#version 330 core\n"
"\n"
"void main ()\n"
"{\n"
" gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n"
"}\n"
)) {
throw std::runtime_error("vertex shader compilaton failed");
}
QOpenGLShader fragment_shader(QOpenGLShader::Fragment);
if (!fragment_shader.compileSourceCode(
"#version 330 core\n"
"\n"
"layout(location = 0) out vec4 colour;\n"
"\n"
"void main ()\n"
"{\n"
" colour = vec4(0.0, 0.0, 0.0, 1.0);\n"
"}\n"
)) {
throw std::runtime_error("fragment shader compilaton failed");
}
QOpenGLShaderProgram program;
if (!program.addShader(&vertex_shader))
throw std::runtime_error("failed to add vertex shader to program");
if (!program.addShader(&fragment_shader))
throw std::runtime_error("failed to add fragment shader to program");
if (!program.link())
throw std::runtime_error("failed to link failed");
if (!program.bind())
throw std::runtime_error("glUseProgram failed");
app.exec();
}
catch (std::exception &ex) {
std::clog << "\n" << ex.what();
}
catch (...) {
std::clog << "\nunrecognized exception";
}
exit(0);
}