├── 3-上色.pdf ├── 4-旋转.pdf ├── 5-向三维进军.pdf ├── 6-纹理映射.pdf ├── 7-纹理滤波、光源和键盘控制.pdf ├── OpenGL_Template ├── OpenGL_Template.pro ├── OpenGL_Template.pro.user ├── help.cpp ├── help.h ├── help.ui ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui ├── view.cpp └── view.h ├── lesson1_console ├── lesson1_console.pro ├── lesson1_console.pro.user ├── main.cpp ├── nehewidget.cpp └── nehewidget.h ├── lesson3_coloring_console ├── lesson2_console.pro ├── lesson2_console.pro.user ├── main.cpp ├── nehewidget.cpp └── nehewidget.h ├── lesson4_3D_console ├── lesson4_console.pro.user ├── lesson5_console.pro ├── main.cpp ├── nehewidget.cpp └── nehewidget.h ├── lesson4_rotationAroundAxis_console ├── lesson4_console.pro ├── lesson4_console.pro.user ├── main.cpp ├── nehewidget.cpp └── nehewidget.h ├── lesson5_3D_console ├── lesson5_console.pro ├── lesson5_console.pro.user ├── main.cpp ├── nehewidget.cpp └── nehewidget.h ├── lesson6_TextureMapping_console ├── lesson6_console.pro ├── lesson6_console.pro.user ├── main.cpp ├── nehewidget.cpp └── nehewidget.h ├── lesson7_TextureFilter_Lighting_KeyPress-error ├── lesson7_TextureFilter_Lighting_KeyPress.pro ├── lesson7_TextureFilter_Lighting_KeyPress.pro.user ├── main.cpp ├── nehewidget.cpp └── nehewidget.h └── lesson7_TextureFilter_Lighting_KeyPress ├── container.png ├── lesson7_TextureFilter_Lighting_KeyPress.pro ├── lesson7_TextureFilter_Lighting_KeyPress.pro.user ├── main.cpp ├── nehewidget.cpp └── nehewidget.h /3-上色.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rookiee/Qt_OpenGL/c80926f499563298eac43a03006484bfc62ebf8d/3-上色.pdf -------------------------------------------------------------------------------- /4-旋转.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rookiee/Qt_OpenGL/c80926f499563298eac43a03006484bfc62ebf8d/4-旋转.pdf -------------------------------------------------------------------------------- /5-向三维进军.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rookiee/Qt_OpenGL/c80926f499563298eac43a03006484bfc62ebf8d/5-向三维进军.pdf -------------------------------------------------------------------------------- /6-纹理映射.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rookiee/Qt_OpenGL/c80926f499563298eac43a03006484bfc62ebf8d/6-纹理映射.pdf -------------------------------------------------------------------------------- /7-纹理滤波、光源和键盘控制.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rookiee/Qt_OpenGL/c80926f499563298eac43a03006484bfc62ebf8d/7-纹理滤波、光源和键盘控制.pdf -------------------------------------------------------------------------------- /OpenGL_Template/OpenGL_Template.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2016-08-08T21:46:37 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = OpenGL_Template 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | mainwindow.cpp \ 17 | view.cpp \ 18 | help.cpp 19 | 20 | HEADERS += mainwindow.h \ 21 | view.h \ 22 | help.h 23 | 24 | FORMS += mainwindow.ui \ 25 | help.ui 26 | 27 | mac{ 28 | LIBS += -framework opengl -framework glut 29 | } 30 | 31 | -------------------------------------------------------------------------------- /OpenGL_Template/OpenGL_Template.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {3de204d9-a1bc-4e3d-9eab-8f1053f1f147} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | 0 45 | 8 46 | true 47 | 1 48 | true 49 | true 50 | true 51 | false 52 | 53 | 54 | 55 | ProjectExplorer.Project.PluginSettings 56 | 57 | 58 | 59 | ProjectExplorer.Project.Target.0 60 | 61 | Desktop Qt 5.6.0 clang 64bit 62 | Desktop Qt 5.6.0 clang 64bit 63 | qt.56.clang_64_kit 64 | 0 65 | 0 66 | 0 67 | 68 | /Users/Haoyang/Documents/TestCodes/build-OpenGL_Template-Desktop_Qt_5_6_0_clang_64bit-Debug 69 | 70 | 71 | true 72 | qmake 73 | 74 | QtProjectManager.QMakeBuildStep 75 | true 76 | 77 | false 78 | false 79 | false 80 | 81 | 82 | true 83 | Make 84 | 85 | Qt4ProjectManager.MakeStep 86 | 87 | -w 88 | -r 89 | 90 | false 91 | 92 | 93 | 94 | 2 95 | Build 96 | 97 | ProjectExplorer.BuildSteps.Build 98 | 99 | 100 | 101 | true 102 | Make 103 | 104 | Qt4ProjectManager.MakeStep 105 | 106 | -w 107 | -r 108 | 109 | true 110 | clean 111 | 112 | 113 | 1 114 | Clean 115 | 116 | ProjectExplorer.BuildSteps.Clean 117 | 118 | 2 119 | false 120 | 121 | Debug 122 | 123 | Qt4ProjectManager.Qt4BuildConfiguration 124 | 2 125 | true 126 | 127 | 128 | /Users/Haoyang/Documents/TestCodes/build-OpenGL_Template-Desktop_Qt_5_6_0_clang_64bit-Release 129 | 130 | 131 | true 132 | qmake 133 | 134 | QtProjectManager.QMakeBuildStep 135 | false 136 | 137 | false 138 | false 139 | false 140 | 141 | 142 | true 143 | Make 144 | 145 | Qt4ProjectManager.MakeStep 146 | 147 | -w 148 | -r 149 | 150 | false 151 | 152 | 153 | 154 | 2 155 | Build 156 | 157 | ProjectExplorer.BuildSteps.Build 158 | 159 | 160 | 161 | true 162 | Make 163 | 164 | Qt4ProjectManager.MakeStep 165 | 166 | -w 167 | -r 168 | 169 | true 170 | clean 171 | 172 | 173 | 1 174 | Clean 175 | 176 | ProjectExplorer.BuildSteps.Clean 177 | 178 | 2 179 | false 180 | 181 | Release 182 | 183 | Qt4ProjectManager.Qt4BuildConfiguration 184 | 0 185 | true 186 | 187 | 188 | /Users/Haoyang/Documents/TestCodes/build-OpenGL_Template-Desktop_Qt_5_6_0_clang_64bit-Profile 189 | 190 | 191 | true 192 | qmake 193 | 194 | QtProjectManager.QMakeBuildStep 195 | true 196 | 197 | false 198 | true 199 | false 200 | 201 | 202 | true 203 | Make 204 | 205 | Qt4ProjectManager.MakeStep 206 | 207 | -w 208 | -r 209 | 210 | false 211 | 212 | 213 | 214 | 2 215 | Build 216 | 217 | ProjectExplorer.BuildSteps.Build 218 | 219 | 220 | 221 | true 222 | Make 223 | 224 | Qt4ProjectManager.MakeStep 225 | 226 | -w 227 | -r 228 | 229 | true 230 | clean 231 | 232 | 233 | 1 234 | Clean 235 | 236 | ProjectExplorer.BuildSteps.Clean 237 | 238 | 2 239 | false 240 | 241 | Profile 242 | 243 | Qt4ProjectManager.Qt4BuildConfiguration 244 | 0 245 | true 246 | 247 | 3 248 | 249 | 250 | 0 251 | Deploy 252 | 253 | ProjectExplorer.BuildSteps.Deploy 254 | 255 | 1 256 | Deploy locally 257 | 258 | ProjectExplorer.DefaultDeployConfiguration 259 | 260 | 1 261 | 262 | 263 | false 264 | 1000 265 | 266 | true 267 | 268 | false 269 | false 270 | false 271 | false 272 | true 273 | 0.01 274 | 10 275 | true 276 | 1 277 | 25 278 | 279 | 1 280 | true 281 | false 282 | true 283 | valgrind 284 | 285 | 0 286 | 1 287 | 2 288 | 3 289 | 4 290 | 5 291 | 6 292 | 7 293 | 8 294 | 9 295 | 10 296 | 11 297 | 12 298 | 13 299 | 14 300 | 301 | 2 302 | 303 | OpenGL_Template 304 | 305 | Qt4ProjectManager.Qt4RunConfiguration:/Users/Haoyang/Documents/TestCodes/OpenGL_Template/OpenGL_Template.pro 306 | true 307 | 308 | OpenGL_Template.pro 309 | false 310 | false 311 | 312 | 3768 313 | false 314 | true 315 | false 316 | false 317 | true 318 | 319 | 1 320 | 321 | 322 | 323 | ProjectExplorer.Project.TargetCount 324 | 1 325 | 326 | 327 | ProjectExplorer.Project.Updater.FileVersion 328 | 18 329 | 330 | 331 | Version 332 | 18 333 | 334 | 335 | -------------------------------------------------------------------------------- /OpenGL_Template/help.cpp: -------------------------------------------------------------------------------- 1 | #include "help.h" 2 | #include "ui_help.h" 3 | 4 | Help::Help(QWidget *parent) : 5 | QWidget(parent), 6 | ui(new Ui::Help) 7 | { 8 | ui->setupUi(this); 9 | ui->keysLabel->clear(); 10 | if(ui->keysTab->isEnabled()){ 11 | setWindowTitle( "Help -- Keys"); 12 | } 13 | 14 | keyContent = "L: Lighting Control\n" 15 | "W: Zoom In\n" 16 | "S: Zoom Out\n" 17 | "A: Rotate Toward Left Around Y\n" 18 | "D: Rotate Toward Right Around Y\n" 19 | "E: Rotate Toward Up Around X\n" 20 | "C: Rotate Toward Down Around X\n" 21 | "Z: Rotate Toward Left Around Z\n" 22 | "X: Rotate Toward Right Around Z\n"; 23 | ui->keysLabel->setText(keyContent); 24 | } 25 | 26 | Help::~Help() 27 | { 28 | delete ui; 29 | } 30 | -------------------------------------------------------------------------------- /OpenGL_Template/help.h: -------------------------------------------------------------------------------- 1 | #ifndef HELP_H 2 | #define HELP_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class Help; 8 | } 9 | 10 | class Help : public QWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit Help(QWidget *parent = 0); 16 | ~Help(); 17 | 18 | private: 19 | Ui::Help *ui; 20 | QString keyContent; 21 | }; 22 | 23 | #endif // HELP_H 24 | -------------------------------------------------------------------------------- /OpenGL_Template/help.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Help 4 | 5 | 6 | 7 | 0 8 | 0 9 | 362 10 | 284 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | 19 | 20 | 0 21 | 22 | 23 | 24 | Keys 25 | 26 | 27 | 28 | 29 | 30 | TextLabel 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | Tab 2 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /OpenGL_Template/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /OpenGL_Template/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | #include "view.h" 5 | 6 | #include "help.h" 7 | 8 | MainWindow::MainWindow(QWidget *parent) : 9 | QMainWindow(parent), 10 | ui(new Ui::MainWindow) 11 | { 12 | ui->setupUi(this); 13 | 14 | ui->widget->show(); 15 | 16 | connect(ui->actionXZ_Plane, SIGNAL(triggered(bool)), this, SLOT(View_actionXZ_Plane_Slot())); 17 | connect(ui->actionHelp, SIGNAL(triggered(bool)), this, SLOT(Help_actionHelp_Slot())); 18 | } 19 | 20 | MainWindow::~MainWindow() 21 | { 22 | delete ui; 23 | } 24 | 25 | void MainWindow::keyPressEvent(QKeyEvent *event) 26 | { 27 | ui->widget->keyPressEvent(event); 28 | } 29 | 30 | void MainWindow::mousePressEvent(QMouseEvent *event) 31 | { 32 | ui->widget->mousePressEvent(event); 33 | } 34 | 35 | 36 | 37 | void MainWindow::on_pushButton_clicked() 38 | { 39 | ui->widget->lighting = ! (ui->widget->lighting); 40 | if(ui->widget->lighting) 41 | ui->widget->EnableLighting(); 42 | else 43 | ui->widget->DisableLighting(); 44 | 45 | } 46 | 47 | void MainWindow::View_actionXZ_Plane_Slot() 48 | { 49 | ui->widget->XZPlaneView(); 50 | } 51 | 52 | void MainWindow::Help_actionHelp_Slot() 53 | { 54 | Help *windowHelp = new Help(); 55 | windowHelp->show(); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /OpenGL_Template/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | 7 | namespace Ui { 8 | class MainWindow; 9 | } 10 | 11 | class MainWindow : public QMainWindow 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit MainWindow(QWidget *parent = 0); 17 | ~MainWindow(); 18 | 19 | protected: 20 | void keyPressEvent(QKeyEvent *event); 21 | void mousePressEvent(QMouseEvent *event); 22 | 23 | 24 | private slots: 25 | void on_pushButton_clicked(); 26 | void View_actionXZ_Plane_Slot(); 27 | void Help_actionHelp_Slot(); 28 | 29 | private: 30 | Ui::MainWindow *ui; 31 | }; 32 | 33 | #endif // MAINWINDOW_H 34 | -------------------------------------------------------------------------------- /OpenGL_Template/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 667 10 | 468 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 20 21 | 10 22 | 451 23 | 381 24 | 25 | 26 | 27 | 28 | 29 | 30 | 510 31 | 10 32 | 141 33 | 391 34 | 35 | 36 | 37 | GroupBox 38 | 39 | 40 | 41 | 42 | 10 43 | 20 44 | 81 45 | 32 46 | 47 | 48 | 49 | Light 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 0 58 | 0 59 | 667 60 | 22 61 | 62 | 63 | 64 | 65 | File 66 | 67 | 68 | 69 | 70 | View 71 | 72 | 73 | 74 | 75 | 76 | Operations 77 | 78 | 79 | 80 | 81 | Help 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | TopToolBarArea 93 | 94 | 95 | false 96 | 97 | 98 | 99 | 100 | 101 | Keyboard Info 102 | 103 | 104 | 105 | 106 | XZ Plane 107 | 108 | 109 | 110 | 111 | Help 112 | 113 | 114 | 115 | 116 | 117 | 118 | View 119 | QWidget 120 |
view.h
121 | 1 122 |
123 |
124 | 125 | 126 |
127 | -------------------------------------------------------------------------------- /OpenGL_Template/view.cpp: -------------------------------------------------------------------------------- 1 | #include "view.h" 2 | 3 | GLfloat lightAmbient[4] = {0.5, 0.5, 0.5, 1.0}; 4 | GLfloat lightDiffuse[4] = {1.0, 1.0, 1.0, 1.0}; 5 | GLfloat lightPosition[4] = {2.0, 2.0, 2.0, 1.0}; 6 | 7 | 8 | View::View(QWidget *parent) 9 | :QGLWidget(parent) 10 | { 11 | zoom = -5.0; 12 | xRot = yRot = zRot = 0.0; 13 | lighting = false; 14 | } 15 | 16 | void View::initializeGL() 17 | { 18 | glShadeModel(GL_SMOOTH); 19 | glClearColor(1.0, 1.0, 1.0, 1.0); 20 | 21 | glClearDepth(1.0); 22 | glEnable(GL_DEPTH_TEST); 23 | glDepthFunc(GL_LEQUAL); 24 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 25 | 26 | glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); 27 | glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); 28 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); 29 | glEnable(GL_LIGHT0); 30 | 31 | 32 | 33 | } 34 | 35 | void View::resizeGL(GLsizei width, GLsizei height) 36 | { 37 | glViewport(0, 0, width, height); 38 | glMatrixMode(GL_PROJECTION); 39 | glLoadIdentity(); 40 | 41 | gluPerspective(45.0, (GLfloat)width/height, 0.1, 100.0); 42 | 43 | glMatrixMode(GL_MODELVIEW); 44 | } 45 | 46 | void View::paintGL() 47 | { 48 | 49 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 50 | DrawCoords(); 51 | DrawTest(); 52 | 53 | } 54 | 55 | void View::DrawTest() 56 | { 57 | glLoadIdentity(); 58 | glTranslatef(0, 0, zoom); 59 | 60 | glRotatef(xRot, 1.0, 0.0, 0.0); 61 | glRotatef(yRot, 0.0, 1.0, 0.0); 62 | glRotatef(zRot, 0.0, 0.0, 1.0); 63 | 64 | glColor3f(0.3, 0.3, 0.3); 65 | glutSolidTeapot(1.0); 66 | 67 | 68 | 69 | 70 | } 71 | 72 | void View::mousePressEvent(QMouseEvent *event) 73 | { 74 | lastPos = event->pos(); 75 | } 76 | 77 | void View::mouseMoveEvent(QMouseEvent *event) 78 | { 79 | GLfloat dx = ((GLfloat)event->x() - lastPos.x()) ; 80 | GLfloat dy = ((GLfloat)event->x() - lastPos.y()) ; 81 | 82 | if(event->buttons() & Qt::LeftButton){ 83 | yRot += 0.05 * dx; 84 | xRot += 0.05 * dy; 85 | updateGL(); 86 | } 87 | if(event->buttons() & Qt::RightButton){ 88 | zRot += 15 * dx; 89 | // xRot -= 20 * dy; 90 | updateGL(); 91 | } 92 | 93 | 94 | lastPos = event->pos(); 95 | } 96 | 97 | void View::wheelEvent(QWheelEvent *event) 98 | { 99 | if(event->delta() > 0) 100 | zoom += 0.1; 101 | else 102 | zoom -= 0.1; 103 | updateGL(); 104 | } 105 | 106 | void View::keyPressEvent(QKeyEvent *event) 107 | { 108 | switch (event->key()) 109 | { 110 | case Qt::Key_L: 111 | lighting = !lighting; 112 | if (lighting) 113 | glEnable(GL_LIGHTING); 114 | else 115 | glDisable(GL_LIGHTING); 116 | updateGL(); 117 | break; 118 | case Qt::Key_W: 119 | zoom += 0.1; 120 | updateGL(); 121 | break; 122 | case Qt::Key_S: 123 | zoom -= 0.1; 124 | updateGL(); 125 | break; 126 | case Qt::Key_A: 127 | yRot -= 5; 128 | updateGL(); 129 | break; 130 | case Qt::Key_D: 131 | yRot += 5; 132 | updateGL(); 133 | break; 134 | case Qt::Key_E: 135 | xRot -= 5; 136 | updateGL(); 137 | break; 138 | case Qt::Key_C: 139 | xRot += 5; 140 | updateGL(); 141 | break; 142 | case Qt::Key_Z: 143 | zRot += 5; 144 | updateGL(); 145 | break; 146 | case Qt::Key_X: 147 | zRot -= 5; 148 | updateGL(); 149 | break; 150 | case Qt::Key_Space: 151 | xRot = yRot = zRot = 0.0; 152 | zoom = -5.0; 153 | updateGL(); 154 | break; 155 | default: 156 | break; 157 | } 158 | } 159 | 160 | void View::EnableLighting() 161 | { 162 | glEnable(GL_LIGHTING); 163 | updateGL(); 164 | } 165 | void View::DisableLighting(){ 166 | glDisable(GL_LIGHTING); 167 | updateGL(); 168 | } 169 | 170 | void View::XZPlaneView() 171 | { 172 | xRot = 90.0; 173 | glRotated(xRot, 1.0, 1.0, 1.0); 174 | updateGL(); 175 | } 176 | 177 | void View::DrawCoords() 178 | { 179 | GLfloat length = 2.0; 180 | glLoadIdentity(); 181 | glTranslated(0.0, 0.0, zoom); 182 | glLineWidth(3.0); 183 | glBegin(GL_LINES); 184 | { 185 | glColor3f(1.0, 0.0, 0.0); 186 | glVertex3f(0.0, 0.0, 0.0); 187 | glVertex3f(length, 0.0, 0.0); 188 | 189 | glColor3f(0.0, 1.0, 0.0); 190 | glVertex3f(0.0, 0.0, 0.0); 191 | glVertex3f(0.0, length, 0.0); 192 | 193 | glColor3f(0.0, 0.0, 1.0); 194 | glVertex3f(0.0, 0.0, 0.0); 195 | glVertex3f(0.0, 0.0, length); 196 | } 197 | glEnd(); 198 | 199 | glPushMatrix(); 200 | glTranslatef(length, 0.0, 0.0); 201 | glRotatef(90.0, 0.0, 1.0, 0.0); 202 | glColor3f(1.0, 0.0, 0.0); 203 | glutWireCone(0.027,0.09,10,10); 204 | glPopMatrix(); 205 | 206 | glPushMatrix(); 207 | glTranslatef(0.0, length, 0.0); 208 | glRotatef(-90.0, 1.0, 0.0, 0.0); 209 | glColor3f(0.0, 1.0, 0.0); 210 | glutWireCone(0.027,0.09,10,10); 211 | glPopMatrix(); 212 | } 213 | 214 | 215 | 216 | 217 | -------------------------------------------------------------------------------- /OpenGL_Template/view.h: -------------------------------------------------------------------------------- 1 | #ifndef VIEW_H 2 | #define VIEW_H 3 | 4 | #include 5 | /* for mac */ 6 | #ifdef Q_OS_MAC 7 | #include 8 | #endif 9 | /* for windows */ 10 | #ifdef Q_OS_WIN32 11 | #include 12 | #endif 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | class View : public QGLWidget 20 | { 21 | Q_OBJECT 22 | public: 23 | View(QWidget *parent); 24 | 25 | public: 26 | void initializeGL(); 27 | void resizeGL(GLsizei width, GLsizei height); 28 | void paintGL(); 29 | 30 | 31 | public: 32 | GLfloat zoom; 33 | GLfloat xRot, yRot, zRot; 34 | bool lighting; 35 | 36 | 37 | public: 38 | 39 | QPoint lastPos; 40 | void mousePressEvent(QMouseEvent *event); 41 | void mouseMoveEvent(QMouseEvent *event); 42 | void wheelEvent(QWheelEvent *event); 43 | void keyPressEvent(QKeyEvent *event); 44 | 45 | public: 46 | void EnableLighting(); 47 | void DisableLighting(); 48 | void XZPlaneView(); 49 | 50 | 51 | private: 52 | void DrawCoords(); 53 | void DrawTest(); 54 | 55 | 56 | 57 | }; 58 | 59 | #endif // VIEW_H 60 | -------------------------------------------------------------------------------- /lesson1_console/lesson1_console.pro: -------------------------------------------------------------------------------- 1 | QT += core gui widgets opengl 2 | 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = lesson1_console 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp \ 13 | nehewidget.cpp 14 | 15 | 16 | LIBS += -framework opengl -framework glut 17 | 18 | HEADERS += \ 19 | nehewidget.h 20 | -------------------------------------------------------------------------------- /lesson1_console/lesson1_console.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {3de204d9-a1bc-4e3d-9eab-8f1053f1f147} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | 0 45 | 8 46 | true 47 | 1 48 | true 49 | true 50 | true 51 | false 52 | 53 | 54 | 55 | ProjectExplorer.Project.PluginSettings 56 | 57 | 58 | 59 | ProjectExplorer.Project.Target.0 60 | 61 | Desktop Qt 5.6.0 clang 64bit 62 | Desktop Qt 5.6.0 clang 64bit 63 | qt.56.clang_64_kit 64 | 0 65 | 0 66 | 0 67 | 68 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson1_console-Desktop_Qt_5_6_0_clang_64bit-Debug 69 | 70 | 71 | true 72 | qmake 73 | 74 | QtProjectManager.QMakeBuildStep 75 | true 76 | 77 | false 78 | false 79 | false 80 | 81 | 82 | true 83 | Make 84 | 85 | Qt4ProjectManager.MakeStep 86 | 87 | -w 88 | -r 89 | 90 | false 91 | 92 | 93 | 94 | 2 95 | Build 96 | 97 | ProjectExplorer.BuildSteps.Build 98 | 99 | 100 | 101 | true 102 | Make 103 | 104 | Qt4ProjectManager.MakeStep 105 | 106 | -w 107 | -r 108 | 109 | true 110 | clean 111 | 112 | 113 | 1 114 | Clean 115 | 116 | ProjectExplorer.BuildSteps.Clean 117 | 118 | 2 119 | false 120 | 121 | Debug 122 | 123 | Qt4ProjectManager.Qt4BuildConfiguration 124 | 2 125 | true 126 | 127 | 128 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson1_console-Desktop_Qt_5_6_0_clang_64bit-Release 129 | 130 | 131 | true 132 | qmake 133 | 134 | QtProjectManager.QMakeBuildStep 135 | false 136 | 137 | false 138 | false 139 | false 140 | 141 | 142 | true 143 | Make 144 | 145 | Qt4ProjectManager.MakeStep 146 | 147 | -w 148 | -r 149 | 150 | false 151 | 152 | 153 | 154 | 2 155 | Build 156 | 157 | ProjectExplorer.BuildSteps.Build 158 | 159 | 160 | 161 | true 162 | Make 163 | 164 | Qt4ProjectManager.MakeStep 165 | 166 | -w 167 | -r 168 | 169 | true 170 | clean 171 | 172 | 173 | 1 174 | Clean 175 | 176 | ProjectExplorer.BuildSteps.Clean 177 | 178 | 2 179 | false 180 | 181 | Release 182 | 183 | Qt4ProjectManager.Qt4BuildConfiguration 184 | 0 185 | true 186 | 187 | 188 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson1_console-Desktop_Qt_5_6_0_clang_64bit-Profile 189 | 190 | 191 | true 192 | qmake 193 | 194 | QtProjectManager.QMakeBuildStep 195 | true 196 | 197 | false 198 | true 199 | false 200 | 201 | 202 | true 203 | Make 204 | 205 | Qt4ProjectManager.MakeStep 206 | 207 | -w 208 | -r 209 | 210 | false 211 | 212 | 213 | 214 | 2 215 | Build 216 | 217 | ProjectExplorer.BuildSteps.Build 218 | 219 | 220 | 221 | true 222 | Make 223 | 224 | Qt4ProjectManager.MakeStep 225 | 226 | -w 227 | -r 228 | 229 | true 230 | clean 231 | 232 | 233 | 1 234 | Clean 235 | 236 | ProjectExplorer.BuildSteps.Clean 237 | 238 | 2 239 | false 240 | 241 | Profile 242 | 243 | Qt4ProjectManager.Qt4BuildConfiguration 244 | 0 245 | true 246 | 247 | 3 248 | 249 | 250 | 0 251 | Deploy 252 | 253 | ProjectExplorer.BuildSteps.Deploy 254 | 255 | 1 256 | Deploy locally 257 | 258 | ProjectExplorer.DefaultDeployConfiguration 259 | 260 | 1 261 | 262 | 263 | false 264 | 1000 265 | 266 | true 267 | 268 | false 269 | false 270 | false 271 | false 272 | true 273 | 0.01 274 | 10 275 | true 276 | 1 277 | 25 278 | 279 | 1 280 | true 281 | false 282 | true 283 | valgrind 284 | 285 | 0 286 | 1 287 | 2 288 | 3 289 | 4 290 | 5 291 | 6 292 | 7 293 | 8 294 | 9 295 | 10 296 | 11 297 | 12 298 | 13 299 | 14 300 | 301 | 2 302 | 303 | lesson1_console 304 | 305 | Qt4ProjectManager.Qt4RunConfiguration:/Users/Haoyang/Documents/TestCodes/Qt_OpenGL/lesson1_console/lesson1_console.pro 306 | true 307 | 308 | lesson1_console.pro 309 | false 310 | true 311 | 312 | 3768 313 | false 314 | true 315 | false 316 | false 317 | true 318 | 319 | 1 320 | 321 | 322 | 323 | ProjectExplorer.Project.TargetCount 324 | 1 325 | 326 | 327 | ProjectExplorer.Project.Updater.FileVersion 328 | 18 329 | 330 | 331 | Version 332 | 18 333 | 334 | 335 | -------------------------------------------------------------------------------- /lesson1_console/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "nehewidget.h" 3 | int main(int argc, char *argv[]) 4 | { 5 | QApplication a(argc, argv); 6 | 7 | 8 | NeheWidget w; 9 | w.resize(400,300); 10 | w.show(); 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /lesson1_console/nehewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "nehewidget.h" 2 | 3 | NeheWidget::NeheWidget(QWidget *parent, bool fs) 4 | :QGLWidget(parent) 5 | { 6 | fullScreen = fs; 7 | } 8 | 9 | void NeheWidget::initializeGL() 10 | { 11 | glShadeModel(GL_SMOOTH); 12 | glClearColor(0.f, 0.f, 0.f, 0.f); 13 | glEnable(GL_DEPTH_TEST); 14 | } 15 | 16 | void NeheWidget::resizeGL(int w, int h) 17 | { 18 | glViewport(0, 0, (GLint)w, h); 19 | glMatrixMode(GL_PROJECTION); 20 | glLoadIdentity(); 21 | gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1f, 100.f); 22 | 23 | glMatrixMode(GL_MODELVIEW); 24 | glLoadIdentity(); 25 | } 26 | 27 | void NeheWidget::paintGL() 28 | { 29 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 30 | 31 | glMatrixMode(GL_MODELVIEW); 32 | glLoadIdentity(); 33 | 34 | glTranslatef(0.f, 0.f, -6.f); 35 | glColor3f(1.f, 1.f, 1.f); 36 | glBegin(GL_TRIANGLES); 37 | glVertex3f(0.f, 1.f, 0.f); 38 | glVertex3f(1.f, -1.f, 0.f); 39 | glVertex3f(-1.f, -1.f, 0.f); 40 | glEnd(); 41 | } 42 | -------------------------------------------------------------------------------- /lesson1_console/nehewidget.h: -------------------------------------------------------------------------------- 1 | #ifndef NEHEWIDGET_H 2 | #define NEHEWIDGET_H 3 | 4 | #include 5 | #include 6 | 7 | class NeheWidget : public QGLWidget 8 | { 9 | Q_OBJECT 10 | public: 11 | NeheWidget(QWidget *parent = 0, bool fs = false); 12 | 13 | protected: 14 | void initializeGL(); 15 | void resizeGL(int w, int h); 16 | void paintGL(); 17 | 18 | private: 19 | bool fullScreen; 20 | }; 21 | 22 | #endif // NEHEWIDGET_H 23 | -------------------------------------------------------------------------------- /lesson3_coloring_console /lesson2_console.pro: -------------------------------------------------------------------------------- 1 | QT += core gui widgets opengl 2 | 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = lesson1_console 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp \ 13 | nehewidget.cpp 14 | 15 | 16 | LIBS += -framework opengl -framework glut 17 | 18 | HEADERS += \ 19 | nehewidget.h 20 | -------------------------------------------------------------------------------- /lesson3_coloring_console /lesson2_console.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {3de204d9-a1bc-4e3d-9eab-8f1053f1f147} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | 0 45 | 8 46 | true 47 | 1 48 | true 49 | true 50 | true 51 | false 52 | 53 | 54 | 55 | ProjectExplorer.Project.PluginSettings 56 | 57 | 58 | 59 | ProjectExplorer.Project.Target.0 60 | 61 | Desktop Qt 5.6.0 clang 64bit 62 | Desktop Qt 5.6.0 clang 64bit 63 | qt.56.clang_64_kit 64 | 0 65 | 0 66 | 0 67 | 68 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson2_console-Desktop_Qt_5_6_0_clang_64bit-Debug 69 | 70 | 71 | true 72 | qmake 73 | 74 | QtProjectManager.QMakeBuildStep 75 | true 76 | 77 | false 78 | false 79 | false 80 | 81 | 82 | true 83 | Make 84 | 85 | Qt4ProjectManager.MakeStep 86 | 87 | -w 88 | -r 89 | 90 | false 91 | 92 | 93 | 94 | 2 95 | Build 96 | 97 | ProjectExplorer.BuildSteps.Build 98 | 99 | 100 | 101 | true 102 | Make 103 | 104 | Qt4ProjectManager.MakeStep 105 | 106 | -w 107 | -r 108 | 109 | true 110 | clean 111 | 112 | 113 | 1 114 | Clean 115 | 116 | ProjectExplorer.BuildSteps.Clean 117 | 118 | 2 119 | false 120 | 121 | Debug 122 | 123 | Qt4ProjectManager.Qt4BuildConfiguration 124 | 2 125 | true 126 | 127 | 128 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson2_console-Desktop_Qt_5_6_0_clang_64bit-Release 129 | 130 | 131 | true 132 | qmake 133 | 134 | QtProjectManager.QMakeBuildStep 135 | false 136 | 137 | false 138 | false 139 | false 140 | 141 | 142 | true 143 | Make 144 | 145 | Qt4ProjectManager.MakeStep 146 | 147 | -w 148 | -r 149 | 150 | false 151 | 152 | 153 | 154 | 2 155 | Build 156 | 157 | ProjectExplorer.BuildSteps.Build 158 | 159 | 160 | 161 | true 162 | Make 163 | 164 | Qt4ProjectManager.MakeStep 165 | 166 | -w 167 | -r 168 | 169 | true 170 | clean 171 | 172 | 173 | 1 174 | Clean 175 | 176 | ProjectExplorer.BuildSteps.Clean 177 | 178 | 2 179 | false 180 | 181 | Release 182 | 183 | Qt4ProjectManager.Qt4BuildConfiguration 184 | 0 185 | true 186 | 187 | 188 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson2_console-Desktop_Qt_5_6_0_clang_64bit-Profile 189 | 190 | 191 | true 192 | qmake 193 | 194 | QtProjectManager.QMakeBuildStep 195 | true 196 | 197 | false 198 | true 199 | false 200 | 201 | 202 | true 203 | Make 204 | 205 | Qt4ProjectManager.MakeStep 206 | 207 | -w 208 | -r 209 | 210 | false 211 | 212 | 213 | 214 | 2 215 | Build 216 | 217 | ProjectExplorer.BuildSteps.Build 218 | 219 | 220 | 221 | true 222 | Make 223 | 224 | Qt4ProjectManager.MakeStep 225 | 226 | -w 227 | -r 228 | 229 | true 230 | clean 231 | 232 | 233 | 1 234 | Clean 235 | 236 | ProjectExplorer.BuildSteps.Clean 237 | 238 | 2 239 | false 240 | 241 | Profile 242 | 243 | Qt4ProjectManager.Qt4BuildConfiguration 244 | 0 245 | true 246 | 247 | 3 248 | 249 | 250 | 0 251 | Deploy 252 | 253 | ProjectExplorer.BuildSteps.Deploy 254 | 255 | 1 256 | Deploy locally 257 | 258 | ProjectExplorer.DefaultDeployConfiguration 259 | 260 | 1 261 | 262 | 263 | false 264 | 1000 265 | 266 | true 267 | 268 | false 269 | false 270 | false 271 | false 272 | true 273 | 0.01 274 | 10 275 | true 276 | 1 277 | 25 278 | 279 | 1 280 | true 281 | false 282 | true 283 | valgrind 284 | 285 | 0 286 | 1 287 | 2 288 | 3 289 | 4 290 | 5 291 | 6 292 | 7 293 | 8 294 | 9 295 | 10 296 | 11 297 | 12 298 | 13 299 | 14 300 | 301 | 2 302 | 303 | lesson2_console 304 | 305 | Qt4ProjectManager.Qt4RunConfiguration:/Users/Haoyang/Documents/TestCodes/Qt_OpenGL/lesson3_coloring_console /lesson2_console.pro 306 | true 307 | 308 | lesson2_console.pro 309 | false 310 | true 311 | 312 | 3768 313 | false 314 | true 315 | false 316 | false 317 | true 318 | 319 | 1 320 | 321 | 322 | 323 | ProjectExplorer.Project.TargetCount 324 | 1 325 | 326 | 327 | ProjectExplorer.Project.Updater.FileVersion 328 | 18 329 | 330 | 331 | Version 332 | 18 333 | 334 | 335 | -------------------------------------------------------------------------------- /lesson3_coloring_console /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "nehewidget.h" 3 | int main(int argc, char *argv[]) 4 | { 5 | QApplication a(argc, argv); 6 | 7 | 8 | NeheWidget w; 9 | w.resize(400,300); 10 | w.show(); 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /lesson3_coloring_console /nehewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "nehewidget.h" 2 | 3 | NeheWidget::NeheWidget(QWidget *parent, bool fs) 4 | :QGLWidget(parent) 5 | { 6 | fullScreen = fs; 7 | } 8 | 9 | void NeheWidget::initializeGL() 10 | { 11 | glShadeModel(GL_SMOOTH); 12 | // glShadeModel(GL_FLAT); 13 | glClearColor(0.f, 0.f, 0.f, 0.f); 14 | glEnable(GL_DEPTH_TEST); 15 | } 16 | 17 | void NeheWidget::resizeGL(int w, int h) 18 | { 19 | glViewport(0, 0, (GLint)w, h); 20 | glMatrixMode(GL_PROJECTION); 21 | glLoadIdentity(); 22 | gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1f, 100.f); 23 | 24 | glMatrixMode(GL_MODELVIEW); 25 | glLoadIdentity(); 26 | } 27 | 28 | void NeheWidget::paintGL() 29 | { 30 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 31 | 32 | glMatrixMode(GL_MODELVIEW); 33 | glLoadIdentity(); 34 | drawTriangle(); 35 | drawSquare(); 36 | 37 | } 38 | 39 | void NeheWidget::drawTriangle() 40 | { 41 | glLoadIdentity(); 42 | glTranslatef(-1.5f, 0.f, -6.f); 43 | 44 | glBegin(GL_TRIANGLES); 45 | glColor3f(1.f, 0.f, 0.f); 46 | glVertex3f(0.f, 1.f, 0.f); 47 | glColor3f(0.f, 1.f, 0.f); 48 | glVertex3f(1.f, -1.f, 0.f); 49 | glColor3f(0.f, 0.f, 1.f); 50 | glVertex3f(-1.f, -1.f, 0.f); 51 | glEnd(); 52 | } 53 | 54 | void NeheWidget::drawSquare() 55 | { 56 | glLoadIdentity(); 57 | glTranslatef(1.5, 0.0, -6.f); 58 | 59 | // 会为蓝色,因为在paintGL()中,先调用了drawTriangle(), 最后一个顶点用的深蓝色,之后为做改动, 60 | // 所以,这里仍为蓝色. 61 | glBegin( GL_QUADS ); 62 | glVertex3f( -1.0, 1.0, 0.0 ); 63 | glVertex3f( 1.0, 1.0, 0.0 ); 64 | glVertex3f( 1.0, -1.0, 0.0 ); 65 | glVertex3f( -1.0, -1.0, 0.0 ); 66 | glEnd(); 67 | } 68 | -------------------------------------------------------------------------------- /lesson3_coloring_console /nehewidget.h: -------------------------------------------------------------------------------- 1 | #ifndef NEHEWIDGET_H 2 | #define NEHEWIDGET_H 3 | 4 | #include 5 | #include 6 | 7 | class NeheWidget : public QGLWidget 8 | { 9 | Q_OBJECT 10 | public: 11 | NeheWidget(QWidget *parent = 0, bool fs = false); 12 | 13 | protected: 14 | void initializeGL(); 15 | void resizeGL(int w, int h); 16 | void paintGL(); 17 | 18 | private: 19 | bool fullScreen; 20 | 21 | private: 22 | void drawTriangle(); 23 | void drawSquare(); 24 | }; 25 | 26 | #endif // NEHEWIDGET_H 27 | -------------------------------------------------------------------------------- /lesson4_3D_console/lesson4_console.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {3de204d9-a1bc-4e3d-9eab-8f1053f1f147} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | 0 45 | 8 46 | true 47 | 1 48 | true 49 | true 50 | true 51 | false 52 | 53 | 54 | 55 | ProjectExplorer.Project.PluginSettings 56 | 57 | 58 | 59 | ProjectExplorer.Project.Target.0 60 | 61 | Desktop Qt 5.6.0 clang 64bit 62 | Desktop Qt 5.6.0 clang 64bit 63 | qt.56.clang_64_kit 64 | 0 65 | 0 66 | 0 67 | 68 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson4_console-Desktop_Qt_5_6_0_clang_64bit-Debug 69 | 70 | 71 | true 72 | qmake 73 | 74 | QtProjectManager.QMakeBuildStep 75 | true 76 | 77 | false 78 | false 79 | false 80 | 81 | 82 | true 83 | Make 84 | 85 | Qt4ProjectManager.MakeStep 86 | 87 | -w 88 | -r 89 | 90 | false 91 | 92 | 93 | 94 | 2 95 | Build 96 | 97 | ProjectExplorer.BuildSteps.Build 98 | 99 | 100 | 101 | true 102 | Make 103 | 104 | Qt4ProjectManager.MakeStep 105 | 106 | -w 107 | -r 108 | 109 | true 110 | clean 111 | 112 | 113 | 1 114 | Clean 115 | 116 | ProjectExplorer.BuildSteps.Clean 117 | 118 | 2 119 | false 120 | 121 | Debug 122 | 123 | Qt4ProjectManager.Qt4BuildConfiguration 124 | 2 125 | true 126 | 127 | 128 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson4_console-Desktop_Qt_5_6_0_clang_64bit-Release 129 | 130 | 131 | true 132 | qmake 133 | 134 | QtProjectManager.QMakeBuildStep 135 | false 136 | 137 | false 138 | false 139 | false 140 | 141 | 142 | true 143 | Make 144 | 145 | Qt4ProjectManager.MakeStep 146 | 147 | -w 148 | -r 149 | 150 | false 151 | 152 | 153 | 154 | 2 155 | Build 156 | 157 | ProjectExplorer.BuildSteps.Build 158 | 159 | 160 | 161 | true 162 | Make 163 | 164 | Qt4ProjectManager.MakeStep 165 | 166 | -w 167 | -r 168 | 169 | true 170 | clean 171 | 172 | 173 | 1 174 | Clean 175 | 176 | ProjectExplorer.BuildSteps.Clean 177 | 178 | 2 179 | false 180 | 181 | Release 182 | 183 | Qt4ProjectManager.Qt4BuildConfiguration 184 | 0 185 | true 186 | 187 | 188 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson4_console-Desktop_Qt_5_6_0_clang_64bit-Profile 189 | 190 | 191 | true 192 | qmake 193 | 194 | QtProjectManager.QMakeBuildStep 195 | true 196 | 197 | false 198 | true 199 | false 200 | 201 | 202 | true 203 | Make 204 | 205 | Qt4ProjectManager.MakeStep 206 | 207 | -w 208 | -r 209 | 210 | false 211 | 212 | 213 | 214 | 2 215 | Build 216 | 217 | ProjectExplorer.BuildSteps.Build 218 | 219 | 220 | 221 | true 222 | Make 223 | 224 | Qt4ProjectManager.MakeStep 225 | 226 | -w 227 | -r 228 | 229 | true 230 | clean 231 | 232 | 233 | 1 234 | Clean 235 | 236 | ProjectExplorer.BuildSteps.Clean 237 | 238 | 2 239 | false 240 | 241 | Profile 242 | 243 | Qt4ProjectManager.Qt4BuildConfiguration 244 | 0 245 | true 246 | 247 | 3 248 | 249 | 250 | 0 251 | Deploy 252 | 253 | ProjectExplorer.BuildSteps.Deploy 254 | 255 | 1 256 | Deploy locally 257 | 258 | ProjectExplorer.DefaultDeployConfiguration 259 | 260 | 1 261 | 262 | 263 | false 264 | 1000 265 | 266 | true 267 | 268 | false 269 | false 270 | false 271 | false 272 | true 273 | 0.01 274 | 10 275 | true 276 | 1 277 | 25 278 | 279 | 1 280 | true 281 | false 282 | true 283 | valgrind 284 | 285 | 0 286 | 1 287 | 2 288 | 3 289 | 4 290 | 5 291 | 6 292 | 7 293 | 8 294 | 9 295 | 10 296 | 11 297 | 12 298 | 13 299 | 14 300 | 301 | 2 302 | 303 | lesson4_console 304 | 305 | Qt4ProjectManager.Qt4RunConfiguration:/Users/Haoyang/Documents/TestCodes/Qt_OpenGL/lesson4_rotationAroundAxis_console/lesson4_console.pro 306 | true 307 | 308 | lesson4_console.pro 309 | false 310 | true 311 | 312 | 3768 313 | false 314 | true 315 | false 316 | false 317 | true 318 | 319 | 1 320 | 321 | 322 | 323 | ProjectExplorer.Project.TargetCount 324 | 1 325 | 326 | 327 | ProjectExplorer.Project.Updater.FileVersion 328 | 18 329 | 330 | 331 | Version 332 | 18 333 | 334 | 335 | -------------------------------------------------------------------------------- /lesson4_3D_console/lesson5_console.pro: -------------------------------------------------------------------------------- 1 | QT += core gui widgets opengl 2 | 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = lesson1_console 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp \ 13 | nehewidget.cpp 14 | 15 | mac{ 16 | LIBS += -framework opengl -framework glut 17 | } 18 | HEADERS += \ 19 | nehewidget.h 20 | -------------------------------------------------------------------------------- /lesson4_3D_console/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "nehewidget.h" 3 | int main(int argc, char *argv[]) 4 | { 5 | QApplication a(argc, argv); 6 | 7 | 8 | NeheWidget w; 9 | w.resize(400,300); 10 | w.show(); 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /lesson4_3D_console/nehewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "nehewidget.h" 2 | 3 | NeheWidget::NeheWidget(QWidget *parent, bool fs) 4 | :QGLWidget(parent) 5 | { 6 | fullScreen = fs; 7 | 8 | tRot = 10.0; 9 | qRot = 10.0; 10 | } 11 | 12 | void NeheWidget::initializeGL() 13 | { 14 | // glShadeModel(GL_SMOOTH); 15 | // glShadeModel(GL_FLAT); 16 | glClearColor(0.f, 0.f, 0.f, 0.f); 17 | glEnable(GL_DEPTH_TEST); 18 | } 19 | 20 | void NeheWidget::resizeGL(int w, int h) 21 | { 22 | glViewport(0, 0, (GLint)w, h); 23 | glMatrixMode(GL_PROJECTION); 24 | glLoadIdentity(); 25 | gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1f, 100.f); 26 | 27 | glMatrixMode(GL_MODELVIEW); 28 | glLoadIdentity(); 29 | } 30 | 31 | void NeheWidget::paintGL() 32 | { 33 | 34 | 35 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 36 | glLoadIdentity(); 37 | glTranslatef( -1.5, 0.0, -6.0 ); 38 | glRotatef( tRot, 0.0, 1.0, 0.0 ); 39 | 40 | glBegin( GL_TRIANGLES ); 41 | glColor3f( 1.0, 0.0, 0.0 ); 42 | glVertex3f( 0.0, 1.0, 0.0 ); 43 | glColor3f( 0.0, 1.0, 0.0 ); 44 | glVertex3f( -1.0, -1.0, 0.0 ); 45 | glColor3f( 0.0, 0.0, 1.0 ); 46 | glVertex3f( 1.0, -1.0, 0.0 ); 47 | glEnd(); 48 | 49 | glLoadIdentity(); 50 | glTranslatef( 1.5, 0.0, -6.0 ); 51 | glRotatef( qRot, 0.0, 1.0, 0.0 ); 52 | 53 | glColor3f( 0.5, 0.5, 1.0 ); 54 | glBegin( GL_QUADS ); 55 | glVertex3f( -1.0, 1.0, 0.0 ); 56 | glVertex3f( 1.0, 1.0, 0.0 ); 57 | glVertex3f( 1.0, -1.0, 0.0 ); 58 | glVertex3f( -1.0, -1.0, 0.0 ); 59 | glEnd(); 60 | 61 | tRot += 2; 62 | qRot -= 1; 63 | 64 | 65 | } 66 | 67 | void NeheWidget::drawTriangle() 68 | { 69 | glLoadIdentity(); 70 | 71 | glTranslatef(-1.5f, 0.f, -6.f); 72 | glRotatef(45, 1.0, 0.0, 0.0); 73 | 74 | 75 | glBegin(GL_TRIANGLES); 76 | glColor3f(1.f, 0.f, 0.f); 77 | glVertex3f(0.f, 1.f, 0.f); 78 | glColor3f(0.f, 1.f, 0.f); 79 | glVertex3f(1.f, -1.f, 0.f); 80 | glColor3f(0.f, 0.f, 1.f); 81 | glVertex3f(-1.f, -1.f, 0.f); 82 | glEnd(); 83 | tRot += 1; 84 | 85 | } 86 | 87 | void NeheWidget::drawSquare() 88 | { 89 | glLoadIdentity(); 90 | glTranslatef(1.5, 0.0, -6.f); 91 | 92 | 93 | glBegin( GL_QUADS ); 94 | glVertex3f( -1.0, 1.0, 0.0 ); 95 | glVertex3f( 1.0, 1.0, 0.0 ); 96 | glVertex3f( 1.0, -1.0, 0.0 ); 97 | glVertex3f( -1.0, -1.0, 0.0 ); 98 | glEnd(); 99 | } 100 | -------------------------------------------------------------------------------- /lesson4_3D_console/nehewidget.h: -------------------------------------------------------------------------------- 1 | #ifndef NEHEWIDGET_H 2 | #define NEHEWIDGET_H 3 | 4 | #include 5 | //#include 6 | #include 7 | 8 | class NeheWidget : public QGLWidget 9 | { 10 | Q_OBJECT 11 | public: 12 | NeheWidget(QWidget *parent = 0, bool fs = false); 13 | 14 | protected: 15 | void initializeGL(); 16 | void resizeGL(int w, int h); 17 | void paintGL(); 18 | 19 | private: 20 | bool fullScreen; 21 | 22 | GLfloat tRot; // control the rotation of triangle 23 | GLfloat qRot; // control the degree of rotation of square 24 | 25 | private: 26 | void drawTriangle(); 27 | void drawSquare(); 28 | }; 29 | 30 | #endif // NEHEWIDGET_H 31 | -------------------------------------------------------------------------------- /lesson4_rotationAroundAxis_console/lesson4_console.pro: -------------------------------------------------------------------------------- 1 | QT += core gui widgets opengl 2 | 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = lesson1_console 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp \ 13 | nehewidget.cpp 14 | 15 | mac{ 16 | LIBS += -framework opengl -framework glut 17 | } 18 | HEADERS += \ 19 | nehewidget.h 20 | -------------------------------------------------------------------------------- /lesson4_rotationAroundAxis_console/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "nehewidget.h" 3 | int main(int argc, char *argv[]) 4 | { 5 | QApplication a(argc, argv); 6 | 7 | 8 | NeheWidget w; 9 | w.resize(400,300); 10 | w.show(); 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /lesson4_rotationAroundAxis_console/nehewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "nehewidget.h" 2 | 3 | NeheWidget::NeheWidget(QWidget *parent, bool fs) 4 | :QGLWidget(parent) 5 | { 6 | fullScreen = fs; 7 | 8 | tRot = 10.0; 9 | qRot = 10.0; 10 | } 11 | 12 | void NeheWidget::initializeGL() 13 | { 14 | // glShadeModel(GL_SMOOTH); 15 | // glShadeModel(GL_FLAT); 16 | glClearColor(0.f, 0.f, 0.f, 0.f); 17 | glEnable(GL_DEPTH_TEST); 18 | } 19 | 20 | void NeheWidget::resizeGL(int w, int h) 21 | { 22 | glViewport(0, 0, (GLint)w, h); 23 | glMatrixMode(GL_PROJECTION); 24 | glLoadIdentity(); 25 | gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1f, 100.f); 26 | 27 | glMatrixMode(GL_MODELVIEW); 28 | glLoadIdentity(); 29 | } 30 | 31 | void NeheWidget::paintGL() 32 | { 33 | 34 | 35 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 36 | glLoadIdentity(); 37 | glTranslatef( -1.5, 0.0, -6.0 ); 38 | glRotatef( tRot, 0.0, 1.0, 0.0 ); 39 | 40 | glBegin( GL_TRIANGLES ); 41 | glColor3f( 1.0, 0.0, 0.0 ); 42 | glVertex3f( 0.0, 1.0, 0.0 ); 43 | glColor3f( 0.0, 1.0, 0.0 ); 44 | glVertex3f( -1.0, -1.0, 0.0 ); 45 | glColor3f( 0.0, 0.0, 1.0 ); 46 | glVertex3f( 1.0, -1.0, 0.0 ); 47 | glEnd(); 48 | 49 | glLoadIdentity(); 50 | glTranslatef( 1.5, 0.0, -6.0 ); 51 | glRotatef( qRot, 0.0, 1.0, 0.0 ); 52 | 53 | glColor3f( 0.5, 0.5, 1.0 ); 54 | glBegin( GL_QUADS ); 55 | glVertex3f( -1.0, 1.0, 0.0 ); 56 | glVertex3f( 1.0, 1.0, 0.0 ); 57 | glVertex3f( 1.0, -1.0, 0.0 ); 58 | glVertex3f( -1.0, -1.0, 0.0 ); 59 | glEnd(); 60 | 61 | tRot += 2; 62 | qRot -= 1; 63 | 64 | 65 | } 66 | 67 | void NeheWidget::drawTriangle() 68 | { 69 | glLoadIdentity(); 70 | 71 | glTranslatef(-1.5f, 0.f, -6.f); 72 | glRotatef(45, 1.0, 0.0, 0.0); 73 | 74 | 75 | glBegin(GL_TRIANGLES); 76 | glColor3f(1.f, 0.f, 0.f); 77 | glVertex3f(0.f, 1.f, 0.f); 78 | glColor3f(0.f, 1.f, 0.f); 79 | glVertex3f(1.f, -1.f, 0.f); 80 | glColor3f(0.f, 0.f, 1.f); 81 | glVertex3f(-1.f, -1.f, 0.f); 82 | glEnd(); 83 | tRot += 1; 84 | 85 | } 86 | 87 | void NeheWidget::drawSquare() 88 | { 89 | glLoadIdentity(); 90 | glTranslatef(1.5, 0.0, -6.f); 91 | 92 | 93 | glBegin( GL_QUADS ); 94 | glVertex3f( -1.0, 1.0, 0.0 ); 95 | glVertex3f( 1.0, 1.0, 0.0 ); 96 | glVertex3f( 1.0, -1.0, 0.0 ); 97 | glVertex3f( -1.0, -1.0, 0.0 ); 98 | glEnd(); 99 | } 100 | -------------------------------------------------------------------------------- /lesson4_rotationAroundAxis_console/nehewidget.h: -------------------------------------------------------------------------------- 1 | #ifndef NEHEWIDGET_H 2 | #define NEHEWIDGET_H 3 | 4 | #include 5 | //#include 6 | #include 7 | 8 | class NeheWidget : public QGLWidget 9 | { 10 | Q_OBJECT 11 | public: 12 | NeheWidget(QWidget *parent = 0, bool fs = false); 13 | 14 | protected: 15 | void initializeGL(); 16 | void resizeGL(int w, int h); 17 | void paintGL(); 18 | 19 | private: 20 | bool fullScreen; 21 | 22 | GLfloat tRot; // control the rotation of triangle 23 | GLfloat qRot; // control the degree of rotation of square 24 | 25 | private: 26 | void drawTriangle(); 27 | void drawSquare(); 28 | }; 29 | 30 | #endif // NEHEWIDGET_H 31 | -------------------------------------------------------------------------------- /lesson5_3D_console/lesson5_console.pro: -------------------------------------------------------------------------------- 1 | QT += core gui widgets opengl 2 | 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = lesson1_console 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp \ 13 | nehewidget.cpp 14 | 15 | mac{ 16 | LIBS += -framework opengl -framework glut 17 | } 18 | HEADERS += \ 19 | nehewidget.h 20 | -------------------------------------------------------------------------------- /lesson5_3D_console/lesson5_console.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {3de204d9-a1bc-4e3d-9eab-8f1053f1f147} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | 0 45 | 8 46 | true 47 | 1 48 | true 49 | true 50 | true 51 | false 52 | 53 | 54 | 55 | ProjectExplorer.Project.PluginSettings 56 | 57 | 58 | 59 | ProjectExplorer.Project.Target.0 60 | 61 | Desktop Qt 5.6.0 clang 64bit 62 | Desktop Qt 5.6.0 clang 64bit 63 | qt.56.clang_64_kit 64 | 0 65 | 0 66 | 0 67 | 68 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson5_console-Desktop_Qt_5_6_0_clang_64bit-Debug 69 | 70 | 71 | true 72 | qmake 73 | 74 | QtProjectManager.QMakeBuildStep 75 | true 76 | 77 | false 78 | false 79 | false 80 | 81 | 82 | true 83 | Make 84 | 85 | Qt4ProjectManager.MakeStep 86 | 87 | -w 88 | -r 89 | 90 | false 91 | 92 | 93 | 94 | 2 95 | Build 96 | 97 | ProjectExplorer.BuildSteps.Build 98 | 99 | 100 | 101 | true 102 | Make 103 | 104 | Qt4ProjectManager.MakeStep 105 | 106 | -w 107 | -r 108 | 109 | true 110 | clean 111 | 112 | 113 | 1 114 | Clean 115 | 116 | ProjectExplorer.BuildSteps.Clean 117 | 118 | 2 119 | false 120 | 121 | Debug 122 | 123 | Qt4ProjectManager.Qt4BuildConfiguration 124 | 2 125 | true 126 | 127 | 128 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson5_console-Desktop_Qt_5_6_0_clang_64bit-Release 129 | 130 | 131 | true 132 | qmake 133 | 134 | QtProjectManager.QMakeBuildStep 135 | false 136 | 137 | false 138 | false 139 | false 140 | 141 | 142 | true 143 | Make 144 | 145 | Qt4ProjectManager.MakeStep 146 | 147 | -w 148 | -r 149 | 150 | false 151 | 152 | 153 | 154 | 2 155 | Build 156 | 157 | ProjectExplorer.BuildSteps.Build 158 | 159 | 160 | 161 | true 162 | Make 163 | 164 | Qt4ProjectManager.MakeStep 165 | 166 | -w 167 | -r 168 | 169 | true 170 | clean 171 | 172 | 173 | 1 174 | Clean 175 | 176 | ProjectExplorer.BuildSteps.Clean 177 | 178 | 2 179 | false 180 | 181 | Release 182 | 183 | Qt4ProjectManager.Qt4BuildConfiguration 184 | 0 185 | true 186 | 187 | 188 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson5_console-Desktop_Qt_5_6_0_clang_64bit-Profile 189 | 190 | 191 | true 192 | qmake 193 | 194 | QtProjectManager.QMakeBuildStep 195 | true 196 | 197 | false 198 | true 199 | false 200 | 201 | 202 | true 203 | Make 204 | 205 | Qt4ProjectManager.MakeStep 206 | 207 | -w 208 | -r 209 | 210 | false 211 | 212 | 213 | 214 | 2 215 | Build 216 | 217 | ProjectExplorer.BuildSteps.Build 218 | 219 | 220 | 221 | true 222 | Make 223 | 224 | Qt4ProjectManager.MakeStep 225 | 226 | -w 227 | -r 228 | 229 | true 230 | clean 231 | 232 | 233 | 1 234 | Clean 235 | 236 | ProjectExplorer.BuildSteps.Clean 237 | 238 | 2 239 | false 240 | 241 | Profile 242 | 243 | Qt4ProjectManager.Qt4BuildConfiguration 244 | 0 245 | true 246 | 247 | 3 248 | 249 | 250 | 0 251 | Deploy 252 | 253 | ProjectExplorer.BuildSteps.Deploy 254 | 255 | 1 256 | Deploy locally 257 | 258 | ProjectExplorer.DefaultDeployConfiguration 259 | 260 | 1 261 | 262 | 263 | false 264 | 1000 265 | 266 | true 267 | 268 | false 269 | false 270 | false 271 | false 272 | true 273 | 0.01 274 | 10 275 | true 276 | 1 277 | 25 278 | 279 | 1 280 | true 281 | false 282 | true 283 | valgrind 284 | 285 | 0 286 | 1 287 | 2 288 | 3 289 | 4 290 | 5 291 | 6 292 | 7 293 | 8 294 | 9 295 | 10 296 | 11 297 | 12 298 | 13 299 | 14 300 | 301 | 2 302 | 303 | lesson5_console 304 | lesson5_console2 305 | Qt4ProjectManager.Qt4RunConfiguration:/Users/Haoyang/Documents/TestCodes/Qt_OpenGL/lesson5_3D_console/lesson5_console.pro 306 | true 307 | 308 | lesson5_console.pro 309 | false 310 | true 311 | 312 | 3768 313 | false 314 | true 315 | false 316 | false 317 | true 318 | 319 | 1 320 | 321 | 322 | 323 | ProjectExplorer.Project.TargetCount 324 | 1 325 | 326 | 327 | ProjectExplorer.Project.Updater.FileVersion 328 | 18 329 | 330 | 331 | Version 332 | 18 333 | 334 | 335 | -------------------------------------------------------------------------------- /lesson5_3D_console/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "nehewidget.h" 3 | int main(int argc, char *argv[]) 4 | { 5 | QApplication a(argc, argv); 6 | 7 | 8 | NeheWidget w; 9 | w.resize(400,300); 10 | w.show(); 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /lesson5_3D_console/nehewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "nehewidget.h" 2 | 3 | NeheWidget::NeheWidget(QWidget *parent, bool fs) 4 | :QGLWidget(parent) 5 | { 6 | fullScreen = fs; 7 | 8 | rTri = 10.0; 9 | rQuad = 10.0; 10 | 11 | startTimer(15); 12 | } 13 | 14 | void NeheWidget::initializeGL() 15 | { 16 | // glShadeModel(GL_SMOOTH); 17 | // glShadeModel(GL_FLAT); 18 | glClearColor(0.f, 0.f, 0.f, 0.f); 19 | glEnable(GL_DEPTH_TEST); 20 | } 21 | 22 | void NeheWidget::resizeGL(int w, int h) 23 | { 24 | glViewport(0, 0, (GLint)w, h); 25 | glMatrixMode(GL_PROJECTION); 26 | glLoadIdentity(); 27 | gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1f, 100.f); 28 | 29 | glMatrixMode(GL_MODELVIEW); 30 | glLoadIdentity(); 31 | } 32 | 33 | void NeheWidget::paintGL() 34 | { 35 | 36 | 37 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 38 | glLoadIdentity(); 39 | glTranslatef( -1.5, 0.0, -6.0 ); 40 | glRotatef( rTri, 0.0, 1.0, 0.0 ); 41 | 42 | glBegin( GL_TRIANGLES ); 43 | glColor3f( 1.0, 0.0, 0.0 ); 44 | glVertex3f( 0.0, 1.0, 0.0 ); 45 | glColor3f( 0.0, 1.0, 0.0 ); 46 | glVertex3f( -1.0, -1.0, 1.0 ); 47 | glColor3f( 0.0, 0.0, 1.0 ); 48 | glVertex3f( 1.0, -1.0, 1.0 ); 49 | 50 | glColor3f( 1.0, 0.0, 0.0 ); 51 | glVertex3f( 0.0, 1.0, 0.0 ); 52 | glColor3f( 0.0, 0.0, 1.0 ); 53 | glVertex3f( 1.0, -1.0, 1.0 ); 54 | glColor3f( 0.0, 1.0, 0.0 ); 55 | glVertex3f( 1.0, -1.0, -1.0 ); 56 | 57 | 58 | glColor3f( 1.0, 0.0, 0.0 ); 59 | glVertex3f( 0.0, 1.0, 0.0 ); 60 | glColor3f( 0.0, 1.0, 0.0 ); 61 | glVertex3f( 1.0, -1.0, -1.0 ); 62 | glColor3f( 0.0, 0.0, 1.0 ); 63 | glVertex3f( -1.0, -1.0, -1.0 ); 64 | 65 | 66 | glColor3f( 1.0, 0.0, 0.0 ); 67 | glVertex3f( 0.0, 1.0, 0.0 ); 68 | glColor3f( 0.0, 0.0, 1.0 ); 69 | glVertex3f( -1.0, -1.0, -1.0 ); 70 | glColor3f( 0.0, 1.0, 0.0 ); 71 | glVertex3f( -1.0, -1.0, 1.0 ); 72 | 73 | glEnd(); 74 | 75 | glLoadIdentity(); 76 | glTranslatef( 1.5, 0.0, -7.0 ); 77 | glRotatef( rQuad, 1.0, 1.0, 1.0 ); 78 | 79 | glBegin( GL_QUADS ); 80 | glColor3f( 0.0, 1.0, 0.0 ); 81 | glVertex3f( 1.0, 1.0, -1.0 ); 82 | glVertex3f( -1.0, 1.0, -1.0 ); 83 | glVertex3f( -1.0, 1.0, 1.0 ); 84 | glVertex3f( 1.0, 1.0, 1.0 ); 85 | 86 | glColor3f( 1.0, 0.5, 0.0 ); 87 | glVertex3f( 1.0, -1.0, 1.0 ); 88 | glVertex3f( -1.0, -1.0, 1.0 ); 89 | glVertex3f( -1.0, -1.0, -1.0 ); 90 | glVertex3f( 1.0, -1.0, -1.0 ); 91 | 92 | glColor3f( 1.0, 0.0, 0.0 ); 93 | glVertex3f( 1.0, 1.0, 1.0 ); 94 | glVertex3f( -1.0, 1.0, 1.0 ); 95 | glVertex3f( -1.0, -1.0, 1.0 ); 96 | glVertex3f( 1.0, -1.0, 1.0 ); 97 | 98 | glColor3f( 1.0, 1.0, 0.0 ); 99 | glVertex3f( 1.0, -1.0, -1.0 ); 100 | glVertex3f( -1.0, -1.0, -1.0 ); 101 | glVertex3f( -1.0, 1.0, -1.0 ); 102 | glVertex3f( 1.0, 1.0, -1.0 ); 103 | 104 | 105 | glColor3f( 0.0, 0.0, 1.0 ); 106 | glVertex3f( -1.0, 1.0, 1.0 ); 107 | glVertex3f( -1.0, 1.0, -1.0 ); 108 | glVertex3f( -1.0, -1.0, -1.0 ); 109 | glVertex3f( -1.0, -1.0, 1.0 ); 110 | 111 | 112 | glColor3f( 1.0, 0.0, 1.0 ); 113 | glVertex3f( 1.0, 1.0, -1.0 ); 114 | glVertex3f( 1.0, 1.0, 1.0 ); 115 | glVertex3f( 1.0, -1.0, 1.0 ); 116 | glVertex3f( 1.0, -1.0, -1.0 ); 117 | 118 | glEnd(); 119 | 120 | 121 | rTri += 0.2; 122 | rQuad -= 0.15; 123 | 124 | 125 | } 126 | 127 | void NeheWidget::timerEvent(QTimerEvent *event) 128 | { 129 | updateGL(); 130 | } 131 | 132 | void NeheWidget::drawTriangle() 133 | { 134 | glLoadIdentity(); 135 | 136 | glTranslatef(-1.5f, 0.f, -6.f); 137 | glRotatef(45, 1.0, 0.0, 0.0); 138 | 139 | 140 | glBegin(GL_TRIANGLES); 141 | glColor3f(1.f, 0.f, 0.f); 142 | glVertex3f(0.f, 1.f, 0.f); 143 | glColor3f(0.f, 1.f, 0.f); 144 | glVertex3f(1.f, -1.f, 0.f); 145 | glColor3f(0.f, 0.f, 1.f); 146 | glVertex3f(-1.f, -1.f, 0.f); 147 | glEnd(); 148 | rTri += 1; 149 | 150 | } 151 | 152 | void NeheWidget::drawSquare() 153 | { 154 | glLoadIdentity(); 155 | glTranslatef(1.5, 0.0, -6.f); 156 | 157 | 158 | glBegin( GL_QUADS ); 159 | glVertex3f( -1.0, 1.0, 0.0 ); 160 | glVertex3f( 1.0, 1.0, 0.0 ); 161 | glVertex3f( 1.0, -1.0, 0.0 ); 162 | glVertex3f( -1.0, -1.0, 0.0 ); 163 | glEnd(); 164 | } 165 | -------------------------------------------------------------------------------- /lesson5_3D_console/nehewidget.h: -------------------------------------------------------------------------------- 1 | #ifndef NEHEWIDGET_H 2 | #define NEHEWIDGET_H 3 | 4 | #include 5 | //#include 6 | #include 7 | 8 | class NeheWidget : public QGLWidget 9 | { 10 | Q_OBJECT 11 | public: 12 | NeheWidget(QWidget *parent = 0, bool fs = false); 13 | 14 | protected: 15 | void initializeGL(); 16 | void resizeGL(int w, int h); 17 | void paintGL(); 18 | 19 | void timerEvent(QTimerEvent * event); 20 | 21 | private: 22 | bool fullScreen; 23 | 24 | GLfloat rTri; // control the rotation of triangle 25 | GLfloat rQuad; // control the degree of rotation of square 26 | 27 | private: 28 | void drawTriangle(); 29 | void drawSquare(); 30 | }; 31 | 32 | #endif // NEHEWIDGET_H 33 | -------------------------------------------------------------------------------- /lesson6_TextureMapping_console/lesson6_console.pro: -------------------------------------------------------------------------------- 1 | QT += core gui widgets opengl 2 | 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = lesson1_console 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp \ 13 | nehewidget.cpp 14 | 15 | mac{ 16 | LIBS += -framework opengl -framework glut 17 | } 18 | 19 | win32{ 20 | INCLUDEPATH += D:\Libraries\OpenGL\glut\include 21 | LIBS += D:\Libraries\OpenGL\glut\lib\glut.lib 22 | LIBS += D:\Libraries\OpenGL\glut\lib\glut32.lib 23 | } 24 | 25 | HEADERS += \ 26 | nehewidget.h 27 | -------------------------------------------------------------------------------- /lesson6_TextureMapping_console/lesson6_console.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {3de204d9-a1bc-4e3d-9eab-8f1053f1f147} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | 0 45 | 8 46 | true 47 | 1 48 | true 49 | true 50 | true 51 | false 52 | 53 | 54 | 55 | ProjectExplorer.Project.PluginSettings 56 | 57 | 58 | 59 | ProjectExplorer.Project.Target.0 60 | 61 | Desktop Qt 5.6.0 clang 64bit 62 | Desktop Qt 5.6.0 clang 64bit 63 | qt.56.clang_64_kit 64 | 0 65 | 0 66 | 0 67 | 68 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson6_console-Desktop_Qt_5_6_0_clang_64bit-Debug 69 | 70 | 71 | true 72 | qmake 73 | 74 | QtProjectManager.QMakeBuildStep 75 | true 76 | 77 | false 78 | false 79 | false 80 | 81 | 82 | true 83 | Make 84 | 85 | Qt4ProjectManager.MakeStep 86 | 87 | -w 88 | -r 89 | 90 | false 91 | 92 | 93 | 94 | 2 95 | Build 96 | 97 | ProjectExplorer.BuildSteps.Build 98 | 99 | 100 | 101 | true 102 | Make 103 | 104 | Qt4ProjectManager.MakeStep 105 | 106 | -w 107 | -r 108 | 109 | true 110 | clean 111 | 112 | 113 | 1 114 | Clean 115 | 116 | ProjectExplorer.BuildSteps.Clean 117 | 118 | 2 119 | false 120 | 121 | Debug 122 | 123 | Qt4ProjectManager.Qt4BuildConfiguration 124 | 2 125 | true 126 | 127 | 128 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson6_console-Desktop_Qt_5_6_0_clang_64bit-Release 129 | 130 | 131 | true 132 | qmake 133 | 134 | QtProjectManager.QMakeBuildStep 135 | false 136 | 137 | false 138 | false 139 | false 140 | 141 | 142 | true 143 | Make 144 | 145 | Qt4ProjectManager.MakeStep 146 | 147 | -w 148 | -r 149 | 150 | false 151 | 152 | 153 | 154 | 2 155 | Build 156 | 157 | ProjectExplorer.BuildSteps.Build 158 | 159 | 160 | 161 | true 162 | Make 163 | 164 | Qt4ProjectManager.MakeStep 165 | 166 | -w 167 | -r 168 | 169 | true 170 | clean 171 | 172 | 173 | 1 174 | Clean 175 | 176 | ProjectExplorer.BuildSteps.Clean 177 | 178 | 2 179 | false 180 | 181 | Release 182 | 183 | Qt4ProjectManager.Qt4BuildConfiguration 184 | 0 185 | true 186 | 187 | 188 | /Users/Haoyang/Documents/TestCodes/Qt_OpenGL/build-lesson6_console-Desktop_Qt_5_6_0_clang_64bit-Profile 189 | 190 | 191 | true 192 | qmake 193 | 194 | QtProjectManager.QMakeBuildStep 195 | true 196 | 197 | false 198 | true 199 | false 200 | 201 | 202 | true 203 | Make 204 | 205 | Qt4ProjectManager.MakeStep 206 | 207 | -w 208 | -r 209 | 210 | false 211 | 212 | 213 | 214 | 2 215 | Build 216 | 217 | ProjectExplorer.BuildSteps.Build 218 | 219 | 220 | 221 | true 222 | Make 223 | 224 | Qt4ProjectManager.MakeStep 225 | 226 | -w 227 | -r 228 | 229 | true 230 | clean 231 | 232 | 233 | 1 234 | Clean 235 | 236 | ProjectExplorer.BuildSteps.Clean 237 | 238 | 2 239 | false 240 | 241 | Profile 242 | 243 | Qt4ProjectManager.Qt4BuildConfiguration 244 | 0 245 | true 246 | 247 | 3 248 | 249 | 250 | 0 251 | Deploy 252 | 253 | ProjectExplorer.BuildSteps.Deploy 254 | 255 | 1 256 | Deploy locally 257 | 258 | ProjectExplorer.DefaultDeployConfiguration 259 | 260 | 1 261 | 262 | 263 | false 264 | 1000 265 | 266 | true 267 | 268 | false 269 | false 270 | false 271 | false 272 | true 273 | 0.01 274 | 10 275 | true 276 | 1 277 | 25 278 | 279 | 1 280 | true 281 | false 282 | true 283 | valgrind 284 | 285 | 0 286 | 1 287 | 2 288 | 3 289 | 4 290 | 5 291 | 6 292 | 7 293 | 8 294 | 9 295 | 10 296 | 11 297 | 12 298 | 13 299 | 14 300 | 301 | 2 302 | 303 | lesson6_console 304 | 305 | Qt4ProjectManager.Qt4RunConfiguration:/Users/Haoyang/Documents/TestCodes/Qt_OpenGL/lesson6_TextureMapping_console/lesson6_console.pro 306 | true 307 | 308 | lesson6_console.pro 309 | false 310 | true 311 | 312 | 3768 313 | false 314 | true 315 | false 316 | false 317 | true 318 | 319 | 1 320 | 321 | 322 | 323 | ProjectExplorer.Project.TargetCount 324 | 1 325 | 326 | 327 | ProjectExplorer.Project.Updater.FileVersion 328 | 18 329 | 330 | 331 | Version 332 | 18 333 | 334 | 335 | -------------------------------------------------------------------------------- /lesson6_TextureMapping_console/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "nehewidget.h" 3 | int main(int argc, char *argv[]) 4 | { 5 | QApplication a(argc, argv); 6 | 7 | 8 | NeheWidget w; 9 | w.resize(400,300); 10 | w.show(); 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /lesson6_TextureMapping_console/nehewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "nehewidget.h" 2 | 3 | NeheWidget::NeheWidget(QWidget *parent, bool fs) 4 | :QGLWidget(parent) 5 | { 6 | fullScreen = fs; 7 | 8 | xRot = yRot = zRot = 0.0; 9 | 10 | startTimer(15); 11 | } 12 | 13 | void NeheWidget::initializeGL() 14 | { 15 | // glShadeModel(GL_SMOOTH); 16 | // glShadeModel(GL_FLAT); 17 | glClearColor(0.f, 0.f, 0.f, 0.f); 18 | glEnable(GL_DEPTH_TEST); 19 | loadGLTextures(); 20 | glEnable(GL_TEXTURE_2D); 21 | } 22 | 23 | void NeheWidget::resizeGL(int w, int h) 24 | { 25 | glViewport(0, 0, (GLint)w, h); 26 | glMatrixMode(GL_PROJECTION); 27 | glLoadIdentity(); 28 | gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1f, 100.f); 29 | 30 | glMatrixMode(GL_MODELVIEW); 31 | glLoadIdentity(); 32 | } 33 | 34 | void NeheWidget::paintGL() 35 | { 36 | 37 | 38 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 39 | drawSquare(); 40 | // drawTriangle(); 41 | 42 | } 43 | 44 | void NeheWidget::loadGLTextures() 45 | { 46 | QImage tex, buf; 47 | if(! buf.load("D:/Pics/girl.png")){ 48 | 49 | qWarning("Can not"); 50 | QImage dummpy(128, 128, QImage::Format_RGBX8888); 51 | // dummpy.fill(Qt::yellow.rgb()); 52 | buf = dummpy; 53 | 54 | } 55 | tex = QGLWidget::convertToGLFormat(buf); 56 | 57 | glGenTextures(1, &texture[0]); 58 | glBindTexture(GL_TEXTURE_2D,texture[0]); 59 | glTexImage2D(GL_TEXTURE_2D,0,3,tex.width(),tex.height(),0,GL_RGBA, 60 | GL_UNSIGNED_BYTE, tex.bits()); 61 | 62 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 63 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 64 | 65 | 66 | 67 | } 68 | 69 | void NeheWidget::timerEvent(QTimerEvent *event) 70 | { 71 | updateGL(); 72 | } 73 | 74 | void NeheWidget::drawTriangle() 75 | { 76 | glLoadIdentity(); 77 | 78 | glTranslatef(-1.5f, 0.f, -6.f); 79 | glRotatef(xRot, 1.0, 0.0, 0.0); 80 | 81 | 82 | glBegin(GL_TRIANGLES); 83 | /* 84 | glColor3f( 1.0, 0.0, 0.0 ); 85 | glVertex3f( 0.0, 1.0, 0.0 ); 86 | glColor3f( 0.0, 1.0, 0.0 ); 87 | glVertex3f( -1.0, -1.0, 1.0 ); 88 | glColor3f( 0.0, 0.0, 1.0 ); 89 | glVertex3f( 1.0, -1.0, 1.0 ); 90 | 91 | glColor3f( 1.0, 0.0, 0.0 ); 92 | glVertex3f( 0.0, 1.0, 0.0 ); 93 | glColor3f( 0.0, 0.0, 1.0 ); 94 | glVertex3f( 1.0, -1.0, 1.0 ); 95 | glColor3f( 0.0, 1.0, 0.0 ); 96 | glVertex3f( 1.0, -1.0, -1.0 ); 97 | 98 | 99 | glColor3f( 1.0, 0.0, 0.0 ); 100 | glVertex3f( 0.0, 1.0, 0.0 ); 101 | glColor3f( 0.0, 1.0, 0.0 ); 102 | glVertex3f( 1.0, -1.0, -1.0 ); 103 | glColor3f( 0.0, 0.0, 1.0 ); 104 | glVertex3f( -1.0, -1.0, -1.0 ); 105 | 106 | 107 | glColor3f( 1.0, 0.0, 0.0 ); 108 | glVertex3f( 0.0, 1.0, 0.0 ); 109 | glColor3f( 0.0, 0.0, 1.0 ); 110 | glVertex3f( -1.0, -1.0, -1.0 ); 111 | glColor3f( 0.0, 1.0, 0.0 ); 112 | glVertex3f( -1.0, -1.0, 1.0 ); 113 | */ 114 | 115 | glEnd(); 116 | 117 | 118 | } 119 | 120 | void NeheWidget::drawSquare() 121 | { 122 | glLoadIdentity(); 123 | glTranslatef(0.0, 0.0, -8.f); 124 | glRotatef(xRot, 1.0, 0.0, 0.0); 125 | glRotatef(yRot, 0.0, 1.0, 0.0); 126 | glRotatef(zRot, 0.0, 0.0, 1.0); 127 | 128 | glBindTexture(GL_TEXTURE_2D, texture[0]); 129 | 130 | glBegin( GL_QUADS ); 131 | // front 132 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); 133 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); 134 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); 135 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); 136 | 137 | // back 138 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 ); 139 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); 140 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); 141 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); 142 | 143 | // top 144 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); 145 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 1.0, 1.0 ); 146 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 1.0, 1.0 ); 147 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); 148 | 149 | // bottom 150 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); 151 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, -1.0, -1.0 ); 152 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); 153 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); 154 | 155 | // right 156 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); 157 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); 158 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); 159 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); 160 | 161 | // left 162 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 ); 163 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); 164 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); 165 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); 166 | 167 | glEnd(); 168 | 169 | xRot += 1; 170 | yRot += 1; 171 | zRot += 1; 172 | } 173 | -------------------------------------------------------------------------------- /lesson6_TextureMapping_console/nehewidget.h: -------------------------------------------------------------------------------- 1 | #ifndef NEHEWIDGET_H 2 | #define NEHEWIDGET_H 3 | 4 | #include 5 | #ifdef Q_OS_MAC 6 | #include 7 | #endif 8 | 9 | #ifdef Q_OS_WIN32 10 | #include 11 | #endif 12 | 13 | 14 | class NeheWidget : public QGLWidget 15 | { 16 | Q_OBJECT 17 | public: 18 | NeheWidget(QWidget *parent = 0, bool fs = false); 19 | 20 | protected: 21 | void initializeGL(); 22 | void resizeGL(int w, int h); 23 | void paintGL(); 24 | 25 | void loadGLTextures(); 26 | 27 | void timerEvent(QTimerEvent * event); 28 | 29 | private: 30 | bool fullScreen; 31 | 32 | GLfloat xRot, yRot, zRot; 33 | GLuint texture[1]; 34 | 35 | private: 36 | void drawTriangle(); 37 | void drawSquare(); 38 | }; 39 | 40 | #endif // NEHEWIDGET_H 41 | -------------------------------------------------------------------------------- /lesson7_TextureFilter_Lighting_KeyPress-error/lesson7_TextureFilter_Lighting_KeyPress.pro: -------------------------------------------------------------------------------- 1 | #QT += core 2 | #QT -= gui 3 | 4 | QT += core gui widgets opengl 5 | 6 | CONFIG += c++11 7 | 8 | TARGET = lesson7_TextureFilter_Lighting_KeyPress 9 | CONFIG += console 10 | CONFIG -= app_bundle 11 | 12 | TEMPLATE = app 13 | 14 | SOURCES += main.cpp \ 15 | nehewidget.cpp 16 | 17 | mac{ 18 | LIBS += -framework opengl -framework glut 19 | } 20 | 21 | HEADERS += \ 22 | nehewidget.h 23 | -------------------------------------------------------------------------------- /lesson7_TextureFilter_Lighting_KeyPress-error/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "nehewidget.h" 3 | int main(int argc, char *argv[]) 4 | { 5 | QApplication a(argc, argv); 6 | 7 | NeheWidget w(0, false); 8 | w.show(); 9 | return a.exec(); 10 | } 11 | -------------------------------------------------------------------------------- /lesson7_TextureFilter_Lighting_KeyPress-error/nehewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "nehewidget.h" 2 | #include 3 | GLfloat lightAmbient[4] = {1.0, 0.5, 0.5, 1.0}; 4 | GLfloat lightDiffuse[4] = {1.0, 1.0, 1.0, 1.0}; 5 | GLfloat lightPosition[4] = {0.0, 0.0, 2.0, 1.0}; 6 | 7 | NeheWidget::NeheWidget(QWidget *parent, bool fs) 8 | :QGLWidget(parent) 9 | { 10 | xRot = yRot = zRot = 0.0; 11 | xSpeed = ySpeed = 0.0; 12 | 13 | lighting = false; 14 | pause = false; 15 | fullScreen = fs; 16 | zoom = -5.0; 17 | resize(640, 480); 18 | setWindowTitle("TextureFilter-Lighting-KeyPress"); 19 | 20 | if(fullScreen) 21 | showFullScreen(); 22 | startTimer(15); 23 | } 24 | 25 | void NeheWidget::initializeGL() 26 | { 27 | loadTexutre(); 28 | glEnable(GL_TEXTURE_2D); 29 | 30 | glShadeModel(GL_SMOOTH); 31 | glClearColor(0.0, 0.0, 0.0, 1.0); 32 | 33 | glClearDepth(1.0); 34 | glEnable(GL_DEPTH_TEST); 35 | glDepthFunc(GL_LEQUAL); 36 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 37 | 38 | glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); 39 | glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); 40 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); 41 | glEnable(GL_LIGHT0); 42 | } 43 | 44 | void NeheWidget::resizeGL(GLsizei width, GLsizei height) 45 | { 46 | glViewport(0, 0, width, height); 47 | glMatrixMode(GL_PROJECTION); 48 | glLoadIdentity(); 49 | 50 | gluPerspective(45.0, (GLfloat)width/height, 0.1, 100.0); 51 | 52 | glMatrixMode(GL_MODELVIEW); 53 | // glLoadIdentity(); 54 | } 55 | 56 | void NeheWidget::paintGL() 57 | { 58 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 59 | draw(); 60 | } 61 | 62 | void NeheWidget::loadTexutre() 63 | { 64 | QImage tex, buf; 65 | #ifdef Q_OS_MAC 66 | if(!buf.load("/Users/Haoyang/Downloads/girl.png")){ 67 | #endif 68 | #ifdef Q_OS_WIN32 69 | if (!buf.load("D:/girl.png")){ 70 | #endif 71 | qWarning("Could not read Image"); 72 | // QImage dummy(128, 128, QImage::Format_RGBX888); 73 | // dummy.fill(Qt::green); 74 | // buf = dummy; 75 | } 76 | tex = QGLWidget::convertToGLFormat(buf); 77 | 78 | // glGenTextures(3, textures); 79 | // glGenTextures(3, &textures[0]); 80 | glGenTextures(3, &textures[0]); 81 | 82 | glBindTexture(GL_TEXTURE_2D, textures[0]); 83 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 84 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 85 | glTexImage2D(GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0, 86 | GL_RGBA, GL_UNSIGNED_BYTE, tex.bits()); 87 | 88 | glBindTexture(GL_TEXTURE_2D, textures[1]); 89 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 90 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 91 | glTexImage2D(GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0, 92 | GL_RGBA, GL_UNSIGNED_BYTE, tex.bits()); 93 | 94 | glBindTexture(GL_TEXTURE_2D, textures[2]); 95 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 96 | /* GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST 97 | * 当图像在屏幕上变得很⼩小的时候,很多细节将会丢失 */ 98 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); 99 | gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, tex.width(), tex.height(), 100 | GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() ); 101 | } 102 | 103 | void NeheWidget::draw() 104 | { 105 | glLoadIdentity(); 106 | 107 | glTranslatef(0, 0, zoom); 108 | glRotatef(xRot, 1.0, 0.0, 0.0); 109 | glRotatef(yRot, 0.0, 1.0, 0.0); 110 | 111 | glBindTexture(GL_TEXTURE_2D, textures[filter]); 112 | { 113 | glBegin( GL_QUADS ); 114 | glNormal3f( 0.0, 0.0, 1.0 ); 115 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); 116 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); 117 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); 118 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); 119 | glNormal3f( 0.0, 0.0, -1.0 ); 120 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 ); 121 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); 122 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); 123 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); 124 | glNormal3f( 0.0, 1.0, 0.0 ); 125 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); 126 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 1.0, 1.0 ); 127 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 1.0, 1.0 ); 128 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); 129 | glNormal3f( 0.0, -1.0, 0.0 ); 130 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); 131 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, -1.0, -1.0 ); 132 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); 133 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); 134 | glNormal3f( 1.0, 0.0, 0.0 ); 135 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); 136 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); 137 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); 138 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); 139 | glNormal3f( -1.0, 0.0, 0.0 ); 140 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 ); 141 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); 142 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); 143 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); 144 | glEnd(); 145 | } 146 | 147 | xRot += xSpeed; 148 | yRot += ySpeed; 149 | 150 | } 151 | 152 | void NeheWidget::keyPressEvent(QKeyEvent *event) 153 | { 154 | 155 | switch (event->key()) { 156 | case Qt::Key_L: 157 | lighting = !lighting; 158 | if (lighting) 159 | glEnable(GL_LIGHTING); 160 | else 161 | glDisable(GL_LIGHTING); 162 | updateGL(); 163 | break; 164 | case Qt::Key_F: 165 | filter++; 166 | if(filter > 2) 167 | filter = 0; 168 | updateGL(); 169 | break; 170 | case Qt::Key_W: 171 | zoom += 0.2; 172 | updateGL(); 173 | break; 174 | case Qt::Key_S: 175 | zoom -= 0.2; 176 | updateGL(); 177 | break; 178 | case Qt::Key_Left: 179 | xSpeed += 0.02; 180 | 181 | updateGL(); 182 | break; 183 | case Qt::Key_Right: 184 | xSpeed -= 0.02; 185 | 186 | updateGL(); 187 | break; 188 | case Qt::Key_Up: 189 | ySpeed += 0.02; 190 | 191 | updateGL(); 192 | break; 193 | case Qt::Key_Down: 194 | ySpeed -= 0.02; 195 | 196 | updateGL(); 197 | break; 198 | case Qt::Key_F2: 199 | fullScreen = !fullScreen; 200 | if(fullScreen) 201 | showFullScreen(); 202 | else { 203 | showNormal(); 204 | resize(640, 480); 205 | } 206 | updateGL(); 207 | break; 208 | case Qt::Key_Space: 209 | xRot = yRot = zRot = 0.0; 210 | xSpeed = ySpeed = 0.0; 211 | 212 | updateGL(); 213 | break; 214 | case Qt::Key_Escape: 215 | close(); 216 | default: 217 | break; 218 | } 219 | } 220 | /* 221 | void NeheWidget::mousePressEvent(QMouseEvent *event) 222 | { 223 | lastPos = event->pos(); 224 | } 225 | */ 226 | /* 227 | void NeheWidget::mouseMoveEvent(QMouseEvent *event) 228 | { 229 | GLfloat dx = ((GLfloat)event->x() - lastPos.x()) / width(); // Notice:lastPos.x() 230 | GLfloat dy = ((GLfloat)event->y() - lastPos.y()) / height(); 231 | GLfloat dz = ((GLfloat)event->x() - lastPos.x()) / 100.0; 232 | if(event->buttons() & Qt::LeftButton){ 233 | xRot += 180 * dx; 234 | yRot += 180 * dy; 235 | zRot += 180 * dz; 236 | } 237 | 238 | updateGL(); 239 | lastPos = event->pos(); 240 | 241 | } 242 | */ 243 | void NeheWidget::timerEvent(QTimerEvent *event) 244 | { 245 | event = NULL; 246 | updateGL(); 247 | } 248 | 249 | 250 | 251 | 252 | -------------------------------------------------------------------------------- /lesson7_TextureFilter_Lighting_KeyPress-error/nehewidget.h: -------------------------------------------------------------------------------- 1 | #ifndef NEHEWIDGET_H 2 | #define NEHEWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #ifdef Q_OS_MAC 8 | #include 9 | #endif 10 | #ifdef Q_OS_WIN32 11 | #include 12 | #endif 13 | class NeheWidget : public QGLWidget 14 | { 15 | // Q_OBJECT 16 | private: 17 | GLfloat xRot, yRot, zRot; 18 | GLfloat xSpeed, ySpeed; 19 | 20 | GLfloat zoom; 21 | GLuint textures[3]; 22 | GLint filter; // Indicate the texture used. 23 | bool lighting; // lighting switch 24 | 25 | bool fullScreen; 26 | bool pause; 27 | 28 | // QPoint lastPos; // 加了就报错 29 | 30 | public: 31 | NeheWidget(QWidget *parent, bool fs = false); 32 | 33 | public: 34 | void initializeGL(); 35 | void resizeGL(GLsizei width, GLsizei height); 36 | void paintGL(); 37 | 38 | protected: 39 | void loadTexutre(); 40 | void draw(); 41 | 42 | void keyPressEvent(QKeyEvent *event); 43 | // void mousePressEvent(QMouseEvent *event); 44 | // void mouseMoveEvent(QMouseEvent *event); // 加了,编译通过,运行报错 45 | void timerEvent(QTimerEvent *event); 46 | // void QWheelEvent(QWheelEvent *event); 47 | 48 | 49 | }; 50 | 51 | #endif // NEHEWIDGET_H 52 | -------------------------------------------------------------------------------- /lesson7_TextureFilter_Lighting_KeyPress/container.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rookiee/Qt_OpenGL/c80926f499563298eac43a03006484bfc62ebf8d/lesson7_TextureFilter_Lighting_KeyPress/container.png -------------------------------------------------------------------------------- /lesson7_TextureFilter_Lighting_KeyPress/lesson7_TextureFilter_Lighting_KeyPress.pro: -------------------------------------------------------------------------------- 1 | #QT += core 2 | #QT -= gui 3 | 4 | QT += core gui widgets opengl 5 | 6 | CONFIG += c++11 7 | 8 | TARGET = lesson7_TextureFilter_Lighting_KeyPress 9 | CONFIG += console 10 | CONFIG -= app_bundle 11 | 12 | TEMPLATE = app 13 | 14 | SOURCES += main.cpp \ 15 | nehewidget.cpp 16 | 17 | mac{ 18 | LIBS += -framework opengl -framework glut 19 | } 20 | 21 | 22 | win32{ 23 | INCLUDEPATH += D:\Libraries\OpenGL\glut\include 24 | LIBS += D:\Libraries\OpenGL\glut\lib\glut.lib 25 | LIBS += D:\Libraries\OpenGL\glut\lib\glut32.lib 26 | } 27 | 28 | HEADERS += \ 29 | nehewidget.h 30 | -------------------------------------------------------------------------------- /lesson7_TextureFilter_Lighting_KeyPress/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "nehewidget.h" 3 | int main(int argc, char *argv[]) 4 | { 5 | QApplication a(argc, argv); 6 | 7 | NeheWidget w(0, false); 8 | w.show(); 9 | return a.exec(); 10 | } 11 | -------------------------------------------------------------------------------- /lesson7_TextureFilter_Lighting_KeyPress/nehewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "nehewidget.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | GLfloat lightAmbient[4] = {0.5, 0.5, 0.5, 1.0}; 8 | GLfloat lightDiffuse[4] = {1.0, 1.0, 1.0, 1.0}; 9 | GLfloat lightPosition[4] = {0.0, 0.0, 2.0, 1.0}; 10 | 11 | NeheWidget::NeheWidget(QWidget *parent, bool fs) 12 | :QGLWidget(parent) 13 | { 14 | xRot = yRot = zRot = 0.0; 15 | xSpeed = ySpeed = 0.0; 16 | 17 | lighting = false; 18 | pause = false; 19 | fullScreen = fs; 20 | zoom = -5.0; 21 | resize(640, 480); 22 | setWindowTitle("TextureFilter-Lighting-KeyPress"); 23 | filter = 0; 24 | if(fullScreen) 25 | showFullScreen(); 26 | startTimer(15); 27 | 28 | introduction(); 29 | 30 | 31 | } 32 | 33 | void NeheWidget::initializeGL() 34 | { 35 | loadTexutre(); 36 | glEnable(GL_TEXTURE_2D); 37 | 38 | glShadeModel(GL_SMOOTH); 39 | glClearColor(0.0, 0.0, 0.0, 1.0); 40 | 41 | glClearDepth(1.0); 42 | glEnable(GL_DEPTH_TEST); 43 | glDepthFunc(GL_LEQUAL); 44 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 45 | 46 | glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); 47 | glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); 48 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); 49 | glEnable(GL_LIGHT0); 50 | } 51 | 52 | void NeheWidget::resizeGL(GLsizei width, GLsizei height) 53 | { 54 | glViewport(0, 0, width, height); 55 | glMatrixMode(GL_PROJECTION); 56 | glLoadIdentity(); 57 | 58 | gluPerspective(45.0, (GLfloat)width/height, 0.1, 100.0); 59 | 60 | glMatrixMode(GL_MODELVIEW); 61 | // glLoadIdentity(); 62 | } 63 | 64 | void NeheWidget::paintGL() 65 | { 66 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 67 | draw(); 68 | } 69 | 70 | void NeheWidget::loadTexutre() 71 | { 72 | QImage tex, buf; 73 | QString path = QDir::currentPath(); 74 | #ifdef Q_OS_MAC 75 | if(!buf.load("/Users/Haoyang/Downloads/girl.png")){ 76 | #endif 77 | #ifdef Q_OS_WIN32 78 | if (!buf.load("D:/11.png")){ 79 | #endif 80 | qWarning("Could not read Image"); 81 | // QImage dummy(128, 128, QImage::Format_RGBX888); 82 | // dummy.fill(Qt::green); 83 | // buf = dummy; 84 | } 85 | tex = QGLWidget::convertToGLFormat(buf); 86 | 87 | // glGenTextures(3, textures); 88 | // glGenTextures(3, &textures[0]); 89 | glGenTextures(3, &textures[0]); 90 | 91 | glBindTexture(GL_TEXTURE_2D, textures[0]); 92 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 93 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 94 | glTexImage2D(GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0, 95 | GL_RGBA, GL_UNSIGNED_BYTE, tex.bits()); 96 | 97 | glBindTexture(GL_TEXTURE_2D, textures[1]); 98 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 99 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 100 | glTexImage2D(GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0, 101 | GL_RGBA, GL_UNSIGNED_BYTE, tex.bits()); 102 | 103 | glBindTexture(GL_TEXTURE_2D, textures[2]); 104 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 105 | /* GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST 106 | * 当图像在屏幕上变得很⼩小的时候,很多细节将会丢失 */ 107 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); 108 | gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, tex.width(), tex.height(), 109 | GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() ); 110 | } 111 | 112 | void NeheWidget::draw() 113 | { 114 | glLoadIdentity(); 115 | 116 | glTranslatef(0, 0, zoom); 117 | glRotatef(xRot, 1.0, 0.0, 0.0); 118 | glRotatef(yRot, 0.0, 1.0, 0.0); 119 | 120 | glBindTexture(GL_TEXTURE_2D, textures[filter]); 121 | { 122 | glBegin( GL_QUADS ); 123 | glNormal3f( 0.0, 0.0, 1.0 ); 124 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); 125 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); 126 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); 127 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); 128 | glNormal3f( 0.0, 0.0, -1.0 ); 129 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 ); 130 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); 131 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); 132 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); 133 | glNormal3f( 0.0, 1.0, 0.0 ); 134 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); 135 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 1.0, 1.0 ); 136 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 1.0, 1.0 ); 137 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); 138 | glNormal3f( 0.0, -1.0, 0.0 ); 139 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); 140 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, -1.0, -1.0 ); 141 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); 142 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); 143 | glNormal3f( 1.0, 0.0, 0.0 ); 144 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); 145 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); 146 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); 147 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); 148 | glNormal3f( -1.0, 0.0, 0.0 ); 149 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 ); 150 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); 151 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); 152 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); 153 | glEnd(); 154 | } 155 | 156 | xRot += xSpeed; 157 | yRot += ySpeed; 158 | 159 | } 160 | 161 | void NeheWidget::keyPressEvent(QKeyEvent *event) 162 | { 163 | 164 | switch (event->key()) { 165 | case Qt::Key_L: 166 | lighting = !lighting; 167 | if (lighting) 168 | glEnable(GL_LIGHTING); 169 | else 170 | glDisable(GL_LIGHTING); 171 | updateGL(); 172 | break; 173 | case Qt::Key_F: 174 | filter++; 175 | if(filter > 2) 176 | filter = 0; 177 | updateGL(); 178 | break; 179 | case Qt::Key_W: 180 | zoom += 0.1; 181 | updateGL(); 182 | break; 183 | case Qt::Key_S: 184 | zoom -= 0.1; 185 | updateGL(); 186 | break; 187 | case Qt::Key_Left: 188 | xSpeed += 0.2; 189 | 190 | updateGL(); 191 | break; 192 | case Qt::Key_Right: 193 | xSpeed -= 0.2; 194 | 195 | updateGL(); 196 | break; 197 | case Qt::Key_Up: 198 | ySpeed += 0.2; 199 | 200 | updateGL(); 201 | break; 202 | case Qt::Key_Down: 203 | ySpeed -= 0.2; 204 | 205 | updateGL(); 206 | break; 207 | case Qt::Key_F2: 208 | fullScreen = !fullScreen; 209 | if(fullScreen) 210 | showFullScreen(); 211 | else { 212 | showNormal(); 213 | resize(640, 480); 214 | } 215 | updateGL(); 216 | break; 217 | case Qt::Key_Space: 218 | xRot = yRot = zRot = 0.0; 219 | xSpeed = ySpeed = 0.0; 220 | 221 | updateGL(); 222 | break; 223 | case Qt::Key_Escape: 224 | close(); 225 | default: 226 | break; 227 | } 228 | } 229 | 230 | void NeheWidget::mousePressEvent(QMouseEvent *event) 231 | { 232 | lastPos = event->pos(); 233 | } 234 | 235 | 236 | void NeheWidget::mouseMoveEvent(QMouseEvent *event) 237 | { 238 | GLfloat dx = ((GLfloat)event->x() - lastPos.x()) / width(); // Notice:lastPos.x() 239 | GLfloat dy = ((GLfloat)event->y() - lastPos.y()) / height(); 240 | // GLfloat dz = ((GLfloat)event->x() - lastPos.x()) / 100.0; 241 | if(event->buttons() & Qt::LeftButton){ 242 | yRot += 180 * dx; 243 | xRot += 180 * dy; 244 | 245 | } 246 | 247 | updateGL(); 248 | lastPos = event->pos(); 249 | 250 | } 251 | 252 | void NeheWidget::timerEvent(QTimerEvent *event) 253 | { 254 | //event = NULL; 255 | updateGL(); 256 | } 257 | 258 | void NeheWidget::wheelEvent(QWheelEvent *event) 259 | { 260 | if(event->delta() > 0) 261 | zoom += 0.1; 262 | else 263 | zoom -= 0.1; 264 | } 265 | 266 | 267 | 268 | void NeheWidget::introduction() 269 | { 270 | using namespace std; 271 | 272 | cout << "Introduction: " < 5 | #include 6 | #include 7 | #include 8 | #ifdef Q_OS_MAC 9 | #include 10 | #endif 11 | #ifdef Q_OS_WIN32 12 | #include "GL/glut.h" 13 | #endif 14 | class NeheWidget : public QGLWidget 15 | { 16 | // Q_OBJECT 17 | private: 18 | GLfloat xRot, yRot, zRot; 19 | GLfloat xSpeed, ySpeed; 20 | 21 | GLfloat zoom; 22 | GLuint textures[3]; 23 | GLint filter; // Indicate the texture used. 24 | bool lighting; // lighting switch 25 | 26 | bool fullScreen; 27 | bool pause; 28 | 29 | QPoint lastPos; // 加了就报错 30 | 31 | public: 32 | NeheWidget(QWidget *parent, bool fs = false); 33 | 34 | public: 35 | void initializeGL(); 36 | void resizeGL(GLsizei width, GLsizei height); 37 | void paintGL(); 38 | 39 | protected: 40 | void loadTexutre(); 41 | void draw(); 42 | 43 | void keyPressEvent(QKeyEvent *event); 44 | void mousePressEvent(QMouseEvent *event); 45 | void mouseMoveEvent(QMouseEvent *event); // 加了,编译通过,运行报错 46 | 47 | void timerEvent(QTimerEvent *event); 48 | void wheelEvent(QWheelEvent *event); 49 | 50 | 51 | private: 52 | void introduction(); 53 | 54 | 55 | }; 56 | 57 | #endif // NEHEWIDGET_H 58 | --------------------------------------------------------------------------------