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.6KB

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