您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

openglrenderdevice.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "openglrenderdevice.h"
  2. #include <QRgb>
  3. #include "GL/gl.h"
  4. #include "GL/glu.h"
  5. OpenGLRenderDevice::OpenGLRenderDevice(QObject *parent)
  6. : AbstractRenderDevice(parent)
  7. , _width(0)
  8. , _height(0)
  9. , _fov(0)
  10. {
  11. }
  12. Vector3D OpenGLRenderDevice::get2DFrom3D(const Vector3D &pos)
  13. {
  14. double gx, gy, gz;
  15. double modelMatrix[16];
  16. double projMatrix[16];
  17. GLint viewport[4];
  18. glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  19. glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  20. glGetIntegerv(GL_VIEWPORT, viewport);
  21. gluProject(pos.getX(), pos.getY(), pos.getZ(), modelMatrix, projMatrix, viewport, &gx, &gy, &gz);
  22. return Vector3D(gx, gy, gz);
  23. }
  24. Vector3D OpenGLRenderDevice::get3DFrom2D(int x, int y)
  25. {
  26. double gx, gy, gz;
  27. double modelMatrix[16];
  28. double projMatrix[16];
  29. GLint viewport[4];
  30. glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  31. glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  32. glGetIntegerv(GL_VIEWPORT, viewport);
  33. gluUnProject(x, y, 1, modelMatrix, projMatrix, viewport, &gx, &gy, &gz);
  34. return Vector3D(gx, gy, gz);
  35. }
  36. void OpenGLRenderDevice::loadTexture(const QVariant &id, const QImage &texture)
  37. {
  38. OpenGLTextureData data;
  39. data.id = 0;
  40. data.image = texture;
  41. char rawData[texture.width() * texture.height() * 4];
  42. for (int y = 0; y < texture.height(); ++y) {
  43. for(int x = 0; x < texture.width(); ++x) {
  44. int p = (y * texture.height() * 4) + x * 4;
  45. QColor px = QColor(texture.pixel(x, y));
  46. rawData[p] = px.red();
  47. rawData[p + 1] = px.green();
  48. rawData[p + 2] = px.blue();
  49. rawData[p + 3] = 255;
  50. }
  51. }
  52. glGenTextures(1, &data.id);
  53. glBindTexture(GL_TEXTURE_2D, data.id);
  54. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width(), texture.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, rawData);
  55. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  56. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  57. _textures.insert(id, data);
  58. }
  59. void OpenGLRenderDevice::initialize(int fov, int width, int height)
  60. {
  61. _fov = fov;
  62. _width = width;
  63. _height = height;
  64. glClearColor(_clearColor.redF(), _clearColor.greenF(),
  65. _clearColor.blueF(), _clearColor.alphaF());
  66. // glShadeModel(GL_SMOOTH);
  67. // glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
  68. // glColorMaterial(GL_FRONT,GL_SPECULAR);
  69. // glCullFace(GL_BACK);
  70. // glEnable(GL_LIGHTING);
  71. // glEnable(GL_LIGHT0);
  72. // glEnable(GL_COLOR_MATERIAL);
  73. // glEnable(GL_CULL_FACE);
  74. glEnable(GL_TEXTURE_2D);
  75. // glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  76. // GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */
  77. // GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */
  78. // glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  79. // glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  80. // glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, light_diffuse);
  81. // glEnable(GL_LIGHT0);
  82. // glEnable(GL_LIGHTING);
  83. glEnable(GL_DEPTH_TEST);
  84. // glDisable(GL_BLEND);
  85. // glDisable(GL_POLYGON_SMOOTH);
  86. // glEnable(GL_TEXTURE_2D);
  87. // glColorMaterial(GL_FRONT,GL_SPECULAR);
  88. // glEnable(GL_COLOR_MATERIAL);
  89. // glEnable(GL_CULL_FACE);
  90. glMatrixMode(GL_PROJECTION);
  91. gluPerspective(_fov, _width / _height, 0.1, 100.0);
  92. glMatrixMode(GL_MODELVIEW);
  93. }
  94. void OpenGLRenderDevice::resize(int width, int height)
  95. {
  96. _width = width;
  97. _height = height;
  98. int side = qMin(_width, _height);
  99. glViewport((_width - side) / 2, (_height - side) / 2, side, side);
  100. }
  101. void OpenGLRenderDevice::preDraw()
  102. {
  103. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  104. glLoadIdentity();
  105. gluLookAt(_lookEye.getX(), _lookEye.getY(), _lookEye.getZ(),
  106. _lookCenter.getX(), _lookCenter.getY(), _lookCenter.getZ(),
  107. _lookUp.getX(), _lookUp.getY(), _lookUp.getZ());
  108. }
  109. void OpenGLRenderDevice::postDraw()
  110. {
  111. }
  112. void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)
  113. {
  114. // GLfloat d[] = { point.getColor().redF(), point.getColor().greenF(), point.getColor().blueF(), point.getColor().alphaF() };
  115. // glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, d);
  116. glColor4f(point.getColor().redF(), point.getColor().greenF(),
  117. point.getColor().blue(), point.getColor().alpha());
  118. glVertex3d(point.getX(), point.getY(), point.getZ());
  119. }
  120. void OpenGLRenderDevice::drawPoint(const ColorVector3D &point)
  121. {
  122. glBegin(GL_POINTS);
  123. drawVertex(point);
  124. glEnd();
  125. }
  126. void OpenGLRenderDevice::drawLine(const ColorVector3D &begin, const ColorVector3D &end, double width)
  127. {
  128. glLineWidth(width);
  129. glBegin(GL_LINES);
  130. drawVertex(begin);
  131. drawVertex(end);
  132. glEnd();
  133. }
  134. void OpenGLRenderDevice::drawPolygon(const QList<ColorVector3D> &points)
  135. {
  136. glBegin(GL_POLYGON);
  137. for (int i = 0; i < points.size(); ++i) {
  138. drawVertex(points[i]);
  139. }
  140. glEnd();
  141. }
  142. void OpenGLRenderDevice::drawPolygonTexture(const QList<TextureVector3D> &points, const QVariant &textureId)
  143. {
  144. const OpenGLTextureData& data = _textures[textureId];
  145. glBindTexture(GL_TEXTURE_2D, data.id);
  146. glBegin(GL_POLYGON);
  147. for (int i = 0; i < points.size(); ++i) {
  148. TextureVector3D p = points[i];
  149. glTexCoord2d(p.getTextureCoord().getX(), p.getTextureCoord().getY());
  150. drawVertex(p);
  151. }
  152. glEnd();
  153. }