Qt 5.10 Book Examples
OGLPyramid.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // OGLPyramid.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QOpenGLFunctions>
12 #include <QtGui>
13 #include "OGLPyramid.h"
14 
15 // ----------------------------------------------------------------------
16 OGLPyramid::OGLPyramid(QWidget* pwgt/*= 0*/) : QOpenGLWidget(pwgt)
17  , m_xRotate(0)
18  , m_yRotate(0)
19 {
20 }
21 
22 // ----------------------------------------------------------------------
23 /*virtual*/void OGLPyramid::initializeGL()
24 {
25  QOpenGLFunctions* pFunc =
26  QOpenGLContext::currentContext()->functions();
27  pFunc->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
28 
29  pFunc->glEnable(GL_DEPTH_TEST);
30  glShadeModel(GL_FLAT);
31  m_nPyramid = createPyramid(1.2f);
32 }
33 
34 // ----------------------------------------------------------------------
35 /*virtual*/void OGLPyramid::resizeGL(int nWidth, int nHeight)
36 {
37  glViewport(0, 0, (GLint)nWidth, (GLint)nHeight);
38  glMatrixMode(GL_PROJECTION);
39  glLoadIdentity();
40  glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
41 }
42 
43 // ----------------------------------------------------------------------
44 /*virtual*/void OGLPyramid::paintGL()
45 {
46  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
47 
48  glMatrixMode(GL_MODELVIEW);
49  glLoadIdentity();
50  glTranslatef(0.0, 0.0, -3.0);
51 
52  glRotatef(m_xRotate, 1.0, 0.0, 0.0);
53  glRotatef(m_yRotate, 0.0, 1.0, 0.0);
54 
55  glCallList(m_nPyramid);
56 }
57 
58 // ----------------------------------------------------------------------
59 /*virtual*/void OGLPyramid::mousePressEvent(QMouseEvent* pe)
60 {
61  m_ptPosition = pe->pos();
62 }
63 
64 // ----------------------------------------------------------------------
65 /*virtual*/void OGLPyramid::mouseMoveEvent(QMouseEvent* pe)
66 {
67  m_xRotate += 180 * (GLfloat)(pe->y() - m_ptPosition.y()) / height();
68  m_yRotate += 180 * (GLfloat)(pe->x() - m_ptPosition.x()) / width();
69  update();
70 
71  m_ptPosition = pe->pos();
72 }
73 
74 // ----------------------------------------------------------------------
75 GLuint OGLPyramid::createPyramid(GLfloat fSize/*=1.0f*/)
76 {
77  GLuint n = glGenLists(1);
78 
79  glNewList(n, GL_COMPILE);
80  glBegin(GL_TRIANGLE_FAN);
81  glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
82  glVertex3f(0.0, fSize, 0.0);
83  glVertex3f(-fSize, -fSize, fSize);
84  glVertex3f(fSize, -fSize, fSize);
85  glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
86  glVertex3f(fSize, -fSize, -fSize);
87  glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
88  glVertex3f(-fSize, -fSize, -fSize);
89  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
90  glVertex3f(-fSize, -fSize, fSize);
91  glEnd();
92 
93  glBegin(GL_QUADS);
94  glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
95  glVertex3f(-fSize, -fSize, fSize);
96  glVertex3f(fSize, -fSize, fSize);
97  glVertex3f(fSize, -fSize, -fSize);
98  glVertex3f(-fSize, -fSize, -fSize);
99  glEnd();
100  glEndList();
101 
102  return n;
103 }
OGLPyramid(QWidget *pwgt=0)
Definition: OGLPyramid.cpp:16
virtual void mousePressEvent(QMouseEvent *pe)
Definition: OGLPyramid.cpp:59
virtual void mouseMoveEvent(QMouseEvent *pe)
Definition: OGLPyramid.cpp:65
virtual void initializeGL()
Definition: OGLPyramid.cpp:23
GLuint createPyramid(GLfloat fSize=1.0f)
Definition: OGLPyramid.cpp:75
virtual void resizeGL(int nWidth, int nHeight)
Definition: OGLPyramid.cpp:35
virtual void paintGL()
Definition: OGLPyramid.cpp:44