選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

openglrenderdevice.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(int x, int y)
  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(x, y, 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. glEnable(GL_DEPTH_TEST);
  89. // glDisable(GL_BLEND);
  90. // glDisable(GL_POLYGON_SMOOTH);
  91. // glEnable(GL_TEXTURE_2D);
  92. // glColorMaterial(GL_FRONT,GL_SPECULAR);
  93. // glEnable(GL_COLOR_MATERIAL);
  94. // glEnable(GL_CULL_FACE);
  95. glMatrixMode(GL_PROJECTION);
  96. gluPerspective(_fov, _width / _height, 0.1, 100.0);
  97. glMatrixMode(GL_MODELVIEW);
  98. }
  99. void OpenGLRenderDevice::resize(int width, int height)
  100. {
  101. _width = width;
  102. _height = height;
  103. // glViewport(0, 0, _width, _height);
  104. int side = qMin(_width, _height);
  105. glViewport((_width - side) / 2, (_height - side) / 2, side, side);
  106. }
  107. void OpenGLRenderDevice::preDraw()
  108. {
  109. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  110. glLoadIdentity();
  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. }
  118. void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)
  119. {
  120. // GLfloat d[] = { point.getColor().redF(), point.getColor().greenF(), point.getColor().blueF(), point.getColor().alphaF() };
  121. // glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, d);
  122. glColor4f(point.getColor().redF(), point.getColor().greenF(),
  123. point.getColor().blue(), point.getColor().alpha());
  124. glVertex3d(point.getX(), point.getY(), point.getZ());
  125. }
  126. void OpenGLRenderDevice::drawPoint(const ColorVector3D &point)
  127. {
  128. glBegin(GL_POINTS);
  129. drawVertex(point);
  130. glEnd();
  131. }
  132. void OpenGLRenderDevice::drawLine(const ColorVector3D &begin, const ColorVector3D &end, double width)
  133. {
  134. glLineWidth(width);
  135. glBegin(GL_LINES);
  136. drawVertex(begin);
  137. drawVertex(end);
  138. glEnd();
  139. }
  140. void OpenGLRenderDevice::drawPolygon(const QList<ColorVector3D> &points)
  141. {
  142. glBegin(GL_POLYGON);
  143. Vector3D p1 = points[0];
  144. // Vector3D p2 = points[1];
  145. // Vector3D n = p1.crossProduct(p2);
  146. // Vector3D n2 = p2.crossProduct(p1);
  147. // qDebug() << n.getX() << n.getY() << n.getZ() << n2.getX() << n2.getY() << n2.getZ();
  148. // glNormal3d(n.getX(), n.getY(), n.getZ());
  149. for (int i = 0; i < points.size(); ++i) {
  150. drawVertex(points[i]);
  151. }
  152. glEnd();
  153. }
  154. void OpenGLRenderDevice::drawPolygonTexture(const QList<TextureVector3D> &points, const QVariant &textureId)
  155. {
  156. const OpenGLTextureData& data = _textures[textureId];
  157. glBindTexture(GL_TEXTURE_2D, data.id);
  158. glBegin(GL_POLYGON);
  159. for (int i = 0; i < points.size(); ++i) {
  160. TextureVector3D p = points[i];
  161. glTexCoord2d(p.getTextureCoord().getX(), p.getTextureCoord().getY());
  162. drawVertex(p);
  163. }
  164. glEnd();
  165. }