6 #include "myglwidget.h"
8 MyGLWidget::MyGLWidget(QWidget
*parent
)
11 std::cout
<< "GLWidget() constructor\n";
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
;
33 if (numVertices
> 2) {
35 glClear(GL_COLOR_BUFFER_BIT
);
36 drawSphere(10, 10, 0.5f
);
43 void MyGLWidget::rotateX(GLfloat angle
)
45 std::cout
<< "MyGLWidget::rotateX()\n";
51 void MyGLWidget::rotateY(GLfloat angle
)
53 std::cout
<< "MyGLWidget::rotateY()\n";
59 void MyGLWidget::rotateZ(GLfloat angle
)
61 std::cout
<< "MyGLWidget::rotateZ()\n";
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
);
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";
92 glClear(GL_COLOR_BUFFER_BIT
);
94 // Reset modelview matrix
95 glMatrixMode(GL_MODELVIEW
);
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
);
107 drawSphere(10, 10, 0.5f
);
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
);
124 //gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
126 // Reset modelview matrix
127 glMatrixMode(GL_MODELVIEW
);
131 void MyGLWidget::drawAxis(void)
135 glEnable(GL_LINE_SMOOTH
);
136 glColor3f(0.0f
, 1.0f
, 0.0f
);
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
);
149 // Restore color and line width
150 glColor3f(1.0f
, 1.0f
, 1.0f
);
151 glDisable(GL_LINE_SMOOTH
);
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
);
170 x
= r
* sin(theta
+ stepTheta
) * cos(phi
);
171 y
= r
* sin(theta
+ stepTheta
) * sin(phi
);
172 z
= r
* cos(theta
+ stepTheta
);
178 // The boundary edges of the polygon are drawn as line segments.
179 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);