First version that actually draws a sphere
[agianapa.git] / qt / sphere-quadstrips / myglwidget.cpp
blob7e5a133cfb7d567463d1205644a5c6a21138cbd8
1 #include <iostream>
2 #include <QKeyEvent>
3 #include <QtOpenGL>
5 #include <cmath>
6 #include "myglwidget.h"
8 MyGLWidget::MyGLWidget(QWidget *parent)
9 : QGLWidget(parent)
11 std::cout << "GLWidget() constructor\n";
13 numVertices = 10;
15 xRot = 0.0f;
16 yRot = 0.0f;
17 zRot = 0.0f;
19 this->setMinimumWidth(300);
20 this->setMinimumHeight(300);
23 MyGLWidget::~MyGLWidget()
25 std::cout << "~GLWidget() deconstructor\n";
28 void MyGLWidget::setNumOfVertices(int dir)
30 std::cout << "myglWidget::setNumOfVertices()" << std::endl;
32 numVertices += dir;
33 if (numVertices > 2) {
34 makeCurrent();
35 glClear(GL_COLOR_BUFFER_BIT);
36 drawSphere(10, 10, 0.5f);
37 updateGL();
39 else
40 numVertices = 2;
43 void MyGLWidget::rotateX(GLfloat angle)
45 std::cout << "MyGLWidget::rotateX()\n";
47 xRot += angle;
48 updateGL();
51 void MyGLWidget::rotateY(GLfloat angle)
53 std::cout << "MyGLWidget::rotateY()\n";
55 yRot += angle;
56 updateGL();
59 void MyGLWidget::rotateZ(GLfloat angle)
61 std::cout << "MyGLWidget::rotateZ()\n";
63 zRot += angle;
64 updateGL();
67 void MyGLWidget::initializeGL()
69 std::cout << "MyGLWidget::initializeGL()\n";
71 // Enable backface culling
72 glEnable(GL_CULL_FACE);
74 // Clear color is set to black
75 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
76 glShadeModel(GL_SMOOTH);
78 // Reset modelview matrix
79 glMatrixMode(GL_MODELVIEW);
80 glLoadIdentity();
82 // We don't need to call drawSphere() here,
83 // since the resizeGL() will be called upon
84 // the initial creation of the window.
87 void MyGLWidget::paintGL()
89 std::cout << "paintGL()\n";
91 // Clear scene
92 glClear(GL_COLOR_BUFFER_BIT);
94 // Reset modelview matrix
95 glMatrixMode(GL_MODELVIEW);
96 glLoadIdentity();
98 // Rotate appropriately
99 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
100 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
101 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
103 // Draw x, y, z axis
104 drawAxis();
106 // Draw sphere
107 drawSphere(10, 10, 0.5f);
109 glFlush();
112 void MyGLWidget::resizeGL(int width, int height)
114 std::cout << "MyGLWidget::resizeGL()\n";
115 std::cout << "New width = " << width
116 << "\nNew height = " << height << "\n";
118 // Update viewport to cover the whole screen
119 glViewport(0, 0, width, height);
121 // Reset projection matrix
122 glMatrixMode(GL_PROJECTION);
123 glLoadIdentity();
124 //gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
126 // Reset modelview matrix
127 glMatrixMode(GL_MODELVIEW);
128 glLoadIdentity();
131 void MyGLWidget::drawAxis(void)
133 // Draw axis
134 glLineWidth(3);
135 glEnable(GL_LINE_SMOOTH);
136 glColor3f(0.0f, 1.0f, 0.0f);
138 glBegin(GL_LINES);
139 glVertex3f(0.0f, 0.0f, 0.0f); // x axis
140 glVertex3f(1.0f, 0.0f, 0.0f);
142 glVertex3f(0.0f, 0.0f, 0.0f); // y axis
143 glVertex3f(0.0f, 1.0f, 0.0f);
145 glVertex3f(0.0f, 0.0f, 0.0f); // z axis
146 glVertex3f(0.0f, 0.0f, 1.0f);
147 glEnd();
149 // Restore color and line width
150 glColor3f(1.0f, 1.0f, 1.0f);
151 glDisable(GL_LINE_SMOOTH);
152 glLineWidth(1);
155 void MyGLWidget::drawSphere(GLuint nStacks, GLuint nSlices, GLfloat r)
157 GLfloat theta, phi, x, y, z, stepTheta, stepPhi;
159 stepTheta = M_PI / nStacks;
160 stepPhi = 2 * M_PI / nSlices;
162 for (theta = 0; theta <= M_PI; theta += stepTheta) {
163 glBegin(GL_QUAD_STRIP);
164 for (phi = 0; phi <= 2.0 * M_PI; phi += stepPhi) {
165 x = r * sin(theta) * cos(phi);
166 y = r * sin(theta) * sin(phi);
167 z = r * cos(theta);
168 glVertex3f(x, y, z);
170 x = r * sin(theta + stepTheta) * cos(phi);
171 y = r * sin(theta + stepTheta) * sin(phi);
172 z = r * cos(theta + stepTheta);
173 glVertex3f(x, y, z);
175 glEnd();
178 // The boundary edges of the polygon are drawn as line segments.
179 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);