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.

220 lines
6.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the examples of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** You may use this file under the terms of the BSD license as follows:
  10. **
  11. ** "Redistribution and use in source and binary forms, with or without
  12. ** modification, are permitted provided that the following conditions are
  13. ** met:
  14. ** * Redistributions of source code must retain the above copyright
  15. ** notice, this list of conditions and the following disclaimer.
  16. ** * Redistributions in binary form must reproduce the above copyright
  17. ** notice, this list of conditions and the following disclaimer in
  18. ** the documentation and/or other materials provided with the
  19. ** distribution.
  20. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
  21. ** of its contributors may be used to endorse or promote products derived
  22. ** from this software without specific prior written permission.
  23. **
  24. **
  25. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. #include <vector>
  41. #include <windows.h>
  42. #include <gl/GL.h>
  43. #include <gl/GLU.h>
  44. #include "../guiClass.h"
  45. #pragma comment(lib, "opengl32.lib")
  46. #pragma comment(lib, "glu32.lib")
  47. #include <QtWidgets/QtWidgets>
  48. #include <QtOpenGL/QtOpenGL>
  49. #include "glwidget.h"
  50. GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget): QGLWidget(parent, shareWidget){
  51. clearColor = Qt::black;
  52. xRot = 0;
  53. yRot = 0;
  54. zRot = 0;
  55. program = 0;
  56. };
  57. GLWidget::~GLWidget()
  58. {
  59. }
  60. QSize GLWidget::minimumSizeHint() const
  61. {
  62. return QSize(50, 50);
  63. }
  64. QSize GLWidget::sizeHint() const
  65. {
  66. return QSize(200, 200);
  67. }
  68. void GLWidget::rotateBy(int xAngle, int yAngle, int zAngle)
  69. {
  70. xRot += xAngle;
  71. yRot += yAngle;
  72. zRot += zAngle;
  73. updateGL();
  74. }
  75. void GLWidget::setClearColor(const QColor &color)
  76. {
  77. clearColor = color;
  78. updateGL();
  79. }
  80. void GLWidget::glInit(){
  81. initializeOpenGLFunctions();
  82. makeObject();
  83. glEnable(GL_DEPTH_TEST);
  84. glEnable(GL_CULL_FACE);
  85. #ifdef GL_TEXTURE_2D
  86. glEnable(GL_TEXTURE_2D);
  87. #endif
  88. #define PROGRAM_VERTEX_ATTRIBUTE 0
  89. #define PROGRAM_TEXCOORD_ATTRIBUTE 1
  90. QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
  91. const char *vsrc =
  92. "attribute highp vec4 vertex;\n"
  93. "attribute mediump vec4 texCoord;\n"
  94. "varying mediump vec4 texc;\n"
  95. "uniform mediump mat4 matrix;\n"
  96. "void main(void)\n"
  97. "{\n"
  98. " gl_Position = matrix * vertex;\n"
  99. " texc = texCoord;\n"
  100. "}\n";
  101. vshader->compileSourceCode(vsrc);
  102. QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
  103. const char *fsrc =
  104. "uniform sampler2D texture;\n"
  105. "varying mediump vec4 texc;\n"
  106. "void main(void)\n"
  107. "{\n"
  108. " gl_FragColor = texture2D(texture, texc.st);\n"
  109. "}\n";
  110. fshader->compileSourceCode(fsrc);
  111. program = new QGLShaderProgram(this);
  112. program->addShader(vshader);
  113. program->addShader(fshader);
  114. program->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE);
  115. program->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE);
  116. program->link();
  117. program->bind();
  118. program->setUniformValue("texture", 0);
  119. }
  120. void GLWidget::paintGL()
  121. {
  122. qglClearColor(clearColor);
  123. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  124. QMatrix4x4 m;
  125. m.ortho(-0.5f, +0.5f, +0.5f, -0.5f, 4.0f, 15.0f);
  126. m.translate(0.0f, 0.0f, -10.0f);
  127. m.rotate(xRot / 16.0f, 1.0f, 0.0f, 0.0f);
  128. m.rotate(yRot / 16.0f, 0.0f, 1.0f, 0.0f);
  129. m.rotate(zRot / 16.0f, 0.0f, 0.0f, 1.0f);
  130. program->setUniformValue("matrix", m);
  131. program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
  132. program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
  133. program->setAttributeArray
  134. (PROGRAM_VERTEX_ATTRIBUTE, vertices.constData());
  135. program->setAttributeArray
  136. (PROGRAM_TEXCOORD_ATTRIBUTE, texCoords.constData());
  137. for (int i = 0; i < 6; ++i) {
  138. glBindTexture(GL_TEXTURE_2D, textures[i]);
  139. glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4);
  140. }
  141. }
  142. void GLWidget::resizeGL(int width, int height)
  143. {
  144. int side = qMin(width, height);
  145. glViewport((width - side) / 2, (height - side) / 2, side, side);
  146. }
  147. void GLWidget::mousePressEvent(QMouseEvent *event)
  148. {
  149. lastPos = event->pos();
  150. }
  151. void GLWidget::mouseMoveEvent(QMouseEvent *event)
  152. {
  153. int dx = event->x() - lastPos.x();
  154. int dy = event->y() - lastPos.y();
  155. if (event->buttons() & Qt::LeftButton) {
  156. rotateBy(8 * dy, 8 * dx, 0);
  157. } else if (event->buttons() & Qt::RightButton) {
  158. rotateBy(8 * dy, 0, 8 * dx);
  159. }
  160. lastPos = event->pos();
  161. }
  162. void GLWidget::mouseReleaseEvent(QMouseEvent * /* event */){
  163. emit clicked();
  164. };
  165. void GLWidget::clicked(){
  166. };
  167. void GLWidget::makeObject()
  168. {
  169. static const int coords[6][4][3] = {
  170. { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
  171. { { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
  172. { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
  173. { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
  174. { { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
  175. { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
  176. };
  177. for (int j=0; j < 6; ++j) {
  178. textures[j] = bindTexture
  179. (QPixmap(QString(":/images/side%1.png").arg(j + 1)), GL_TEXTURE_2D);
  180. }
  181. for (int i = 0; i < 6; ++i) {
  182. for (int j = 0; j < 4; ++j) {
  183. texCoords.append
  184. (QVector2D(j == 0 || j == 3, j == 0 || j == 1));
  185. vertices.append
  186. (QVector3D(0.2 * coords[i][j][0], 0.2 * coords[i][j][1],
  187. 0.2 * coords[i][j][2]));
  188. }
  189. }
  190. }