Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

openglrenderdevice.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, 0.0, 0.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, _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. int side = qMin(_width, _height);
  104. glViewport((_width - side) / 2, (_height - side) / 2, side, side);
  105. }
  106. void OpenGLRenderDevice::preDraw()
  107. {
  108. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109. glLoadIdentity();
  110. glEnable(GL_DEPTH_TEST);
  111. gluLookAt(_lookEye.getX(), _lookEye.getY(), _lookEye.getZ(),
  112. _lookCenter.getX(), _lookCenter.getY(), _lookCenter.getZ(),
  113. _lookUp.getX(), _lookUp.getY(), _lookUp.getZ());
  114. }
  115. void OpenGLRenderDevice::postDraw()
  116. {
  117. glDisable(GL_DEPTH_TEST);
  118. }
  119. void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)
  120. {
  121. // GLfloat d[] = { point.getColor().redF(), point.getColor().greenF(), point.getColor().blueF(), point.getColor().alphaF() };
  122. // glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, d);
  123. glColor4f(point.getColor().redF(), point.getColor().greenF(),
  124. point.getColor().blue(), point.getColor().alpha());
  125. glVertex3d(point.getX(), point.getY(), point.getZ());
  126. }
  127. void OpenGLRenderDevice::drawPoint(const ColorVector3D &point)
  128. {
  129. glBegin(GL_POINTS);
  130. drawVertex(point);
  131. glEnd();
  132. }
  133. void OpenGLRenderDevice::drawLine(const ColorVector3D &begin, const ColorVector3D &end, double width)
  134. {
  135. glLineWidth(width);
  136. glBegin(GL_LINES);
  137. drawVertex(begin);
  138. drawVertex(end);
  139. glEnd();
  140. }
  141. void OpenGLRenderDevice::drawPolygon(const QList<ColorVector3D> &points)
  142. {
  143. glBegin(GL_POLYGON);
  144. // Vector3D p1 = points[0];
  145. // Vector3D p2 = points[1];
  146. // Vector3D n = p1.crossProduct(p2);
  147. // Vector3D n2 = p2.crossProduct(p1);
  148. // qDebug() << n.getX() << n.getY() << n.getZ() << n2.getX() << n2.getY() << n2.getZ();
  149. // glNormal3d(n.getX(), n.getY(), n.getZ());
  150. for (int i = 0; i < points.size(); ++i) {
  151. drawVertex(points[i]);
  152. }
  153. glEnd();
  154. }
  155. void OpenGLRenderDevice::drawPolygonTexture(const QList<TextureVector3D> &points, const QVariant &textureId)
  156. {
  157. const OpenGLTextureData& data = _textures[textureId];
  158. glBindTexture(GL_TEXTURE_2D, data.id);
  159. glBegin(GL_POLYGON);
  160. for (int i = 0; i < points.size(); ++i) {
  161. const TextureVector3D& p = points[i];
  162. const Vector2D& coord = p.getTextureCoord();
  163. glTexCoord2d(coord.getX(), coord.getY());
  164. drawVertex(p);
  165. }
  166. glEnd();
  167. }