├── .gitignore ├── LICENSE ├── Lesson01_CreatingAnOpenGLWindow ├── Lesson1_CreatingAnOpenGLWindow.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson02_CreatingYourFirstPolygonAndQuad ├── Lesson2_CreatingYourFirstPolygonAndQuad.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson03_FlatandSmoothColors ├── Lesson3_FlatandSmoothColors.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson04_RotatingAPolygon ├── Lesson4_RotatingAPolygon.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson05_SolidObjects ├── Lesson5_SolidObjects.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson06_TextureMapping ├── Lesson6_TextureMapping.pro ├── image │ ├── NeHe.bmp │ ├── background.png │ └── cubelogo.png ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson07_TextureFiltersAndBasicLighting ├── Lesson7_TextureFiltersAndBasicLighting.pro ├── image │ └── Crate.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson08_Blending ├── Lesson8_Blending.pro ├── image │ └── Glass.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson09_CreatingMovingScenesWithBlendedTextures ├── Lesson9_CreatingMovingScenesWithBlendedTextures.pro ├── image │ └── Star.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson10_LoadingAndMovingThroughA3DWorld ├── Lesson10_LoadingAndMovingThroughA3DWorld.pro ├── image │ └── Crate.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h ├── src.qrc └── world │ └── World.txt ├── Lesson11_WavingTextureMap ├── Lesson11_WavingTextureMap.pro ├── image │ └── Tim.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson12_DisplayLists ├── Lesson12_DisplayLists.pro ├── image │ └── Cube.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson13_BitmapFonts ├── Lesson13_BitmapFonts.pro ├── fonts │ └── Test.TTF ├── freetypestruct.cpp ├── freetypestruct.h ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson14_OutlineFonts ├── Lesson14_OutlineFonts.pro ├── fonts │ └── Test.TTF ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson15_AddingTextures ├── Lesson15_AddingTextures.pro ├── fonts │ └── Wingdings.ttf ├── image │ └── Lights.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson16_CoolLookingFog ├── Lesson16_ CoolLookingFog.pro ├── image │ └── Crate.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson17_2DBitmapTextureFont ├── Lesson17_2DBitmapTextureFont.pro ├── image │ ├── Bumps.bmp │ └── Font.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson18_Quadratics ├── Lesson18_Quadratics.pro ├── image │ └── Wall.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson19_TriangleStripAndParticleEngine ├── Lesson19_TriangleStripAndParticleEngine.pro ├── image │ └── Particle.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson20_Masking ├── Lesson20_Masking.pro ├── image │ ├── Image1.bmp │ ├── Image2.bmp │ ├── Logo.bmp │ ├── Mask1.bmp │ └── Mask2.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson21_Amidar ├── Lesson21_Amidar.pro ├── image │ ├── Font.bmp │ └── Image.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h ├── src.qrc └── voice │ ├── Complete.wav │ ├── Die.wav │ ├── Freeze.wav │ └── Hourglass.wav ├── Lesson22_BumpMapping ├── Lesson22_BumpMapping.pro ├── image │ ├── Base.bmp │ ├── Bump.bmp │ ├── Multi_On.bmp │ ├── Multi_On_Alpha.bmp │ ├── OpenGL.bmp │ └── OpenGL_Alpha.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson23_SphereMapping ├── Lesson23_SphereMapping.pro ├── image │ ├── BG.bmp │ └── Reflect.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson24_Extensions ├── Lesson24_Extensions.pro ├── image │ └── Font.tga ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson25_MorphingPoints ├── Lesson25_MorphingPoints.pro ├── data │ ├── Sphere.txt │ ├── Torus.txt │ └── Tube.txt ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson26_StencilAndReflection ├── Lesson26_StencilAndReflection.pro ├── image │ ├── Ball.bmp │ ├── Envroll.bmp │ └── Envwall.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson27_ShadowCasting ├── Lesson27_ShadowCasting.pro ├── data │ ├── Object.txt │ ├── Object1.txt │ ├── Object2.txt │ └── SimpleObject.txt ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson28_BezierSurfaces ├── Lesson28_BezierSurfaces.pro ├── image │ └── NeHe.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson29_Blitter ├── Lesson29_Blitter.pro ├── data │ ├── GL.raw │ └── Monitor.raw ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson30_CollisionDetection ├── Lesson30_CollisionDetection.pro ├── image │ ├── Boden.bmp │ ├── Marble.bmp │ ├── Spark.bmp │ └── Wand.bmp ├── main.cpp ├── mathex.h ├── myglwidget.cpp ├── myglwidget.h ├── src.qrc ├── tray.cpp ├── tray.h ├── tvector.cpp ├── tvector.h └── voice │ └── Explode.wav ├── Lesson31_MilkshapeModels ├── Data │ ├── Model.ms3d │ └── Wood.bmp ├── Lesson31_MilkshapeModels.pro ├── main.cpp ├── milkshapemodel.cpp ├── milkshapemodel.h ├── model.cpp ├── model.h ├── myglwidget.cpp └── myglwidget.h ├── Lesson32_PickingGame ├── Lesson32_PickingGame.pro ├── image │ ├── BlueFace.tga │ ├── Bucket.tga │ ├── Coke.tga │ ├── Crosshair.tga │ ├── Explode.tga │ ├── Font.tga │ ├── Ground.tga │ ├── Sky.tga │ ├── Target.tga │ └── Vase.tga ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h ├── src.qrc └── voice │ └── Shot.wav ├── Lesson33_LoadingTGAFiles ├── Lesson33_LoadingTGAFiles.pro ├── image │ ├── Compressed.tga │ └── Uncompressed.tga ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h ├── src.qrc ├── texture.h ├── tgaloader.cpp └── tgaloader.h ├── Lesson34_HeightMapping ├── Lesson34_HeightMapping.pro ├── data │ └── Terrain.raw ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson35_PlayAnAVI ├── Lesson35_PlayAnAVI.pro ├── avi │ └── Face3.avi ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h ├── videowidgetsurface.cpp └── videowidgetsurface.h ├── Lesson36_RenderingToATexture ├── Lesson36_RenderingToATexture.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson37_CelShading ├── Data │ ├── Model.txt │ └── Shader.txt ├── Lesson37_CelShading.pro ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson38_LoadingBitmapsFromTheResource ├── Data │ ├── Butterfly1.bmp │ ├── Butterfly2.bmp │ └── Butterfly3.bmp ├── Lesson38_LoadingBitmapsFromTheResource.pro ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson39_SimplePhysicalSimulationEngine ├── Lesson39_SimplePhysicalSimulationEngine.pro ├── image │ └── Font.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson40_RopePhysics ├── Lesson40_RopePhysics.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson41_VolumetricFog ├── Lesson41_VolumetricFog.pro ├── image │ └── Wall.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson42_MultipleViewports ├── Lesson42_MultipleViewports.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson43_TrueTypeFonts ├── Lesson43_TrueTypeFonts.pro ├── fonts │ └── Test.TTF ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson44_LensFlare ├── Lesson44_LensFlare.pro ├── image │ ├── BigGlow3.bmp │ ├── Font.bmp │ ├── Halo3.bmp │ ├── HardGlow2.bmp │ └── Streaks4.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson45_VertexBuffer ├── Lesson45_VertexBuffer.pro ├── image │ └── Terrain.bmp ├── main.cpp ├── myglwidget.cpp ├── myglwidget.h └── src.qrc ├── Lesson46_FSAA ├── Lesson46_FSAA.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h ├── Lesson48_ArcBallRotation ├── Lesson1_CreatingAnOpenGLWindow.pro ├── Lesson48_ArcBallRotation.pro ├── main.cpp ├── myglwidget.cpp └── myglwidget.h └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | 3 | *.slo 4 | *.lo 5 | *.o 6 | *.a 7 | *.la 8 | *.lai 9 | *.so 10 | *.dll 11 | *.dylib 12 | 13 | # Qt-es 14 | 15 | /.qmake.cache 16 | /.qmake.stash 17 | *.pro.user 18 | *.pro.user.* 19 | *.moc 20 | moc_*.cpp 21 | qrc_*.cpp 22 | ui_*.h 23 | Makefile* 24 | *-build-* 25 | 26 | # QtCreator 27 | 28 | *.autosave 29 | -------------------------------------------------------------------------------- /Lesson01_CreatingAnOpenGLWindow/Lesson1_CreatingAnOpenGLWindow.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-08T15:13:31 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson1_CreatingAnOpenGLWindow 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | FORMS += 21 | -------------------------------------------------------------------------------- /Lesson01_CreatingAnOpenGLWindow/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson01_CreatingAnOpenGLWindow/myglwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | 3 | //创建一个OpenGL窗口: 4 | //在这个教程里,我将教你使用Qt创建OpenGL程序. 5 | //它将显示一个空的OpenGL窗口,可以在窗口和全屏模式下切换,按ESC退出.它是我们以后应用程序的框架. 6 | 7 | MyGLWidget::MyGLWidget(QWidget *parent) : 8 | QGLWidget(parent), m_show_full_screen(false) 9 | { 10 | showNormal(); 11 | } 12 | 13 | MyGLWidget::~MyGLWidget() 14 | { 15 | } 16 | 17 | //下面的代码的作用是重新设置OpenGL场景的大小,而不管窗口的大小是否已经改变(假定您没有使用全屏模式)。 18 | //甚至您无法改变窗口的大小时(例如您在全屏模式下),它至少仍将运行一次--在程序开始时设置我们的透视图。 19 | //OpenGL场景的尺寸将被设置成它显示时所在窗口的大小。 20 | void MyGLWidget::resizeGL(int w, int h) 21 | { 22 | if(h == 0)// 防止被零除 23 | { 24 | h = 1;// 将高设为1 25 | } 26 | glViewport(0, 0, w, h); //重置当前的视口 27 | //下面几行为透视图设置屏幕。意味着越远的东西看起来越小。这么做创建了一个现实外观的场景。 28 | //此处透视按照基于窗口宽度和高度的45度视角来计算。0.1f,100.0f是我们在场景中所能绘制深度的起点和终点。 29 | //glMatrixMode(GL_PROJECTION)指明接下来的两行代码将影响projection matrix(投影矩阵)。 30 | //投影矩阵负责为我们的场景增加透视。 glLoadIdentity()近似于重置。它将所选的矩阵状态恢复成其原始状态。 31 | //调用glLoadIdentity()之后我们为场景设置透视图。 32 | //glMatrixMode(GL_MODELVIEW)指明任何新的变换将会影响 modelview matrix(模型观察矩阵)。 33 | //模型观察矩阵中存放了我们的物体讯息。最后我们重置模型观察矩阵。如果您还不能理解这些术语的含义,请别着急。 34 | //在以后的教程里,我会向大家解释。只要知道如果您想获得一个精彩的透视场景的话,必须这么做。 35 | glMatrixMode(GL_PROJECTION);// 选择投影矩阵 36 | glLoadIdentity();// 重置投影矩阵 37 | //设置视口的大小 38 | gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); 39 | 40 | glMatrixMode(GL_MODELVIEW); //选择模型观察矩阵 41 | glLoadIdentity(); // 重置模型观察矩阵 42 | } 43 | 44 | //接下的代码段中,我们将对OpenGL进行所有的设置。我们将设置清除屏幕所用的颜色,打开深度缓存,启用smooth shading(阴影平滑),等等。 45 | //这个例程直到OpenGL窗口创建之后才会被调用。此过程将有返回值。但我们此处的初始化没那么复杂,现在还用不着担心这个返回值。 46 | void MyGLWidget::initializeGL() 47 | { 48 | //下一行启用smooth shading(阴影平滑)。阴影平滑通过多边形精细的混合色彩,并对外部光进行平滑。 49 | //我将在另一个教程中更详细的解释阴影平滑。 50 | glShadeModel(GL_SMOOTH); // 启用阴影平滑 51 | 52 | //下一行设置清除屏幕时所用的颜色。如果您对色彩的工作原理不清楚的话,我快速解释一下。 53 | //色彩值的范围从0.0f到1.0f。0.0f代表最黑的情况,1.0f就是最亮的情况。 54 | //glClearColor 后的第一个参数是Red Intensity(红色分量),第二个是绿色,第三个是蓝色。 55 | //最大值也是1.0f,代表特定颜色分量的最亮情况。最后一个参数是Alpha值。当它用来清除屏幕的时候,我们不用关心第四个数字。 56 | //现在让它为0.0f。我会用另一个教程来解释这个参数。 57 | //通过混合三种原色(红、绿、蓝),您可以得到不同的色彩。希望您在学校里学过这些。 58 | //因此,当您使用glClearColor(0.0f,0.0f,1.0f,0.0f),您将用亮蓝色来清除屏幕。 59 | //如果您用 glClearColor(0.5f,0.0f,0.0f,0.0f)的话,您将使用中红色来清除屏幕。不是最亮(1.0f),也不是最暗 (0.0f)。 60 | //要得到白色背景,您应该将所有的颜色设成最亮(1.0f)。要黑色背景的话,您该将所有的颜色设为最暗(0.0f)。 61 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑色背景 62 | 63 | //接下来的三行必须做的是关于depth buffer(深度缓存)的。将深度缓存设想为屏幕后面的层。 64 | //深度缓存不断的对物体进入屏幕内部有多深进行跟踪。 65 | //我们本节的程序其实没有真正使用深度缓存,但几乎所有在屏幕上显示3D场景OpenGL程序都使用深度缓存。 66 | //它的排序决定那个物体先画。这样您就不会将一个圆形后面的正方形画到圆形上来。深度缓存是OpenGL十分重要的部分。 67 | glClearDepth(1.0f); // 设置深度缓存 68 | glEnable(GL_DEPTH_TEST); // 启用深度测试 69 | glDepthFunc(GL_LEQUAL); // 所作深度测试的类型 70 | 71 | //接着告诉OpenGL我们希望进行最好的透视修正。这会十分轻微的影响性能。但使得透视图看起来好一点。 72 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 告诉系统对透视进行修正 73 | } 74 | 75 | //下一段包括了所有的绘图代码。任何您所想在屏幕上显示的东东都将在此段代码中出现。 76 | //以后的每个教程中我都会在例程的此处增加新的代码。如果您对OpenGL已经有所了解的话,您可以在glLoadIdentity()调用之后, 77 | //试着添加一些OpenGL代码来创建基本的形。 78 | //如果您是OpenGL新手,等着我的下个教程。目前我们所作的全部就是将屏幕清除成我们前面所决定的颜色,清除深度缓存并且重置场景。 79 | //我们仍没有绘制任何东东。 80 | void MyGLWidget::paintGL() 81 | { 82 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除屏幕和深度缓存 83 | glLoadIdentity(); // 重置当前的模型观察矩阵 84 | } 85 | 86 | void MyGLWidget::keyPressEvent(QKeyEvent *event) 87 | { 88 | switch(event->key()) 89 | { 90 | case Qt::Key_F2: 91 | { 92 | m_show_full_screen = !m_show_full_screen; 93 | if(m_show_full_screen) 94 | { 95 | showFullScreen(); 96 | } 97 | else 98 | { 99 | showNormal(); 100 | } 101 | updateGL(); 102 | break; 103 | } 104 | case Qt::Key_Escape: 105 | { 106 | qApp->exit(); 107 | break; 108 | } 109 | } 110 | } 111 | 112 | //在这一课中,我已试着尽量详细解释一切。每一步都与设置有关,并创建了一个全屏OpenGL程序。 113 | //当您按下ESC键程序就会退出,并监视窗口是否激活。 114 | //如果您有什么意见或建议请给我EMAIL。如果您认为有什么不对或可以改进,请告诉我。 115 | //我想做最好的OpenGL教程并对您的反馈感兴趣。 116 | -------------------------------------------------------------------------------- /Lesson01_CreatingAnOpenGLWindow/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MyGLWidget : public QGLWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MyGLWidget(QWidget *parent = 0); 16 | ~MyGLWidget(); 17 | protected: 18 | void resizeGL(int w, int h); 19 | 20 | void initializeGL(); 21 | 22 | void paintGL(); 23 | 24 | void keyPressEvent(QKeyEvent *event); 25 | private: 26 | bool m_show_full_screen; 27 | }; 28 | 29 | #endif // MYGLWIDGET_H 30 | -------------------------------------------------------------------------------- /Lesson02_CreatingYourFirstPolygonAndQuad/Lesson2_CreatingYourFirstPolygonAndQuad.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-08T15:50:08 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson2_CreatingYourFirstPolygonAndQuad 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | -------------------------------------------------------------------------------- /Lesson02_CreatingYourFirstPolygonAndQuad/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson02_CreatingYourFirstPolygonAndQuad/myglwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | 3 | MyGLWidget::MyGLWidget(QWidget *parent) : 4 | QGLWidget(parent), m_show_full_screen(false) 5 | { 6 | showNormal(); 7 | } 8 | 9 | MyGLWidget::~MyGLWidget() 10 | { 11 | } 12 | 13 | //下面的代码的作用是重新设置OpenGL场景的大小,而不管窗口的大小是否已经改变(假定您没有使用全屏模式)。 14 | //甚至您无法改变窗口的大小时(例如您在全屏模式下),它至少仍将运行一次--在程序开始时设置我们的透视图。 15 | //OpenGL场景的尺寸将被设置成它显示时所在窗口的大小。 16 | void MyGLWidget::resizeGL(int w, int h) 17 | { 18 | if(h == 0)// 防止被零除 19 | { 20 | h = 1;// 将高设为1 21 | } 22 | glViewport(0, 0, w, h); //重置当前的视口 23 | //下面几行为透视图设置屏幕。意味着越远的东西看起来越小。这么做创建了一个现实外观的场景。 24 | //此处透视按照基于窗口宽度和高度的45度视角来计算。0.1f,100.0f是我们在场景中所能绘制深度的起点和终点。 25 | //glMatrixMode(GL_PROJECTION)指明接下来的两行代码将影响projection matrix(投影矩阵)。 26 | //投影矩阵负责为我们的场景增加透视。 glLoadIdentity()近似于重置。它将所选的矩阵状态恢复成其原始状态。 27 | //调用glLoadIdentity()之后我们为场景设置透视图。 28 | //glMatrixMode(GL_MODELVIEW)指明任何新的变换将会影响 modelview matrix(模型观察矩阵)。 29 | //模型观察矩阵中存放了我们的物体讯息。最后我们重置模型观察矩阵。如果您还不能理解这些术语的含义,请别着急。 30 | //在以后的教程里,我会向大家解释。只要知道如果您想获得一个精彩的透视场景的话,必须这么做。 31 | glMatrixMode(GL_PROJECTION);// 选择投影矩阵 32 | glLoadIdentity();// 重置投影矩阵 33 | //设置视口的大小 34 | gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); 35 | 36 | glMatrixMode(GL_MODELVIEW); //选择模型观察矩阵 37 | glLoadIdentity(); // 重置模型观察矩阵 38 | } 39 | 40 | //接下的代码段中,我们将对OpenGL进行所有的设置。我们将设置清除屏幕所用的颜色,打开深度缓存,启用smooth shading(阴影平滑),等等。 41 | //这个例程直到OpenGL窗口创建之后才会被调用。此过程将有返回值。但我们此处的初始化没那么复杂,现在还用不着担心这个返回值。 42 | void MyGLWidget::initializeGL() 43 | { 44 | //下一行启用smooth shading(阴影平滑)。阴影平滑通过多边形精细的混合色彩,并对外部光进行平滑。 45 | //我将在另一个教程中更详细的解释阴影平滑。 46 | glShadeModel(GL_SMOOTH); // 启用阴影平滑 47 | 48 | //下一行设置清除屏幕时所用的颜色。如果您对色彩的工作原理不清楚的话,我快速解释一下。 49 | //色彩值的范围从0.0f到1.0f。0.0f代表最黑的情况,1.0f就是最亮的情况。 50 | //glClearColor 后的第一个参数是Red Intensity(红色分量),第二个是绿色,第三个是蓝色。 51 | //最大值也是1.0f,代表特定颜色分量的最亮情况。最后一个参数是Alpha值。当它用来清除屏幕的时候,我们不用关心第四个数字。 52 | //现在让它为0.0f。我会用另一个教程来解释这个参数。 53 | //通过混合三种原色(红、绿、蓝),您可以得到不同的色彩。希望您在学校里学过这些。 54 | //因此,当您使用glClearColor(0.0f,0.0f,1.0f,0.0f),您将用亮蓝色来清除屏幕。 55 | //如果您用 glClearColor(0.5f,0.0f,0.0f,0.0f)的话,您将使用中红色来清除屏幕。不是最亮(1.0f),也不是最暗 (0.0f)。 56 | //要得到白色背景,您应该将所有的颜色设成最亮(1.0f)。要黑色背景的话,您该将所有的颜色设为最暗(0.0f)。 57 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑色背景 58 | 59 | //接下来的三行必须做的是关于depth buffer(深度缓存)的。将深度缓存设想为屏幕后面的层。 60 | //深度缓存不断的对物体进入屏幕内部有多深进行跟踪。 61 | //我们本节的程序其实没有真正使用深度缓存,但几乎所有在屏幕上显示3D场景OpenGL程序都使用深度缓存。 62 | //它的排序决定那个物体先画。这样您就不会将一个圆形后面的正方形画到圆形上来。深度缓存是OpenGL十分重要的部分。 63 | glClearDepth(1.0f); // 设置深度缓存 64 | glEnable(GL_DEPTH_TEST); // 启用深度测试 65 | glDepthFunc(GL_LEQUAL); // 所作深度测试的类型 66 | 67 | //接着告诉OpenGL我们希望进行最好的透视修正。这会十分轻微的影响性能。但使得透视图看起来好一点。 68 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 告诉系统对透视进行修正 69 | } 70 | 71 | //第一课中,我教您如何创建一个OpenGL窗口。这一课中,我将教您如何创建三角形和四边形。 72 | //我们讲使用来创建GL_TRIANGLES一个三角形,GL_QUADS来创建一个四边形。 73 | //在第一课代码的基础上,我们只需在DrawGLScene()过程中增加代码。下面我重写整个过程。 74 | //如果您计划修改上节课的代码,只需用下面的代码覆盖原来的DrawGLScene()就可以了 75 | void MyGLWidget::paintGL() 76 | { 77 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕及深度缓存 78 | glLoadIdentity(); // 重置当前的模型观察矩阵 79 | 80 | //当您调用glLoadIdentity()之后,您实际上将当前点移到了屏幕中心,X坐标轴从左至右,Y坐标轴从下至上,Z坐标轴从里至外。 81 | //OpenGL屏幕中心的坐标值是X和Y轴上的0.0f点。中心左面的坐标值是负值,右面是正值。 82 | //移向屏幕顶端是正值,移向屏幕底端是负值。移入屏幕深处是负值,移出屏幕则是正值。 83 | //glTranslatef(x, y, z)沿着 X, Y 和 Z 轴移动。根据前面的次序,下面的代码沿着X轴左移1.5个单位,Y轴不动(0.0f), 84 | //最后移入屏幕6.0f个单位。注意在glTranslatef(x, y, z)中当您移动的时候,您并不是相对屏幕中心移动, 85 | //而是相对与当前所在的屏幕位置。 86 | glTranslatef(-1.5f,0.0f,-6.0f); // 左移 1.5 单位,并移入屏幕 6.0 87 | //现在我们已经移到了屏幕的左半部分,并且将视图推入屏幕背后足够的距离以便我们可以看见全部的场景-创建三角形。 88 | //glBegin(GL_TRIANGLES)的意思是开始绘制三角形,glEnd() 告诉OpenGL三角形已经创建好了。 89 | //通常您会需要画3个顶点,可以使用GL_TRIANGLES。在绝大多数的显卡上,绘制三角形是相当快速的。 90 | //如果要画四个顶点,使用GL_QUADS的话会更方便。但据我所知,绝大多数的显卡都使用三角形来为对象着色。 91 | //最后,如果您想要画更多的顶点时,可以使用GL_POLYGON。 92 | //本节的简单示例中,我们只画一个三角形。如果要画第二个三角形的话,可以在这三点之后,再加三行代码(3点)。 93 | //所有六点代码都应包含在glBegin(GL_TRIANGLES) 和 glEnd()之间。 94 | //在他们之间再不会有多余的点出现,也就是说,(GL_TRIANGLES) 和 glEnd()之间的点都是以三点为一个集合的。 95 | //这同样适用于四边形。如果您知道实在绘制四边形的话,您必须在第一个四点之后,再加上四点为一个集合的点组。 96 | //另一方面,多边形可以由任意个顶点,(GL_POLYGON)不在乎glBegin(GL_TRIANGLES) 和 glEnd()之间有多少行代码。 97 | //glBegin之后的第一行设置了多边形的第一个顶点,glVertex 的第一个参数是X坐标,然后依次是Y坐标和Z坐标。 98 | //第一个点是上顶点,然后是左下顶点和右下顶点。glEnd()告诉OpenGL没有其他点了。这样将显示一个填充的三角形。 99 | glBegin(GL_TRIANGLES); // 绘制三角形 100 | glVertex3f(0.0f, 1.0f, 0.0f); // 上顶点 101 | glVertex3f(-1.0f,-1.0f, 0.0f); // 左下 102 | glVertex3f( 1.0f,-1.0f, 0.0f); // 右下 103 | glEnd(); // 三角形绘制结束 104 | 105 | //在屏幕的左半部分画完三角形后,我们要移到右半部分来画正方形。 106 | //为此要再次使用glTranslate。这次右移,所以X坐标值为正值。 107 | //因为前面左移了1.5个单位,这次要先向右移回屏幕中心(1.5个单位),再向右移动1.5个单位。总共要向右移3.0个单位。 108 | glTranslatef(3.0f,0.0f,0.0f); // 右移3单位 109 | //现在使用GL_QUADS绘制正方形。与绘制三角形的代码相类似,画四边形也很简单。 110 | //唯一的区别是用GL_QUADS来替换了GL_TRIANGLES。并增加了一个点。 111 | //我们使用顺时针次序来画正方形-左上-右上-右下-左下。采用顺时针绘制的是对象的后表面。 112 | //这就是说我们所看见的是正方形的背面。逆时针画出来的正方形才是正面朝着我们的。现在这对您来说并不重要,但以后您必须知道。 113 | glBegin(GL_QUADS); // 绘制正方形 114 | glVertex3f(-1.0f, 1.0f, 0.0f); // 左上 115 | glVertex3f( 1.0f, 1.0f, 0.0f); // 右上 116 | glVertex3f( 1.0f,-1.0f, 0.0f); // 左下 117 | glVertex3f(-1.0f,-1.0f, 0.0f); // 右下 118 | glEnd(); // 正方形绘制结束 119 | } 120 | 121 | void MyGLWidget::keyPressEvent(QKeyEvent *event) 122 | { 123 | switch(event->key()) 124 | { 125 | case Qt::Key_F2: 126 | { 127 | m_show_full_screen = !m_show_full_screen; 128 | if(m_show_full_screen) 129 | { 130 | showFullScreen(); 131 | } 132 | else 133 | { 134 | showNormal(); 135 | } 136 | updateGL(); 137 | break; 138 | } 139 | case Qt::Key_Escape: 140 | { 141 | qApp->exit(); 142 | break; 143 | } 144 | } 145 | } 146 | 147 | //在这一课中,我已试着尽量详细的解释与多边形绘制有关的步骤。 148 | //并创建了一个绘制三角形和正方形的OpenGL程序。如果您有什么意见或建议请给我EMAIL。 149 | //如果您认为有什么不对或可以改进,请告诉我。我想做最好的OpenGL教程并对您的反馈感兴趣。 150 | -------------------------------------------------------------------------------- /Lesson02_CreatingYourFirstPolygonAndQuad/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MyGLWidget : public QGLWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MyGLWidget(QWidget *parent = 0); 16 | ~MyGLWidget(); 17 | protected: 18 | void resizeGL(int w, int h); 19 | 20 | void initializeGL(); 21 | 22 | void paintGL(); 23 | 24 | void keyPressEvent(QKeyEvent *event); 25 | private: 26 | bool m_show_full_screen; 27 | }; 28 | 29 | #endif // MYGLWIDGET_H 30 | -------------------------------------------------------------------------------- /Lesson03_FlatandSmoothColors/Lesson3_FlatandSmoothColors.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-08T16:01:22 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson3_FlatandSmoothColors 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | -------------------------------------------------------------------------------- /Lesson03_FlatandSmoothColors/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson03_FlatandSmoothColors/myglwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | 3 | //你的第一个多边形: 4 | //在第一个教程的基础上,我们添加了一个三角形和一个四边形。 5 | //也许你认为这很简单,但你已经迈出了一大步,要知道任何在OpenGL中绘制的模型都会被分解为这两种简单的图形。 6 | //读完了这一课,你会学到如何在空间放置模型,并且会知道深度缓存的概念。 7 | 8 | MyGLWidget::MyGLWidget(QWidget *parent) : 9 | QGLWidget(parent), m_show_full_screen(false) 10 | { 11 | showNormal(); 12 | } 13 | 14 | MyGLWidget::~MyGLWidget() 15 | { 16 | } 17 | 18 | //下面的代码的作用是重新设置OpenGL场景的大小,而不管窗口的大小是否已经改变(假定您没有使用全屏模式)。 19 | //甚至您无法改变窗口的大小时(例如您在全屏模式下),它至少仍将运行一次--在程序开始时设置我们的透视图。 20 | //OpenGL场景的尺寸将被设置成它显示时所在窗口的大小。 21 | void MyGLWidget::resizeGL(int w, int h) 22 | { 23 | if(h == 0)// 防止被零除 24 | { 25 | h = 1;// 将高设为1 26 | } 27 | glViewport(0, 0, w, h); //重置当前的视口 28 | //下面几行为透视图设置屏幕。意味着越远的东西看起来越小。这么做创建了一个现实外观的场景。 29 | //此处透视按照基于窗口宽度和高度的45度视角来计算。0.1f,100.0f是我们在场景中所能绘制深度的起点和终点。 30 | //glMatrixMode(GL_PROJECTION)指明接下来的两行代码将影响projection matrix(投影矩阵)。 31 | //投影矩阵负责为我们的场景增加透视。 glLoadIdentity()近似于重置。它将所选的矩阵状态恢复成其原始状态。 32 | //调用glLoadIdentity()之后我们为场景设置透视图。 33 | //glMatrixMode(GL_MODELVIEW)指明任何新的变换将会影响 modelview matrix(模型观察矩阵)。 34 | //模型观察矩阵中存放了我们的物体讯息。最后我们重置模型观察矩阵。如果您还不能理解这些术语的含义,请别着急。 35 | //在以后的教程里,我会向大家解释。只要知道如果您想获得一个精彩的透视场景的话,必须这么做。 36 | glMatrixMode(GL_PROJECTION);// 选择投影矩阵 37 | glLoadIdentity();// 重置投影矩阵 38 | //设置视口的大小 39 | gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); 40 | 41 | glMatrixMode(GL_MODELVIEW); //选择模型观察矩阵 42 | glLoadIdentity(); // 重置模型观察矩阵 43 | } 44 | 45 | //接下的代码段中,我们将对OpenGL进行所有的设置。我们将设置清除屏幕所用的颜色,打开深度缓存,启用smooth shading(阴影平滑),等等。 46 | //这个例程直到OpenGL窗口创建之后才会被调用。此过程将有返回值。但我们此处的初始化没那么复杂,现在还用不着担心这个返回值。 47 | void MyGLWidget::initializeGL() 48 | { 49 | //下一行启用smooth shading(阴影平滑)。阴影平滑通过多边形精细的混合色彩,并对外部光进行平滑。 50 | //我将在另一个教程中更详细的解释阴影平滑。 51 | glShadeModel(GL_SMOOTH); // 启用阴影平滑 52 | 53 | //下一行设置清除屏幕时所用的颜色。如果您对色彩的工作原理不清楚的话,我快速解释一下。 54 | //色彩值的范围从0.0f到1.0f。0.0f代表最黑的情况,1.0f就是最亮的情况。 55 | //glClearColor 后的第一个参数是Red Intensity(红色分量),第二个是绿色,第三个是蓝色。 56 | //最大值也是1.0f,代表特定颜色分量的最亮情况。最后一个参数是Alpha值。当它用来清除屏幕的时候,我们不用关心第四个数字。 57 | //现在让它为0.0f。我会用另一个教程来解释这个参数。 58 | //通过混合三种原色(红、绿、蓝),您可以得到不同的色彩。希望您在学校里学过这些。 59 | //因此,当您使用glClearColor(0.0f,0.0f,1.0f,0.0f),您将用亮蓝色来清除屏幕。 60 | //如果您用 glClearColor(0.5f,0.0f,0.0f,0.0f)的话,您将使用中红色来清除屏幕。不是最亮(1.0f),也不是最暗 (0.0f)。 61 | //要得到白色背景,您应该将所有的颜色设成最亮(1.0f)。要黑色背景的话,您该将所有的颜色设为最暗(0.0f)。 62 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑色背景 63 | 64 | //接下来的三行必须做的是关于depth buffer(深度缓存)的。将深度缓存设想为屏幕后面的层。 65 | //深度缓存不断的对物体进入屏幕内部有多深进行跟踪。 66 | //我们本节的程序其实没有真正使用深度缓存,但几乎所有在屏幕上显示3D场景OpenGL程序都使用深度缓存。 67 | //它的排序决定那个物体先画。这样您就不会将一个圆形后面的正方形画到圆形上来。深度缓存是OpenGL十分重要的部分。 68 | glClearDepth(1.0f); // 设置深度缓存 69 | glEnable(GL_DEPTH_TEST); // 启用深度测试 70 | glDepthFunc(GL_LEQUAL); // 所作深度测试的类型 71 | 72 | //接着告诉OpenGL我们希望进行最好的透视修正。这会十分轻微的影响性能。但使得透视图看起来好一点。 73 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 告诉系统对透视进行修正 74 | } 75 | 76 | //上一课中我教给您三角形和四边形的绘制方法。这一课我将教您给三角形和四边形添加2种不同类型的着色方法。 77 | //使用Flat coloring(单调着色)给四边形涂上固定的一种颜色。 78 | //使用Smooth coloring(平滑着色)将三角形的三个顶点的不同颜色混合在一起,创建漂亮的色彩混合。 79 | //继续在上节课的DrawGLScene例程上修改。下面将整个例程重写了一遍。 80 | //如果您计划修改上节课的代码,只需用下面的代码覆盖原来的DrawGLScene()就可以了。 81 | void MyGLWidget::paintGL() 82 | { 83 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// 清除屏幕及深度缓存 84 | glLoadIdentity();// 重置模型观察矩阵 85 | glTranslatef(-1.5f,0.0f,-6.0f);// 左移 1.5 单位,并移入屏幕 6.0 86 | glBegin(GL_TRIANGLES);// 绘制三角形 87 | //如果您还记得上节课的内容,这段代码在屏幕的左半部分绘制三角形。 88 | //下一行代码是我们第一次使用命令glColor3f(r,g,b)。括号中的三个参数依次是红、绿、蓝三色分量。 89 | //取值范围可以从0,0f到1.0f。类似于以前所讲的清除屏幕背景命令。 90 | //我们将颜色设为红色(纯红色,无绿色,无蓝色)。 91 | //接下来的一行代码设置三角形的第一个顶点(三角形的上顶点),并使用当前颜色(红色)来绘制。 92 | //从现在开始所有的绘制的对象的颜色都是红色,直到我们将红色改变成别的什么颜色。 93 | glColor3f(1.0f,0.0f,0.0f);// 设置当前色为红色 94 | glVertex3f( 0.0f, 1.0f, 0.0f);// 上顶点 95 | //第一个红色顶点已经设置完毕。接下来我们设置第二个绿色顶点。三角形的左下顶点被设为绿色。 96 | glColor3f(0.0f,1.0f,0.0f);// 设置当前色为绿色 97 | glVertex3f(-1.0f,-1.0f, 0.0f);// 左下 98 | //在设置第三个也就是最后一个顶点。开始绘制之前将颜色设为蓝色。 99 | //这将是三角形的右下顶点。glEnd()出现后,三角形将被填充。 100 | //但是因为每个顶点有不同的颜色,因此看起来颜色从每个角喷出,并刚好在三角形的中心汇合,三种颜色相互混合。这就是平滑着色。 101 | glColor3f(0.0f,0.0f,1.0f);// 设置当前色为蓝色 102 | glVertex3f( 1.0f,-1.0f, 0.0f);// 右下 103 | glEnd();// 三角形绘制结束 104 | 105 | glTranslatef(3.0f,0.0f,0.0f);// 右移3单位 106 | //现在我们绘制一个单调着色-蓝色的正方形。 107 | //最重要的是要记住,设置当前色之后绘制的所有东东都是当前色的。以后您所创建的每个工程都要使用颜色。 108 | //即便是在完全采用纹理贴图的时候,glColor3f仍旧可以用来调节纹理的色调。等等....,以后再说吧。 109 | //我们必须要做的事只需将颜色一次性的设为我们想采用的颜色(本例采用蓝色),然后绘制场景。 110 | //每个顶点都是蓝色的,因为我们没有告诉OpenGL要改变顶点的颜色。 111 | //最后的结果是.....全蓝色的正方形。再说一遍,顺时针绘制的正方形意味着我们所看见的是四边形的背面。 112 | glColor3f(0.5f,0.5f,1.0f);// 一次性将当前色设置为蓝色 113 | glBegin(GL_QUADS);// 绘制正方形 114 | glVertex3f(-1.0f, 1.0f, 0.0f);// 左上 115 | glVertex3f( 1.0f, 1.0f, 0.0f);// 右上 116 | glVertex3f( 1.0f,-1.0f, 0.0f);// 左下 117 | glVertex3f(-1.0f,-1.0f, 0.0f);// 右下 118 | glEnd();// 正方形绘制结束 119 | } 120 | 121 | void MyGLWidget::keyPressEvent(QKeyEvent *event) 122 | { 123 | switch(event->key()) 124 | { 125 | case Qt::Key_F2: 126 | { 127 | m_show_full_screen = !m_show_full_screen; 128 | if(m_show_full_screen) 129 | { 130 | showFullScreen(); 131 | } 132 | else 133 | { 134 | showNormal(); 135 | } 136 | updateGL(); 137 | break; 138 | } 139 | case Qt::Key_Escape: 140 | { 141 | qApp->exit(); 142 | break; 143 | } 144 | } 145 | } 146 | 147 | //在这一课中,我已试着尽量详细的解释与多边形绘制有关的步骤。 148 | //并创建了一个绘制三角形和正方形的OpenGL程序。如果您有什么意见或建议请给我EMAIL。 149 | //如果您认为有什么不对或可以改进,请告诉我。我想做最好的OpenGL教程并对您的反馈感兴趣。 150 | -------------------------------------------------------------------------------- /Lesson03_FlatandSmoothColors/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MyGLWidget : public QGLWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MyGLWidget(QWidget *parent = 0); 16 | ~MyGLWidget(); 17 | protected: 18 | void resizeGL(int w, int h); 19 | 20 | void initializeGL(); 21 | 22 | void paintGL(); 23 | 24 | void keyPressEvent(QKeyEvent *event); 25 | private: 26 | bool m_show_full_screen; 27 | }; 28 | 29 | #endif // MYGLWIDGET_H 30 | -------------------------------------------------------------------------------- /Lesson04_RotatingAPolygon/Lesson4_RotatingAPolygon.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-08T16:08:49 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson4_RotatingAPolygon 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | -------------------------------------------------------------------------------- /Lesson04_RotatingAPolygon/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson04_RotatingAPolygon/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MyGLWidget : public QGLWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MyGLWidget(QWidget *parent = 0); 16 | ~MyGLWidget(); 17 | protected: 18 | void resizeGL(int w, int h); 19 | 20 | void initializeGL(); 21 | 22 | void paintGL(); 23 | 24 | void keyPressEvent(QKeyEvent *event); 25 | 26 | void timerEvent(QTimerEvent *event); 27 | private: 28 | bool m_show_full_screen; 29 | GLfloat m_rotate_tri; 30 | GLfloat m_rotate_quad; 31 | }; 32 | 33 | #endif // MYGLWIDGET_H 34 | -------------------------------------------------------------------------------- /Lesson05_SolidObjects/Lesson5_SolidObjects.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-08T16:24:55 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson5_SolidObjects 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | -------------------------------------------------------------------------------- /Lesson05_SolidObjects/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson05_SolidObjects/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MyGLWidget : public QGLWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MyGLWidget(QWidget *parent = 0); 16 | ~MyGLWidget(); 17 | protected: 18 | void resizeGL(int w, int h); 19 | 20 | void initializeGL(); 21 | 22 | void paintGL(); 23 | 24 | void keyPressEvent(QKeyEvent *event); 25 | 26 | void timerEvent(QTimerEvent *event); 27 | private: 28 | bool m_show_full_screen; 29 | GLfloat m_rotate_tri; 30 | GLfloat m_rotate_quad; 31 | }; 32 | 33 | #endif // MYGLWIDGET_H 34 | -------------------------------------------------------------------------------- /Lesson06_TextureMapping/Lesson6_TextureMapping.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-08T16:42:42 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson6_TextureMapping 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson06_TextureMapping/image/NeHe.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson06_TextureMapping/image/NeHe.bmp -------------------------------------------------------------------------------- /Lesson06_TextureMapping/image/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson06_TextureMapping/image/background.png -------------------------------------------------------------------------------- /Lesson06_TextureMapping/image/cubelogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson06_TextureMapping/image/cubelogo.png -------------------------------------------------------------------------------- /Lesson06_TextureMapping/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson06_TextureMapping/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit MyGLWidget(QWidget *parent = 0); 17 | ~MyGLWidget(); 18 | protected: 19 | void resizeGL(int w, int h); 20 | 21 | void initializeGL(); 22 | 23 | void paintGL(); 24 | 25 | void keyPressEvent(QKeyEvent *event); 26 | 27 | void timerEvent(QTimerEvent *event); 28 | private: 29 | void loadGLTexture(); 30 | private: 31 | bool m_show_full_screen; 32 | GLfloat m_x_rotate; 33 | GLfloat m_y_rotate; 34 | GLfloat m_z_rotate; 35 | GLuint m_texture[1]; 36 | }; 37 | 38 | #endif // MYGLWIDGET_H 39 | -------------------------------------------------------------------------------- /Lesson06_TextureMapping/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/cubelogo.png 4 | image/background.png 5 | image/NeHe.bmp 6 | 7 | 8 | -------------------------------------------------------------------------------- /Lesson07_TextureFiltersAndBasicLighting/Lesson7_TextureFiltersAndBasicLighting.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-08T17:30:24 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson7_TextureFiltersAndBasicLighting 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson07_TextureFiltersAndBasicLighting/image/Crate.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson07_TextureFiltersAndBasicLighting/image/Crate.bmp -------------------------------------------------------------------------------- /Lesson07_TextureFiltersAndBasicLighting/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson07_TextureFiltersAndBasicLighting/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MyGLWidget : public QGLWidget 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit MyGLWidget(QWidget *parent = 0); 18 | ~MyGLWidget(); 19 | protected: 20 | void resizeGL(int w, int h); 21 | 22 | void initializeGL(); 23 | 24 | void paintGL(); 25 | 26 | void keyPressEvent(QKeyEvent *event); 27 | 28 | void timerEvent(QTimerEvent *event); 29 | private: 30 | void loadGLTexture(); 31 | private: 32 | bool m_show_full_screen; 33 | bool m_light; 34 | 35 | GLfloat m_x_rotate; 36 | GLfloat m_y_rotate; 37 | GLfloat m_x_speed; 38 | GLfloat m_y_speed; 39 | GLfloat m_z; 40 | 41 | GLfloat m_light_ambient[4]; 42 | GLfloat m_light_diffuse[4]; 43 | GLfloat m_light_position[4]; 44 | 45 | GLuint m_filter; 46 | GLuint m_texture[3]; 47 | }; 48 | 49 | #endif // MYGLWIDGET_H 50 | -------------------------------------------------------------------------------- /Lesson07_TextureFiltersAndBasicLighting/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Crate.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson08_Blending/Lesson8_Blending.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-10T11:28:36 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson8_Blending 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson08_Blending/image/Glass.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson08_Blending/image/Glass.bmp -------------------------------------------------------------------------------- /Lesson08_Blending/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson08_Blending/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MyGLWidget : public QGLWidget 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit MyGLWidget(QWidget *parent = 0); 18 | ~MyGLWidget(); 19 | protected: 20 | void resizeGL(int w, int h); 21 | 22 | void initializeGL(); 23 | 24 | void paintGL(); 25 | 26 | void keyPressEvent(QKeyEvent *event); 27 | 28 | void timerEvent(QTimerEvent *event); 29 | private: 30 | void loadGLTexture(); 31 | private: 32 | bool m_show_full_screen; 33 | bool m_light; 34 | 35 | GLfloat m_x_rotate; 36 | GLfloat m_y_rotate; 37 | GLfloat m_x_speed; 38 | GLfloat m_y_speed; 39 | GLfloat m_z; 40 | 41 | GLfloat m_light_ambient[4]; 42 | GLfloat m_light_diffuse[4]; 43 | GLfloat m_light_position[4]; 44 | 45 | GLuint m_filter; 46 | GLuint m_texture[3]; 47 | 48 | bool m_blend; 49 | }; 50 | 51 | #endif // MYGLWIDGET_H 52 | -------------------------------------------------------------------------------- /Lesson08_Blending/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Glass.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson09_CreatingMovingScenesWithBlendedTextures/Lesson9_CreatingMovingScenesWithBlendedTextures.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-10T13:45:57 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson9_CreatingMovingScenesWithBlendedTextures 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson09_CreatingMovingScenesWithBlendedTextures/image/Star.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson09_CreatingMovingScenesWithBlendedTextures/image/Star.bmp -------------------------------------------------------------------------------- /Lesson09_CreatingMovingScenesWithBlendedTextures/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson09_CreatingMovingScenesWithBlendedTextures/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | //我们来创建一个结构。 结构这词听起来有点可怕,但实际上并非如此。 17 | //一个结构使用一组简单类型的数据 (以及变量等)来表达较大的具有相似性的数据组合。 18 | //我们知道我们在保持对星星的跟踪。 您可以看到下面的第七行就是 stars;并且每个星星有三个整型的色彩值。 19 | //第三行 int r,g,b设置了三个整数. 一个红色 (r), 一个绿色 (g), 以及一个蓝色 (b). 20 | //此外,每个星星离屏幕中心的距离不同, 而且可以是以屏幕中心为原点的任意360度中的一个角度。 21 | //如果你看下面第四行的话, 会发现我们使用了一个叫做 dist的浮点数来保持对距离 的跟踪. 22 | //第五行则用一个叫做 angle的浮点数保持对星星角度值的跟踪。 23 | //因此我们使用了一组数据来描述屏幕上星星的色彩, 距离, 和角度。 不幸的是我们不止对一个星星进行跟踪。 24 | //但是无需创建 50 个红色值、 50 个绿色值、 50 个蓝色值、 50 个距离值 以及 50 个角度值,而只需创建一个数组star。 25 | //star数组的每个元素都是stars类型的,里面存放了描述星星的所有数据。 26 | //star数组在下面的第八行创建。 第八行的样子是这样的: stars star[num]。数组类型是 stars结构. 27 | //所数组 能存放所有stars结构的信息。 数组名字是 star. 数组大小是 [num]。 数组中存放着 stars结构的元素. 28 | //跟踪结构元素会比跟踪各自分开的变量容易的多. 不过这样也很笨, 因为我们竟然不能改变常量 num来增减星星数量。 29 | 30 | typedef struct // 为星星创建一个结构 31 | { 32 | int r, g, b; // 星星的颜色 33 | GLfloat dist; // 星星距离中心的距离 34 | GLfloat angle; // 当前星星所处的角度 35 | }stars; // 结构命名为stars 36 | 37 | explicit MyGLWidget(QWidget *parent = 0); 38 | ~MyGLWidget(); 39 | protected: 40 | void resizeGL(int w, int h); 41 | 42 | void initializeGL(); 43 | 44 | void paintGL(); 45 | 46 | void keyPressEvent(QKeyEvent *event); 47 | 48 | void timerEvent(QTimerEvent *event); 49 | private: 50 | void loadTexture(); 51 | private: 52 | bool m_show_full_screen; 53 | 54 | bool m_twinkle; 55 | QVector m_stars; 56 | GLfloat m_zoom; // 星星离观察者的距离 57 | GLfloat m_tilt; // 星星的倾角 58 | GLfloat m_spin; // 闪烁星星的自转 59 | GLuint m_texture[1]; // 存放一个纹理 60 | 61 | }; 62 | 63 | #endif // MYGLWIDGET_H 64 | -------------------------------------------------------------------------------- /Lesson09_CreatingMovingScenesWithBlendedTextures/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Star.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson10_LoadingAndMovingThroughA3DWorld/Lesson10_LoadingAndMovingThroughA3DWorld.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-10T14:36:22 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson10_LoadingAndMovingThroughA3DWorld 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson10_LoadingAndMovingThroughA3DWorld/image/Crate.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson10_LoadingAndMovingThroughA3DWorld/image/Crate.bmp -------------------------------------------------------------------------------- /Lesson10_LoadingAndMovingThroughA3DWorld/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson10_LoadingAndMovingThroughA3DWorld/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MyGLWidget : public QGLWidget 13 | { 14 | Q_OBJECT 15 | public: 16 | //三角形本质上是由一些(两个以上)顶点组成的多边形,顶点同时也是我们的最基本的分类单位。 17 | //顶点包含了OpenGL真正感兴趣的数据。我们用3D空间中的坐标值(x,y,z)以及它们的纹理坐标(u,v)来定义三角形的每个顶点。 18 | typedef struct tagVERTEX // 创建Vertex顶点结构 19 | { 20 | float x, y, z; // 3D 坐标 21 | float u, v; // 纹理坐标 22 | } VERTEX; // 命名为VERTEX 23 | //一个sector(区段)包含了一系列的多边形,所以下一个目标就是triangle(我们将只用三角形,这样写代码更容易些)。 24 | typedef struct tagTRIANGLE // 创建Triangle三角形结构 25 | { 26 | VERTEX vertex[3]; // VERTEX矢量数组,大小为3 27 | }TRIANGLE;// 命名为 TRIANGLE 28 | typedef struct tagSECTOR // 创建Sector区段结构 29 | { 30 | int numtriangles; // Sector中的三角形个数 31 | TRIANGLE* triangle; // 指向三角数组的指针 32 | } SECTOR; // 命名为SECTOR 33 | 34 | explicit MyGLWidget(QWidget *parent = 0); 35 | ~MyGLWidget(); 36 | protected: 37 | void resizeGL(int w, int h); 38 | 39 | void initializeGL(); 40 | 41 | void paintGL(); 42 | 43 | void keyPressEvent(QKeyEvent *event); 44 | private: 45 | void setupWorld(); 46 | 47 | void readStr(QTextStream *stream, QString &string); 48 | 49 | void loadTexture(); 50 | private: 51 | bool m_show_full_screen; 52 | 53 | SECTOR m_sector1; 54 | 55 | GLfloat m_yrot; 56 | GLfloat m_xpos; 57 | GLfloat m_zpos; 58 | GLfloat m_heading; 59 | GLfloat m_walkbias; 60 | GLfloat m_walkbiasangle; 61 | GLfloat m_lookupdown; 62 | 63 | GLuint m_filter; 64 | GLuint m_texture[3]; 65 | 66 | bool m_blend; 67 | }; 68 | 69 | #endif // MYGLWIDGET_H 70 | -------------------------------------------------------------------------------- /Lesson10_LoadingAndMovingThroughA3DWorld/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | world/World.txt 4 | image/Crate.bmp 5 | 6 | 7 | -------------------------------------------------------------------------------- /Lesson10_LoadingAndMovingThroughA3DWorld/world/World.txt: -------------------------------------------------------------------------------- 1 | 2 | NUMPOLLIES 36 3 | 4 | // Floor 1 5 | -3.0 0.0 -3.0 0.0 6.0 6 | -3.0 0.0 3.0 0.0 0.0 7 | 3.0 0.0 3.0 6.0 0.0 8 | 9 | -3.0 0.0 -3.0 0.0 6.0 10 | 3.0 0.0 -3.0 6.0 6.0 11 | 3.0 0.0 3.0 6.0 0.0 12 | 13 | // Ceiling 1 14 | -3.0 1.0 -3.0 0.0 6.0 15 | -3.0 1.0 3.0 0.0 0.0 16 | 3.0 1.0 3.0 6.0 0.0 17 | -3.0 1.0 -3.0 0.0 6.0 18 | 3.0 1.0 -3.0 6.0 6.0 19 | 3.0 1.0 3.0 6.0 0.0 20 | 21 | // A1 22 | 23 | -2.0 1.0 -2.0 0.0 1.0 24 | -2.0 0.0 -2.0 0.0 0.0 25 | -0.5 0.0 -2.0 1.5 0.0 26 | -2.0 1.0 -2.0 0.0 1.0 27 | -0.5 1.0 -2.0 1.5 1.0 28 | -0.5 0.0 -2.0 1.5 0.0 29 | 30 | // A2 31 | 32 | 2.0 1.0 -2.0 2.0 1.0 33 | 2.0 0.0 -2.0 2.0 0.0 34 | 0.5 0.0 -2.0 0.5 0.0 35 | 2.0 1.0 -2.0 2.0 1.0 36 | 0.5 1.0 -2.0 0.5 1.0 37 | 0.5 0.0 -2.0 0.5 0.0 38 | 39 | // B1 40 | 41 | -2.0 1.0 2.0 2.0 1.0 42 | -2.0 0.0 2.0 2.0 0.0 43 | -0.5 0.0 2.0 0.5 0.0 44 | -2.0 1.0 2.0 2.0 1.0 45 | -0.5 1.0 2.0 0.5 1.0 46 | -0.5 0.0 2.0 0.5 0.0 47 | 48 | // B2 49 | 50 | 2.0 1.0 2.0 2.0 1.0 51 | 2.0 0.0 2.0 2.0 0.0 52 | 0.5 0.0 2.0 0.5 0.0 53 | 2.0 1.0 2.0 2.0 1.0 54 | 0.5 1.0 2.0 0.5 1.0 55 | 0.5 0.0 2.0 0.5 0.0 56 | 57 | // C1 58 | 59 | -2.0 1.0 -2.0 0.0 1.0 60 | -2.0 0.0 -2.0 0.0 0.0 61 | -2.0 0.0 -0.5 1.5 0.0 62 | -2.0 1.0 -2.0 0.0 1.0 63 | -2.0 1.0 -0.5 1.5 1.0 64 | -2.0 0.0 -0.5 1.5 0.0 65 | 66 | // C2 67 | 68 | -2.0 1.0 2.0 2.0 1.0 69 | -2.0 0.0 2.0 2.0 0.0 70 | -2.0 0.0 0.5 0.5 0.0 71 | -2.0 1.0 2.0 2.0 1.0 72 | -2.0 1.0 0.5 0.5 1.0 73 | -2.0 0.0 0.5 0.5 0.0 74 | 75 | // D1 76 | 77 | 2.0 1.0 -2.0 0.0 1.0 78 | 2.0 0.0 -2.0 0.0 0.0 79 | 2.0 0.0 -0.5 1.5 0.0 80 | 2.0 1.0 -2.0 0.0 1.0 81 | 2.0 1.0 -0.5 1.5 1.0 82 | 2.0 0.0 -0.5 1.5 0.0 83 | 84 | // D2 85 | 86 | 2.0 1.0 2.0 2.0 1.0 87 | 2.0 0.0 2.0 2.0 0.0 88 | 2.0 0.0 0.5 0.5 0.0 89 | 2.0 1.0 2.0 2.0 1.0 90 | 2.0 1.0 0.5 0.5 1.0 91 | 2.0 0.0 0.5 0.5 0.0 92 | 93 | // Upper hallway - L 94 | -0.5 1.0 -3.0 0.0 1.0 95 | -0.5 0.0 -3.0 0.0 0.0 96 | -0.5 0.0 -2.0 1.0 0.0 97 | -0.5 1.0 -3.0 0.0 1.0 98 | -0.5 1.0 -2.0 1.0 1.0 99 | -0.5 0.0 -2.0 1.0 0.0 100 | 101 | // Upper hallway - R 102 | 0.5 1.0 -3.0 0.0 1.0 103 | 0.5 0.0 -3.0 0.0 0.0 104 | 0.5 0.0 -2.0 1.0 0.0 105 | 0.5 1.0 -3.0 0.0 1.0 106 | 0.5 1.0 -2.0 1.0 1.0 107 | 0.5 0.0 -2.0 1.0 0.0 108 | 109 | // Lower hallway - L 110 | -0.5 1.0 3.0 0.0 1.0 111 | -0.5 0.0 3.0 0.0 0.0 112 | -0.5 0.0 2.0 1.0 0.0 113 | -0.5 1.0 3.0 0.0 1.0 114 | -0.5 1.0 2.0 1.0 1.0 115 | -0.5 0.0 2.0 1.0 0.0 116 | 117 | // Lower hallway - R 118 | 0.5 1.0 3.0 0.0 1.0 119 | 0.5 0.0 3.0 0.0 0.0 120 | 0.5 0.0 2.0 1.0 0.0 121 | 0.5 1.0 3.0 0.0 1.0 122 | 0.5 1.0 2.0 1.0 1.0 123 | 0.5 0.0 2.0 1.0 0.0 124 | 125 | 126 | // Left hallway - Lw 127 | 128 | -3.0 1.0 0.5 1.0 1.0 129 | -3.0 0.0 0.5 1.0 0.0 130 | -2.0 0.0 0.5 0.0 0.0 131 | -3.0 1.0 0.5 1.0 1.0 132 | -2.0 1.0 0.5 0.0 1.0 133 | -2.0 0.0 0.5 0.0 0.0 134 | 135 | // Left hallway - Hi 136 | 137 | -3.0 1.0 -0.5 1.0 1.0 138 | -3.0 0.0 -0.5 1.0 0.0 139 | -2.0 0.0 -0.5 0.0 0.0 140 | -3.0 1.0 -0.5 1.0 1.0 141 | -2.0 1.0 -0.5 0.0 1.0 142 | -2.0 0.0 -0.5 0.0 0.0 143 | 144 | // Right hallway - Lw 145 | 146 | 3.0 1.0 0.5 1.0 1.0 147 | 3.0 0.0 0.5 1.0 0.0 148 | 2.0 0.0 0.5 0.0 0.0 149 | 3.0 1.0 0.5 1.0 1.0 150 | 2.0 1.0 0.5 0.0 1.0 151 | 2.0 0.0 0.5 0.0 0.0 152 | 153 | // Right hallway - Hi 154 | 155 | 3.0 1.0 -0.5 1.0 1.0 156 | 3.0 0.0 -0.5 1.0 0.0 157 | 2.0 0.0 -0.5 0.0 0.0 158 | 3.0 1.0 -0.5 1.0 1.0 159 | 2.0 1.0 -0.5 0.0 1.0 160 | 2.0 0.0 -0.5 0.0 0.0 -------------------------------------------------------------------------------- /Lesson11_WavingTextureMap/Lesson11_WavingTextureMap.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-10T16:05:31 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson11_WavingTextureMap 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson11_WavingTextureMap/image/Tim.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson11_WavingTextureMap/image/Tim.bmp -------------------------------------------------------------------------------- /Lesson11_WavingTextureMap/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson11_WavingTextureMap/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MyGLWidget : public QGLWidget 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit MyGLWidget(QWidget *parent = 0); 18 | ~MyGLWidget(); 19 | protected: 20 | void resizeGL(int w, int h); 21 | 22 | void initializeGL(); 23 | 24 | void paintGL(); 25 | 26 | void keyPressEvent(QKeyEvent *event); 27 | 28 | void timerEvent(QTimerEvent *event); 29 | private: 30 | void loadGLTexture(); 31 | private: 32 | bool m_show_full_screen; 33 | GLfloat m_x_rotate; 34 | GLfloat m_y_rotate; 35 | GLfloat m_z_rotate; 36 | GLuint m_texture[1]; 37 | 38 | //我们将使用points数组来存放网格各顶点独立的x,y,z坐标。这里网格由45×45点形成, 39 | //换句话说也就是由44格×44格的小方格子依次组成了。 40 | float m_points[45][45][3]; // Points网格顶点数组 41 | }; 42 | 43 | #endif // MYGLWIDGET_H 44 | -------------------------------------------------------------------------------- /Lesson11_WavingTextureMap/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Tim.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson12_DisplayLists/Lesson12_DisplayLists.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-13T13:02:23 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson12_DisplayLists 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson12_DisplayLists/image/Cube.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson12_DisplayLists/image/Cube.bmp -------------------------------------------------------------------------------- /Lesson12_DisplayLists/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson12_DisplayLists/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MyGLWidget : public QGLWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MyGLWidget(QWidget *parent = 0); 16 | ~MyGLWidget(); 17 | protected: 18 | void resizeGL(int w, int h); 19 | 20 | void initializeGL(); 21 | 22 | void paintGL(); 23 | 24 | void keyPressEvent(QKeyEvent *event); 25 | private: 26 | void buildLists(); // 创建盒子的显示列表 27 | 28 | void loadGLTexture(); 29 | private: 30 | bool m_show_full_screen; 31 | 32 | //下面设置变量。首先是存储纹理的变量,然后两个新的变量用于显示列表。这些变量是指向内存中显示列表的指针。命名为box和top。 33 | //然后用两个变量xloop,yloop表示屏幕上立方体的位置,两个变量xrot,yrot表示立方体的旋转。 34 | GLuint m_box; // 保存盒子的显示列表 35 | GLuint m_top; // 保存盒子顶部的显示列表 36 | GLfloat m_yrot; 37 | GLfloat m_xrot; 38 | GLuint m_texture[1]; 39 | }; 40 | 41 | #endif // MYGLWIDGET_H 42 | -------------------------------------------------------------------------------- /Lesson12_DisplayLists/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Cube.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson13_BitmapFonts/Lesson13_BitmapFonts.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-05-05T15:05:23 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson13_BitmapFonts 12 | TEMPLATE = app 13 | 14 | LIBS += libfreetype libz 15 | 16 | SOURCES += main.cpp\ 17 | myglwidget.cpp \ 18 | freetypestruct.cpp 19 | 20 | HEADERS += myglwidget.h \ 21 | freetypestruct.h 22 | 23 | fonts.files += fonts/* 24 | fonts.path = $$OUT_PWD/debug 25 | INSTALLS += fonts 26 | -------------------------------------------------------------------------------- /Lesson13_BitmapFonts/fonts/Test.TTF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson13_BitmapFonts/fonts/Test.TTF -------------------------------------------------------------------------------- /Lesson13_BitmapFonts/freetypestruct.h: -------------------------------------------------------------------------------- 1 | #ifndef FREETYPESTRUCT_H 2 | #define FREETYPESTRUCT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | //我们将把每个字符需要的信息封装在一个结构中,这样就像使用WGL字体一样,我们可以分别控制每个字符的显示状态。 11 | namespace freetype 12 | { 13 | struct font_data//这个结构保存字体信息 14 | { 15 | float h; // 字体的高度 16 | GLuint *textures; // 使用的纹理 17 | GLuint list_base; // 显示列表的值 18 | 19 | // 初始化结构 20 | void init(const char *fname, unsigned int h); 21 | // 清楚所有的资源 22 | void clean(); 23 | }; 24 | //最后一件事是定义我们输出字符串的原形: 25 | // 把字符输出到屏幕 26 | void print(const font_data &ft_font, float x, float y, const char *fmt, ...); 27 | } 28 | 29 | 30 | #endif // FREETYPESTRUCT_H 31 | -------------------------------------------------------------------------------- /Lesson13_BitmapFonts/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson13_BitmapFonts/myglwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | 3 | //图像字体: 4 | //这一课我们将创建一些基于2D图像的字体,它们可以缩放,但不能旋转,并且总是面向前方,但作为基本的显示来说,我想已经够了。 5 | 6 | MyGLWidget::MyGLWidget(QWidget *parent) : 7 | QGLWidget(parent), m_show_full_screen(false), m_cnt1(0.0f) 8 | { 9 | showNormal(); 10 | startTimer(15); 11 | } 12 | 13 | MyGLWidget::~MyGLWidget() 14 | { 15 | m_our_font.clean(); 16 | } 17 | 18 | void MyGLWidget::resizeGL(int w, int h) 19 | { 20 | if(h == 0)// 防止被零除 21 | { 22 | h = 1;// 将高设为1 23 | } 24 | glViewport(0, 0, w, h); //重置当前的视口 25 | glMatrixMode(GL_PROJECTION);// 选择投影矩阵 26 | glLoadIdentity();// 重置投影矩阵 27 | //设置视口的大小 28 | gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); 29 | 30 | glMatrixMode(GL_MODELVIEW); //选择模型观察矩阵 31 | glLoadIdentity(); // 重置模型观察矩阵 32 | } 33 | 34 | void MyGLWidget::initializeGL() 35 | { 36 | glShadeModel(GL_SMOOTH); // 启用阴影平滑 37 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑色背景 38 | glClearDepth(1.0f); // 设置深度缓存 39 | glEnable(GL_DEPTH_TEST); // 启用深度测试 40 | glDepthFunc(GL_LEQUAL); // 所作深度测试的类型 41 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 告诉系统对透视进行修正 42 | QString fontPath = QApplication::applicationDirPath() + "/Test.ttf"; 43 | m_our_font.init(fontPath.toLatin1().data(), 24); 44 | } 45 | 46 | void MyGLWidget::paintGL() 47 | { 48 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// 清除屏幕及深度缓存 49 | glLoadIdentity();// 重置当前的模型观察矩阵// 重置当前的模型观察矩阵 50 | glTranslatef(0.0f,0.0f,-1.0f);// 移入屏幕一个单位 51 | 52 | // 红色文字 53 | //下面我们使用一些奇妙的数学方法来产生颜色变化脉冲。如果你不懂我在做什么你也不必担心。我喜欢利用教多的变量和教简单的方法来达到我的目的。 54 | //这样,我使用那两个用来改变文字在屏幕上位置的计数器来改变红、绿、蓝这些颜色。红色值使用COS和计数器1在-1.0到1.0之间变化。 55 | //绿色值使用SIN和计数器2也在-1.0到1.0之间变化。蓝色值使用COS和计数器1和2在0.5到1.5之间变化。于是,蓝色值就永远不会等于0, 56 | //文字的颜色也永远不会消失。笨办法,但很管用。 57 | glColor3f(1.0f*float(cos(m_cnt1)),1.0f*float(sin(m_cnt2)),1.0f-0.5f*float(cos(m_cnt1+m_cnt2))); 58 | glPushMatrix(); 59 | glLoadIdentity(); 60 | freetype::print(m_our_font, 320*float(cos(m_cnt1)), 61 | 200 + 200*float(sin(m_cnt2)), "Active OpenGL Text With NeHe - %7.2f", m_cnt1); 62 | glPopMatrix(); 63 | } 64 | 65 | void MyGLWidget::keyPressEvent(QKeyEvent *event) 66 | { 67 | switch(event->key()) 68 | { 69 | case Qt::Key_F2: 70 | { 71 | m_show_full_screen = !m_show_full_screen; 72 | if(m_show_full_screen) 73 | { 74 | showFullScreen(); 75 | } 76 | else 77 | { 78 | showNormal(); 79 | } 80 | updateGL(); 81 | break; 82 | } 83 | case Qt::Key_Escape: 84 | { 85 | qApp->exit(); 86 | break; 87 | } 88 | } 89 | } 90 | 91 | void MyGLWidget::timerEvent(QTimerEvent *event) 92 | { 93 | m_cnt1+=0.051f; // Increase The First Counter 94 | m_cnt2+=0.005f; // Increase The First Counter 95 | updateGL(); 96 | QGLWidget::timerEvent(event); 97 | } 98 | 99 | //好了,用于使用位图字体的所有一切都在你的OpenGL程序中了。我在网上寻找过与这篇教程相似的文章,但没有找到。 100 | //或许我的网站是第一个涉及这个主题的C代码的网站吧。不管怎样,享用这篇教程,快乐编码! 101 | -------------------------------------------------------------------------------- /Lesson13_BitmapFonts/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "freetypestruct.h" 12 | 13 | class MyGLWidget : public QGLWidget 14 | { 15 | Q_OBJECT 16 | 17 | public: 18 | explicit MyGLWidget(QWidget *parent = 0); 19 | ~MyGLWidget(); 20 | protected: 21 | void resizeGL(int w, int h); 22 | 23 | void initializeGL(); 24 | 25 | void paintGL(); 26 | 27 | void keyPressEvent(QKeyEvent *event); 28 | 29 | void timerEvent(QTimerEvent *event); 30 | private: 31 | bool m_show_full_screen; 32 | freetype::font_data m_our_font; 33 | GLfloat m_cnt1; 34 | GLfloat m_cnt2; 35 | }; 36 | 37 | #endif // MYGLWIDGET_H 38 | -------------------------------------------------------------------------------- /Lesson14_OutlineFonts/Lesson14_OutlineFonts.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-05-09T11:21:18 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson14_OutlineFonts 12 | TEMPLATE = app 13 | 14 | LIBS += libftgl libfreetype libz libopengl32 libglu32 15 | 16 | SOURCES += main.cpp\ 17 | myglwidget.cpp 18 | 19 | HEADERS += myglwidget.h 20 | 21 | fonts.files += fonts/* 22 | fonts.path = $$OUT_PWD/debug 23 | INSTALLS += fonts 24 | -------------------------------------------------------------------------------- /Lesson14_OutlineFonts/fonts/Test.TTF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson14_OutlineFonts/fonts/Test.TTF -------------------------------------------------------------------------------- /Lesson14_OutlineFonts/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson14_OutlineFonts/myglwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | 3 | //图形字体: 4 | //在一课我们将教你绘制3D的图形字体,它们可像一般的3D模型一样被变换。 5 | 6 | MyGLWidget::MyGLWidget(QWidget *parent) : 7 | QGLWidget(parent), m_show_full_screen(false), 8 | m_freeTypeFont(NULL), m_rot(0.0f) 9 | { 10 | showNormal(); 11 | startTimer(15); 12 | } 13 | 14 | MyGLWidget::~MyGLWidget() 15 | { 16 | } 17 | 18 | void MyGLWidget::resizeGL(int w, int h) 19 | { 20 | if (h==0) // Prevent A Divide By Zero By 21 | { 22 | h=1; // Making Height Equal One 23 | } 24 | 25 | glViewport(0,0,w,h); // Reset The Current Viewport 26 | 27 | glMatrixMode(GL_PROJECTION); // Select The Projection Matrix 28 | glLoadIdentity(); // Reset The Projection Matrix 29 | 30 | // Calculate The Aspect Ratio Of The Window 31 | gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); 32 | 33 | glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix 34 | glLoadIdentity(); // Reset The Modelview Matrix 35 | } 36 | 37 | void MyGLWidget::initializeGL() 38 | { 39 | glShadeModel(GL_SMOOTH); // Enable Smooth Shading 40 | glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background 41 | glClearDepth(1.0f); // Depth Buffer Setup 42 | glEnable(GL_DEPTH_TEST); // Enables Depth Testing 43 | glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do 44 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations 45 | glEnable(GL_LIGHT0); // Enable Default Light (Quick And Dirty) 46 | glEnable(GL_LIGHTING); // Enable Lighting 47 | glEnable(GL_COLOR_MATERIAL); // Enable Coloring Of Material 48 | 49 | QString fontPath = QApplication::applicationDirPath() + "/Test.ttf"; 50 | m_freeTypeFont = new FTGLExtrdFont(fontPath.toLatin1().data()); 51 | if(m_freeTypeFont == NULL) 52 | { 53 | QMessageBox::warning(this, tr("Warning"), tr("%1 file can't find").arg(fontPath)); 54 | } 55 | else 56 | { 57 | m_freeTypeFont->FaceSize(1); 58 | m_freeTypeFont->Depth(0.2); 59 | m_freeTypeFont->CharMap(ft_encoding_unicode); 60 | } 61 | } 62 | 63 | void MyGLWidget::paintGL() 64 | { 65 | //下面就是画图的代码了。我们从清除屏幕和深度缓存开始。我们调用glLoadIdentity()来重置所有东西。然后我们将坐标系向屏幕里移动十个单位。 66 | //轮廓字体在透视图模式下表现非常好。你将文字移入屏幕越深,文字开起来就更小。文字离你越近,它看起来就更大。 67 | //也可以使用glScalef(x,y,z)命令来操作轮廓字体。如果你想把字体放大两倍,可以使用glScalef(1.0f,2.0f,1.0f). 2.0f 作用在y轴, 68 | //它告诉OpenGL将显示列表的高度绘制为原来的两倍。如果2.0f作用在x轴,那么文本的宽度将变成原来的两倍 69 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕及深度缓存 70 | glLoadIdentity();// 重置当前的模型观察矩阵 71 | glTranslatef(0.0f,0.0f,-10.0f);// 移入屏幕一个单位 72 | //在向屏幕里移动以后,我们希望文本能旋转起来。下面3行代码用来在3个轴上旋转屏幕。我将rot乘以不同的数,以便每个方向上的旋转速度不同。 73 | glRotatef(m_rot,1.0f,0.0f,0.0f);// X轴旋转 74 | glRotatef(m_rot*1.5f,0.0f,1.0f,0.0f);// Y轴旋转 75 | glRotatef(m_rot*1.4f,0.0f,0.0f,1.0f);// Z轴旋转 76 | // Pulsing Colors Based On The Rotation 77 | //下面是令人兴奋的颜色循环了。照常,我们使用唯一递增的变量(rot)。颜色通过使用COS和SIN来循环变化。我将rot除以不同的数, 78 | //这样每种颜色会以不同的速度递增。最终的效果非常好 79 | glColor3f(1.0f*float(cos(m_rot/20.0f)),1.0f*float(sin(m_rot/25.0f)),1.0f-0.5f*float(cos(m_rot/17.0f))); 80 | QString renderText = QString("NeHe - %1").arg(m_rot/50); 81 | glTranslatef(-3.0,0.0f,0.0f); // Center Our Text On The Screen 82 | m_freeTypeFont->Render(renderText.toLatin1().data()); // Print GL Text To The Screen 83 | } 84 | 85 | void MyGLWidget::keyPressEvent(QKeyEvent *event) 86 | { 87 | switch(event->key()) 88 | { 89 | case Qt::Key_F2: 90 | { 91 | m_show_full_screen = !m_show_full_screen; 92 | if(m_show_full_screen) 93 | { 94 | showFullScreen(); 95 | } 96 | else 97 | { 98 | showNormal(); 99 | } 100 | updateGL(); 101 | break; 102 | } 103 | case Qt::Key_Escape: 104 | { 105 | qApp->exit(); 106 | break; 107 | } 108 | } 109 | } 110 | 111 | void MyGLWidget::timerEvent(QTimerEvent *event) 112 | { 113 | m_rot+=0.5f; // Increase The Rotation Variable 114 | updateGL(); 115 | QGLWidget::timerEvent(event); 116 | } 117 | 118 | //在这节课结束的时候,你应该已经学会在你的OpenGL程序中使用轮廓字体了。就像第13课,我曾在网上寻找一篇与这一课相似的教程, 119 | //但是也没有找到。或许我的网站是第一个涉及这个主题同时又把它解释的简单易懂的C代码的网站吧。享用这篇教程,快乐编码! 120 | -------------------------------------------------------------------------------- /Lesson14_OutlineFonts/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | class MyGLWidget : public QGLWidget 18 | { 19 | Q_OBJECT 20 | 21 | public: 22 | explicit MyGLWidget(QWidget *parent = 0); 23 | ~MyGLWidget(); 24 | protected: 25 | void resizeGL(int w, int h); 26 | 27 | void initializeGL(); 28 | 29 | void paintGL(); 30 | 31 | void keyPressEvent(QKeyEvent *event); 32 | 33 | void timerEvent(QTimerEvent *event); 34 | private: 35 | bool m_show_full_screen; 36 | FTGLExtrdFont *m_freeTypeFont; 37 | GLfloat m_rot; 38 | }; 39 | 40 | #endif // MYGLWIDGET_H 41 | -------------------------------------------------------------------------------- /Lesson15_AddingTextures/Lesson15_AddingTextures.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-05-14T10:17:52 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson15_AddingTextures 12 | TEMPLATE = app 13 | 14 | LIBS += libftgl libfreetype libz libopengl32 libglu32 15 | 16 | SOURCES += main.cpp\ 17 | myglwidget.cpp 18 | 19 | HEADERS += myglwidget.h 20 | 21 | fonts.files += fonts/* 22 | fonts.path = $$OUT_PWD/debug 23 | INSTALLS += fonts 24 | 25 | RESOURCES += \ 26 | src.qrc 27 | -------------------------------------------------------------------------------- /Lesson15_AddingTextures/fonts/Wingdings.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson15_AddingTextures/fonts/Wingdings.ttf -------------------------------------------------------------------------------- /Lesson15_AddingTextures/image/Lights.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson15_AddingTextures/image/Lights.bmp -------------------------------------------------------------------------------- /Lesson15_AddingTextures/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson15_AddingTextures/myglwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | 3 | //图形字体的纹理映射: 4 | //这一课,我们将在上一课的基础上创建带有纹理的字体,它真的很简单。 5 | 6 | MyGLWidget::MyGLWidget(QWidget *parent) : 7 | QGLWidget(parent), m_show_full_screen(false), 8 | m_freeTypeFont(NULL), m_rot(0.0f) 9 | { 10 | showNormal(); 11 | startTimer(15); 12 | } 13 | 14 | MyGLWidget::~MyGLWidget() 15 | { 16 | glDeleteTextures(1, &m_texture[0]); 17 | } 18 | 19 | void MyGLWidget::resizeGL(int w, int h) 20 | { 21 | if (h==0) // Prevent A Divide By Zero By 22 | { 23 | h=1; // Making Height Equal One 24 | } 25 | 26 | glViewport(0,0,w,h); // Reset The Current Viewport 27 | 28 | glMatrixMode(GL_PROJECTION); // Select The Projection Matrix 29 | glLoadIdentity(); // Reset The Projection Matrix 30 | 31 | // Calculate The Aspect Ratio Of The Window 32 | gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); 33 | 34 | glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix 35 | glLoadIdentity(); // Reset The Modelview Matrix 36 | } 37 | 38 | void MyGLWidget::initializeGL() 39 | { 40 | //glEnable(GL_COLOR_MATERIAL) 这行被删掉了,如果你想使用glColor3f(r,g,b)来改变纹理的颜色, 41 | //那么就把glEnable(GL_COLOR_MATERIAL)这行重新加到这部分代码中。 42 | loadGLTexture(); 43 | glShadeModel(GL_SMOOTH);// 启用阴影平滑 44 | glClearColor(0.0f, 0.0f, 0.0f, 0.5f);// 黑色背景 45 | glClearDepth(1.0f);// 设置深度缓存 46 | glEnable(GL_DEPTH_TEST);// 启用深度测试 47 | glDepthFunc(GL_LEQUAL);// 所作深度测试的类型 48 | glEnable(GL_LIGHT0);// 使用第0号灯 49 | glEnable(GL_LIGHTING);// 使用光照 50 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// 告诉系统对透视进行修正 51 | glEnable(GL_TEXTURE_2D);// 使用二维纹理 52 | glBindTexture(GL_TEXTURE_2D, m_texture[0]); // 选择使用的纹理 53 | 54 | QString fontPath = QApplication::applicationDirPath() + "/Wingdings.ttf"; 55 | m_freeTypeFont = new FTGLExtrdFont(fontPath.toLatin1().data()); 56 | if(m_freeTypeFont == NULL) 57 | { 58 | QMessageBox::warning(this, tr("Warning"), tr("%1 file can't find").arg(fontPath)); 59 | } 60 | else 61 | { 62 | m_freeTypeFont->FaceSize(1); 63 | m_freeTypeFont->Depth(0.2); 64 | m_freeTypeFont->CharMap(ft_encoding_unicode); 65 | } 66 | } 67 | 68 | void MyGLWidget::paintGL() 69 | { 70 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer 71 | glLoadIdentity(); // Reset The View 72 | //这里是第一处变动。我们打算使用COS和SIN让物体绕着屏幕旋转而不是把它固定在屏幕中间。 73 | //我们将把物体向屏幕里移动3个单位。在x轴,我们将移动范围限制在-1.1到+1.1之间。我们使用rot变量来控制左右移动。 74 | //我们把上下移动的范围限制在+0.8到-0.8之间。同样使用rot变量来控制上下移动(最好充分利用你的变量)。 75 | glTranslatef(1.1f*float(cos(m_rot/16.0f)),0.8f*float(sin(m_rot/20.0f)),-3.0f);// 设置字体的位置 76 | //这会使符号在X,Y和Z轴旋转。 77 | glRotatef(m_rot,1.0f,0.0f,0.0f); // Rotate On The X Axis 78 | glRotatef(m_rot*1.2f,0.0f,1.0f,0.0f); // Rotate On The Y Axis 79 | glRotatef(m_rot*1.4f,0.0f,0.0f,1.0f); // Rotate On The Z Axis 80 | //将物体相对观察点向左向下移动一点,以便于把符号定位于每个轴的中心。 81 | //否则,当我们旋转它的时候,看起来就不像是在围绕它自己的中心在旋转。-0.35只是一个能让符号正确显示的数。 82 | //我也试过一些其它数,因为我不知道这种字体的宽度是多少,可以适情况作出调整。我不知道为什么这种字体没有一个中心。 83 | glTranslatef(-0.35f,-0.35f,0.1f); // Center On X, Y, Z Axis 84 | m_freeTypeFont->Render("N"); // Draw A Skull And Crossbones Symbol 85 | } 86 | 87 | void MyGLWidget::keyPressEvent(QKeyEvent *event) 88 | { 89 | switch(event->key()) 90 | { 91 | case Qt::Key_F2: 92 | { 93 | m_show_full_screen = !m_show_full_screen; 94 | if(m_show_full_screen) 95 | { 96 | showFullScreen(); 97 | } 98 | else 99 | { 100 | showNormal(); 101 | } 102 | updateGL(); 103 | break; 104 | } 105 | case Qt::Key_Escape: 106 | { 107 | qApp->exit(); 108 | break; 109 | } 110 | } 111 | } 112 | 113 | void MyGLWidget::timerEvent(QTimerEvent *event) 114 | { 115 | m_rot+=0.1f; // Increase The Rotation Variable 116 | updateGL(); 117 | QGLWidget::timerEvent(event); 118 | } 119 | 120 | void MyGLWidget::loadGLTexture() 121 | { 122 | QImage image(":/image/Lights.bmp"); 123 | image = image.convertToFormat(QImage::Format_RGB888); 124 | image = image.mirrored(); 125 | glGenTextures(1, &m_texture[0]); 126 | glBindTexture(GL_TEXTURE_2D, m_texture[0]); 127 | gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width(), image.height(), 128 | GL_RGB, GL_UNSIGNED_BYTE, image.bits()); 129 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 130 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); 131 | //下面四行代码将为我们绘制在屏幕上的任何物体自动生成纹理坐标。函数glTexGen非常强大,而且复杂, 132 | //如果要完全讲清楚它的数学原理需要再写一篇教程。不过,你只要知道GL_S和GL_T是纹理坐标就可以了。默认状态下, 133 | //它被设置为提取物体此刻在屏幕上的x坐标和y坐标,并把它们转换为顶点坐标。你会发现到物体在z平面没有纹理,只显示一些斑纹。 134 | //正面和反面都被赋予了纹理,这些都是由glTexGen函数产生的。(X(GL_S)用于从左到右映射纹理,Y(GL_T)用于从上到下映射纹理。 135 | //GL_TEXTURE_GEN_MODE允许我们选择我们想在S和T纹理坐标上使用的纹理映射模式。你有3种选择: 136 | //GL_EYE_LINEAR - 纹理会固定在屏幕上。它永远不会移动。物体将被赋予处于它通过的地区的那一块纹理。 137 | //GL_OBJECT_LINEAR - 这种就是我们使用的模式。纹理被固定于在屏幕上运动的物体上。 138 | //GL_SPHERE_MAP - 每个人都喜欢。创建一种有金属质感的物体。 139 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); 140 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); 141 | glEnable(GL_TEXTURE_GEN_S); 142 | glEnable(GL_TEXTURE_GEN_T); 143 | } 144 | 145 | //尽管我没有讲的细致入微,但我想你应该很好的理解了如何让OpenGL为你生成纹理坐标。在给你的字体或者是同类物体赋予纹理映射时, 146 | //应该没有问题了,而且只需要改变两行代码,你就可以启用球体映射了,它的效果简直酷毙了! 147 | -------------------------------------------------------------------------------- /Lesson15_AddingTextures/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | class MyGLWidget : public QGLWidget 18 | { 19 | Q_OBJECT 20 | 21 | public: 22 | explicit MyGLWidget(QWidget *parent = 0); 23 | ~MyGLWidget(); 24 | protected: 25 | void resizeGL(int w, int h); 26 | 27 | void initializeGL(); 28 | 29 | void paintGL(); 30 | 31 | void keyPressEvent(QKeyEvent *event); 32 | 33 | void timerEvent(QTimerEvent *event); 34 | private: 35 | void loadGLTexture(); 36 | private: 37 | bool m_show_full_screen; 38 | GLuint m_texture[1]; // One Texture Map 39 | FTGLExtrdFont *m_freeTypeFont; 40 | GLfloat m_rot; 41 | }; 42 | 43 | #endif // MYGLWIDGET_H 44 | -------------------------------------------------------------------------------- /Lesson15_AddingTextures/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Lights.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson16_CoolLookingFog/Lesson16_ CoolLookingFog.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-13T13:51:54 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson16_CoolLookingFog 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson16_CoolLookingFog/image/Crate.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson16_CoolLookingFog/image/Crate.bmp -------------------------------------------------------------------------------- /Lesson16_CoolLookingFog/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson16_CoolLookingFog/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MyGLWidget : public QGLWidget 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit MyGLWidget(QWidget *parent = 0); 18 | ~MyGLWidget(); 19 | protected: 20 | void resizeGL(int w, int h); 21 | 22 | void initializeGL(); 23 | 24 | void paintGL(); 25 | 26 | void keyPressEvent(QKeyEvent *event); 27 | 28 | void timerEvent(QTimerEvent *event); 29 | private: 30 | void loadGLTexture(); 31 | private: 32 | bool m_show_full_screen; 33 | bool m_light; 34 | 35 | GLfloat m_x_rotate; 36 | GLfloat m_y_rotate; 37 | GLfloat m_x_speed; 38 | GLfloat m_y_speed; 39 | GLfloat m_z; 40 | 41 | GLfloat m_light_ambient[4]; 42 | GLfloat m_light_diffuse[4]; 43 | GLfloat m_light_position[4]; 44 | 45 | GLuint m_filter; 46 | GLuint m_texture[3]; 47 | 48 | GLuint m_fogMode[3]; // 雾气的模式 49 | GLuint m_fogfilter; // 使用哪一种雾气 50 | GLfloat m_fogColor[4]; // 雾的颜色设为白色 51 | }; 52 | 53 | #endif // MYGLWIDGET_H 54 | -------------------------------------------------------------------------------- /Lesson16_CoolLookingFog/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Crate.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson17_2DBitmapTextureFont/Lesson17_2DBitmapTextureFont.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-13T14:07:56 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson17_2DBitmapTextureFont 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson17_2DBitmapTextureFont/image/Bumps.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson17_2DBitmapTextureFont/image/Bumps.bmp -------------------------------------------------------------------------------- /Lesson17_2DBitmapTextureFont/image/Font.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson17_2DBitmapTextureFont/image/Font.bmp -------------------------------------------------------------------------------- /Lesson17_2DBitmapTextureFont/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson17_2DBitmapTextureFont/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MyGLWidget : public QGLWidget 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit MyGLWidget(QWidget *parent = 0); 18 | ~MyGLWidget(); 19 | protected: 20 | void resizeGL(int w, int h); 21 | 22 | void initializeGL(); 23 | 24 | void paintGL(); 25 | 26 | void keyPressEvent(QKeyEvent *event); 27 | 28 | void timerEvent(QTimerEvent *event); 29 | private: 30 | void loadGLTexture(); 31 | 32 | void buildFont(); // 创建我们的字符显示列表 33 | 34 | void glPrint(GLint x, GLint y, char *string, int set); 35 | private: 36 | bool m_show_full_screen; 37 | 38 | GLuint m_base; 39 | GLuint m_texture[2]; 40 | GLfloat m_cnt1; 41 | GLfloat m_cnt2; 42 | }; 43 | 44 | #endif // MYGLWIDGET_H 45 | -------------------------------------------------------------------------------- /Lesson17_2DBitmapTextureFont/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Bumps.bmp 4 | image/Font.bmp 5 | 6 | 7 | -------------------------------------------------------------------------------- /Lesson18_Quadratics/Lesson18_Quadratics.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-14T01:00:21 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson18_Quadratics 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson18_Quadratics/image/Wall.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson18_Quadratics/image/Wall.bmp -------------------------------------------------------------------------------- /Lesson18_Quadratics/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson18_Quadratics/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MyGLWidget : public QGLWidget 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit MyGLWidget(QWidget *parent = 0); 18 | ~MyGLWidget(); 19 | protected: 20 | void resizeGL(int w, int h); 21 | 22 | void initializeGL(); 23 | 24 | void paintGL(); 25 | 26 | void keyPressEvent(QKeyEvent *event); 27 | 28 | void timerEvent(QTimerEvent *event); 29 | private: 30 | void loadGLTexture(); 31 | 32 | void glDrawCube(); 33 | private: 34 | bool m_show_full_screen; 35 | bool m_light; 36 | 37 | GLfloat m_x_rotate; 38 | GLfloat m_y_rotate; 39 | GLfloat m_x_speed; 40 | GLfloat m_y_speed; 41 | GLfloat m_z; 42 | 43 | GLfloat m_light_ambient[4]; 44 | GLfloat m_light_diffuse[4]; 45 | GLfloat m_light_position[4]; 46 | 47 | GLuint m_filter; 48 | GLuint m_texture[3]; 49 | 50 | GLUquadricObj *m_quadratic; // 二次几何体 51 | int m_part1; // 圆盘的起始角度 52 | int m_part2; // 圆盘的结束角度 53 | int m_p1; // 增量1 54 | int m_p2; // 增量1 55 | GLuint m_object; // 二次几何体标示符 56 | }; 57 | 58 | #endif // MYGLWIDGET_H 59 | -------------------------------------------------------------------------------- /Lesson18_Quadratics/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Wall.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson19_TriangleStripAndParticleEngine/Lesson19_TriangleStripAndParticleEngine.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-14T01:25:35 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson19_TriangleStripAndParticleEngine 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson19_TriangleStripAndParticleEngine/image/Particle.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson19_TriangleStripAndParticleEngine/image/Particle.bmp -------------------------------------------------------------------------------- /Lesson19_TriangleStripAndParticleEngine/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson19_TriangleStripAndParticleEngine/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define MAX_PARTICLES 1000 // 定义最大的粒子数 12 | 13 | class MyGLWidget : public QGLWidget 14 | { 15 | Q_OBJECT 16 | 17 | public: 18 | //好!现在是有趣的东西.下段程序描述单一粒子结构,这是我们给予粒子的属性. 19 | //我们用布尔型变量active开始,如果为true,我们的粒子为活跃的.如果为false则粒子为死的,此时我们就删除它. 20 | //在程序中我没有使用活跃的,因为它很好的实现.变量life和fade来控制粒子显示多久以及显示时候的亮度. 21 | //随着life数值的降低fade的数值也相应降低.这将导致一些粒子比其他粒子燃烧的时间长. 22 | typedef struct // 创建粒子数据结构 23 | { 24 | bool active; // 是否激活 25 | float life; // 粒子生命 26 | float fade; // 衰减速度 27 | //变量r,g和b用来表示粒子的红色强度,绿色强度和蓝色强度.当r的值变成1.0f时粒子将会很红,当三个变量全为1.0f时则粒子将变成白色. 28 | float r; // 红色值 29 | float g; // 绿色值 30 | float b; // 蓝色值 31 | //变量x.y和z控制粒子在屏幕上显示的位置.x表示粒子在x轴上的位置.y表示y轴上的位置.z表示粒子z轴上的位置 32 | float x; // X 位置 33 | float y; // Y 位置 34 | float z; // Z 位置 35 | //下面三个变量很重要.这三个变量控制粒子在每个轴上移动的快慢和方向.如果xi是负价粒子将会向左移动,正值将会向右移动. 36 | //如果yi是负值粒子将会向下移动,正值将向上.最后,如果zi负值粒子将会向荧屏内部移动,正植将移向观察者. 37 | float xi; // X 方向 38 | float yi; // Y 方向 39 | float zi; // Z 方向 40 | //最后,另外3个变量!每一个变量可被看成加速度.如果xg正值时,粒子将会被拉倒右边,负值将拉向左边. 41 | //所以如果粒子向左移动(负的)而我们给它一个正的加速度,粒子速度将变慢.最后将向反方向移动(高中物理). 42 | //yg拉下或拉上.zg拉进或拉出屏幕. 43 | float xg; // X 方向重力加速度 44 | float yg; // Y 方向重力加速度 45 | float zg; // Z 方向重力加速度 46 | //结构的名字为particles. 47 | }particles; 48 | 49 | explicit MyGLWidget(QWidget *parent = 0); 50 | ~MyGLWidget(); 51 | protected: 52 | void resizeGL(int w, int h); 53 | 54 | void initializeGL(); 55 | 56 | void paintGL(); 57 | 58 | void keyPressEvent(QKeyEvent *event); 59 | 60 | void timerEvent(QTimerEvent *event); 61 | private: 62 | void loadGLTexture(); 63 | private: 64 | bool m_show_full_screen; 65 | GLuint m_texture[1]; 66 | 67 | bool m_rainbow; // 是否为彩虹模式 68 | 69 | //下面四行是复杂的变量.变量slowdown控制粒子移动的快慢.数值愈高,移动越慢.数值越底,移动越快. 70 | //如果数值降低,粒子将快速的移动!粒子的速度影响它们在荧屏中移动的距离.记住速度慢的粒子不会射很远的. 71 | //变量xspeed和yspeed控制尾部的方向.xspeed将会增加粒子在x轴上速度.如果xspeed是正值粒子将会向右边移动多. 72 | //如果xspeed负价值,粒子将会向左边移动多.那个值越高,就向那个方向移动比较多.yspeed工作相同的方法,但是在y轴上. 73 | //因为有其它的因素影响粒子的运动,所以我要说"多".xspeed和yspeed有助于在我们想要的方向上移动粒子.最后是变量zoom, 74 | //我们用该变量移入或移出我们的屏幕.在粒子引擎里,有时可看见更多的图象,而且当接近你时很酷 75 | float m_slowdown; // 减速粒子 76 | float m_xspeed; // X方向的速度 77 | float m_yspeed; // Y方向的速度 78 | float m_zoom; // 沿Z轴缩放 79 | //下面我们创建一个数组叫particle.数组存储MAX_PARTICLES个元素.也就是说我们创建1000(MAX_PARTICLES)个粒子, 80 | //存储空间为每个粒子提供相应的信息 81 | particles particle[MAX_PARTICLES]; // 保存1000个粒子的数组 82 | 83 | GLuint m_col; // 当前的颜色 84 | GLuint m_delay; // 彩虹效果延迟 85 | }; 86 | 87 | #endif // MYGLWIDGET_H 88 | -------------------------------------------------------------------------------- /Lesson19_TriangleStripAndParticleEngine/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Particle.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson20_Masking/Lesson20_Masking.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-14T13:31:54 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson20_Masking 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson20_Masking/image/Image1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson20_Masking/image/Image1.bmp -------------------------------------------------------------------------------- /Lesson20_Masking/image/Image2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson20_Masking/image/Image2.bmp -------------------------------------------------------------------------------- /Lesson20_Masking/image/Logo.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson20_Masking/image/Logo.bmp -------------------------------------------------------------------------------- /Lesson20_Masking/image/Mask1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson20_Masking/image/Mask1.bmp -------------------------------------------------------------------------------- /Lesson20_Masking/image/Mask2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson20_Masking/image/Mask2.bmp -------------------------------------------------------------------------------- /Lesson20_Masking/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson20_Masking/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit MyGLWidget(QWidget *parent = 0); 17 | ~MyGLWidget(); 18 | protected: 19 | void resizeGL(int w, int h); 20 | 21 | void initializeGL(); 22 | 23 | void paintGL(); 24 | 25 | void keyPressEvent(QKeyEvent *event); 26 | 27 | void timerEvent(QTimerEvent *event); 28 | private: 29 | void loadGLTexture(); 30 | private: 31 | bool m_show_full_screen; 32 | 33 | GLuint m_texture[5]; 34 | bool m_masking; 35 | bool m_scene; 36 | GLfloat m_roll; 37 | }; 38 | 39 | #endif // MYGLWIDGET_H 40 | -------------------------------------------------------------------------------- /Lesson20_Masking/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Image1.bmp 4 | image/Image2.bmp 5 | image/Logo.bmp 6 | image/Mask1.bmp 7 | image/Mask2.bmp 8 | 9 | 10 | -------------------------------------------------------------------------------- /Lesson21_Amidar/Lesson21_Amidar.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-14T14:42:35 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl multimedia 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson21_Amidar 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson21_Amidar/image/Font.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson21_Amidar/image/Font.bmp -------------------------------------------------------------------------------- /Lesson21_Amidar/image/Image.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson21_Amidar/image/Image.bmp -------------------------------------------------------------------------------- /Lesson21_Amidar/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson21_Amidar/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | //接下来我们需要一个结构来记录游戏中的对象。fx和fy每次在网格上移动我们的英雄和敌人一些较小的象素,以创建一个平滑的动画效果。 17 | //x和y则记录着对象处于网格的那个交点上。 18 | //上下左右各有11个点,因此x和y可以是0到10之间的任意值。这也是我们为什么需要fx和fy的原因。 19 | //考虑如果我们只能够在上下和左右方向的11个点间移动的话,我们的英雄不得不 20 | //在各个点间跳跃前进。这样显然是不够平滑美观的。 21 | //最后一个变量spin用来使对象在Z轴上旋转。 22 | struct Object // 记录游戏中的对象 23 | { 24 | int fx, fy; // 使移动变得平滑 25 | int x, y; // 当前游戏者的位置 26 | float spin; // 旋转方向 27 | Object() 28 | { 29 | fx = 0; 30 | fy = 0; 31 | x = 0; 32 | y = 0; 33 | spin = 0.0f; 34 | } 35 | }; 36 | 37 | explicit MyGLWidget(QWidget *parent = 0); 38 | ~MyGLWidget(); 39 | protected: 40 | void resizeGL(int w, int h); 41 | 42 | void initializeGL(); 43 | 44 | void paintGL(); 45 | 46 | void keyPressEvent(QKeyEvent *event); 47 | 48 | void timerEvent(QTimerEvent *event); 49 | private: 50 | void resetObjects(); 51 | 52 | void loadGLTexture(); 53 | 54 | void buildFont(); 55 | 56 | void glPrint(GLint x, GLint y, int set, char *fmt); 57 | 58 | void resetLines(); 59 | private: 60 | bool m_show_full_screen; 61 | 62 | //bool类型的变量,vline保存了组成我们游戏网格垂直方向上的121条线,上下水平各11条。hline保存了水平方向上的 121条线, 63 | //用ap来检查A键是否已经按下。 64 | //当网格被填满时, filled被设置为TRUE而反之则为FALSE。gameover这个变量的作用显而易见,当他的值为TRUE时,游戏结束。 65 | //anti指出抗锯齿功能是否打开,当设置为TRUE时,该功能是打开着的。 66 | bool m_vline[11][10]; // 保存垂直方向的11根线条中,每根线条中的10段是否被走过 67 | bool m_hline[10][11]; //保存水平方向的11根线条中,每根线条中的10段是否被走过 68 | bool m_filled; // 网格是否被填满? 69 | bool m_gameover; // 游戏是否结束? 70 | bool m_anti; // 是否启用反走样? 71 | //delay 是一个计数器,我用他来减慢那些坏蛋的动作。当delay的值大于某一个馈值的时候,敌人才可以行动,此时delay将被重置。 72 | int m_delay; 73 | //我们把lives的值设置成5,这样我们的英雄一出场就拥有5条命。level是一个内部变量,用来指出当前游戏的难度。 74 | //当然,这并不是你在屏幕上所看到的那个Level。变量level2开始的时候和Level拥有相同的值,但是随着你技能的提高,这个值也会增加。 75 | //当你成功通过难度3之后,这个值也将在难度3上停止增加。level 是一个用来表示游戏难度的内部变量,stage才是用来记录当前游戏关卡的变量。 76 | int m_lives; // 玩家的生命 77 | int m_level; // 内部游戏的等级 78 | int m_stage; // 游戏的关卡 79 | 80 | //既然我们已经为我们的玩家,敌人,甚至是秘密武器。设置了结构体,那么同样的,为了表现刚刚创设的结构体的功能和特性, 81 | //我们也可以为此设置新的结构体。 82 | //为我们的玩家创设结构体之下的第一条直线。基本上我们将会为玩家提供fx,fy,x,y和spin值几种不同的结构体。 83 | //通过增加这些直线,仅需查看玩家的x值我们就很容易取得玩家的位置,同时我们也可以通过增加玩家的旋转度来改变玩家的spin值。 84 | //第二条直线略有不同。因为同一屏幕我们可以同时拥有至多15个敌人。我们需要为每个敌人创造上面所提到的可变量。我 85 | //们通过设置一个有15个敌人的组来实现这个目标,如第一个敌人的位置被设定为敌人(0).x.第二个敌人的位置为(1),x等等 86 | //第三条直线使得为宝物创设结构体实现了可能。宝物是一个会时不时在屏幕上出现的沙漏。我们需要通过沙漏来追踪x和y值。 87 | //但是因为沙漏的位置是固定的所以我们不需要寻找最佳位置,而通过为程序后面的其他物品寻找好的可变量来实现(如fx和fy) 88 | Object m_player; // 玩家信息 89 | Object m_enemy[9]; // 最多9个敌人的信息 90 | Object m_hourglass; // 宝物信息 91 | 92 | GLuint m_texture[2]; // 字符纹理 93 | GLuint m_base; // 字符显示列表的开始值 94 | 95 | QSound m_freezeSound; 96 | }; 97 | 98 | #endif // MYGLWIDGET_H 99 | -------------------------------------------------------------------------------- /Lesson21_Amidar/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Font.bmp 4 | image/Image.bmp 5 | voice/Complete.wav 6 | voice/Die.wav 7 | voice/Freeze.wav 8 | voice/Hourglass.wav 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lesson21_Amidar/voice/Complete.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson21_Amidar/voice/Complete.wav -------------------------------------------------------------------------------- /Lesson21_Amidar/voice/Die.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson21_Amidar/voice/Die.wav -------------------------------------------------------------------------------- /Lesson21_Amidar/voice/Freeze.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson21_Amidar/voice/Freeze.wav -------------------------------------------------------------------------------- /Lesson21_Amidar/voice/Hourglass.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson21_Amidar/voice/Hourglass.wav -------------------------------------------------------------------------------- /Lesson22_BumpMapping/Lesson22_BumpMapping.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-14T16:53:14 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson22_BumpMapping 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson22_BumpMapping/image/Base.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson22_BumpMapping/image/Base.bmp -------------------------------------------------------------------------------- /Lesson22_BumpMapping/image/Bump.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson22_BumpMapping/image/Bump.bmp -------------------------------------------------------------------------------- /Lesson22_BumpMapping/image/Multi_On.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson22_BumpMapping/image/Multi_On.bmp -------------------------------------------------------------------------------- /Lesson22_BumpMapping/image/Multi_On_Alpha.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson22_BumpMapping/image/Multi_On_Alpha.bmp -------------------------------------------------------------------------------- /Lesson22_BumpMapping/image/OpenGL.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson22_BumpMapping/image/OpenGL.bmp -------------------------------------------------------------------------------- /Lesson22_BumpMapping/image/OpenGL_Alpha.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson22_BumpMapping/image/OpenGL_Alpha.bmp -------------------------------------------------------------------------------- /Lesson22_BumpMapping/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson22_BumpMapping/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | class MyGLWidget : public QGLWidget 14 | { 15 | Q_OBJECT 16 | 17 | public: 18 | explicit MyGLWidget(QWidget *parent = 0); 19 | ~MyGLWidget(); 20 | protected: 21 | void resizeGL(int w, int h); 22 | 23 | void initializeGL(); 24 | 25 | void paintGL(); 26 | 27 | void keyPressEvent(QKeyEvent *event); 28 | 29 | void timerEvent(QTimerEvent *event); 30 | private: 31 | void loadGLTexture(); 32 | 33 | bool initMultitexture(); 34 | 35 | void initLights(); 36 | 37 | void doMesh1TexelUnits(); 38 | 39 | void doMesh2TexelUnits(); 40 | 41 | void doMeshNoBumps(); 42 | 43 | void doCube(); 44 | 45 | void doLogo(); 46 | private: 47 | bool m_show_full_screen; 48 | GLfloat m_z; 49 | GLfloat m_xrot; 50 | GLfloat m_yrot; 51 | GLfloat m_xspeed; 52 | GLfloat m_yspeed; 53 | 54 | bool m_multitextureSupported; // 标志是否支持多重渲染 55 | bool m_useMultitexture; // 如果支持,是否使用它 56 | bool m_emboss; 57 | bool m_bumps; 58 | GLint m_maxTexelUnits; // 纹理处理单元的个数 59 | //下面我们来定义一些变量: 60 | //filter定义过滤器类型 61 | //texture[3]保存三个纹理 62 | //bump[3]保存三个凹凸纹理 63 | //invbump[3]保存三个反转了的凹凸纹理 64 | //glLogo保存标志 65 | //multiLogo保存多重纹理标志 66 | GLuint m_filter; // 定义过滤器类型 67 | GLuint m_texture[3]; // 保存三个纹理 68 | GLuint m_bump[3]; //保存三个凹凸纹理 69 | GLuint m_invbump[3]; // 保存三个反转了的凹凸纹理 70 | GLuint m_glLogo; // glLogo保存标志 71 | GLuint m_multiLogo; // multiLogo保存多重纹理标志 72 | }; 73 | 74 | #endif // MYGLWIDGET_H 75 | -------------------------------------------------------------------------------- /Lesson22_BumpMapping/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Base.bmp 4 | image/Bump.bmp 5 | image/Multi_On.bmp 6 | image/Multi_On_Alpha.bmp 7 | image/OpenGL.bmp 8 | image/OpenGL_Alpha.bmp 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lesson23_SphereMapping/Lesson23_SphereMapping.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-16T00:58:53 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson23_SphereMapping 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson23_SphereMapping/image/BG.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson23_SphereMapping/image/BG.bmp -------------------------------------------------------------------------------- /Lesson23_SphereMapping/image/Reflect.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson23_SphereMapping/image/Reflect.bmp -------------------------------------------------------------------------------- /Lesson23_SphereMapping/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson23_SphereMapping/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MyGLWidget : public QGLWidget 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit MyGLWidget(QWidget *parent = 0); 18 | ~MyGLWidget(); 19 | protected: 20 | void resizeGL(int w, int h); 21 | 22 | void initializeGL(); 23 | 24 | void paintGL(); 25 | 26 | void keyPressEvent(QKeyEvent *event); 27 | 28 | void timerEvent(QTimerEvent *event); 29 | private: 30 | void loadGLTexture(); 31 | 32 | void glDrawCube(); 33 | private: 34 | bool m_show_full_screen; 35 | bool m_light; 36 | 37 | GLfloat m_x_rotate; 38 | GLfloat m_y_rotate; 39 | GLfloat m_x_speed; 40 | GLfloat m_y_speed; 41 | GLfloat m_z; 42 | 43 | GLfloat m_light_ambient[4]; 44 | GLfloat m_light_diffuse[4]; 45 | GLfloat m_light_position[4]; 46 | 47 | GLuint m_filter; 48 | GLuint m_texture[6]; 49 | 50 | GLUquadricObj *m_quadratic; // 二次几何体 51 | int m_part1; // 圆盘的起始角度 52 | int m_part2; // 圆盘的结束角度 53 | int m_p1; // 增量1 54 | int m_p2; // 增量1 55 | GLuint m_object; // 二次几何体标示符 56 | }; 57 | 58 | #endif // MYGLWIDGET_H 59 | -------------------------------------------------------------------------------- /Lesson23_SphereMapping/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/BG.bmp 4 | image/Reflect.bmp 5 | 6 | 7 | -------------------------------------------------------------------------------- /Lesson24_Extensions/Lesson24_Extensions.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-16T01:44:28 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson24_Extensions 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson24_Extensions/image/Font.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson24_Extensions/image/Font.tga -------------------------------------------------------------------------------- /Lesson24_Extensions/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson24_Extensions/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MyGLWidget : public QGLWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MyGLWidget(QWidget *parent = 0); 16 | ~MyGLWidget(); 17 | protected: 18 | void resizeGL(int w, int h); 19 | 20 | void initializeGL(); 21 | 22 | void paintGL(); 23 | 24 | void keyPressEvent(QKeyEvent *event); 25 | 26 | void timerEvent(QTimerEvent *event); 27 | private: 28 | void loadGLTexture(); 29 | 30 | void buildFont(); 31 | 32 | void glPrint(GLint x, GLint y, int set, char *string); 33 | private: 34 | bool m_show_full_screen; 35 | //接下来我们添加一些变量,第一个为滚动参数。第二给变量记录扩展的个数,swidth和sheight记录剪切矩形的大小。 36 | //base为字体显示列表的开始值。 37 | int m_scroll; // 用来滚动屏幕 38 | int m_maxtokens; // 保存扩展的个数 39 | int m_swidth; // 剪裁宽度 40 | int m_sheight; // 剪裁高度 41 | GLuint m_base;// 字符显示列表的开始值 42 | 43 | GLuint m_textures[1]; 44 | }; 45 | 46 | #endif // MYGLWIDGET_H 47 | -------------------------------------------------------------------------------- /Lesson24_Extensions/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Font.tga 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson25_MorphingPoints/Lesson25_MorphingPoints.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-16T18:26:41 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson25_MorphingPoints 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson25_MorphingPoints/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson25_MorphingPoints/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | //下面的结构定义一个三维顶点 17 | typedef struct 18 | { 19 | float x, y, z; 20 | } VERTEX; 21 | //下面的结构使用顶点来描述一个三维物体 22 | typedef struct // 物体结构 23 | { 24 | int verts;// 物体中顶点的个数 25 | VERTEX *points;// 包含顶点数据的指针 26 | } OBJECT; 27 | 28 | explicit MyGLWidget(QWidget *parent = 0); 29 | ~MyGLWidget(); 30 | protected: 31 | void resizeGL(int w, int h); 32 | 33 | void initializeGL(); 34 | 35 | void paintGL(); 36 | 37 | void keyPressEvent(QKeyEvent *event); 38 | 39 | void timerEvent(QTimerEvent *event); 40 | private: 41 | void objallocate(OBJECT *k,int n); 42 | 43 | void objfree(OBJECT *k); 44 | 45 | void readstr(QTextStream *stream, QString &string); 46 | 47 | void objload(QString name, OBJECT *k); 48 | 49 | VERTEX calculate(int i); 50 | private: 51 | bool m_show_full_screen; 52 | //我们结下来添加几个旋转变量,用来记录旋转的信息。并使用cx,cy,cz设置物体在屏幕上的位置。 53 | //变量key用来记录当前的模型,step用来设置相邻变形之间的中间步骤。如step为200,则需要200次,才能把一个物体变为另一个物体。 54 | //最后我们用一个变量来设置是否使用变形。 55 | GLfloat m_xrot; 56 | GLfloat m_yrot; 57 | GLfloat m_zrot; // X, Y & Z 轴的旋转角度 58 | GLfloat m_xspeed; 59 | GLfloat m_yspeed; 60 | GLfloat m_zspeed;// X, Y & Z 轴的旋转速度 61 | GLfloat m_cx; 62 | GLfloat m_cy; 63 | GLfloat m_cz;// 物体的位置 64 | int m_key;// 物体的标识符 65 | int m_step; 66 | int m_steps;// 变换的步数 67 | bool m_morph;// 是否使用变形 68 | //maxver用来记录各个物体中最大的顶点数,如一个物体使用5个顶点,另一个物体使用20个顶点,那么物体的顶点个数为20。 69 | //结下来定义了四个我们使用的模型物体,并把相邻模型变形的中间状态保存在helper中,sour保存原模型物体,dest保存将要变形的模型物体。 70 | int m_maxver;// 最大的顶点数 71 | OBJECT m_morph1; 72 | OBJECT m_morph2; 73 | OBJECT m_morph3; 74 | OBJECT m_morph4;// 我们的四个物体 75 | OBJECT m_helper; 76 | OBJECT *m_sour; 77 | OBJECT *m_dest;// 帮助物体,原物体,目标物体 78 | }; 79 | 80 | #endif // MYGLWIDGET_H 81 | -------------------------------------------------------------------------------- /Lesson25_MorphingPoints/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | data/Sphere.txt 4 | data/Torus.txt 5 | data/Tube.txt 6 | 7 | 8 | -------------------------------------------------------------------------------- /Lesson26_StencilAndReflection/Lesson26_StencilAndReflection.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-16T19:16:56 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson26_StencilAndReflection 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson26_StencilAndReflection/image/Ball.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson26_StencilAndReflection/image/Ball.bmp -------------------------------------------------------------------------------- /Lesson26_StencilAndReflection/image/Envroll.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson26_StencilAndReflection/image/Envroll.bmp -------------------------------------------------------------------------------- /Lesson26_StencilAndReflection/image/Envwall.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson26_StencilAndReflection/image/Envwall.bmp -------------------------------------------------------------------------------- /Lesson26_StencilAndReflection/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson26_StencilAndReflection/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MyGLWidget : public QGLWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MyGLWidget(QWidget *parent = 0); 16 | ~MyGLWidget(); 17 | protected: 18 | void resizeGL(int w, int h); 19 | 20 | void initializeGL(); 21 | 22 | void paintGL(); 23 | 24 | void keyPressEvent(QKeyEvent *event); 25 | 26 | void timerEvent(QTimerEvent *event); 27 | private: 28 | void loadGLTexture(); 29 | 30 | void drawObject(); 31 | 32 | void drawFloor(); 33 | private: 34 | bool m_show_full_screen; 35 | //下面用二次几何体创建一个球,并设置纹理 36 | GLUquadricObj *m_q;// 使用二次几何体创建球 37 | GLfloat m_xrot;// X方向的旋转角度 38 | GLfloat m_yrot;// Y方向的旋转角的 39 | GLfloat m_xrotspeed;// X方向的旋转速度 40 | GLfloat m_yrotspeed;// Y方向的旋转速度 41 | GLfloat m_zoom;// 移入屏幕7个单位 42 | GLfloat m_height;// 球离开地板的高度 43 | GLuint m_textures[3];// 使用三个纹理 44 | }; 45 | 46 | #endif // MYGLWIDGET_H 47 | -------------------------------------------------------------------------------- /Lesson26_StencilAndReflection/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Ball.bmp 4 | image/Envroll.bmp 5 | image/Envwall.bmp 6 | 7 | 8 | -------------------------------------------------------------------------------- /Lesson27_ShadowCasting/Lesson27_ShadowCasting.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-16T20:46:10 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson27_ShadowCasting 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson27_ShadowCasting/data/Object.txt: -------------------------------------------------------------------------------- 1 | 8 2 | -1 1 -1 3 | 1 1 -1 4 | 1 1 1 5 | -1 1 1 6 | -1 -1 -1 7 | 1 -1 -1 8 | 1 -1 1 9 | -1 -1 1 10 | 12 11 | 1 3 2 0 1 0 0 1 0 0 1 0 12 | 1 4 3 0 1 0 0 1 0 0 1 0 13 | 5 6 7 0 -1 0 0 -1 0 0 -1 0 14 | 5 7 8 0 -1 0 0 -1 0 0 -1 0 15 | 5 4 1 -1 0 0 -1 0 0 -1 0 0 16 | 5 8 4 -1 0 0 -1 0 0 -1 0 0 17 | 3 6 2 1 0 0 1 0 0 1 0 0 18 | 3 7 6 1 0 0 1 0 0 1 0 0 19 | 5 1 2 0 0 -1 0 0 -1 0 0 -1 20 | 5 2 6 0 0 -1 0 0 -1 0 0 -1 21 | 3 4 8 0 0 1 0 0 1 0 0 1 22 | 3 8 7 0 0 1 0 0 1 0 0 1 -------------------------------------------------------------------------------- /Lesson27_ShadowCasting/data/Object1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | -2 0.2 -0.2 3 | 2 0.2 -0.2 4 | 2 0.2 0.2 5 | -2 0.2 0.2 6 | -2 -0.2 -0.2 7 | 2 -0.2 -0.2 8 | 2 -0.2 0.2 9 | -2 -0.2 0.2 10 | 11 | -0.2 2 -0.2 12 | 0.2 2 -0.2 13 | 0.2 2 0.2 14 | -0.2 2 0.2 15 | -0.2 -2 -0.2 16 | 0.2 -2 -0.2 17 | 0.2 -2 0.2 18 | -0.2 -2 0.2 19 | 24 20 | 1 3 2 0 1 0 0 1 0 0 1 0 21 | 1 4 3 0 1 0 0 1 0 0 1 0 22 | 5 6 7 0 -1 0 0 -1 0 0 -1 0 23 | 5 7 8 0 -1 0 0 -1 0 0 -1 0 24 | 5 4 1 -1 0 0 -1 0 0 -1 0 0 25 | 5 8 4 -1 0 0 -1 0 0 -1 0 0 26 | 3 6 2 1 0 0 1 0 0 1 0 0 27 | 3 7 6 1 0 0 1 0 0 1 0 0 28 | 5 1 2 0 0 -1 0 0 -1 0 0 -1 29 | 5 2 6 0 0 -1 0 0 -1 0 0 -1 30 | 3 4 8 0 0 1 0 0 1 0 0 1 31 | 3 8 7 0 0 1 0 0 1 0 0 1 32 | 33 | 9 11 10 0 1 0 0 1 0 0 1 0 34 | 9 12 11 0 1 0 0 1 0 0 1 0 35 | 13 14 15 0 -1 0 0 -1 0 0 -1 0 36 | 13 15 16 0 -1 0 0 -1 0 0 -1 0 37 | 13 12 9 -1 0 0 -1 0 0 -1 0 0 38 | 13 16 12 -1 0 0 -1 0 0 -1 0 0 39 | 11 14 10 1 0 0 1 0 0 1 0 0 40 | 11 15 14 1 0 0 1 0 0 1 0 0 41 | 13 9 10 0 0 -1 0 0 -1 0 0 -1 42 | 13 10 14 0 0 -1 0 0 -1 0 0 -1 43 | 11 12 16 0 0 1 0 0 1 0 0 1 44 | 11 16 15 0 0 1 0 0 1 0 0 1 -------------------------------------------------------------------------------- /Lesson27_ShadowCasting/data/Object2.txt: -------------------------------------------------------------------------------- 1 | 24 2 | -2 0.2 -0.2 3 | 2 0.2 -0.2 4 | 2 0.2 0.2 5 | -2 0.2 0.2 6 | -2 -0.2 -0.2 7 | 2 -0.2 -0.2 8 | 2 -0.2 0.2 9 | -2 -0.2 0.2 10 | 11 | -0.2 2 -0.2 12 | 0.2 2 -0.2 13 | 0.2 2 0.2 14 | -0.2 2 0.2 15 | -0.2 -2 -0.2 16 | 0.2 -2 -0.2 17 | 0.2 -2 0.2 18 | -0.2 -2 0.2 19 | 20 | -0.2 0.2 -2 21 | 0.2 0.2 -2 22 | 0.2 0.2 2 23 | -0.2 0.2 2 24 | -0.2 -0.2 -2 25 | 0.2 -0.2 -2 26 | 0.2 -0.2 2 27 | -0.2 -0.2 2 28 | 29 | 36 30 | 1 3 2 0 1 0 0 1 0 0 1 0 31 | 1 4 3 0 1 0 0 1 0 0 1 0 32 | 5 6 7 0 -1 0 0 -1 0 0 -1 0 33 | 5 7 8 0 -1 0 0 -1 0 0 -1 0 34 | 5 4 1 -1 0 0 -1 0 0 -1 0 0 35 | 5 8 4 -1 0 0 -1 0 0 -1 0 0 36 | 3 6 2 1 0 0 1 0 0 1 0 0 37 | 3 7 6 1 0 0 1 0 0 1 0 0 38 | 5 1 2 0 0 -1 0 0 -1 0 0 -1 39 | 5 2 6 0 0 -1 0 0 -1 0 0 -1 40 | 3 4 8 0 0 1 0 0 1 0 0 1 41 | 3 8 7 0 0 1 0 0 1 0 0 1 42 | 43 | 9 11 10 0 1 0 0 1 0 0 1 0 44 | 9 12 11 0 1 0 0 1 0 0 1 0 45 | 13 14 15 0 -1 0 0 -1 0 0 -1 0 46 | 13 15 16 0 -1 0 0 -1 0 0 -1 0 47 | 13 12 9 -1 0 0 -1 0 0 -1 0 0 48 | 13 16 12 -1 0 0 -1 0 0 -1 0 0 49 | 11 14 10 1 0 0 1 0 0 1 0 0 50 | 11 15 14 1 0 0 1 0 0 1 0 0 51 | 13 9 10 0 0 -1 0 0 -1 0 0 -1 52 | 13 10 14 0 0 -1 0 0 -1 0 0 -1 53 | 11 12 16 0 0 1 0 0 1 0 0 1 54 | 11 16 15 0 0 1 0 0 1 0 0 1 55 | 56 | 17 19 18 0 1 0 0 1 0 0 1 0 57 | 17 20 19 0 1 0 0 1 0 0 1 0 58 | 21 22 23 0 -1 0 0 -1 0 0 -1 0 59 | 21 23 24 0 -1 0 0 -1 0 0 -1 0 60 | 21 20 17 -1 0 0 -1 0 0 -1 0 0 61 | 21 24 20 -1 0 0 -1 0 0 -1 0 0 62 | 19 22 18 1 0 0 1 0 0 1 0 0 63 | 19 23 22 1 0 0 1 0 0 1 0 0 64 | 21 17 18 0 0 -1 0 0 -1 0 0 -1 65 | 21 18 22 0 0 -1 0 0 -1 0 0 -1 66 | 19 20 24 0 0 1 0 0 1 0 0 1 67 | 19 24 23 0 0 1 0 0 1 0 0 1 -------------------------------------------------------------------------------- /Lesson27_ShadowCasting/data/SimpleObject.txt: -------------------------------------------------------------------------------- 1 | 4 2 | -1.0 1.0 -1.0 3 | 1.0 1.0 -1.0 4 | 1.0 1.0 1.0 5 | -1.0 1.0 1.0 6 | 2 7 | 1 4 3 8 | 1 3 2 -------------------------------------------------------------------------------- /Lesson27_ShadowCasting/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson27_ShadowCasting/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | //下面定义一个3D顶点结构 17 | struct sPoint 18 | { 19 | GLfloat x, y, z; 20 | }; 21 | //定义一个平面结构 22 | // 平面方程为: ax + by + cz + d = 0 23 | struct sPlaneEq 24 | { 25 | GLfloat a, b, c, d; 26 | }; 27 | //下面定义一个用来投影的三角形的结构 28 | //3个整形索引指定了模型中三角形的三个顶点 29 | //第二个变量指定了三角形面的法线 30 | //平面方程描述了三角所在的平面 31 | //临近的3个顶点索引,指定了与这个三角形相邻的三个顶点 32 | //最后一个变量指定这个三角形是否投出阴影 33 | // 描述一个模型表面的结构 34 | struct sPlane 35 | { 36 | unsigned int p[3]; // 3个整形索引指定了模型中三角形的三个顶点 37 | sPoint normals[3]; // 第二个变量指定了三角形面的法线 38 | unsigned int neigh[3]; // 与本三角形三个边相邻的面的索引 39 | sPlaneEq PlaneEq; // 平面方程描述了三角所在的平面 40 | bool visible; // 最后一个变量指定这个三角形是否投出阴影? 41 | }; 42 | //最后我们用下面的结构描述一个产生阴影的物体。 43 | struct glObject 44 | { 45 | GLuint nPlanes, nPoints; 46 | sPoint points[100]; 47 | sPlane planes[200]; 48 | }; 49 | explicit MyGLWidget(QWidget *parent = 0); 50 | ~MyGLWidget(); 51 | protected: 52 | void resizeGL(int w, int h); 53 | 54 | void initializeGL(); 55 | 56 | void paintGL(); 57 | 58 | void keyPressEvent(QKeyEvent *event); 59 | 60 | void timerEvent(QTimerEvent *event); 61 | private: 62 | bool readObject(QString fileName, glObject* o); 63 | 64 | void setConnectivity(glObject *o); 65 | 66 | void drawObject(glObject o); 67 | 68 | void calculatePlane(glObject o, sPlane *plane); 69 | 70 | void castShadow(glObject *o, float *lp); 71 | 72 | void drawGLScene(); 73 | 74 | void drawGLRoom(); 75 | 76 | bool initGLObjects(); 77 | private: 78 | bool m_show_full_screen; 79 | glObject m_obj; 80 | GLfloat m_xrot; 81 | GLfloat m_yrot; 82 | GLfloat m_xspeed; 83 | GLfloat m_yspeed; 84 | float m_lightPos[4];// Light Position 85 | float m_lightAmb[4];// Ambient Light Values 86 | float m_lightDif[4];// Diffuse Light Values 87 | float m_lightSpc[4];// Specular Light Values 88 | float m_matAmb[4];// Material - Ambient Values 89 | float m_matDif[4];// Material - Diffuse Values 90 | float m_matSpc[4];// Material - Specular Values 91 | float m_matShn[1];// Material - Shininess 92 | float m_objPos[3];// Object Position 93 | GLUquadricObj *m_q;// Quadratic For Drawing A Sphere 94 | float m_spherePos[3]; 95 | }; 96 | 97 | #endif // MYGLWIDGET_H 98 | -------------------------------------------------------------------------------- /Lesson27_ShadowCasting/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | data/Object.txt 4 | data/Object1.txt 5 | data/Object2.txt 6 | data/SimpleObject.txt 7 | 8 | 9 | -------------------------------------------------------------------------------- /Lesson28_BezierSurfaces/Lesson28_BezierSurfaces.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-17T10:31:29 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson28_BezierSurfaces 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson28_BezierSurfaces/image/NeHe.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson28_BezierSurfaces/image/NeHe.bmp -------------------------------------------------------------------------------- /Lesson28_BezierSurfaces/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson28_BezierSurfaces/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | typedef struct point_3d 13 | { 14 | // 3D点的结构 15 | double x, y, z; 16 | } POINT_3D; 17 | 18 | class MyGLWidget : public QGLWidget 19 | { 20 | Q_OBJECT 21 | public: 22 | typedef struct bpatch 23 | { 24 | // 贝塞尔面片结构 25 | POINT_3D anchors[4][4];// 由4x4网格组成 26 | GLuint dlBPatch;// 绘制面片的显示列表名称 27 | GLuint texture;// 面片的纹理 28 | } BEZIER_PATCH; 29 | 30 | explicit MyGLWidget(QWidget *parent = 0); 31 | ~MyGLWidget(); 32 | protected: 33 | void resizeGL(int w, int h); 34 | 35 | void initializeGL(); 36 | 37 | void paintGL(); 38 | 39 | void keyPressEvent(QKeyEvent *event); 40 | 41 | void timerEvent(QTimerEvent *event); 42 | private: 43 | GLuint genBezier(BEZIER_PATCH patch, int divs); 44 | 45 | void initBezier(); 46 | 47 | void loadGLTexture(); 48 | private: 49 | bool m_show_full_screen; 50 | BEZIER_PATCH m_mybezier;// 创建一个贝塞尔曲面结构 51 | bool m_showCPoints;// 是否显示控制点 52 | int m_divs;// 细分精度,控制曲面的显示精度 53 | GLfloat m_rotz; 54 | }; 55 | 56 | #endif // MYGLWIDGET_H 57 | -------------------------------------------------------------------------------- /Lesson28_BezierSurfaces/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/NeHe.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson29_Blitter/Lesson29_Blitter.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-17T11:34:44 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson29_Blitter 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson29_Blitter/data/GL.raw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson29_Blitter/data/GL.raw -------------------------------------------------------------------------------- /Lesson29_Blitter/data/Monitor.raw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson29_Blitter/data/Monitor.raw -------------------------------------------------------------------------------- /Lesson29_Blitter/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson29_Blitter/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | typedef struct Texture_Image 12 | { 13 | int width;// 宽 14 | int height;// 高 15 | int format;// 像素格式 16 | unsigned char *data;// 纹理数据 17 | } TEXTURE_IMAGE; 18 | typedef TEXTURE_IMAGE *P_TEXTURE_IMAGE; 19 | 20 | class MyGLWidget : public QGLWidget 21 | { 22 | Q_OBJECT 23 | 24 | public: 25 | explicit MyGLWidget(QWidget *parent = 0); 26 | ~MyGLWidget(); 27 | protected: 28 | void resizeGL(int w, int h); 29 | 30 | void initializeGL(); 31 | 32 | void paintGL(); 33 | 34 | void keyPressEvent(QKeyEvent *event); 35 | 36 | void timerEvent(QTimerEvent *event); 37 | private: 38 | int readTextureData(QString filename, P_TEXTURE_IMAGE buffer); 39 | 40 | void buildTexture (P_TEXTURE_IMAGE tex); 41 | 42 | void blit(P_TEXTURE_IMAGE src, P_TEXTURE_IMAGE dst, int src_xstart, 43 | int src_ystart, int src_width, int src_height,int dst_xstart, 44 | int dst_ystart, int blend, int alpha); 45 | private: 46 | bool m_show_full_screen; 47 | //接下来定义了两个指向这个结构的指针 48 | P_TEXTURE_IMAGE m_t1;// 指向保存图像结构的指针 49 | P_TEXTURE_IMAGE m_t2;// 指向保存图像结构的指针 50 | GLuint m_texture[1]; 51 | 52 | GLfloat m_xrot; 53 | GLfloat m_yrot; 54 | GLfloat m_zrot; 55 | }; 56 | 57 | #endif // MYGLWIDGET_H 58 | -------------------------------------------------------------------------------- /Lesson29_Blitter/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | data/GL.raw 4 | data/Monitor.raw 5 | 6 | 7 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/Lesson30_CollisionDetection.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-17T21:31:34 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl multimedia 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson30_CollisionDetection 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp \ 17 | tvector.cpp \ 18 | tray.cpp 19 | 20 | HEADERS += myglwidget.h \ 21 | tvector.h \ 22 | tray.h \ 23 | mathex.h 24 | 25 | RESOURCES += \ 26 | src.qrc 27 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/image/Boden.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson30_CollisionDetection/image/Boden.bmp -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/image/Marble.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson30_CollisionDetection/image/Marble.bmp -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/image/Spark.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson30_CollisionDetection/image/Spark.bmp -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/image/Wand.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson30_CollisionDetection/image/Wand.bmp -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/mathex.h: -------------------------------------------------------------------------------- 1 | #ifndef MATHEX_H 2 | #define MATHEX_H 3 | 4 | template inline T sqr(const T &x) 5 | { 6 | return x*x; 7 | } 8 | 9 | #endif // MATHEX_H 10 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "tvector.h" 11 | #include "tray.h" 12 | 13 | struct Plane 14 | { 15 | TVector _Position; 16 | TVector _Normal; 17 | }; 18 | 19 | //Cylinder structure 20 | struct Cylinder 21 | { 22 | TVector _Position; 23 | TVector _Axis; 24 | double _Radius; 25 | }; 26 | 27 | //Explosion structure 28 | struct Explosion 29 | { 30 | TVector _Position; 31 | float _Alpha; 32 | float _Scale; 33 | }; 34 | 35 | class MyGLWidget : public QGLWidget 36 | { 37 | Q_OBJECT 38 | public: 39 | explicit MyGLWidget(QWidget *parent = 0); 40 | ~MyGLWidget(); 41 | protected: 42 | void resizeGL(int w, int h); 43 | 44 | void initializeGL(); 45 | 46 | void paintGL(); 47 | 48 | void keyPressEvent(QKeyEvent *event); 49 | 50 | void timerEvent(QTimerEvent *event); 51 | private: 52 | int findBallCol(TVector& point, double& TimePoint, double Time2, int& BallNr1, int& BallNr2); 53 | 54 | void idle(); 55 | 56 | int testIntersionPlane(const Plane& plane,const TVector& position,const TVector& direction, 57 | double& lamda, TVector& pNormal); 58 | 59 | int testIntersionCylinder(const Cylinder& cylinder,const TVector& position,const TVector& direction, 60 | double& lamda, TVector& pNormal,TVector& newposition); 61 | 62 | void initVars(); 63 | 64 | void loadGLTexture(); 65 | private: 66 | bool m_show_full_screen; 67 | TVector m_arrayVel[10];//holds velocity of balls 68 | TVector m_arrayPos[10];//position of balls 69 | TVector m_oldPos[10];//old position of balls 70 | Explosion m_explosionArray[20];//holds max 20 explosions at once 71 | TVector m_pos;//initial position of camera 72 | GLuint m_texture[4]; 73 | GLuint m_dlist;//stores texture objects and display list 74 | int m_hook_toball1; 75 | int m_sounds;//hook camera on ball, and sound on/off 76 | float m_camera_rotation;//holds rotation around the Y axis 77 | double m_time;//timestep of simulation 78 | int m_nrOfBalls;//sets the number of balls 79 | GLUquadricObj *m_cylinder_obj;//Quadratic object to render the cylinders 80 | Plane m_pl1; 81 | Plane m_pl2; 82 | Plane m_pl3; 83 | Plane m_pl4; 84 | Plane m_pl5; //the 5 planes of the room 85 | Cylinder m_cyl1; 86 | Cylinder m_cyl2; 87 | Cylinder m_cyl3; //the 2 cylinders of the room 88 | }; 89 | 90 | #endif // MYGLWIDGET_H 91 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Boden.bmp 4 | image/Marble.bmp 5 | image/Spark.bmp 6 | image/Wand.bmp 7 | voice/Explode.wav 8 | 9 | 10 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/tray.cpp: -------------------------------------------------------------------------------- 1 | #include "tray.h" 2 | 3 | // Line betwen two points OR point and a direction 4 | TRay::TRay(const TVector &point1, const TVector &point2) 5 | : _P(point1) 6 | { 7 | _V = (point2.isUnit() ? point2 : TVector::unit(point2-point1)); 8 | } 9 | 10 | // Distance between a ray and a point 11 | double TRay::dist(const TVector &point) const 12 | { 13 | if (isValid() && point.isValid()) 14 | { 15 | TVector tv, point2; 16 | double lambda = TVector::dot(_V, point-_P); 17 | TVector::add(_P, TVector::multiply(_V, lambda, tv), point2); 18 | return point.dist(point2); 19 | } 20 | return 0.0; 21 | } 22 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/tray.h: -------------------------------------------------------------------------------- 1 | #ifndef TRAY_H 2 | #define TRAY_H 3 | 4 | #include "tvector.h" 5 | 6 | class TRay 7 | { 8 | public: 9 | TRay(){}; 10 | // Line betwen two points OR point and a direction 11 | TRay(const TVector &point1, const TVector &point2); 12 | // Selectors 13 | TVector P() const { return _P; } 14 | TVector V() const { return _V; } 15 | int isValid() const { return V().isUnit() && P().isValid(); } 16 | // Distances 17 | double dist(const TVector &point) const; 18 | private: 19 | TVector _P; // Any point on the line 20 | TVector _V; // Direction of the line 21 | }; 22 | 23 | #endif // TRAY_H 24 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/tvector.cpp: -------------------------------------------------------------------------------- 1 | #include "tvector.h" 2 | 3 | #define EPSILON 1.0e-8 4 | 5 | // Make a unit vector 6 | TVector &TVector::unit() 7 | { 8 | if (isDefault()) 9 | { 10 | double rep = mag(); 11 | if (rep < EPSILON) 12 | { 13 | _x = 0.0; 14 | _y = 0.0; 15 | _z = 0.0; 16 | } 17 | else 18 | { 19 | double temp = 1.0 / rep; 20 | _x *= temp; 21 | _y *= temp; 22 | _z *= temp; 23 | } 24 | _Status = UNIT; 25 | } 26 | return *this; 27 | } 28 | 29 | TVector &TVector::add(const TVector &v1, const TVector &v2, TVector &result) 30 | { 31 | if (v1.isValid() && v2.isValid()) 32 | { 33 | result._x = v1._x + v2._x; 34 | result._y = v1._y + v2._y; 35 | result._z = v1._z + v2._z; 36 | result._Status = DEFAULT; 37 | } 38 | else 39 | { 40 | result = TVector(); 41 | } 42 | return result; 43 | } 44 | 45 | TVector &TVector::subtract(const TVector &v1, const TVector &v2, TVector &result) 46 | { 47 | if (v1.isValid() && v2.isValid()) 48 | { 49 | result._x = v1._x - v2._x; 50 | result._y = v1._y - v2._y; 51 | result._z = v1._z - v2._z; 52 | result._Status = DEFAULT; 53 | } 54 | else 55 | { 56 | result = TVector(); 57 | } 58 | return result; 59 | } 60 | 61 | TVector &TVector::multiply(const TVector &v1, const double &scale, TVector &result) 62 | { 63 | if (v1.isValid()) 64 | { 65 | result._x = v1._x * scale; 66 | result._y = v1._y * scale; 67 | result._z = v1._z * scale; 68 | result._Status = DEFAULT; 69 | } 70 | else 71 | { 72 | result = TVector(); 73 | } 74 | return result; 75 | } 76 | 77 | TVector &TVector::cross(const TVector &v1, const TVector &v2, TVector &result) 78 | { 79 | if (v1.isValid() && v2.isValid()) 80 | { 81 | result._x = v1._y * v2._z - v1._z * v2._y; 82 | result._y = v1._z * v2._x - v1._x * v2._z; 83 | result._z = v1._x * v2._y - v1._y * v2._x; 84 | result._Status = DEFAULT; 85 | } 86 | else 87 | { 88 | result = TVector(); 89 | } 90 | return result; 91 | } 92 | 93 | TVector &TVector::invert(const TVector &v1, TVector result) 94 | { 95 | if (v1.isValid()) 96 | { 97 | result._x = -v1._x; 98 | result._y = -v1._y; 99 | result._z = -v1._z; 100 | result._Status = v1._Status; 101 | } 102 | else 103 | { 104 | result = TVector(); 105 | } 106 | return result; 107 | } 108 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/tvector.h: -------------------------------------------------------------------------------- 1 | #ifndef TVECTOR_H 2 | #define TVECTOR_H 3 | 4 | #include 5 | #include "mathex.h" 6 | 7 | class TVector 8 | { 9 | public: 10 | enum TStatus { INVALID, DEFAULT, UNIT }; 11 | // Constructors 12 | TVector() : _x(0.0), _y(0.0), _z(0.0), _Status(INVALID) {} 13 | TVector(double x, double y, double z) : _x(x), _y(y), _z(z), _Status(DEFAULT) {} 14 | 15 | // Selectors 16 | double X() const { return _x; } 17 | double Y() const { return _y; } 18 | double Z() const { return _z; } 19 | int isUnit() const { return _Status==UNIT; } 20 | int isDefault() const { return _Status==DEFAULT; } 21 | int isValid() const { return _Status!=INVALID; } 22 | 23 | // Change the status of a vector 24 | TVector &unit(); 25 | static TVector unit(const TVector &v) { return TVector(v).unit(); } 26 | static TVector &unit(const TVector &v, TVector &result) { result = v; return result.unit(); } 27 | 28 | // Magnitude 29 | double mag() const { return (isValid() ? (isUnit() ? 1.0 : sqrt(sqr(X()) + sqr(Y()) + sqr(Z()))) : 0.0); } 30 | 31 | // Dot or scalar product 32 | double dot(const TVector &v) const { return ((isValid() && v.isValid()) ? 33 | (X()*v.X() + Y()*v.Y() + Z()*v.Z()) : 0.0); } 34 | static double dot(const TVector &v1, const TVector &v2) { return v1.dot(v2); } 35 | 36 | // Distance between two vectors 37 | double dist(const TVector &v) const { return (*this-v).mag(); } 38 | 39 | // Optimised arithmetic methods 40 | static TVector &invert(const TVector &v1, TVector result); 41 | static TVector &add(const TVector &v1, const TVector &v2, TVector &result); 42 | static TVector &subtract(const TVector &v1, const TVector &v2, TVector &result); 43 | static TVector &multiply(const TVector &v1, const double &scale, TVector &result); 44 | static TVector &cross(const TVector &v1, const TVector &v2, TVector &result); 45 | 46 | // Vector arithmetic, addition, subtraction and vector product 47 | TVector operator-() const { return invert(*this, TVector()); } 48 | TVector &operator+=(const TVector &v) { return add(*this, v, *this); } 49 | TVector operator-(const TVector &v) const { TVector tv; return subtract(*this, v, tv); } 50 | TVector operator+(const TVector &v) const { TVector tv; return add(*this, v, tv); } 51 | TVector operator*(const double &scale) const { TVector tv; return multiply(*this, scale, tv); } 52 | private: 53 | double _x, _y, _z; 54 | TStatus _Status; 55 | }; 56 | 57 | #endif // TVECTOR_H 58 | -------------------------------------------------------------------------------- /Lesson30_CollisionDetection/voice/Explode.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson30_CollisionDetection/voice/Explode.wav -------------------------------------------------------------------------------- /Lesson31_MilkshapeModels/Data/Model.ms3d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson31_MilkshapeModels/Data/Model.ms3d -------------------------------------------------------------------------------- /Lesson31_MilkshapeModels/Data/Wood.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson31_MilkshapeModels/Data/Wood.bmp -------------------------------------------------------------------------------- /Lesson31_MilkshapeModels/Lesson31_MilkshapeModels.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-19T10:23:49 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson31_MilkshapeModels 12 | TEMPLATE = app 13 | 14 | SOURCES += main.cpp\ 15 | myglwidget.cpp \ 16 | model.cpp \ 17 | milkshapemodel.cpp 18 | 19 | HEADERS += myglwidget.h \ 20 | model.h \ 21 | milkshapemodel.h 22 | 23 | CONFIG(debug, debug|release) { 24 | sourcefiles.files = $$PWD/Data/* 25 | sourcefiles.path = $$OUT_PWD/debug/Data/ 26 | } else { 27 | sourcefiles.files = $$PWD/Data/* 28 | sourcefiles.path = $$OUT_PWD/release/Data/ 29 | } 30 | INSTALLS += sourcefiles 31 | -------------------------------------------------------------------------------- /Lesson31_MilkshapeModels/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson31_MilkshapeModels/milkshapemodel.h: -------------------------------------------------------------------------------- 1 | #ifndef MILKSHAPEMODEL_H 2 | #define MILKSHAPEMODEL_H 3 | 4 | #include 5 | #include 6 | #include "model.h" 7 | 8 | class MilkshapeModel : public Model 9 | { 10 | public: 11 | /* Constructor. */ 12 | MilkshapeModel(); 13 | /* Destructor. */ 14 | virtual ~MilkshapeModel(); 15 | /* 16 | Load the model data into the private variables. 17 | filename Model filename 18 | */ 19 | virtual bool loadModelData(char *filename); 20 | }; 21 | 22 | #endif // MILKSHAPEMODEL_H 23 | -------------------------------------------------------------------------------- /Lesson31_MilkshapeModels/model.cpp: -------------------------------------------------------------------------------- 1 | #include "model.h" 2 | 3 | GLuint LoadGLTexture( char *filename ) // Load Bitmaps And Convert To Textures 4 | { 5 | QString filePath = QApplication::applicationDirPath() + "/" + QString(filename); 6 | QImage image(filePath);// // Loads The Bitmap Specified By filename 7 | image = image.convertToFormat(QImage::Format_RGB888); 8 | image = image.mirrored(); 9 | GLuint texture = 0;//Texture ID 10 | if(!image.isNull())//Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit 11 | { 12 | glGenTextures(1, &texture); 13 | 14 | // Typical Texture Generation Using Data From The Bitmap 15 | glBindTexture(GL_TEXTURE_2D, texture); 16 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(), 17 | 0, GL_RGB, GL_UNSIGNED_BYTE, image.bits()); 18 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 19 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 20 | } 21 | return texture;// Return The Status 22 | } 23 | 24 | Model::Model() 25 | { 26 | m_numMeshes = 0; 27 | m_pMeshes = NULL; 28 | m_numMaterials = 0; 29 | m_pMaterials = NULL; 30 | m_numTriangles = 0; 31 | m_pTriangles = NULL; 32 | m_numVertices = 0; 33 | m_pVertices = NULL; 34 | } 35 | 36 | Model::~Model() 37 | { 38 | for (int i = 0; i < m_numMeshes; i++ ) 39 | { 40 | delete[] m_pMeshes[i].m_pTriangleIndices; 41 | } 42 | for (int i = 0; i < m_numMaterials; i++ ) 43 | { 44 | delete[] m_pMaterials[i].m_pTextureFilename; 45 | } 46 | m_numMeshes = 0; 47 | if ( m_pMeshes != NULL ) 48 | { 49 | delete[] m_pMeshes; 50 | m_pMeshes = NULL; 51 | } 52 | m_numMaterials = 0; 53 | if ( m_pMaterials != NULL ) 54 | { 55 | delete[] m_pMaterials; 56 | m_pMaterials = NULL; 57 | } 58 | m_numTriangles = 0; 59 | if ( m_pTriangles != NULL ) 60 | { 61 | delete[] m_pTriangles; 62 | m_pTriangles = NULL; 63 | } 64 | m_numVertices = 0; 65 | if ( m_pVertices != NULL ) 66 | { 67 | delete[] m_pVertices; 68 | m_pVertices = NULL; 69 | } 70 | } 71 | 72 | //有了数据,就可以写出绘制函数了,下面的函数根据模型的信息,按网格分组,分别绘制每一组的数据。 73 | void Model::draw() 74 | { 75 | GLboolean texEnabled = glIsEnabled( GL_TEXTURE_2D ); 76 | // 按网格分组绘制 77 | for ( int i = 0; i < m_numMeshes; i++ ) 78 | { 79 | int materialIndex = m_pMeshes[i].m_materialIndex; 80 | if ( materialIndex >= 0 ) 81 | { 82 | glMaterialfv( GL_FRONT, GL_AMBIENT, m_pMaterials[materialIndex].m_ambient ); 83 | glMaterialfv( GL_FRONT, GL_DIFFUSE, m_pMaterials[materialIndex].m_diffuse ); 84 | glMaterialfv( GL_FRONT, GL_SPECULAR, m_pMaterials[materialIndex].m_specular ); 85 | glMaterialfv( GL_FRONT, GL_EMISSION, m_pMaterials[materialIndex].m_emissive ); 86 | glMaterialf( GL_FRONT, GL_SHININESS, m_pMaterials[materialIndex].m_shininess ); 87 | if ( m_pMaterials[materialIndex].m_texture > 0 ) 88 | { 89 | glBindTexture( GL_TEXTURE_2D, m_pMaterials[materialIndex].m_texture ); 90 | glEnable( GL_TEXTURE_2D ); 91 | } 92 | else 93 | { 94 | glDisable( GL_TEXTURE_2D ); 95 | } 96 | } 97 | else 98 | { 99 | // Material properties? 100 | glDisable( GL_TEXTURE_2D ); 101 | } 102 | 103 | glBegin( GL_TRIANGLES ); 104 | { 105 | for ( int j = 0; j < m_pMeshes[i].m_numTriangles; j++ ) 106 | { 107 | int triangleIndex = m_pMeshes[i].m_pTriangleIndices[j]; 108 | const Triangle* pTri = &m_pTriangles[triangleIndex]; 109 | 110 | for ( int k = 0; k < 3; k++ ) 111 | { 112 | int index = pTri->m_vertexIndices[k]; 113 | glNormal3fv( pTri->m_vertexNormals[k] ); 114 | glTexCoord2f( pTri->m_s[k], pTri->m_t[k] ); 115 | glVertex3fv( m_pVertices[index].m_location ); 116 | } 117 | } 118 | } 119 | glEnd(); 120 | } 121 | 122 | if ( texEnabled ) 123 | { 124 | glEnable( GL_TEXTURE_2D ); 125 | } 126 | else 127 | { 128 | glDisable( GL_TEXTURE_2D ); 129 | } 130 | } 131 | 132 | void Model::reloadTextures() 133 | { 134 | //设置好了一切参数,但纹理还没有载入内存,下面的代码完成这个功能 135 | for ( int i = 0; i < m_numMaterials; i++ ) 136 | { 137 | if ( strlen( m_pMaterials[i].m_pTextureFilename ) > 0 ) 138 | { 139 | m_pMaterials[i].m_texture = LoadGLTexture( m_pMaterials[i].m_pTextureFilename ); 140 | } 141 | else 142 | { 143 | m_pMaterials[i].m_texture = 0; 144 | } 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /Lesson31_MilkshapeModels/model.h: -------------------------------------------------------------------------------- 1 | #ifndef MODEL_H 2 | #define MODEL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // 网格结构 9 | struct Mesh 10 | { 11 | int m_materialIndex; 12 | int m_numTriangles; 13 | int *m_pTriangleIndices; 14 | }; 15 | // 材质属性 16 | struct Material 17 | { 18 | float m_ambient[4], m_diffuse[4], m_specular[4], m_emissive[4]; 19 | float m_shininess; 20 | GLuint m_texture; 21 | char *m_pTextureFilename; 22 | }; 23 | // 三角形结构 24 | struct Triangle 25 | { 26 | float m_vertexNormals[3][3]; 27 | float m_s[3], m_t[3]; 28 | int m_vertexIndices[3]; 29 | }; 30 | // 顶点结构 31 | struct Vertex 32 | { 33 | char m_boneID; // 顶点所在的骨骼 34 | float m_location[3]; 35 | }; 36 | 37 | class Model 38 | { 39 | public: 40 | /* Constructor. */ 41 | Model(); 42 | /* Destructor. */ 43 | virtual ~Model(); 44 | /* 45 | Load the model data into the private variables. 46 | filename Model filename 47 | */ 48 | virtual bool loadModelData( char *filename ) = 0; 49 | /* 50 | Draw the model. 51 | */ 52 | void draw(); 53 | /* 54 | Called if OpenGL context was lost and we need to reload textures, display lists, etc. 55 | */ 56 | void reloadTextures(); 57 | protected: 58 | // 使用的网格 59 | int m_numMeshes; 60 | Mesh *m_pMeshes; 61 | 62 | // 使用的纹理 63 | int m_numMaterials; 64 | Material *m_pMaterials; 65 | 66 | // 使用的三角形 67 | int m_numTriangles; 68 | Triangle *m_pTriangles; 69 | 70 | // 顶点的个数和数据 71 | int m_numVertices; 72 | Vertex *m_pVertices; 73 | }; 74 | 75 | #endif // MODEL_H 76 | -------------------------------------------------------------------------------- /Lesson31_MilkshapeModels/myglwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include "milkshapemodel.h" 3 | 4 | //模型加载: 5 | //你知道大名鼎鼎的Milkshape3D建模软件么,我们将加载它的模型,当然你可以加载任何你认为不错的模型。 6 | //这篇渲染模型的文章是由Brett Porter所写的。 7 | //这篇教程的代码是从PortaLib3D中提取出来的,PortaLib3D是一个可以读取3D文件实用库。 8 | //这篇教程的代码是以第六课为基础的,我们只讨论改变的部分。 9 | //这课中使用的模型是从Milkshape3D中提取出来的,Milkshape3D是一个非常好的建模软件, 10 | //它包含了自己的文件格式,所以你能很容易去分析和理解。 11 | //但是文件格式并不能使你加载一个模型,你必须自己定义一个结构去保存数据, 12 | //接着把数据读入那个结构,我们将告诉你如何定义这样一个结构。 13 | //模型的定义在model.h中,好吧我们开始吧: 14 | 15 | MyGLWidget::MyGLWidget(QWidget *parent) : 16 | QGLWidget(parent), m_show_full_screen(false), m_pModel(NULL), m_yrot(0.0f) 17 | { 18 | showNormal(); 19 | startTimer(15); 20 | m_pModel = new MilkshapeModel(); 21 | QString filePath = QApplication::applicationDirPath() + "/Data/Model.ms3d"; 22 | m_pModel->loadModelData(filePath.toLatin1().data()); 23 | m_pModel->reloadTextures(); 24 | } 25 | 26 | MyGLWidget::~MyGLWidget() 27 | { 28 | delete m_pModel; 29 | } 30 | 31 | //下面的代码的作用是重新设置OpenGL场景的大小,而不管窗口的大小是否已经改变(假定您没有使用全屏模式)。 32 | //甚至您无法改变窗口的大小时(例如您在全屏模式下),它至少仍将运行一次--在程序开始时设置我们的透视图。 33 | //OpenGL场景的尺寸将被设置成它显示时所在窗口的大小。 34 | void MyGLWidget::resizeGL(int w, int h) 35 | { 36 | if (h==0) // Prevent A Divide By Zero By 37 | { 38 | h=1; // Making Height Equal One 39 | } 40 | 41 | glViewport(0,0,w,h); // Reset The Current Viewport 42 | 43 | glMatrixMode(GL_PROJECTION); // Select The Projection Matrix 44 | glLoadIdentity(); // Reset The Projection Matrix 45 | 46 | // Calculate The Aspect Ratio Of The Window 47 | gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,1.0f,1000.0f); // View Depth of 1000 48 | 49 | glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix 50 | glLoadIdentity(); // Reset The Modelview Matrix 51 | } 52 | 53 | //接下的代码段中,我们将对OpenGL进行所有的设置。我们将设置清除屏幕所用的颜色,打开深度缓存,启用smooth shading(阴影平滑),等等。 54 | //这个例程直到OpenGL窗口创建之后才会被调用。此过程将有返回值。但我们此处的初始化没那么复杂,现在还用不着担心这个返回值。 55 | void MyGLWidget::initializeGL() 56 | { 57 | if(m_pModel) 58 | { 59 | m_pModel->reloadTextures();// Loads Model Textures 60 | } 61 | glEnable(GL_TEXTURE_2D);// Enable Texture Mapping ( NEW ) 62 | glShadeModel(GL_SMOOTH);// Enable Smooth Shading 63 | glClearColor(0.0f, 0.0f, 0.0f, 0.5f);// Black Background 64 | glClearDepth(1.0f);// Depth Buffer Setup 65 | glEnable(GL_DEPTH_TEST);// Enables Depth Testing 66 | glDepthFunc(GL_LEQUAL);// The Type Of Depth Testing To Do 67 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculations 68 | } 69 | 70 | //完成了初始化操作,我们来实际绘制我们的模型 71 | void MyGLWidget::paintGL() 72 | { 73 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer 74 | glLoadIdentity();// Reset The Modelview Matrix 75 | gluLookAt( 75, 75, 75, 0, 0, 0, 0, 1, 0 );// (3) Eye Postion (3) Center Point (3) Y-Axis Up Vector 76 | glRotatef(m_yrot,0.0f,1.0f,0.0f);// Rotate On The Y-Axis By yrot 77 | m_pModel->draw();// Draw The Model 78 | } 79 | 80 | void MyGLWidget::keyPressEvent(QKeyEvent *event) 81 | { 82 | switch(event->key()) 83 | { 84 | case Qt::Key_F2: 85 | { 86 | m_show_full_screen = !m_show_full_screen; 87 | if(m_show_full_screen) 88 | { 89 | showFullScreen(); 90 | } 91 | else 92 | { 93 | showNormal(); 94 | } 95 | updateGL(); 96 | break; 97 | } 98 | case Qt::Key_Escape: 99 | { 100 | qApp->exit(); 101 | break; 102 | } 103 | } 104 | QGLWidget::keyPressEvent(event); 105 | } 106 | 107 | void MyGLWidget::timerEvent(QTimerEvent *event) 108 | { 109 | m_yrot+=1.0f;// Increase yrot By One 110 | updateGL(); 111 | QGLWidget::timerEvent(event); 112 | } 113 | 114 | //简单吧?下一步我们该做什么?在以后的教程中,我将会加入骨骼动画的知识,到时候见吧 115 | -------------------------------------------------------------------------------- /Lesson31_MilkshapeModels/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "model.h" 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit MyGLWidget(QWidget *parent = 0); 17 | ~MyGLWidget(); 18 | protected: 19 | void resizeGL(int w, int h); 20 | 21 | void initializeGL(); 22 | 23 | void paintGL(); 24 | 25 | void keyPressEvent(QKeyEvent *event); 26 | 27 | void timerEvent(QTimerEvent *event); 28 | private: 29 | bool m_show_full_screen; 30 | Model *m_pModel; // 定义一个指向模型类的指针 31 | GLfloat m_yrot; 32 | }; 33 | 34 | #endif // MYGLWIDGET_H 35 | -------------------------------------------------------------------------------- /Lesson32_PickingGame/Lesson32_PickingGame.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-21T14:36:42 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl multimedia 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson32_PickingGame 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/BlueFace.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/BlueFace.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/Bucket.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/Bucket.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/Coke.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/Coke.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/Crosshair.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/Crosshair.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/Explode.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/Explode.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/Font.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/Font.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/Ground.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/Ground.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/Sky.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/Sky.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/Target.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/Target.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/image/Vase.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/image/Vase.tga -------------------------------------------------------------------------------- /Lesson32_PickingGame/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson32_PickingGame/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | //现在为我们物体的结构. 这个结构存了所有一个物体的信息. 旋转的方向, 若被打中, 在屏幕的位置, 等等. 13 | //一个快速运动的变量... rot 我想让物体旋转特别的方向. hit 若物体没被打中将是 FALSE . 若物体给打中或飞出, 变量将是 TRUE. 14 | //变量frame 是用来存我们爆炸动画的周期. 每一帧改变增加一个爆炸材质. 在这课有更多在不久. 15 | //保存单个物体的移动方向, 我们用变量 dir. 一个dir 能有4 个值: 0 - 物体左移, 1 - 物体右移, 2 - 物体上移 和最后 3 - 物体下移 16 | //texid 能是从0到4的数. 0 表示是蓝面材质, 1 是水桶材质, 2 是靶子的材质 , 3 是 可乐的材质 和 4 是 花瓶 材质. 17 | //最近在调入材质的代码, 你将看到先前5种材质来自目标图片. 18 | //x 和 y 两者都用来记屏模上物体的位置. x 表示物体在 x-轴, y 表示物体在 y-轴. 19 | //物体在z-轴上的旋转是记在变量spin. 在以后的代码, 我们将加或减spin基数在旅行的方向上. 20 | //最后, distance 保存我们物体到屏幕的距离. 距离是极端重要的变量, 我们将用他来计算屏幕的左右两边, 而且在对象关闭之前排序物体, 21 | //画出物体的距离. 22 | 23 | struct objects 24 | { 25 | GLuint rot;// 旋转 (0-不转, 1-顺时针转, 2-逆时针转) 26 | bool hit;// 物体碰撞? 27 | GLuint frame;//当前爆炸效果的动画帧 28 | GLuint dir;// 物体的方向 (0-左, 1-右, 2-上, 3-下) 29 | GLuint texid;// 物体材质 ID 30 | GLfloat x;// 物体 X 位置 31 | GLfloat y;// 物体 Y 位置 32 | GLfloat spin;//物体旋转 33 | GLfloat distance;// 物体距离 34 | }; 35 | 36 | //我不想限制每个物体的大小. 我想瓶子(vase)比can高, 我想水桶bucket比瓶子宽. 去改变一切是简单的, 37 | //我建了一个结构存物体的宽和高. 38 | //我然后在最后一行代码中设每个物体的宽高. 得到这个coke cans的宽, 我将检查size[3].w. 蓝面是 0, 水桶是 1, 和靶子是 2, 39 | //等. 宽度表现在 w. 使有意义? 40 | struct dimensions // 物体维数 41 | { 42 | GLfloat w; // 物体宽 43 | GLfloat h; // 物体高 44 | }; 45 | 46 | typedef struct 47 | { 48 | QImage imageData;// 图片数据 49 | QImage::Format bpp;// 图片格式 50 | GLuint width;// 图片宽度 51 | GLuint height;// 图片高度 52 | GLuint texID;// 贴图材质 ID 用来选择一个材质 53 | } TextureImage;// 结构 名称 54 | 55 | class MyGLWidget : public QGLWidget 56 | { 57 | Q_OBJECT 58 | public: 59 | explicit MyGLWidget(QWidget *parent = 0); 60 | ~MyGLWidget(); 61 | protected: 62 | void resizeGL(int w, int h); 63 | 64 | void initializeGL(); 65 | 66 | void paintGL(); 67 | 68 | void keyPressEvent(QKeyEvent *event); 69 | 70 | void timerEvent(QTimerEvent *event); 71 | 72 | void mousePressEvent(QMouseEvent *event); 73 | 74 | void mouseMoveEvent(QMouseEvent *event); 75 | private: 76 | bool loadTGA(TextureImage *texture, const QString &filename); 77 | 78 | void buildFont(); 79 | 80 | void glPrint(GLint x, GLint y, const QString &string); 81 | 82 | void initObject(int num); 83 | 84 | void selection(); 85 | 86 | void object(float width,float height,GLuint texid); 87 | 88 | void explosion(int num); 89 | 90 | void drawTargets(); 91 | 92 | void update(quint32 milliseconds); 93 | private: 94 | bool m_show_full_screen; 95 | GLint m_level;// 现在的等级 96 | GLint m_miss;// 丢失的数 97 | GLint m_kills;// 打到的数 98 | GLint m_score;// 当前的分数 99 | bool m_game;// 游戏是否结束? 100 | 101 | TextureImage m_textures[10];// 定义10个材质 102 | objects m_object[30];// 定义 30 个物体 103 | GLuint m_base;// 字体显示列表 104 | GLfloat m_roll;// 旋转的云 105 | 106 | QPoint m_mousePoint; 107 | }; 108 | 109 | #endif // MYGLWIDGET_H 110 | -------------------------------------------------------------------------------- /Lesson32_PickingGame/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/BlueFace.tga 4 | image/Bucket.tga 5 | image/Coke.tga 6 | image/Crosshair.tga 7 | image/Explode.tga 8 | image/Font.tga 9 | image/Ground.tga 10 | image/Sky.tga 11 | image/Target.tga 12 | image/Vase.tga 13 | voice/Shot.wav 14 | 15 | 16 | -------------------------------------------------------------------------------- /Lesson32_PickingGame/voice/Shot.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson32_PickingGame/voice/Shot.wav -------------------------------------------------------------------------------- /Lesson33_LoadingTGAFiles/Lesson33_LoadingTGAFiles.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-24T14:53:46 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson33_LoadingTGAFiles 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp \ 17 | tgaloader.cpp 18 | 19 | HEADERS += myglwidget.h \ 20 | tgaloader.h 21 | 22 | CONFIG(debug, debug|release) { 23 | sourcefiles.files = $$PWD/image/* 24 | sourcefiles.path = $$OUT_PWD/debug/image/ 25 | } else { 26 | sourcefiles.files = $$PWD/Data/* 27 | sourcefiles.path = $$OUT_PWD/release/image/ 28 | } 29 | INSTALLS += sourcefiles 30 | -------------------------------------------------------------------------------- /Lesson33_LoadingTGAFiles/image/Compressed.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson33_LoadingTGAFiles/image/Compressed.tga -------------------------------------------------------------------------------- /Lesson33_LoadingTGAFiles/image/Uncompressed.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson33_LoadingTGAFiles/image/Uncompressed.tga -------------------------------------------------------------------------------- /Lesson33_LoadingTGAFiles/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson33_LoadingTGAFiles/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "tgaloader.h" 10 | 11 | bool LoadTGA(Texture * texture, const QString &filename); 12 | 13 | class MyGLWidget : public QGLWidget 14 | { 15 | Q_OBJECT 16 | 17 | public: 18 | explicit MyGLWidget(QWidget *parent = 0); 19 | ~MyGLWidget(); 20 | protected: 21 | void resizeGL(int w, int h); 22 | 23 | void initializeGL(); 24 | 25 | void paintGL(); 26 | 27 | void keyPressEvent(QKeyEvent *event); 28 | 29 | void timerEvent(QTimerEvent *event); 30 | private: 31 | int loadGLTextures(); 32 | private: 33 | bool m_show_full_screen; 34 | Texture m_textures[2]; 35 | GLfloat m_spin; 36 | }; 37 | 38 | #endif // MYGLWIDGET_H 39 | -------------------------------------------------------------------------------- /Lesson33_LoadingTGAFiles/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Compressed.tga 4 | image/Uncompressed.tga 5 | 6 | 7 | -------------------------------------------------------------------------------- /Lesson33_LoadingTGAFiles/texture.h: -------------------------------------------------------------------------------- 1 | #ifndef TEXTURE_H 2 | #define TEXTURE_H 3 | 4 | #include // Header for OpenGL32 library 5 | 6 | 7 | 8 | 9 | #endif // TEXTURE_H 10 | -------------------------------------------------------------------------------- /Lesson33_LoadingTGAFiles/tgaloader.h: -------------------------------------------------------------------------------- 1 | #ifndef TGALOADER_H 2 | #define TGALOADER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef struct 10 | { 11 | GLubyte *imageData;// 控制整个图像的颜色值 12 | GLuint bpp;// 控制单位像素的bit数 13 | GLuint width;// 整个图像的宽度 14 | GLuint height;// 整个图像的高度 15 | GLuint texID;// 使用glBindTexture所需的纹理ID. 16 | GLuint type;// 描述存储在*ImageData中的数据(GL_RGB Or GL_RGBA) 17 | } Texture; 18 | 19 | typedef struct 20 | { 21 | GLubyte Header[12];// 文件头决定文件类型 22 | } TGAHeader; 23 | 24 | typedef struct 25 | { 26 | GLubyte header[6];// 控制前6个字节 27 | GLuint bytesPerPixel;// 每像素的字节数 (3 或 4) 28 | GLuint imageSize;// 控制存储图像所需的内存空间 29 | GLuint type;// 图像类型 GL_RGB 或 GL_RGBA 30 | GLuint Height;// 图像的高度 31 | GLuint Width;// 图像宽度 32 | GLuint Bpp;// 每像素的比特数 (24 或 32) 33 | } TGA; 34 | 35 | bool LoadTGA(Texture * texture, const QString &filename); 36 | bool LoadUncompressedTGA(Texture *, QFile *); // Load an Uncompressed file 37 | bool LoadCompressedTGA(Texture *, QFile *); // Load a Compressed file 38 | 39 | #endif // TGALOADER_H 40 | -------------------------------------------------------------------------------- /Lesson34_HeightMapping/Lesson34_HeightMapping.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-26T10:39:31 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson34_HeightMapping 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson34_HeightMapping/data/Terrain.raw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson34_HeightMapping/data/Terrain.raw -------------------------------------------------------------------------------- /Lesson34_HeightMapping/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson34_HeightMapping/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define MAP_SIZE 1024 11 | #define STEP_SIZE 16 // 相邻顶点的距离 12 | #define HEIGHT_RATIO 1.5f 13 | 14 | class MyGLWidget : public QGLWidget 15 | { 16 | Q_OBJECT 17 | 18 | public: 19 | explicit MyGLWidget(QWidget *parent = 0); 20 | ~MyGLWidget(); 21 | protected: 22 | void resizeGL(int w, int h); 23 | 24 | void initializeGL(); 25 | 26 | void paintGL(); 27 | 28 | void keyPressEvent(QKeyEvent *event); 29 | 30 | void timerEvent(QTimerEvent *event); 31 | 32 | void mousePressEvent(QMouseEvent *event); 33 | private: 34 | void loadRawFile(const QString &fileName, int nSize, unsigned char *pHeightMap); 35 | 36 | int height(unsigned char *pHeightMap, int x, int y); 37 | 38 | void setVertexColor(unsigned char *pHeightMap, int x, int y); 39 | 40 | void renderHeightMap(unsigned char *pHeightMap); 41 | private: 42 | bool m_show_full_screen; 43 | unsigned char m_gHeightMap[MAP_SIZE*MAP_SIZE];// 保存高度数据 44 | bool m_bRender;//true为多边形渲染,false为线渲染 45 | GLfloat m_scaleValue;// 地形的缩放比例 46 | }; 47 | 48 | #endif // MYGLWIDGET_H 49 | -------------------------------------------------------------------------------- /Lesson34_HeightMapping/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | data/Terrain.raw 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson35_PlayAnAVI/Lesson35_PlayAnAVI.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-05-14T14:40:05 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui multimedia opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson35_PlayAnAVI 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp \ 17 | videowidgetsurface.cpp 18 | 19 | HEADERS += myglwidget.h \ 20 | videowidgetsurface.h 21 | 22 | avi.files += avi/* 23 | avi.path = $$OUT_PWD/debug 24 | INSTALLS += avi 25 | -------------------------------------------------------------------------------- /Lesson35_PlayAnAVI/avi/Face3.avi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson35_PlayAnAVI/avi/Face3.avi -------------------------------------------------------------------------------- /Lesson35_PlayAnAVI/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson35_PlayAnAVI/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "videowidgetsurface.h" 12 | 13 | class MyGLWidget : public QGLWidget 14 | { 15 | Q_OBJECT 16 | 17 | public: 18 | explicit MyGLWidget(QWidget *parent = 0); 19 | ~MyGLWidget(); 20 | protected: 21 | void resizeGL(int w, int h); 22 | 23 | void initializeGL(); 24 | 25 | void paintGL(); 26 | 27 | void keyPressEvent(QKeyEvent *event); 28 | 29 | void timerEvent(QTimerEvent *event); 30 | private slots: 31 | void newAVIImage(QImage image); 32 | private: 33 | bool m_show_full_screen; 34 | float m_angle;// 旋转用 35 | int m_effect;// 当前效果 36 | bool m_env;// 环境映射(默认开) 37 | bool m_bg;// 背景(默认开) 38 | GLUquadricObj *m_quadratic;// 存储二次曲面对象 39 | QMediaPlayer *m_player; 40 | VideoWidgetSurface *m_surface; 41 | QImage m_oriImage; 42 | }; 43 | 44 | #endif // MYGLWIDGET_H 45 | -------------------------------------------------------------------------------- /Lesson35_PlayAnAVI/videowidgetsurface.cpp: -------------------------------------------------------------------------------- 1 | #include "videowidgetsurface.h" 2 | #include 3 | #include 4 | #include 5 | 6 | VideoWidgetSurface::VideoWidgetSurface(QObject *parent) 7 | : QAbstractVideoSurface(parent), 8 | imageFormat(QImage::Format_Invalid), 9 | m_counter(0) 10 | { 11 | } 12 | 13 | QList VideoWidgetSurface::supportedPixelFormats( 14 | QAbstractVideoBuffer::HandleType handleType) const 15 | { 16 | if (handleType == QAbstractVideoBuffer::NoHandle) 17 | { 18 | return QList() 19 | << QVideoFrame::Format_RGB32 20 | << QVideoFrame::Format_ARGB32 21 | << QVideoFrame::Format_ARGB32_Premultiplied 22 | << QVideoFrame::Format_RGB565 23 | << QVideoFrame::Format_RGB555; 24 | } 25 | else 26 | { 27 | return QList(); 28 | } 29 | } 30 | 31 | bool VideoWidgetSurface::isFormatSupported(const QVideoSurfaceFormat &format) const 32 | { 33 | const QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat()); 34 | const QSize size = format.frameSize(); 35 | 36 | return imageFormat != QImage::Format_Invalid 37 | && !size.isEmpty() 38 | && format.handleType() == QAbstractVideoBuffer::NoHandle; 39 | } 40 | 41 | bool VideoWidgetSurface::start(const QVideoSurfaceFormat &format) 42 | { 43 | const QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat()); 44 | const QSize size = format.frameSize(); 45 | 46 | if (imageFormat != QImage::Format_Invalid && !size.isEmpty()) 47 | { 48 | this->imageFormat = imageFormat; 49 | imageSize = size; 50 | QAbstractVideoSurface::start(format); 51 | return true; 52 | } 53 | else 54 | { 55 | return false; 56 | } 57 | } 58 | 59 | void VideoWidgetSurface::stop() 60 | { 61 | currentFrame = QVideoFrame(); 62 | QAbstractVideoSurface::stop(); 63 | } 64 | 65 | bool VideoWidgetSurface::present(const QVideoFrame &frame) 66 | { 67 | if (surfaceFormat().pixelFormat() != frame.pixelFormat() 68 | || surfaceFormat().frameSize() != frame.size()) 69 | { 70 | setError(IncorrectFormatError); 71 | stop(); 72 | 73 | return false; 74 | } 75 | else 76 | { 77 | currentFrame = frame; 78 | currentFrame.map(QAbstractVideoBuffer::ReadOnly); 79 | QImage image(currentFrame.bits(), 80 | currentFrame.width(), 81 | currentFrame.height(), 82 | imageFormat); 83 | emit aviImage(image); 84 | currentFrame.unmap(); 85 | return true; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Lesson35_PlayAnAVI/videowidgetsurface.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEOWIDGETSURFACE_H 2 | #define VIDEOWIDGETSURFACE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class VideoWidgetSurface : public QAbstractVideoSurface 10 | { 11 | Q_OBJECT 12 | 13 | public: 14 | VideoWidgetSurface(QObject *parent = 0); 15 | 16 | QList supportedPixelFormats( 17 | QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const; 18 | bool isFormatSupported(const QVideoSurfaceFormat &format) const; 19 | 20 | bool start(const QVideoSurfaceFormat &format); 21 | void stop(); 22 | 23 | bool present(const QVideoFrame &frame); 24 | signals: 25 | void aviImage(QImage image); 26 | private: 27 | QImage::Format imageFormat; 28 | QSize imageSize; 29 | QVideoFrame currentFrame; 30 | int m_counter; 31 | }; 32 | #endif 33 | -------------------------------------------------------------------------------- /Lesson36_RenderingToATexture/Lesson36_RenderingToATexture.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-03-05T20:32:35 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson36_RenderingToATexture 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | -------------------------------------------------------------------------------- /Lesson36_RenderingToATexture/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson36_RenderingToATexture/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit MyGLWidget(QWidget *parent = 0); 17 | ~MyGLWidget(); 18 | protected: 19 | void resizeGL(int w, int h); 20 | 21 | void initializeGL(); 22 | 23 | void paintGL(); 24 | 25 | void keyPressEvent(QKeyEvent *event); 26 | 27 | void timerEvent(QTimerEvent *event); 28 | private: 29 | GLuint emptyTexture(); 30 | 31 | void reduceToUnit(float vector[3]); 32 | 33 | void calcNormal(float v[3][3], float out[3]); 34 | 35 | void viewOrtho(); 36 | 37 | void viewPerspective(); 38 | 39 | void renderToTexture(); 40 | 41 | void processHelix(); 42 | 43 | void drawBlur(int times, float inc); 44 | private: 45 | float m_angle;// 用来旋转那个螺旋 46 | float m_vertexes[4][3];// 为3个设置的顶点保存浮点信息 47 | float m_normal[3];// 存放法线数据的数组 48 | GLuint m_blurTexture;// 存放纹理编号的一个无符号整型 49 | }; 50 | 51 | #endif // MYGLWIDGET_H 52 | -------------------------------------------------------------------------------- /Lesson37_CelShading/Data/Model.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson37_CelShading/Data/Model.txt -------------------------------------------------------------------------------- /Lesson37_CelShading/Data/Shader.txt: -------------------------------------------------------------------------------- 1 | 0.2 2 | 0.2 3 | 0.2 4 | 0.2 5 | 0.5 6 | 0.5 7 | 0.5 8 | 0.5 9 | 0.5 10 | 0.5 11 | 0.5 12 | 0.5 13 | 0.5 14 | 0.5 15 | 1.0 16 | 1.0 17 | 1.0 18 | 1.0 19 | 1.0 20 | 1.0 21 | 1.0 22 | 1.0 23 | 1.0 24 | 1.0 25 | 1.0 26 | 1.0 27 | 1.0 28 | 1.0 29 | 1.0 30 | 1.0 31 | 1.0 32 | 1.0 -------------------------------------------------------------------------------- /Lesson37_CelShading/Lesson37_CelShading.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-03-05T21:57:55 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson37_CelShading 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson37_CelShading/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson37_CelShading/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | //现在我们将定义一些结构体来帮助我们存贮我们的数据(保存好几百浮点数组)。第一个是tagMATRIX结构体。如果你仔细地看, 12 | //你将看到我们正象包含一个十六个浮点数的1维数组~一个2维4×4数族一样存储那个矩阵。 13 | //这下至OpenGL存储它的矩阵的方式。如果我们使用4x4数组,这些值将发生错误的顺序。 14 | 15 | // 保存OpenGL矩阵的结构体 16 | typedef struct tagMATRIX 17 | { 18 | float Data[16];// 由于OpenGL的矩阵的格式我们使用[16 19 | }MATRIX; 20 | 21 | //第二是向量的类。 仅存储X,Y和Z的值 22 | // 存储一个单精度向量的结构体 23 | typedef struct tagVECTOR 24 | { 25 | float X, Y, Z;// 向量的分量 26 | }VECTOR; 27 | 28 | //第三,我们持有顶点的结构。每一个顶点仅需要它的法线和位置(没有纹理的现行纵坐标)信息。它们必须以这样的次序被存放, 29 | //否则当它停止装载文件的事件将发生严重的错误(我发现艰难的情形:(教我分块出租我的代码。)。 30 | // 存放单一顶点的结构 31 | typedef struct tagVERTEX 32 | { 33 | VECTOR Nor;// 顶点法线 34 | VECTOR Pos;// 顶点位置 35 | }VERTEX; 36 | 37 | //最后是多边形的结构。我知道这是存储顶点的愚蠢的方法,要不是它完美工作的简单的缘故。 38 | //通常我愿意使用一个顶点数组,一个多边形数组,和包括一个在多边形中的3个顶点的指数,但这比较容易显示你想干什么。 39 | // 存储单一多边形的结构 40 | typedef struct tagPOLYGON 41 | { 42 | VERTEX Verts[3]; // 3个顶点结构数组 43 | }POLYGON; 44 | 45 | class MyGLWidget : public QGLWidget 46 | { 47 | Q_OBJECT 48 | 49 | public: 50 | explicit MyGLWidget(QWidget *parent = 0); 51 | ~MyGLWidget(); 52 | protected: 53 | void resizeGL(int w, int h); 54 | 55 | void initializeGL(); 56 | 57 | void paintGL(); 58 | 59 | void keyPressEvent(QKeyEvent *event); 60 | 61 | void timerEvent(QTimerEvent *event); 62 | private: 63 | bool readMesh(); 64 | private: 65 | bool m_show_full_screen; 66 | 67 | //优美简单的材料也在这里了。为每一个变量的一个解释考虑那个注释。 68 | bool m_outlineDraw;// 绘制轮廓的标记 69 | bool m_outlineSmooth;// Anti-Alias 线段的标记 70 | float m_outlineColor[3];// 线段的颜色 71 | float m_outlineWidth;// 线段的宽度 72 | VECTOR m_lightAngle;// 灯光的方向 73 | bool m_lightRotate;// 是否我们旋转灯光的标记 74 | float m_modelAngle;// 模型的Y轴角度 75 | bool m_modelRotate;// 旋转模型的标记 76 | POLYGON *m_polyData;// 多边形数据 77 | int m_polyNum;// 多边形的编号 78 | GLuint m_shaderTexture[1];// 存储纹理ID 79 | }; 80 | 81 | #endif // MYGLWIDGET_H 82 | -------------------------------------------------------------------------------- /Lesson37_CelShading/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | Data/Model.txt 4 | Data/Shader.txt 5 | 6 | 7 | -------------------------------------------------------------------------------- /Lesson38_LoadingBitmapsFromTheResource/Data/Butterfly1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson38_LoadingBitmapsFromTheResource/Data/Butterfly1.bmp -------------------------------------------------------------------------------- /Lesson38_LoadingBitmapsFromTheResource/Data/Butterfly2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson38_LoadingBitmapsFromTheResource/Data/Butterfly2.bmp -------------------------------------------------------------------------------- /Lesson38_LoadingBitmapsFromTheResource/Data/Butterfly3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson38_LoadingBitmapsFromTheResource/Data/Butterfly3.bmp -------------------------------------------------------------------------------- /Lesson38_LoadingBitmapsFromTheResource/Lesson38_LoadingBitmapsFromTheResource.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-03-05T23:21:09 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson38_LoadingBitmapsFromTheResource 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson38_LoadingBitmapsFromTheResource/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson38_LoadingBitmapsFromTheResource/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | // 定义一个物体 11 | struct object 12 | { 13 | int tex;// 纹理值 14 | float x;// 位置 15 | float y; 16 | float z; 17 | float yi;// 速度 18 | float spinz;// 沿Z轴旋转的角度和速度 19 | float spinzi; 20 | float flap; // 是否翻转三角形 21 | float fi; 22 | object() 23 | { 24 | tex=qrand()%3; // 纹理 25 | x=qrand()%34-17.0f;// 位置 26 | y=18.0f; 27 | z=-((qrand()%30000/1000.0f)+10.0f); 28 | spinzi=(qrand()%10000)/5000.0f-1.0f;// 旋转 29 | flap=0.0f; 30 | fi=0.05f+(qrand()%100)/1000.0f; 31 | yi=0.001f+(qrand()%1000)/10000.0f; 32 | } 33 | }; 34 | 35 | 36 | class MyGLWidget : public QGLWidget 37 | { 38 | Q_OBJECT 39 | 40 | public: 41 | explicit MyGLWidget(QWidget *parent = 0); 42 | ~MyGLWidget(); 43 | protected: 44 | void resizeGL(int w, int h); 45 | 46 | void initializeGL(); 47 | 48 | void paintGL(); 49 | 50 | void keyPressEvent(QKeyEvent *event); 51 | 52 | void timerEvent(QTimerEvent *event); 53 | private: 54 | void loadGLTexture(); 55 | private: 56 | bool m_show_full_screen; 57 | GLuint m_textures[3];// 保存三个纹理 58 | object m_obj[50];// 创建50个物体 59 | }; 60 | 61 | #endif // MYGLWIDGET_H 62 | -------------------------------------------------------------------------------- /Lesson38_LoadingBitmapsFromTheResource/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | Data/Butterfly1.bmp 4 | Data/Butterfly2.bmp 5 | Data/Butterfly3.bmp 6 | 7 | 8 | -------------------------------------------------------------------------------- /Lesson39_SimplePhysicalSimulationEngine/Lesson39_SimplePhysicalSimulationEngine.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-03-23T23:15:38 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson39_SimplePhysicalSimulationEngine 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson39_SimplePhysicalSimulationEngine/image/Font.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson39_SimplePhysicalSimulationEngine/image/Font.bmp -------------------------------------------------------------------------------- /Lesson39_SimplePhysicalSimulationEngine/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson39_SimplePhysicalSimulationEngine/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Font.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson40_RopePhysics/Lesson40_RopePhysics.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-04-13T20:26:07 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson40_RopePhysics 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | -------------------------------------------------------------------------------- /Lesson40_RopePhysics/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson41_VolumetricFog/Lesson41_VolumetricFog.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-04-13T21:19:38 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson41_VolumetricFog 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson41_VolumetricFog/image/Wall.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson41_VolumetricFog/image/Wall.bmp -------------------------------------------------------------------------------- /Lesson41_VolumetricFog/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson41_VolumetricFog/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MyGLWidget : public QGLWidget 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MyGLWidget(QWidget *parent = 0); 16 | ~MyGLWidget(); 17 | protected: 18 | void resizeGL(int w, int h); 19 | 20 | void initializeGL(); 21 | 22 | void paintGL(); 23 | 24 | void keyPressEvent(QKeyEvent *event); 25 | private: 26 | int extension_Init(); 27 | 28 | void loadGLTexture(); 29 | private: 30 | bool m_show_full_screen; 31 | GLfloat m_camz;// 摄像机在Z方向的深度 32 | GLuint m_texture[1];// 纹理 33 | }; 34 | 35 | #endif // MYGLWIDGET_H 36 | -------------------------------------------------------------------------------- /Lesson41_VolumetricFog/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Wall.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson42_MultipleViewports/Lesson42_MultipleViewports.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-04-13T21:46:50 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson42_MultipleViewports 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | -------------------------------------------------------------------------------- /Lesson42_MultipleViewports/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson42_MultipleViewports/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit MyGLWidget(QWidget *parent = 0); 17 | ~MyGLWidget(); 18 | protected: 19 | void resizeGL(int w, int h); 20 | 21 | void initializeGL(); 22 | 23 | void paintGL(); 24 | 25 | void keyPressEvent(QKeyEvent *event); 26 | 27 | void timerEvent(QTimerEvent *event); 28 | private: 29 | void updateTex(int dmx, int dmy); 30 | 31 | void reset(); 32 | private: 33 | bool m_show_full_screen; 34 | //然后我们声明一些我们打算在整个程序中都要用到的全局变量。 35 | //mx和my纪录了当前所在迷宫中的房间。每个房间都被墙隔开(因此房间都是2个单元大小的部分)。 36 | //with和height是用来建立纹理需要的。它也是迷宫的宽和高。让迷宫和贴图的大小一致的原因是使迷宫中的象素和纹理中的象素一一对应。 37 | //我倾向于把宽和高都设成256,尽管这要花更长的时间来建立迷宫。 38 | //如果你的显卡能支持处理大型贴图。可以试着以2次幂增加这个值(256, 512, 1023)。确保这个值不至于太大。 39 | //如果这个主窗口的宽度有1024个象素,并且每个视口的大小都是主窗口的一半,相应的你应该设置你的贴图宽度也是窗口宽度的一半。 40 | //如果你使贴图宽度为1024象素,但你的视口大小只有512,空间不足于容纳贴图中所有得象素,这样每两个象素就会重叠在一起。贴图的高度也作同样处理:高度是窗口高度的1/2. 41 | //当然你还必须四舍五入到2的幂。 42 | int m_mx; 43 | int m_my; 44 | //r[4]保存了4个随机的红色分量值,g[4]保存了4个随机的绿色分量值,b[4]保存了4个随机的兰色分量值。 45 | //这些值赋给各个视口不同的颜色。第一个视口颜色为r[0],g[0],b[0]。 46 | //请注意每一个颜色都是一个字节的值,而不是常用的浮点值。我这里用字节是因为产生0-255的随机值比产生0.0f-1.0f的浮点值更容易。 47 | //tex_data指向我们的贴图数据。 48 | unsigned char m_r[4]; 49 | unsigned char m_g[4]; 50 | unsigned char m_b[4];// 随机的颜色 51 | unsigned char *m_tex_data;//保存纹理数据 52 | //xrot,yrot和zrot是旋转3d物体用到的变量。 53 | //最后,我们声明一个二次曲面物体,这样我们可以用gluCylinder和gluSphere来画圆柱和球体,这比手工绘制这些物体容易多了。 54 | GLfloat m_xrot; 55 | GLfloat m_yrot; 56 | GLfloat m_zrot;// 旋转物体 57 | GLUquadricObj *m_quadric;// 二次几何体对象 58 | }; 59 | 60 | #endif // MYGLWIDGET_H 61 | -------------------------------------------------------------------------------- /Lesson43_TrueTypeFonts/Lesson43_TrueTypeFonts.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-04-15T22:56:37 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson43_TrueTypeFonts 12 | TEMPLATE = app 13 | 14 | LIBS += libfreetype libz 15 | 16 | #INCLUDEPATH += freetype2 17 | 18 | SOURCES += main.cpp\ 19 | myglwidget.cpp 20 | 21 | HEADERS += myglwidget.h 22 | 23 | fonts.files += fonts/* 24 | fonts.path = $$OUT_PWD/debug 25 | INSTALLS += fonts 26 | -------------------------------------------------------------------------------- /Lesson43_TrueTypeFonts/fonts/Test.TTF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson43_TrueTypeFonts/fonts/Test.TTF -------------------------------------------------------------------------------- /Lesson43_TrueTypeFonts/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson43_TrueTypeFonts/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | //#include 15 | //#include 16 | 17 | //我们将把每个字符需要的信息封装在一个结构中,这样就像使用WGL字体一样,我们可以分别控制每个字符的显示状态。 18 | namespace freetype 19 | { 20 | struct font_data//这个结构保存字体信息 21 | { 22 | float h; // 字体的高度 23 | GLuint *textures; // 使用的纹理 24 | GLuint list_base; // 显示列表的值 25 | 26 | // 初始化结构 27 | void init(const char *fname, unsigned int h); 28 | // 清楚所有的资源 29 | void clean(); 30 | }; 31 | //最后一件事是定义我们输出字符串的原形: 32 | // 把字符输出到屏幕 33 | void print(const font_data &ft_font, float x, float y, const char *fmt, ...); 34 | } 35 | 36 | class MyGLWidget : public QGLWidget 37 | { 38 | Q_OBJECT 39 | 40 | public: 41 | explicit MyGLWidget(QWidget *parent = 0); 42 | ~MyGLWidget(); 43 | protected: 44 | void resizeGL(int w, int h); 45 | 46 | void initializeGL(); 47 | 48 | void paintGL(); 49 | 50 | void keyPressEvent(QKeyEvent *event); 51 | 52 | void timerEvent(QTimerEvent *event); 53 | private: 54 | bool m_show_full_screen; 55 | freetype::font_data m_our_font; 56 | GLfloat m_cnt1; // 1st Counter Used To Move Text & For Coloring 57 | }; 58 | 59 | #endif // MYGLWIDGET_H 60 | -------------------------------------------------------------------------------- /Lesson44_LensFlare/Lesson44_LensFlare.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-04-24T21:39:09 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson44_LensFlare 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson44_LensFlare/image/BigGlow3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson44_LensFlare/image/BigGlow3.bmp -------------------------------------------------------------------------------- /Lesson44_LensFlare/image/Font.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson44_LensFlare/image/Font.bmp -------------------------------------------------------------------------------- /Lesson44_LensFlare/image/Halo3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson44_LensFlare/image/Halo3.bmp -------------------------------------------------------------------------------- /Lesson44_LensFlare/image/HardGlow2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson44_LensFlare/image/HardGlow2.bmp -------------------------------------------------------------------------------- /Lesson44_LensFlare/image/Streaks4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson44_LensFlare/image/Streaks4.bmp -------------------------------------------------------------------------------- /Lesson44_LensFlare/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson44_LensFlare/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class glVector 12 | { 13 | public: 14 | void operator =(glVector v); 15 | glVector operator+(glVector v); 16 | glVector operator*(GLfloat scalar); 17 | void Normalize(void); 18 | GLfloat Magnitude(void); 19 | GLfloat m_Mag; 20 | void operator *=(GLfloat scalar); 21 | glVector(); 22 | virtual ~glVector(); 23 | 24 | GLfloat k; 25 | GLfloat j; 26 | GLfloat i; 27 | }; 28 | 29 | class glPoint 30 | { 31 | public: 32 | void operator+=(glPoint p); 33 | glPoint operator+(glPoint p); 34 | glVector operator-(glPoint p); 35 | void operator =(glVector v); 36 | void operator =(glPoint p); 37 | glPoint(); 38 | virtual ~glPoint(); 39 | 40 | GLfloat z; 41 | GLfloat y; 42 | GLfloat x; 43 | }; 44 | 45 | class glCamera 46 | { 47 | public: 48 | //////////// CONSTRUCTORS ///////////////////////////////////////// 49 | glCamera(); 50 | virtual ~glCamera(); 51 | 52 | //////////// FRUSTUM TESTING FUNCTIONS //////////////////////////// 53 | BOOL SphereInFrustum(glPoint p, GLfloat Radius); 54 | BOOL SphereInFrustum(GLfloat x, GLfloat y, GLfloat z, GLfloat Radius); 55 | BOOL PointInFrustum(GLfloat x, GLfloat y, GLfloat z); 56 | BOOL PointInFrustum(glPoint p); 57 | 58 | //////////// FUNCTIONS TO RENDER LENS FLARES ////////////////////// 59 | void RenderLensFlare(void); 60 | void RenderStreaks(GLfloat r, GLfloat g, GLfloat b, GLfloat a, glPoint p, GLfloat scale); 61 | void RenderBigGlow(GLfloat r, GLfloat g, GLfloat b, GLfloat a, glPoint p, GLfloat scale); 62 | void RenderGlow(GLfloat r, GLfloat g, GLfloat b, GLfloat a, glPoint p, GLfloat scale); 63 | void RenderHalo(GLfloat r, GLfloat g, GLfloat b, GLfloat a, glPoint p, GLfloat scale); 64 | 65 | //////////// FUNCTIONS TO UPDATE THE FRUSTUM ////////////////////// 66 | void UpdateFrustumFaster(void); 67 | void UpdateFrustum(void); 68 | 69 | //////////// FUNCTIONS TO CHANGE CAMERA ORIENTATION AND SPEED ///// 70 | void ChangeVelocity(GLfloat vel); 71 | void ChangeHeading(GLfloat degrees); 72 | void ChangePitch(GLfloat degrees); 73 | void SetPrespective(void); 74 | 75 | //############################### NEW STUFF ########################## 76 | /////////// OCCLUSION TESTING FUNCTIONS /////////////////////////// 77 | bool IsOccluded(glPoint p); 78 | 79 | //////////// MEMBER VARIBLES ////////////////////////////////////// 80 | glVector vLightSourceToCamera, vLightSourceToIntersect; 81 | glPoint ptIntersect, pt; 82 | GLsizei m_WindowHeight; 83 | GLsizei m_WindowWidth; 84 | GLuint m_StreakTexture; 85 | GLuint m_HaloTexture; 86 | GLuint m_GlowTexture; 87 | GLuint m_BigGlowTexture; 88 | GLfloat m_MaxPointSize; 89 | GLfloat m_Frustum[6][4]; 90 | glPoint m_LightSourcePos; 91 | GLfloat m_MaxPitchRate; 92 | GLfloat m_MaxHeadingRate; 93 | GLfloat m_HeadingDegrees; 94 | GLfloat m_PitchDegrees; 95 | GLfloat m_MaxForwardVelocity; 96 | GLfloat m_ForwardVelocity; 97 | glPoint m_Position; 98 | glVector m_DirectionVector; 99 | }; 100 | 101 | class glFont 102 | { 103 | public: 104 | GLuint GetListBase(void); 105 | GLuint GetTexture(void); 106 | void SetWindowSize(GLint width, GLint height); 107 | void glPrintf(GLint x, GLint y, GLint set, const char *Format, ...); 108 | void BuildFont(GLfloat Scale=1.0f); 109 | void SetFontTexture(GLuint tex); 110 | glFont(); 111 | virtual ~glFont(); 112 | 113 | protected: 114 | GLdouble m_WindowWidth; 115 | GLdouble m_WindowHeight; 116 | GLuint m_ListBase; 117 | GLuint m_FontTexture; 118 | }; 119 | 120 | class MyGLWidget : public QGLWidget 121 | { 122 | Q_OBJECT 123 | 124 | public: 125 | explicit MyGLWidget(QWidget *parent = 0); 126 | ~MyGLWidget(); 127 | protected: 128 | void resizeGL(int w, int h); 129 | 130 | void initializeGL(); 131 | 132 | void paintGL(); 133 | 134 | void keyPressEvent(QKeyEvent *event); 135 | 136 | void timerEvent(QTimerEvent *event); 137 | private: 138 | void loadTexture(const QString &filePath, GLuint &texId); 139 | 140 | void drawGLInfo(); 141 | private: 142 | bool m_show_full_screen; 143 | 144 | glFont m_gFont; 145 | glCamera m_gCamera; 146 | GLint m_cylList; 147 | qlonglong m_gStartTime; 148 | qlonglong m_gCurrentTime; 149 | qlonglong m_timeCounter; 150 | GLUquadricObj *m_qobj; 151 | int m_gFrames; 152 | double m_gFPS; 153 | bool m_infoOn; 154 | }; 155 | 156 | #endif // MYGLWIDGET_H 157 | -------------------------------------------------------------------------------- /Lesson44_LensFlare/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/BigGlow3.bmp 4 | image/Font.bmp 5 | image/Halo3.bmp 6 | image/HardGlow2.bmp 7 | image/Streaks4.bmp 8 | 9 | 10 | -------------------------------------------------------------------------------- /Lesson45_VertexBuffer/Lesson45_VertexBuffer.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-04-28T23:47:53 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson45_VertexBuffer 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | RESOURCES += \ 21 | src.qrc 22 | -------------------------------------------------------------------------------- /Lesson45_VertexBuffer/image/Terrain.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwc1987/NeHe_OpenGL_Qt4/ee3f46bd14f6d1a2cfb6d1c5d5fe365a67ac7c88/Lesson45_VertexBuffer/image/Terrain.bmp -------------------------------------------------------------------------------- /Lesson45_VertexBuffer/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Lesson45_VertexBuffer/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | //现在我们来定义自己的网格类: 13 | class CVert // 顶点类 14 | { 15 | public: 16 | float x; 17 | float y; 18 | float z; 19 | }; 20 | typedef CVert CVec; 21 | 22 | class CTexCoord // 纹理坐标类 23 | { 24 | public: 25 | float u; 26 | float v; 27 | }; 28 | 29 | //网格类 30 | 31 | class CMesh 32 | { 33 | public: 34 | // 网格数据 35 | int m_nVertexCount; // 顶点个数 36 | CVert* m_pVertices; // 顶点数据的指针 37 | CTexCoord* m_pTexCoords; // 顶点的纹理坐标 38 | unsigned int m_nTextureId; // 纹理的ID 39 | unsigned int m_nVBOVertices; // 顶点缓存对象的名称 40 | unsigned int m_nVBOTexCoords; // 顶点纹理缓存对象的名称 41 | QImage m_pTextureImage; // 高度数据 42 | public: 43 | CMesh(); // 构造函数 44 | ~CMesh(); // 析构函数 45 | // 载入高度图 46 | bool loadHeightmap(float flHeightScale, float flResolution ); 47 | // 返回单个点的高度 48 | float ptHeight( int nX, int nY ); 49 | // 创建顶点缓存对象 50 | void buildVBOs(); 51 | }; 52 | 53 | 54 | class MyGLWidget : public QGLWidget 55 | { 56 | Q_OBJECT 57 | 58 | public: 59 | explicit MyGLWidget(QWidget *parent = 0); 60 | ~MyGLWidget(); 61 | protected: 62 | void resizeGL(int w, int h); 63 | 64 | void initializeGL(); 65 | 66 | void paintGL(); 67 | 68 | void keyPressEvent(QKeyEvent *event); 69 | 70 | void timerEvent(QTimerEvent *event); 71 | private: 72 | bool m_show_full_screen; 73 | CMesh* m_pMesh; // 网格数据 74 | float m_flYRot; // 旋转角度 75 | int m_nFPS; 76 | int m_nFrames; // 帧率计数器 77 | long m_dwLastFPS; // 上一帧的计数 78 | qlonglong m_timerCounter; 79 | 80 | }; 81 | 82 | #endif // MYGLWIDGET_H 83 | -------------------------------------------------------------------------------- /Lesson45_VertexBuffer/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/Terrain.bmp 4 | 5 | 6 | -------------------------------------------------------------------------------- /Lesson46_FSAA/Lesson46_FSAA.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-04-30T11:53:13 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson46_FSAA 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | -------------------------------------------------------------------------------- /Lesson46_FSAA/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | QGLFormat format = QGLFormat::defaultFormat(); 8 | format.setSampleBuffers(true); 9 | MyGLWidget *w = new MyGLWidget(format, NULL); 10 | w->show(); 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /Lesson46_FSAA/myglwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | 3 | //全屏反走样 4 | //当今显卡的强大功能,你几乎什么都不用做,只需要在创建窗口的时候该一个数据。看看吧,驱动程序为你做完了一切。 5 | 6 | //在图形的绘制中,直线的走样是非常影响美观的,我们可以使用反走样解决这个问题。 7 | //在众多的解决方案里,多重采样是一种易于硬件实现的方法,也是一种快速的方法。 8 | //全凭多重采样可以使你的图形看起来更美观,我们可以使用ARB_MULTISAMPLE扩展完成这个功能,但它会降低你的程序的速度。 9 | 10 | //如果你想知道更多的关于多重采样的信息,请访问下面的链接: 11 | //GDC2002 -- OpenGL Multisample 12 | //OpenGL Pixel Formats and Multisample Antialiasing 13 | //下面我们来介绍如何使用多重采样,不向其他的扩展,我们在使用多重采样时,必须在窗口创建时告诉它使用多重采样, 14 | //典型的步骤如下: 15 | //1、创建一个窗口 16 | //2、查询是否支持多重采样 17 | //3、如果支持删除当前的窗口,使用支持多重采样的格式创建窗口 18 | //4、如果我们想使用多重采样,仅仅启用它既可。 19 | //了解了上面,我们从头说明如何使用多重采样,并介绍ARB_Multisample的实现方法: 20 | 21 | MyGLWidget::MyGLWidget(QGLFormat format, QWidget *parent) : 22 | QGLWidget(format, parent), m_show_full_screen(false), m_angle(0.0f), 23 | m_domulti(true), m_doangle(true) 24 | { 25 | showNormal(); 26 | startTimer(15); 27 | } 28 | 29 | MyGLWidget::MyGLWidget(QWidget *parent) : 30 | QGLWidget(parent), m_show_full_screen(false), m_angle(0.0f), 31 | m_domulti(true), m_doangle(true) 32 | { 33 | showNormal(); 34 | startTimer(15); 35 | } 36 | 37 | MyGLWidget::~MyGLWidget() 38 | { 39 | } 40 | 41 | //下面的代码的作用是重新设置OpenGL场景的大小,而不管窗口的大小是否已经改变(假定您没有使用全屏模式)。 42 | //甚至您无法改变窗口的大小时(例如您在全屏模式下),它至少仍将运行一次--在程序开始时设置我们的透视图。 43 | //OpenGL场景的尺寸将被设置成它显示时所在窗口的大小。 44 | void MyGLWidget::resizeGL(int w, int h) 45 | { 46 | glViewport (0, 0, (GLsizei)(w), (GLsizei)(h)); // Reset The Current Viewport 47 | glMatrixMode (GL_PROJECTION); // Select The Projection Matrix 48 | glLoadIdentity (); // Reset The Projection Matrix 49 | gluPerspective(50, (float)w/(float)h, 5, 2000); 50 | glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix 51 | glLoadIdentity (); // Reset The Modelview Matrix 52 | } 53 | 54 | //接下的代码段中,我们将对OpenGL进行所有的设置。我们将设置清除屏幕所用的颜色,打开深度缓存,启用smooth shading(阴影平滑),等等。 55 | //这个例程直到OpenGL窗口创建之后才会被调用。此过程将有返回值。但我们此处的初始化没那么复杂,现在还用不着担心这个返回值。 56 | void MyGLWidget::initializeGL() 57 | { 58 | // Start Of User Initialization 59 | m_angle = 0.0f; // Set Starting Angle To Zero 60 | glViewport(0 , 0, this->window()->width(), this->window()->height()); // Set Up A Viewport 61 | glMatrixMode(GL_PROJECTION); // Select The Projection Matrix 62 | glLoadIdentity(); // Reset The Projection Matrix 63 | gluPerspective(50, (float)this->window()->width()/(float)this->window()->height(), 5, 2000); // Set Our Perspective 64 | glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix 65 | glLoadIdentity(); // Reset The Modelview Matrix 66 | glEnable(GL_DEPTH_TEST); // Enable Depth Testing 67 | glShadeModel(GL_SMOOTH); // Select Smooth Shading 68 | glClearColor(0.0f, 0.0f, 0.0f, 0.5); // Set The Clear Color To Black 69 | } 70 | 71 | void MyGLWidget::paintGL() 72 | { 73 | if(m_domulti) 74 | glEnable(GL_MULTISAMPLE_ARB); // Enable Our Multisampling 75 | // ENDROACH 76 | 77 | glClearColor(0.0f, 0.0f, 0.0f, 0.5); // Set The Clear Color To Black 78 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer 79 | glLoadIdentity(); // Reset The View 80 | 81 | for(float i=-10;i<10;i++) 82 | for(float j=-10;j<10;j++) 83 | { 84 | glPushMatrix(); 85 | glTranslatef(i*2.0f,j*2.0f,-5.0f); 86 | glRotatef(m_angle,0.f,0.f,1.f); 87 | glBegin(GL_QUADS); 88 | glColor3f(1.0f,0.0f,0.0f);glVertex3f(i,j,0.0f); 89 | glColor3f(0.0f,1.0f,0.0f);glVertex3f(i + 2.0f,j,0.0f); 90 | glColor3f(0.0f,0.0f,1.0f);glVertex3f(i + 2.0f,j + 2.0f,0.0f); 91 | glColor3f(1.0f,1.0f,1.0f);glVertex3f(i,j + 2.0f,0.0f); 92 | glEnd(); 93 | glPopMatrix(); 94 | } 95 | 96 | if(m_doangle) 97 | m_angle+=0.05f; 98 | 99 | glFlush (); // Flush The GL Rendering Pipeline 100 | 101 | // ROACH 102 | if(m_domulti) 103 | glDisable(GL_MULTISAMPLE_ARB); 104 | // ENDROACH 105 | } 106 | 107 | void MyGLWidget::keyPressEvent(QKeyEvent *event) 108 | { 109 | switch(event->key()) 110 | { 111 | case Qt::Key_F2: 112 | { 113 | m_show_full_screen = !m_show_full_screen; 114 | if(m_show_full_screen) 115 | { 116 | showFullScreen(); 117 | } 118 | else 119 | { 120 | showNormal(); 121 | } 122 | updateGL(); 123 | break; 124 | } 125 | case Qt::Key_Escape: 126 | { 127 | qApp->exit(); 128 | break; 129 | } 130 | case Qt::Key_Space: 131 | { 132 | m_domulti=!m_domulti; 133 | break; 134 | } 135 | case Qt::Key_M: 136 | { 137 | m_doangle=!m_doangle; 138 | break; 139 | } 140 | } 141 | QGLWidget::keyPressEvent(event); 142 | } 143 | 144 | void MyGLWidget::timerEvent(QTimerEvent *event) 145 | { 146 | updateGL(); 147 | 148 | // QGLFormat f = this->format(); 149 | // if(m_domulti) 150 | // { 151 | // f.setSampleBuffers(false); 152 | // } 153 | // else 154 | // { 155 | // f.setSampleBuffers(true); 156 | // } 157 | // setFormat(f); 158 | QGLWidget::timerEvent(event); 159 | } 160 | 161 | //好了,那就是全部,希望你能喜欢:) 162 | -------------------------------------------------------------------------------- /Lesson46_FSAA/myglwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGLWIDGET_H 2 | #define MYGLWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MyGLWidget : public QGLWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit MyGLWidget(QGLFormat format, QWidget *parent = 0); 17 | 18 | explicit MyGLWidget(QWidget *parent = 0); 19 | ~MyGLWidget(); 20 | protected: 21 | void resizeGL(int w, int h); 22 | 23 | void initializeGL(); 24 | 25 | void paintGL(); 26 | 27 | void keyPressEvent(QKeyEvent *event); 28 | 29 | void timerEvent(QTimerEvent *event); 30 | private: 31 | bool m_show_full_screen; 32 | float m_angle; 33 | bool m_domulti; 34 | bool m_doangle; 35 | }; 36 | 37 | #endif // MYGLWIDGET_H 38 | -------------------------------------------------------------------------------- /Lesson48_ArcBallRotation/Lesson1_CreatingAnOpenGLWindow.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-02-08T15:13:31 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson1_CreatingAnOpenGLWindow 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | 20 | FORMS += 21 | -------------------------------------------------------------------------------- /Lesson48_ArcBallRotation/Lesson48_ArcBallRotation.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-04-30T15:08:26 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui opengl 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Lesson48_ArcBallRotation 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | myglwidget.cpp 17 | 18 | HEADERS += myglwidget.h 19 | -------------------------------------------------------------------------------- /Lesson48_ArcBallRotation/main.cpp: -------------------------------------------------------------------------------- 1 | #include "myglwidget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MyGLWidget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | NeHe_OpenGL_Qt4 2 | =============== 3 | 4 | build NeHe OpenGL tutorials in Qt4 5 | 6 | As Qt is cross-platform framework, so used freetype and ftgl in lesson13, lesson14, lesson15 to create font. 7 | 8 | freetype-2.5.3:http://sourceforge.net/projects/freetype/files/freetype2/2.5.3/ 9 | 10 | ftgl-2.1.3:http://sourceforge.net/projects/ftgl/files/FTGL%20Source/2.1.3~rc5/ 11 | 12 | Note: Lesson13,Lesson14,Lesson15,Lesson35,Lesson43 should copy resource file to their execution directory. 13 | 14 | Missed Lesson47 CG Vertex Shader, I'm sorry that my computer is ATI card, and maybe it can't used in other platform except windows. 15 | 16 | Author:Cai Wancang 17 | --------------------------------------------------------------------------------