Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

openglrenderdevice.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */
  43. // GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */
  44. // glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  45. // glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  46. // glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, light_diffuse);
  47. // glEnable(GL_LIGHT0);
  48. // glEnable(GL_LIGHTING);
  49. glDisable(GL_LIGHTING);
  50. glEnable(GL_DEPTH_TEST);
  51. glMatrixMode(GL_PROJECTION);
  52. gluPerspective(_fov, _width / _height, 0.1, 100.0);
  53. glMatrixMode(GL_MODELVIEW);
  54. }
  55. void OpenGLRenderDevice::resize(int width, int height)
  56. {
  57. _width = width;
  58. _height = height;
  59. int side = qMin(_width, _height);
  60. glViewport((_width - side) / 2, (_height - side) / 2, side, side);
  61. }
  62. void OpenGLRenderDevice::preDraw()
  63. {
  64. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  65. glLoadIdentity();
  66. gluLookAt(_lookEye.getX(), _lookEye.getY(), _lookEye.getZ(),
  67. _lookCenter.getX(), _lookCenter.getY(), _lookCenter.getZ(),
  68. _lookUp.getX(), _lookUp.getY(), _lookUp.getZ());
  69. }
  70. void OpenGLRenderDevice::postDraw()
  71. {
  72. }
  73. void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)
  74. {
  75. // GLfloat d[] = { point.getColor().redF(), point.getColor().greenF(), point.getColor().blueF(), point.getColor().alphaF() };
  76. // glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, d);
  77. glColor4f(point.getColor().redF(), point.getColor().greenF(),
  78. point.getColor().blue(), point.getColor().alpha());
  79. glVertex3d(point.getX(), point.getY(), point.getZ());
  80. }
  81. void OpenGLRenderDevice::drawPoint(const ColorVector3D &point)
  82. {
  83. glBegin(GL_POINTS);
  84. drawVertex(point);
  85. glEnd();
  86. }
  87. void OpenGLRenderDevice::drawLine(const ColorVector3D &begin, const ColorVector3D &end, double width)
  88. {
  89. glLineWidth(width);
  90. glBegin(GL_LINES);
  91. drawVertex(begin);
  92. drawVertex(end);
  93. glEnd();
  94. }
  95. void OpenGLRenderDevice::drawPolygon(const QList<ColorVector3D> &points)
  96. {
  97. glBegin(GL_POLYGON);
  98. for (int i = 0; i < points.size(); ++i) {
  99. drawVertex(points[i]);
  100. }
  101. glEnd();
  102. }