《基于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
原创粉丝点击