《基于MFC的OpenGL编程》Part 15 Reflection
来源:互联网 发布:剑网三重置网络命令 编辑:程序博客网 时间:2024/05/22 00:44
《基于MFC的OpenGL编程》Part 16 Reflection
原文链接
源码链接
Reflections
Adding reflections to a program too can improve its realism to a great extent. Here we'll look at a simple method to create reflection where we simply redraw the object with an appropriate transformation and make the surface in between translucent. This creates an effective illusion of reflection!!
1、设置光源代码(主要是参数的修改)修改如下:
void CMy2OpenGLView::SetupLighting(){//Material PropertiesGLfloat matSpecular[] = {1.0f,0.0f,0.0f,0.7f};//反射材质特性GLfloat matShininess[] = {50.0f};//镜面反射指数特性GLfloat matAmbient[] = {0.25f,0.25f,0.25f,0.7f};//周围环境特性GLfloat matDiffuse[] = {0.5f,0.5f,0.5f,0.7f};//散射特性glMaterialfv(GL_FRONT,GL_SPECULAR,matSpecular);glMaterialfv(GL_FRONT,GL_SHININESS,matShininess);glMaterialfv(GL_FRONT,GL_AMBIENT,matAmbient);glMaterialfv(GL_FRONT,GL_DIFFUSE,matDiffuse);//Lighting Parameters//Enable Lighting glEnable(GL_LIGHTING);//Specify a single directional light GLfloat ambient1[] = {0.5f,0.5f,0.5f};GLfloat diffuse1[] = {0.5f,0.5f,0.5f};GLfloat specular1[] = {1.0f,0.0f,0.0f};GLfloat position1[] = {0.0f,0.0f,5.0f,0.0f};glLightfv(GL_LIGHT0,GL_AMBIENT,ambient1);glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse1);glLightfv(GL_LIGHT0,GL_SPECULAR,specular1);glLightfv(GL_LIGHT0,GL_POSITION,position1);glEnable(GL_LIGHT0);//Specify a single positional spotlightGLfloat ambient2[] = {1.0f,1.0f,0.0f};GLfloat diffuse2[] = {1.0f,0.0f,0.0f};GLfloat position2[] = {1.0f,0.0f,5.0f,1.0f};GLfloat direction2[] = {0.0f,0.0f,-5.0f};glLightfv(GL_LIGHT1,GL_AMBIENT,ambient2);glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuse2);glLightfv(GL_LIGHT1,GL_POSITION,position2);glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,direction2);glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,15.0f);glEnable(GL_LIGHT1);}
2、绘制函数void CMy2OpenGLView::RenderScene3D() 修改如下:
if (m_bCubeTexture)//画一个纹理自旋立方体{glLoadIdentity();//立方体中心移至坐标系原点 glTranslatef(0.0f,-0.0f,-8.0f);//this is the position of the cube,//so the parameter should be proper,then we can see the cube //save the matrix and do the rotation glPushMatrix();glRotatef(m_xRot,1.0f,0.0f,0.0f); glCallList(m_sceneList);////////////glPopMatrix();glPushMatrix();glTranslatef(0.0f,-3.0f,0.0f);glScalef(1.0f,-1.0f,1.0f);glRotatef(m_xRot,1.0f,0.0f,0.0f); glCallList(m_sceneList);<span style="font-family: 宋体;">////////////////////////</span>glPopMatrix();//Draw bottom of floorglEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);glPushMatrix();glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D,m_Texture[3]);//set background of the floorglBegin(GL_POLYGON);glTexCoord2f(0.0f,0.0f);glVertex3f(-5.0f,-1.5f,5.0f);glTexCoord2f(1.0f,0.0f);glVertex3f(-5.0f,-1.5f,-5.0f);glTexCoord2f(1.0f,1.0f);glVertex3f(5.0f,-1.5f,-5.0f);glTexCoord2f(0.0f,1.0f);glVertex3f(5.0f,-1.5f,5.0f); glEnd(); glPopMatrix();glDisable(GL_TEXTURE_2D);glDisable(GL_BLEND); // glRotatef(m_yRot,0.0f,1.0f,0.0f);// glCallList(m_sceneList);}3、修改函数CMy2OpenGLView::CreateSceneList()(该函数画自旋立方体,将其添加到列表函数中):
void CMy2OpenGLView::CreateSceneList(){//创建显示列表 m_sceneList = glGenLists(1);glNewList(m_sceneList,GL_COMPILE);//SetupLighting();glEnable(GL_TEXTURE_2D);glShadeModel(GL_SMOOTH);LoadGLTextures();glBindTexture(GL_TEXTURE_2D,m_Texture[0]);//Front Face glBegin(GL_POLYGON); glTexCoord2f(0,0); glVertex3f(-1.0f,-1.0f,0.0f); glTexCoord2f(1,0); glVertex3f( 1.0f,-1.0f,0.0f); glTexCoord2f(1,1); glVertex3f( 1.0f, 1.0f,0.0f); glTexCoord2f(0,1); glVertex3f(-1.0f, 1.0f,0.0f); glEnd(); //Back Face glBegin(GL_POLYGON); glTexCoord2f(1,0); glVertex3f(-1.0f,-1.0f,-1.0f); glTexCoord2f(1,1); glVertex3f(-1.0f, 1.0f,-1.0f); glTexCoord2f(0,1); glVertex3f( 1.0f, 1.0f,-1.0f); glTexCoord2f(0,0); glVertex3f( 1.0f,-1.0f,-1.0f); glEnd(); glBindTexture(GL_TEXTURE_2D,m_Texture[1]); //Left Face glBegin(GL_POLYGON); glTexCoord2f(1,0); glVertex3f(-1.0f,-1.0f, 0.0f); glTexCoord2f(1,1); glVertex3f(-1.0f, 1.0f, 0.0f); glTexCoord2f(0,1); glVertex3f(-1.0f, 1.0f,-1.0f); glTexCoord2f(0,0); glVertex3f(-1.0f,-1.0f,-1.0f); glEnd(); //Right Face glBegin(GL_POLYGON); glTexCoord2f(0,0); glVertex3f(1.0f,-1.0f, 0.0f); glTexCoord2f(1,0); glVertex3f(1.0f,-1.0f,-1.0f); glTexCoord2f(1,1); glVertex3f(1.0f, 1.0f,-1.0f); glTexCoord2f(0,1); glVertex3f(1.0f, 1.0f, 0.0f); glEnd(); glBindTexture(GL_TEXTURE_2D,m_Texture[2]); //Top Face glBegin(GL_POLYGON); glTexCoord2f(0,0); glVertex3f(-1.0f, 1.0f, 0.0f); glTexCoord2f(0,1); glVertex3f( 1.0f, 1.0f, 0.0f); glTexCoord2f(1,1); glVertex3f( 1.0f, 1.0f, -1.0f); glTexCoord2f(1,0); glVertex3f(-1.0f, 1.0f, -1.0f); glEnd(); //Bottom Face glBegin(GL_POLYGON); glTexCoord2f(0,1); glVertex3f(-1.0f, -1.0f, 0.0f); glTexCoord2f(0,0); glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); glTexCoord2f(1,1); glVertex3f( 1.0f, -1.0f, 0.0f); glEnd(); glDisable(GL_TEXTURE_2D);glEndList();//this is important}
4、加上大理石纹理:
void CMy2OpenGLView::LoadGLTextures(){ //Create Texture Names glGenTextures(4, m_Texture); LoadTexture("Apple.bmp",0); LoadTexture("Fauve.bmp",1); LoadTexture("Flower.bmp",2); LoadTexture("Floor.bmp",3);}
运行效果,估计是SetLighting的参数原因,没有明显的Reflection效果:
0 0
- 《基于MFC的OpenGL编程》Part 15 Reflection
- 《基于MFC的OpenGL编程》Part 16 Reflection
- 《基于MFC的OpenGL编程》Part 15 Selection
- 《基于MFC的OpenGL编程》Part 17 Shadows
- 《基于MFC的OpenGL编程》Part 14 Quadrics
- 《基于MFC的OpenGL编程》Part 10 Texture Mapping
- 《基于MFC的OpenGL编程》Part 9 Lighting
- 《基于MFC的OpenGL编程》Part 8 Colors
- 《基于MFC的OpenGL编程》Part 7 Animation
- 《基于MFC的OpenGL编程》Part 1 A Primer
- 《基于MFC的OpenGL编程》Part 1 A Primer
- 《基于MFC的OpenGL编程》Part 6 Animation
- 《基于MFC的OpenGL编程》Part 7 Colors
- 《基于MFC的OpenGL编程》Part 8 Lighting
- 《基于MFC的OpenGL编程》Part 9 Texture Mapping
- 《基于MFC的OpenGL编程》Part 13 Quadrics
- 《基于MFC的OpenGL编程》Part 14 Selection
- 《基于MFC的OpenGL编程》Part 0 基于MFC的OpenGL基本框架
- ytu 1330: 数字密码发生器
- Java 征途:行者的地图
- 3626: [LNOI2014]LCA|动态树
- 设置自定义控件view(自定义相对布局和对话框)
- Canal+Otter - Canal篇(1)
- 《基于MFC的OpenGL编程》Part 15 Reflection
- C#使用linq to xml完成对XML文件的创建、增加、删除、修改、查询
- Android Studio 的使用[转]
- js中会使用到的一种表单遍历验证的方法,访问当前节点的兄弟节点
- ytu 1338: 制作表格
- (android实战)Service 生命周期和使用注意项
- IOC和DI 真心的接地气
- ytu 1335: 信用卡号校验
- ios开发显示wifi速率功能开发