You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

openglrenderdevice.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "openglrenderdevice.h"
  2. #include "GL/gl.h"
  3. #include "GL/glu.h"
  4. OpenGLRenderDevice::OpenGLRenderDevice(QObject *parent)
  5. : AbstractRenderDevice(parent)
  6. , _width(0)
  7. , _height(0)
  8. , _fov(0)
  9. {
  10. }
  11. Vector3D OpenGLRenderDevice::get2DFrom3D(const Vector3D &pos)
  12. {
  13. double gx, gy, gz;
  14. double modelMatrix[16];
  15. double projMatrix[16];
  16. GLint viewport[4];
  17. glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  18. glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  19. glGetIntegerv(GL_VIEWPORT, viewport);
  20. gluProject(pos.getX(), pos.getY(), pos.getZ(), modelMatrix, projMatrix, viewport, &gx, &gy, &gz);
  21. return Vector3D(gx, gy, gz);
  22. }
  23. Vector3D OpenGLRenderDevice::get3DFrom2D(int x, int y)
  24. {
  25. double gx, gy, gz;
  26. double modelMatrix[16];
  27. double projMatrix[16];
  28. GLint viewport[4];
  29. glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  30. glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  31. glGetIntegerv(GL_VIEWPORT, viewport);
  32. gluUnProject(x, y, 1, modelMatrix, projMatrix, viewport, &gx, &gy, &gz);
  33. return Vector3D(gx, gy, gz);
  34. }
  35. void OpenGLRenderDevice::initialize(int fov, int width, int height)
  36. {
  37. _fov = fov;
  38. _width = width;
  39. _height = height;
  40. glClearColor(_clearColor.redF(), _clearColor.greenF(),
  41. _clearColor.blueF(), _clearColor.alphaF());
  42. glEnable(GL_DEPTH_TEST);
  43. glEnable(GL_CULL_FACE);
  44. glShadeModel(GL_SMOOTH);
  45. glEnable(GL_MULTISAMPLE);
  46. glMatrixMode(GL_PROJECTION);
  47. gluPerspective(_fov, _width / _height, 0.1, 100.0);
  48. glMatrixMode(GL_MODELVIEW);
  49. }
  50. void OpenGLRenderDevice::resize(int width, int height)
  51. {
  52. _width = width;
  53. _height = height;
  54. int side = qMin(_width, _height);
  55. glViewport((_width - side) / 2, (_height - side) / 2, side, side);
  56. }
  57. void OpenGLRenderDevice::preDraw()
  58. {
  59. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  60. glLoadIdentity();
  61. gluLookAt(_lookEye.getX(), _lookEye.getY(), _lookEye.getZ(),
  62. _lookCenter.getX(), _lookCenter.getY(), _lookCenter.getZ(),
  63. _lookUp.getX(), _lookUp.getY(), _lookUp.getZ());
  64. }
  65. void OpenGLRenderDevice::postDraw()
  66. {
  67. }
  68. void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)
  69. {
  70. glColor4f(point.getColor().redF(), point.getColor().greenF(),
  71. point.getColor().blue(), point.getColor().alpha());
  72. glVertex3d(point.getX(), point.getY(), point.getZ());
  73. }
  74. void OpenGLRenderDevice::drawPoint(const ColorVector3D &point)
  75. {
  76. glBegin(GL_POINTS);
  77. drawVertex(point);
  78. glEnd();
  79. }
  80. void OpenGLRenderDevice::drawLine(const ColorVector3D &begin, const ColorVector3D &end, double width)
  81. {
  82. glLineWidth(width);
  83. glBegin(GL_LINES);
  84. drawVertex(begin);
  85. drawVertex(end);
  86. glEnd();
  87. }
  88. void OpenGLRenderDevice::drawPolygon(const QList<ColorVector3D> &points)
  89. {
  90. glBegin(GL_POLYGON);
  91. for (int i = 0; i < points.size(); ++i) {
  92. drawVertex(points[i]);
  93. }
  94. glEnd();
  95. }