Upgrade gluCylinder from QGLWidget to QOpenGLWidget
To upgrade the 'gluCylinder' function from 'QGLWidget' to 'QOpenGLWidget', you can use the modern OpenGL functions provided by the 'QOpenGLWidget' class. Here's an example of how you can create a cylinder using 'QOpenGLWidget':
- Subclass 'QOpenGLWidget':
class MyOpenGLWidget : public QOpenGLWidget
{
//...
};
- Implement the 'initializeGL' function to set up the OpenGL context:
void MyOpenGLWidget::initializeGL()
{
// Set the clear color and enable depth testing
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
}
- Implement the 'paintGL' function to render the cylinder:
void MyOpenGLWidget::paintGL()
{
// Clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up the camera
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, width() / height(), 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// Render the cylinder
glColor3f(1.0, 1.0, 1.0);
GLUquadric* quadric = gluNewQuadric();
gluCylinder(quadric, 1.0, 1.0, 2.0, 32, 32);
gluDeleteQuadric(quadric);
}
- Finally, create an instance of your 'MyOpenGLWidget' class and set it as the central widget of your main window:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow mainWindow;
MyOpenGLWidget* glWidget = new MyOpenGLWidget(&mainWindow);
mainWindow.setCentralWidget(glWidget);
mainWindow.show();
return app.exec();
}
Note that the 'gluCylinder' function is part of the legacy OpenGL utility library (GLU). To use it with 'QOpenGLWidget', you need to include the 'glu.h' header and link against the GLU library.
原文地址: https://www.cveoy.top/t/topic/pjYV 著作权归作者所有。请勿转载和采集!