Qt 5.10 Book Examples
OGLDraw.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // OGLDraw.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 <math.h>
13 #include "OGLDraw.h"
14 
15 // ----------------------------------------------------------------------
16 OGLDraw::OGLDraw(QWidget* pwgt/*= 0*/) : QOpenGLWidget(pwgt)
17 {
18 }
19 
20 // ----------------------------------------------------------------------
21 /*virtual*/void OGLDraw::initializeGL()
22 {
23  QOpenGLFunctions* pFunc =
24  QOpenGLContext::currentContext()->functions();
25  pFunc->glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
26 }
27 
28 // ----------------------------------------------------------------------
29 /*virtual*/void OGLDraw::resizeGL(int nWidth, int nHeight)
30 {
31  glMatrixMode(GL_PROJECTION);
32  glLoadIdentity();
33  glViewport(0, 0, (GLint)nWidth, (GLint)nHeight);
34  glOrtho(0, 400, 200, 0, -1, 1);
35 }
36 
37 // ----------------------------------------------------------------------
38 /*virtual*/void OGLDraw::paintGL()
39 {
40  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
41 
42  draw(0, 0, GL_POINTS);
43  draw(100, 0, GL_LINES);
44  draw(200, 0, GL_LINE_STRIP);
45  draw(300, 0, GL_LINE_LOOP);
46 
47  draw(0, 100, GL_TRIANGLE_STRIP);
48  draw(100, 100, GL_POLYGON);
49  draw(200, 100, GL_QUADS);
50  draw(300, 100, GL_TRIANGLES);
51 }
52 
53 // ----------------------------------------------------------------------
54 void OGLDraw::draw(int xOffset, int yOffset, GLenum type)
55 {
56  int n = 8;
57 
58  glPointSize(2);
59  glBegin(type);
60  glColor3f(0, 0, 0);
61  for (int i = 0; i < n; ++i) {
62  float fAngle = 2 * 3.14 * i / n;
63  int x = (int)(50 + cos(fAngle) * 40 + xOffset);
64  int y = (int)(50 + sin(fAngle) * 40 + yOffset);
65  glVertex2f(x, y);
66  }
67  glEnd();
68 }
void draw(int xOffset, int yOffset, GLenum type)
Definition: OGLDraw.cpp:54
virtual void initializeGL()
Definition: OGLDraw.cpp:21
virtual void resizeGL(int nWidth, int nHeight)
Definition: OGLDraw.cpp:29
OGLDraw(QWidget *pwgt=0)
Definition: OGLDraw.cpp:16
virtual void paintGL()
Definition: OGLDraw.cpp:38