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 5.9KB

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