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.

514 lines
16 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "OpenGLWidget.h"
  2. #include <QtGui/QPainter>
  3. #include <QtGui/QPaintEngine>
  4. #include <QtGui/QOpenGLShaderProgram>
  5. #include <QtGui/QOpenGLTexture>
  6. #include <QtCore/QCoreApplication>
  7. #include <math.h>
  8. //#include "bubble.h"
  9. const int bubbleNum = 8;
  10. GLWidget::GLWidget(MainWindow *mw, bool button, const QColor &background)
  11. : m_mainWindow(mw),
  12. //m_showBubbles(true),
  13. m_qtLogo(false),
  14. m_frames(0),
  15. m_program1(0),
  16. m_program2(0),
  17. m_texture(0),
  18. m_transparent(false),
  19. m_btn(0),
  20. m_hasButton(button),
  21. m_background(background)
  22. {
  23. setMinimumSize(500, 375);
  24. }
  25. GLWidget::~GLWidget()
  26. {
  27. // qDeleteAll(m_bubbles);
  28. // And now release all OpenGL resources.
  29. makeCurrent();
  30. delete m_texture;
  31. delete m_program1;
  32. delete m_program2;
  33. delete m_vshader1;
  34. delete m_fshader1;
  35. delete m_vshader2;
  36. delete m_fshader2;
  37. m_vbo1.destroy();
  38. m_vbo2.destroy();
  39. doneCurrent();
  40. }
  41. void GLWidget::setScaling(int scale)
  42. {
  43. if (scale > 30)
  44. m_fScale = 1 + qreal(scale - 30) / 30 * 0.25;
  45. else if (scale < 30)
  46. m_fScale = 1 - (qreal(30 - scale) / 30 * 0.25);
  47. else
  48. m_fScale = 1;
  49. }
  50. void GLWidget::setLogo()
  51. {
  52. m_qtLogo = true;
  53. }
  54. void GLWidget::setTexture()
  55. {
  56. m_qtLogo = false;
  57. }
  58. //void GLWidget::setShowBubbles(bool bubbles)
  59. //{
  60. // m_showBubbles = bubbles;
  61. //}
  62. void GLWidget::paintQtLogo()
  63. {
  64. m_program1->enableAttributeArray(m_vertexAttr1);
  65. m_program1->enableAttributeArray(m_normalAttr1);
  66. m_vbo1.bind();
  67. // The data in the buffer is placed like this:
  68. // vertex1.x, vertex1.y, vertex1.z, normal1.x, normal1.y, normal1.z, vertex2.x, ...
  69. m_program1->setAttributeBuffer(m_vertexAttr1, GL_FLOAT, 0, 3, 6 * sizeof(GLfloat));
  70. m_program1->setAttributeBuffer(m_normalAttr1, GL_FLOAT, 3 * sizeof(GLfloat), 3, 6 * sizeof(GLfloat));
  71. m_vbo1.release();
  72. glDrawArrays(GL_TRIANGLES, 0, m_vertices.size());
  73. m_program1->disableAttributeArray(m_normalAttr1);
  74. m_program1->disableAttributeArray(m_vertexAttr1);
  75. }
  76. void GLWidget::paintTexturedCube()
  77. {
  78. m_texture->bind();
  79. if (!m_vbo2.isCreated()) {
  80. static GLfloat afVertices[] = {
  81. -0.5, 0.5, 0.5, 0.5,-0.5,0.5,-0.5,-0.5,0.5,
  82. 0.5, -0.5, 0.5, -0.5,0.5,0.5,0.5,0.5,0.5,
  83. -0.5, -0.5, -0.5, 0.5,-0.5,-0.5,-0.5,0.5,-0.5,
  84. 0.5, 0.5, -0.5, -0.5,0.5,-0.5,0.5,-0.5,-0.5,
  85. 0.5, -0.5, -0.5, 0.5,-0.5,0.5,0.5,0.5,-0.5,
  86. 0.5, 0.5, 0.5, 0.5,0.5,-0.5,0.5,-0.5,0.5,
  87. -0.5, 0.5, -0.5, -0.5,-0.5,0.5,-0.5,-0.5,-0.5,
  88. -0.5, -0.5, 0.5, -0.5,0.5,-0.5,-0.5,0.5,0.5,
  89. 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5,
  90. -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5,
  91. -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5,
  92. 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5
  93. };
  94. static GLfloat afTexCoord[] = {
  95. 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f,
  96. 1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f,
  97. 1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
  98. 0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f,
  99. 1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
  100. 0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f,
  101. 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f,
  102. 1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f,
  103. 0.0f,1.0f, 1.0f,0.0f, 1.0f,1.0f,
  104. 1.0f,0.0f, 0.0f,1.0f, 0.0f,0.0f,
  105. 1.0f,0.0f, 1.0f,1.0f, 0.0f,0.0f,
  106. 0.0f,1.0f, 0.0f,0.0f, 1.0f,1.0f
  107. };
  108. GLfloat afNormals[] = {
  109. 0,0,-1, 0,0,-1, 0,0,-1,
  110. 0,0,-1, 0,0,-1, 0,0,-1,
  111. 0,0,1, 0,0,1, 0,0,1,
  112. 0,0,1, 0,0,1, 0,0,1,
  113. -1,0,0, -1,0,0, -1,0,0,
  114. -1,0,0, -1,0,0, -1,0,0,
  115. 1,0,0, 1,0,0, 1,0,0,
  116. 1,0,0, 1,0,0, 1,0,0,
  117. 0,-1,0, 0,-1,0, 0,-1,0,
  118. 0,-1,0, 0,-1,0, 0,-1,0,
  119. 0,1,0, 0,1,0, 0,1,0,
  120. 0,1,0, 0,1,0, 0,1,0
  121. };
  122. m_vbo2.create();
  123. m_vbo2.bind();
  124. m_vbo2.allocate(36 * 8 * sizeof(GLfloat));
  125. m_vbo2.write(0, afVertices, sizeof(afVertices));
  126. m_vbo2.write(sizeof(afVertices), afTexCoord, sizeof(afTexCoord));
  127. m_vbo2.write(sizeof(afVertices) + sizeof(afTexCoord), afNormals, sizeof(afNormals));
  128. m_vbo2.release();
  129. }
  130. m_program2->setUniformValue(m_textureUniform2, 0); // use texture unit 0
  131. m_program2->enableAttributeArray(m_vertexAttr2);
  132. m_program2->enableAttributeArray(m_normalAttr2);
  133. m_program2->enableAttributeArray(m_texCoordAttr2);
  134. m_vbo2.bind();
  135. // In the buffer we first have 36 vertices (3 floats for each), then 36 texture
  136. // coordinates (2 floats for each), then 36 normals (3 floats for each).
  137. m_program2->setAttributeBuffer(m_vertexAttr2, GL_FLOAT, 0, 3);
  138. m_program2->setAttributeBuffer(m_texCoordAttr2, GL_FLOAT, 36 * 3 * sizeof(GLfloat), 2);
  139. m_program2->setAttributeBuffer(m_normalAttr2, GL_FLOAT, 36 * 5 * sizeof(GLfloat), 3);
  140. m_vbo2.release();
  141. glDrawArrays(GL_TRIANGLES, 0, 36);
  142. m_program2->disableAttributeArray(m_vertexAttr2);
  143. m_program2->disableAttributeArray(m_normalAttr2);
  144. m_program2->disableAttributeArray(m_texCoordAttr2);
  145. }
  146. void GLWidget::initializeGL()
  147. {
  148. initializeOpenGLFunctions();
  149. m_texture = new QOpenGLTexture(QImage("qt.png"));
  150. m_vshader1 = new QOpenGLShader(QOpenGLShader::Vertex);
  151. const char *vsrc1 =
  152. "attribute highp vec4 vertex;\n"
  153. "attribute mediump vec3 normal;\n"
  154. "uniform mediump mat4 matrix;\n"
  155. "varying mediump vec4 color;\n"
  156. "void main(void)\n"
  157. "{\n"
  158. " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
  159. " float angle = max(dot(normal, toLight), 0.0);\n"
  160. " vec3 col = vec3(0.40, 1.0, 0.0);\n"
  161. " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n"
  162. " color = clamp(color, 0.0, 1.0);\n"
  163. " gl_Position = matrix * vertex;\n"
  164. "}\n";
  165. //m_vshader1->compileSourceFile();
  166. m_vshader1->compileSourceCode(vsrc1);
  167. m_fshader1 = new QOpenGLShader(QOpenGLShader::Fragment);
  168. const char *fsrc1 =
  169. "varying mediump vec4 color;\n"
  170. "void main(void)\n"
  171. "{\n"
  172. " gl_FragColor = color;\n"
  173. "}\n";
  174. m_fshader1->compileSourceCode(fsrc1);
  175. m_program1 = new QOpenGLShaderProgram;
  176. m_program1->addShader(m_vshader1);
  177. m_program1->addShader(m_fshader1);
  178. m_program1->link();
  179. m_vertexAttr1 = m_program1->attributeLocation("vertex");
  180. m_normalAttr1 = m_program1->attributeLocation("normal");
  181. m_matrixUniform1 = m_program1->uniformLocation("matrix");
  182. m_vshader2 = new QOpenGLShader(QOpenGLShader::Vertex);
  183. const char *vsrc2 =
  184. "attribute highp vec4 vertex;\n"
  185. "attribute highp vec4 texCoord;\n"
  186. "attribute mediump vec3 normal;\n"
  187. "uniform mediump mat4 matrix;\n"
  188. "varying highp vec4 texc;\n"
  189. "varying mediump float angle;\n"
  190. "void main(void)\n"
  191. "{\n"
  192. " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
  193. " angle = max(dot(normal, toLight), 0.0);\n"
  194. " gl_Position = matrix * vertex;\n"
  195. " texc = texCoord;\n"
  196. "}\n";
  197. m_vshader2->compileSourceCode(vsrc2);
  198. m_fshader2 = new QOpenGLShader(QOpenGLShader::Fragment);
  199. const char *fsrc2 =
  200. "varying highp vec4 texc;\n"
  201. "uniform sampler2D tex;\n"
  202. "varying mediump float angle;\n"
  203. "void main(void)\n"
  204. "{\n"
  205. " highp vec3 color = texture2D(tex, texc.st).rgb;\n"
  206. " color = color * 0.2 + color * 0.8 * angle;\n"
  207. " gl_FragColor = vec4(clamp(color, 0.0, 1.0), 1.0);\n"
  208. "}\n";
  209. m_fshader2->compileSourceCode(fsrc2);
  210. m_program2 = new QOpenGLShaderProgram;
  211. m_program2->addShader(m_vshader2);
  212. m_program2->addShader(m_fshader2);
  213. m_program2->link();
  214. m_vertexAttr2 = m_program2->attributeLocation("vertex");
  215. m_normalAttr2 = m_program2->attributeLocation("normal");
  216. m_texCoordAttr2 = m_program2->attributeLocation("texCoord");
  217. m_matrixUniform2 = m_program2->uniformLocation("matrix");
  218. m_textureUniform2 = m_program2->uniformLocation("tex");
  219. m_fAngle = 0;
  220. m_fScale = 1;
  221. createGeometry();
  222. // Use a vertex buffer object. Client-side pointers are old-school and should be avoided.
  223. m_vbo1.create();
  224. m_vbo1.bind();
  225. // For the cube all the data belonging to the texture coordinates and
  226. // normals is placed separately, after the vertices. Here, for the Qt logo,
  227. // let's do something different and potentially more efficient: create a
  228. // properly interleaved data set.
  229. const int vertexCount = m_vertices.count();
  230. QVector<GLfloat> buf;
  231. buf.resize(vertexCount * 3 * 2);
  232. GLfloat *p = buf.data();
  233. for (int i = 0; i < vertexCount; ++i) {
  234. *p++ = m_vertices[i].x();
  235. *p++ = m_vertices[i].y();
  236. *p++ = m_vertices[i].z();
  237. *p++ = m_normals[i].x();
  238. *p++ = m_normals[i].y();
  239. *p++ = m_normals[i].z();
  240. }
  241. m_vbo1.allocate(buf.constData(), buf.count() * sizeof(GLfloat));
  242. m_vbo1.release();
  243. //createBubbles(bubbleNum - m_bubbles.count());
  244. }
  245. void GLWidget::paintGL()
  246. {
  247. //createBubbles(bubbleNum - m_bubbles.count());
  248. QPainter painter;
  249. painter.begin(this);
  250. painter.beginNativePainting();
  251. glClearColor(m_background.redF(), m_background.greenF(), m_background.blueF(), m_transparent ? 0.0f : 1.0f);
  252. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  253. glFrontFace(GL_CW);
  254. glCullFace(GL_FRONT);
  255. glEnable(GL_CULL_FACE);
  256. glEnable(GL_DEPTH_TEST);
  257. QMatrix4x4 modelview;
  258. modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f);
  259. modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f);
  260. modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f);
  261. modelview.scale(m_fScale);
  262. modelview.translate(0.0f, -0.2f, 0.0f);
  263. if (m_qtLogo) {
  264. m_program1->bind();
  265. m_program1->setUniformValue(m_matrixUniform1, modelview);
  266. paintQtLogo();
  267. m_program1->release();
  268. } else {
  269. m_program2->bind();
  270. m_program2->setUniformValue(m_matrixUniform2, modelview);
  271. paintTexturedCube();
  272. m_program2->release();
  273. }
  274. glDisable(GL_DEPTH_TEST);
  275. glDisable(GL_CULL_FACE);
  276. painter.endNativePainting();
  277. //if (m_showBubbles)
  278. // foreach (Bubble *bubble, m_bubbles) {
  279. // bubble->drawBubble(&painter);
  280. // }
  281. /*if (const int elapsed = m_time.elapsed()) {
  282. QString framesPerSecond;
  283. framesPerSecond.setNum(m_frames /(elapsed / 1000.0), 'f', 2);
  284. painter.setPen(m_transparent ? Qt::black : Qt::white);
  285. painter.drawText(20, 40, framesPerSecond + " paintGL calls / s");
  286. }*/
  287. painter.end();
  288. //QMutableListIterator<Bubble*> iter(m_bubbles);
  289. //while (iter.hasNext()) {
  290. // Bubble *bubble = iter.next();
  291. // bubble->move(rect());
  292. //}
  293. if (!(m_frames % 100)) {
  294. m_time.start();
  295. m_frames = 0;
  296. }
  297. m_fAngle += 1.0f;
  298. ++m_frames;
  299. // When requested, follow the ideal way to animate: Rely on
  300. // blocking swap and just schedule updates continuously.
  301. //if (!m_mainWindow->timerEnabled())
  302. update();
  303. }
  304. //void GLWidget::createBubbles(int number)
  305. //{
  306. // for (int i = 0; i < number; ++i) {
  307. // QPointF position(width()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))),
  308. // height()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))));
  309. // qreal radius = qMin(width(), height())*(0.0175 + 0.0875*qrand()/(RAND_MAX+1.0));
  310. // QPointF velocity(width()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0)),
  311. // height()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0)));
  312. //
  313. // m_bubbles.append(new Bubble(position, radius, velocity));
  314. // }
  315. //}
  316. void GLWidget::createGeometry()
  317. {
  318. m_vertices.clear();
  319. m_normals.clear();
  320. qreal x1 = +0.06f;
  321. qreal y1 = -0.14f;
  322. qreal x2 = +0.14f;
  323. qreal y2 = -0.06f;
  324. qreal x3 = +0.08f;
  325. qreal y3 = +0.00f;
  326. qreal x4 = +0.30f;
  327. qreal y4 = +0.22f;
  328. quad(x1, y1, x2, y2, y2, x2, y1, x1);
  329. quad(x3, y3, x4, y4, y4, x4, y3, x3);
  330. extrude(x1, y1, x2, y2);
  331. extrude(x2, y2, y2, x2);
  332. extrude(y2, x2, y1, x1);
  333. extrude(y1, x1, x1, y1);
  334. extrude(x3, y3, x4, y4);
  335. extrude(x4, y4, y4, x4);
  336. extrude(y4, x4, y3, x3);
  337. const qreal Pi = 3.14159f;
  338. const int NumSectors = 100;
  339. for (int i = 0; i < NumSectors; ++i) {
  340. qreal angle1 = (i * 2 * Pi) / NumSectors;
  341. qreal x5 = 0.30 * sin(angle1);
  342. qreal y5 = 0.30 * cos(angle1);
  343. qreal x6 = 0.20 * sin(angle1);
  344. qreal y6 = 0.20 * cos(angle1);
  345. qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors;
  346. qreal x7 = 0.20 * sin(angle2);
  347. qreal y7 = 0.20 * cos(angle2);
  348. qreal x8 = 0.30 * sin(angle2);
  349. qreal y8 = 0.30 * cos(angle2);
  350. quad(x5, y5, x6, y6, x7, y7, x8, y8);
  351. extrude(x6, y6, x7, y7);
  352. extrude(x8, y8, x5, y5);
  353. }
  354. for (int i = 0;i < m_vertices.size();i++)
  355. m_vertices[i] *= 2.0f;
  356. }
  357. void GLWidget::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
  358. {
  359. m_vertices << QVector3D(x1, y1, -0.05f);
  360. m_vertices << QVector3D(x2, y2, -0.05f);
  361. m_vertices << QVector3D(x4, y4, -0.05f);
  362. m_vertices << QVector3D(x3, y3, -0.05f);
  363. m_vertices << QVector3D(x4, y4, -0.05f);
  364. m_vertices << QVector3D(x2, y2, -0.05f);
  365. QVector3D n = QVector3D::normal
  366. (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f));
  367. m_normals << n;
  368. m_normals << n;
  369. m_normals << n;
  370. m_normals << n;
  371. m_normals << n;
  372. m_normals << n;
  373. m_vertices << QVector3D(x4, y4, 0.05f);
  374. m_vertices << QVector3D(x2, y2, 0.05f);
  375. m_vertices << QVector3D(x1, y1, 0.05f);
  376. m_vertices << QVector3D(x2, y2, 0.05f);
  377. m_vertices << QVector3D(x4, y4, 0.05f);
  378. m_vertices << QVector3D(x3, y3, 0.05f);
  379. n = QVector3D::normal
  380. (QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f));
  381. m_normals << n;
  382. m_normals << n;
  383. m_normals << n;
  384. m_normals << n;
  385. m_normals << n;
  386. m_normals << n;
  387. }
  388. void GLWidget::extrude(qreal x1, qreal y1, qreal x2, qreal y2)
  389. {
  390. m_vertices << QVector3D(x1, y1, +0.05f);
  391. m_vertices << QVector3D(x2, y2, +0.05f);
  392. m_vertices << QVector3D(x1, y1, -0.05f);
  393. m_vertices << QVector3D(x2, y2, -0.05f);
  394. m_vertices << QVector3D(x1, y1, -0.05f);
  395. m_vertices << QVector3D(x2, y2, +0.05f);
  396. QVector3D n = QVector3D::normal
  397. (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f));
  398. m_normals << n;
  399. m_normals << n;
  400. m_normals << n;
  401. m_normals << n;
  402. m_normals << n;
  403. m_normals << n;
  404. }
  405. void GLWidget::setTransparent(bool transparent)
  406. {
  407. setAttribute(Qt::WA_AlwaysStackOnTop, transparent);
  408. m_transparent = transparent;
  409. // Call update() on the top-level window after toggling AlwayStackOnTop to make sure
  410. // the entire backingstore is updated accordingly.
  411. window()->update();
  412. }
  413. void GLWidget::resizeGL(int, int)
  414. {
  415. if (m_hasButton) {
  416. if (!m_btn) {
  417. m_btn = new QPushButton("A widget on top.\nPress for more widgets.", this);
  418. connect(m_btn, &QPushButton::clicked, this, &GLWidget::handleButtonPress);
  419. }
  420. m_btn->move(20, 80);
  421. }
  422. }
  423. void GLWidget::handleButtonPress()
  424. {
  425. // m_mainWindow->addNew();
  426. }