您的位置:首页 > 运维架构

[NEHE Couse] 01.Create an empty OpenGL Window

2009-01-08 19:42 197 查看
题记:

从今天起系统学习下NEHE前辈的OpenGL教程,虽然这半个学期间歇也学了不少,红宝书也看了,但感觉自己对OpenGL掌握的还不够,其实在看红宝书时就想看下这个教程了,现在趁这个机会好好学习下,我没有用教程中提供的SDK,我用的就是GLUT工具箱中给出的框架,自己感觉GLUT工具箱已经足够强大了。

我不会照搬教程中的程序代码,所有的代码我都会自己理解一遍然后会有所修改,以期达到学习的目的,自己的代码会附在文中,程序结果会以附图的形式放在文章最后,谢谢NEHE前辈这么好的教程,也谢谢提供资源的网络。顺祝大家新年快乐,Happy 牛 Year !

下面从教程的第一节开始。

第一节比较简单,创建一个空的OpenGL窗口,我在代码中加了对窗口全屏与否的控制,其它的没什么变化,代码如下:

1 /*

2 Introduction:Written by krisdy,finished at 2009.1.8

3 Apply to:NEHE Couse 01

4 */

5 #include <gl/glut.h>

6 #include <stdlib.h>

7

8 #define WinWidth 500 //the width of the window

9 #define WinHeight 500 //the height of the window

10

11 static GLint t;

12

13 void init(void)

14 {

15 glShadeModel(GL_SMOOTH);

16 glClearColor(0.0f,0.0f,0.0f,0.0f);

17 glClearDepth(1.0f);

18 glEnable(GL_DEPTH_TEST);

19 glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

20 }

21 void display(void)

22 {

23 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

24 /*

25 draw nothing here,so you can see nothing when the window appears first.

26 */

27 glFlush();

28 }

29 void reshape(int w,int h)

30 {

31 if(h == 0)

32 h = 1;

33 glViewport(0,0,w,h);

34 glMatrixMode(GL_PROJECTION);

35 glLoadIdentity();

36 gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);

37 glMatrixMode(GL_MODELVIEW);

38 glLoadIdentity();

39 }

40 void keyboard(unsigned char key,int x,int y)

41 {

42 switch(key)

43 {

44 //use the space key to decide whether the window will be full-screen displayed.

45 case 32:

46 if(t%2 == 0)

47 //requests that the current window be made full screen

48 glutFullScreen();

49 else

50 //requests a change to the size of the current window

51 glutReshapeWindow(WinWidth,WinHeight);

52 t++;

53 break;

54 //use the esc key to quit the application

55 case 27:

56 exit(0);

57 break;

58 default:

59 break;

60 }

61 }

62 int main(int argc,char *argv[])

63 {

64 glutInit(&argc,argv);

65 glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_SINGLE);

66 glutInitWindowSize(WinWidth,WinHeight);

67 glutInitWindowPosition(100,100);

68 glutCreateWindow("Lesson 01");

69

70 init();

71 glutDisplayFunc(display);

72 glutReshapeFunc(reshape);

73 glutKeyboardFunc(keyboard);

74 glutMainLoop();

75 return 0;

76 }

程序效果图如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: