分享

QT 使用OpenGL 进行 3D 绘图

 大傻子的文渊阁 2022-11-25 发布于浙江

目录

一、成果展示:

二、代码展示:

1、main.cpp

2、MyGLWidget.h

3、MyGLWidget.cpp

三、编译错误 解析:

1、gluPerspective 和 gluLookAt 找不到标识符

2、error: LNK2019: 无法解析的外部符号 __imp_glBegin等


一、成果展示:

二、代码展示:

1、main.cpp

  1. #include <QtWidgets/QApplication>
  2. #include "myglwidget.h"
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. MyGLWidget w;
  7. w.resize(400, 300);
  8. w.show();
  9. return a.exec();
  10. }

2、MyGLWidget.h

  1. #ifndef MYGLWIDGET_H
  2. #define MYGLWIDGET_H
  3. #include <QGLWidget>
  4. #include <GL/GLU.h>
  5. class MyGLWidget : public QGLWidget
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit MyGLWidget(QWidget *parent = 0);
  10. protected:
  11. void initializeGL();
  12. void resizeGL(int w, int h);
  13. void paintGL();
  14. void keyPressEvent(QKeyEvent *);
  15. void paintEvent(QPaintEvent *);
  16. void setupViewport(int w, int h);
  17. private:
  18. GLfloat translate, xRot, yRot, zRot;
  19. GLuint textures[3];
  20. signals:
  21. public slots:
  22. };
  23. #endif // MYGLWIDGET_H

3、MyGLWidget.cpp

  1. #include "myglwidget.h"
  2. #include <QKeyEvent>
  3. MyGLWidget::MyGLWidget(QWidget *parent) :
  4. QGLWidget(parent)
  5. {
  6. translate = -6.0;
  7. xRot = yRot = zRot = 0.0;
  8. // 关闭自动填充背景
  9. setAutoFillBackground(false);
  10. }
  11. // 初始化环境
  12. void MyGLWidget::initializeGL()
  13. {
  14. }
  15. // 调整视口大小
  16. void MyGLWidget::resizeGL(int w, int h)
  17. {
  18. setupViewport(w, h);
  19. }
  20. // 渲染场景
  21. void MyGLWidget::paintGL()
  22. {
  23. }
  24. void MyGLWidget::keyPressEvent(QKeyEvent *event)
  25. {
  26. switch (event->key())
  27. {
  28. case Qt::Key_Up :
  29. xRot += 10;
  30. break;
  31. case Qt::Key_Left :
  32. yRot += 10;
  33. break;
  34. case Qt::Key_Right :
  35. zRot += 10;
  36. break;
  37. case Qt::Key_Down :
  38. translate -= 1;
  39. if (translate <= -20)
  40. translate = -1;
  41. break;
  42. }
  43. //updateGL();
  44. update();
  45. QGLWidget::keyPressEvent(event);
  46. }
  47. void MyGLWidget::paintEvent(QPaintEvent *)
  48. {
  49. // 在当前窗口中进行OpenGL的绘制
  50. makeCurrent();
  51. // 将模型视图矩阵压入堆栈
  52. glMatrixMode(GL_MODELVIEW);
  53. glPushMatrix();
  54. // 以下是以前initializeGL()函数中的全部内容
  55. glClearColor(0.0, 0.0, 0.0, 0.0);
  56. glShadeModel(GL_SMOOTH);
  57. glClearDepth(1.0);
  58. glEnable(GL_DEPTH_TEST);
  59. textures[0] = bindTexture(QPixmap(":/LHCGraphicsView/Resources/side1.png"));
  60. textures[1] = bindTexture(QPixmap(":/LHCGraphicsView/Resources/side2.png"));
  61. textures[2] = bindTexture(QPixmap(":/LHCGraphicsView/Resources/side3.png"));
  62. glEnable(GL_TEXTURE_2D);
  63. // 该函数中是以前resizeGL()函数中的全部内容
  64. setupViewport(width(), height());
  65. // 以下是以前paintGL()函数中的全部内容
  66. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  67. glLoadIdentity();
  68. glTranslatef(0.0, 0.0, translate);
  69. glRotatef(xRot, 1.0, 0.0, 0.0);
  70. glRotatef(yRot, 0.0, 1.0, 0.0);
  71. glRotatef(zRot, 0.0, 0.0, 1.0);
  72. glBindTexture(GL_TEXTURE_2D, textures[2]);
  73. glBegin(GL_QUADS);
  74. glTexCoord2f(1.0, 0.0);
  75. glVertex3f(1.0, 1.0, 1.0);
  76. glTexCoord2f(1.0, 1.0);
  77. glVertex3f(1.0, 1.0, -1.0);
  78. glTexCoord2f(0.0, 1.0);
  79. glVertex3f(-1.0, 1.0, -1.0);
  80. glTexCoord2f(0.0, 0.0);
  81. glVertex3f(-1.0, 1.0, 1.0);
  82. glEnd();
  83. glBindTexture(GL_TEXTURE_2D, textures[1]);
  84. glBegin(GL_QUADS);
  85. glTexCoord2f(1.0, 0.0);
  86. glVertex3f(1.0, -1.0, 1.0);
  87. glTexCoord2f(1.0, 1.0);
  88. glVertex3f(1.0, -1.0, -1.0);
  89. glTexCoord2f(0.0, 1.0);
  90. glVertex3f(-1.0, -1.0, -1.0);
  91. glTexCoord2f(0.0, 0.0);
  92. glVertex3f(-1.0, -1.0, 1.0);
  93. glEnd();
  94. glBindTexture(GL_TEXTURE_2D, textures[0]);
  95. glBegin(GL_QUADS);
  96. glTexCoord2f(1.0, 1.0);
  97. glVertex3f(1.0, 1.0, 1.0);
  98. glTexCoord2f(0.0, 1.0);
  99. glVertex3f(-1.0, 1.0, 1.0);
  100. glTexCoord2f(0.0, 0.0);
  101. glVertex3f(-1.0, -1.0, 1.0);
  102. glTexCoord2f(1.0, 0.0);
  103. glVertex3f(1.0, -1.0, 1.0);
  104. glEnd();
  105. // 关闭启用的功能并弹出模型视图矩阵
  106. glDisable(GL_DEPTH_TEST);
  107. glDisable(GL_TEXTURE_2D);
  108. glMatrixMode(GL_MODELVIEW);
  109. glPopMatrix();
  110. // 下面是2D绘图的内容
  111. QPainter painter(this);
  112. painter.setRenderHint(QPainter::Antialiasing);
  113. painter.setBrush(Qt::red);
  114. painter.drawRect(0, 0, 100, 100);
  115. painter.end();
  116. }
  117. void MyGLWidget::setupViewport(int w, int h)
  118. {
  119. glViewport(0, 0, (GLint)w, (GLint)h);
  120. glMatrixMode(GL_PROJECTION);
  121. glLoadIdentity();
  122. gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
  123. glMatrixMode(GL_MODELVIEW);
  124. glLoadIdentity();
  125. }

4、图片资源

三、编译错误 解析:

1、gluPerspective 和 gluLookAt 找不到标识符

请添加头文件 #include <GL/glu.h>

且,必须要加在 #include <QGLWidget> 的后面

2、error: LNK2019: 无法解析的外部符号 __imp_glBegin等

我用的 vs+qt 开发

链接器 -- 常规 -- 附加库目录--添加 D:\Windows Kits\10\Lib\10.0.17763.0\um\x64 (vs其实已经继承了该路径。个人根据自己的windows kits路径来找)

链接器 -- 输入 -- 附加依赖项 : OpenGL32.Lib; GlU32.lib

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多