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.

565 lines
18 KiB

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