《面向对象的程序设计》课程设计作业.docx
《《面向对象的程序设计》课程设计作业.docx》由会员分享,可在线阅读,更多相关《《面向对象的程序设计》课程设计作业.docx(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、郑州大学环境与水利学院面向对象的程序设计课程设计计 算 说 明 书专 业: 地理信息系统 学生姓名: 学生学号: 指导教师: 2011年 12 月 7 日Lesson 07 Texture Filters, Lighting & Keyboard Control第八课 纹理滤波方式,和键盘控制In this tutorial Ill teach you how to use three different texture filters. Ill teach you how to move an object using keys on the keyboard, and Ill also t
2、each you how to apply simple lighting to your OpenGL scene. Lots covered in this tutorial, so if the previous tutorials are giving you problems, go back and review. Its important to have a good understanding of the basics before you jump into the following code. Were going to be modifying the code f
3、rom lesson one again. As usual, if there are any major changes, I will write out the entire section of code that has been modified. Well start off by adding a few new variables to the program.这一课我将教你如何使用三种不同的纹理滤波方式。我将教你如何使用键盘来移动场景中的对象,还会教你在OpenGL场景中添加简单的光照。这一课包含了很多内容,所以如果你之前的课程仍存在问题,就先回头复习一下。学习后面的代码
4、之前,很好的理解基础知识十分重要。 我们准备在第一课的代码上稍作修改。跟以前一样,只要有任何大的改动,我都会写出整段被改动的代码。我们将以添加几个新的变量作为开始。#include/ Window头文件#include/ Standard Input/Output(ADD)头文件#include/ The OpenGL32 Library头文件#include/ The GLu32 Library头文件#include/ The GLaux Library头文件HDC hDC=NULL; / GDI Device ContextHGLRC hRC=NULL; / Rendering Conte
5、xtHWND hWnd=NULL; / 窗体Window句柄HINSTANCE hInstance; / 程序Instance句柄bool keys256; / 用于键盘控制的矩阵bool active=TRUE; / 窗体激活标志bool fullscreen=TRUE; / 全屏标志,缺省为真The lines below are new. Were going to add three boolean variables. BOOL means the variable can only be TRUE or FALSE. We create a variable called ligh
6、t to keep track of whether or not the lighting is on or off. The variables lp and fp are used to store whether or not the L or F key has been pressed. Ill explain why we need these variables later on in the code. For now, just know that they are important下面几行是新的。我们将增加三个布尔变量。BOOL意味着变量只能是TRUE和FALSE。我们
7、创造了 light 变量跟踪光照是否打开。变量lp和fp用来存储L 和F键是否按下。后面我会解释为什么我们在代码中需要这些变量。现在,仅仅先记住他们很重要。BOOL light; / 光源的开/关BOOL lp; / L键按下了么?BOOL fp; / F键按下了么?Now were going to set up five variables that will control the angle on the x axis (xrot), the angle on the y axis (yrot), the speed the crate is spinning at on the x
8、axis (xspeed), and the speed the crate is spinning at on the y axis (yspeed). Well also create a variable called z that will control how deep into the screen (on the z axis) the crate is现在我们设置5个变量来控制绕x轴和y轴旋转角度的步长,以及绕x轴和y轴的旋转速度。另外我们还创建了一个z变量来控制进入屏幕的深度。GLfloat xrot; / X 旋转GLfloat yrot; / Y 旋转GLfloat x
9、speed; / X 旋转速度GLfloat yspeed; / Y 旋转速度GLfloat z=-5.0f; / 深入屏幕的距离Now we set up the arrays that will be used to create the lighting. Well use two different types of light. The first type of light is called ambient light. Ambient light is light that doesnt come from any particular direction. All the o
10、bjects in your scene will be lit up by the ambient light. The second type of light is called diffuse light. Diffuse light is created by your light source and is reflected off the surface of an object in your scene. Any surface of an object that the light hits directly will be very bright, and areas
11、the light barely gets to will be darker. This creates a nice shading effect on the sides of our crate. Light is created the same way color is created. If the first number is 1.0f, and the next two are 0.0f, we will end up with a bright red light. If the third number is 1.0f, and the first two are 0.
12、0f, we will have a bright blue light. The last number is an alpha value. Well leave it at 1.0f for now. So in the line below, we are storing the values for a white ambient light at half intensity (0.5f). Because all the numbers are 0.5f, we will end up with a light thats halfway between off (black)
13、and full brightness (white). Red, blue and green mixed at the same value will create a shade from black(0.0f) to white(1.0f). Without an ambient light, spots where there is no diffuse light will appear very dark现在,我们来设置创建光源的数组。我们将使用两种不同的光。第一种光被称为环境光。环境光是一种来自于各个方向的光。所有场景中的对象都处于环境光的照射中。第二种光被称为漫射光。漫射光由
14、特定的光源产生,并在你的场景中的对象表面上产生反射。处于漫射光直接照射下的任何对象表面都变得很亮,而几乎未被照射到的区域就显得要暗一些。这样在我们所创建的木板箱的棱边上就会产生的很不错的阴影效果。 创建光源的过程和颜色的创建过程一样。如果第一个数字是1.0f,接下来的两个数字是0.0f,我们将得到艺术明亮的蓝色光。最后一个数字是阿尔法值。我们将把它设为1.0f。所以在接下来的代码中,我们得到的是半亮(0.5f)的白色环境光。因为所有的数字都是0.5f,我们将得到一束一半黑一半白的光。红,蓝和绿混合在一起将出现从黑到白的阴影。如果没有环境光,未被漫射光照到的地方会变得十分黑暗。GLfloat L
15、ightAmbient=0.5f,0.5f,0.5f,1.0f; / 环境光参数In the next line were storing the values for a super bright, full intensity diffuse light. All the values are 1.0f. This means the light is as bright as we can get it. A diffuse light this bright lights up the front of the crate nicely.下一行代码我们将得到最亮的漫射光。所有的参数值都
16、取成最大值1.0f。这意味着这束光和我们能得到的一样亮。它将照在我们木板箱的前面。 GLfloat LightDiffuse=1.0f,1.0f,1.0f,1.0f;/ 漫射光参数Finally we store the position of the light. The first three numbers are the same as glTranslates three numbers. The first number is for moving left and right on the x plane, the second number is for moving up a
17、nd down on the y plane, and the third number is for moving into and out of the screen on the z plane. Because we want our light hitting directly on the front of the crate, we dont move left or right so the first value is 0.0f (no movement on x), we dont want to move up and down, so the second value
18、is 0.0f as well. For the third value we want to make sure the light is always in front of the crate. So well position the light off the screen, towards the viewer. Lets say the glass on your monitor is at 0.0f on the z plane. Well position the light at 2.0f on the z plane. If you could actually see
19、the light, it would be floating in front of the glass on your monitor. By doing this, the only way the light would be behind the crate is if the crate was also in front of the glass on your monitor. Of course if the crate was no longer behind the glass on your monitor, you would no longer see the cr
20、ate, so it doesnt matter where the light is. Does that make sense? Theres no real easy way to explain the third parameter. You should know that -2.0f is going to be closer to you than -5.0f. and -100.0f would be WAY into the screen. Once you get to 0.0f, the image is so big, it fills the entire moni
21、tor. Once you start going into positive values, the image no longer appears on the screen cause it has gone past the screen. Thats what I mean when I say out of the screen. The object is still there, you just cant see it anymore. Leave the last number at 1.0f. This tells OpenGL the designated coordi
22、nates are the position of the light source. More about this in a later tutorial最后我们保存光源的位置。前三个参数和glTranslate中的一样。依次分别是XYZ轴上的位移。由于我们想要光线直接照射在木箱的正面,所以XY轴上的位移都是0.0f。第三个值是Z轴上的位移。为了保证光线总在木箱的前面,所以我们将光源的位置朝着观察者挪出屏幕。我们通常将屏幕也就是显示器的屏幕玻璃所处的位置称作Z轴的0.0f点。所以Z轴上的位移最后定为2.0f。假如你能够看见光源的话,它就浮在显示器的前方。当然,如果木箱不在显示器的屏幕玻璃后
23、面的话,你也无法看见箱子。没有解释三个参数的简单方法。你应该知道,-2.0f比 -5.0f更接近你。-100.0f将成为进入屏幕的方法。一旦你得到了0.0f,图像就大了,它充满了监视设备,一旦你进入,图像不再在屏幕上显示造成它已经穿过屏幕。这就是我所说的穿出屏幕。物体还在,只是你看不见了。最后一个参数取为1.0f。这将告诉OpenGL这里指定的坐标就是光源的位置,以后的教程中我会有更多解释。GLfloat LightPosition=0.0f,0.0f,2.0f,1.0f; / 光源位置The filter variable below is to keep track of which te
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象的程序设计 面向 对象 程序设计 课程设计 作业
限制150内