《基于MFC的OpenGL编程》Part 9 Lighting
来源:百度文库 编辑:神马文学网 时间:2024/04/27 14:57:45
2008年12月30日
关键字:
《基于MFC的OpenGL编程》Part 9 Lighting。
本文在第8篇文章的基础上,为其加入灯光效果。
Materials
OpenGL materials are description of what objects are made of. It basically specifies how much of light they reflect and this is what we would be seeing. OpenGL lighting goes a long way towards modeling real world lighting. In order achieve proper lighting effects in OpenGL we need to specify material properties. Material properties are of the following types - Diffuse and Ambient properties, Specular and Shininess Properties and Emissive Properties. These are the properties that specify how a surface dissipates light.
Diffuse and Ambient Properties
The diffuse and ambient reflective material properties are a type of reflective effect that is independent of the viewpoint. Diffuse lighting is light from a particular direction and ambient lighting is light that is coming from all directions. A particular light source can have both a diffuse and an ambient component. These material properties describe how the material reflects diffuse and ambient light falling on it.
Specular and Shininess Properties
The specular and the shininess properties of the surface describe the reflective effects that are affected by the position of the viewpoint. Specular light is reflected from a surface that produces the reflective highlights in a surface. The shininess is a value that describes how focussed the reflective properties are.
Emissive Property
This is the light that an object gives off by itself. A typical example of this is a light source itself.
Specifying a material property
In order to specify a material property we have to use the OpenGL function - glMaterial*(). We can individually specify the material properties using this function. Another way to go about this is by color tracking. This can be achieved by enabling color tracking and specifying what parameters of the material would be affected by the glColor command. This is an optimization feature that OpenGL provides and is useful in cases where we will be changing only particular properties of the material and not all of them. In this tutorial we will use glMaterial to specify the material properties as we will be individually setting each one of them.
Choosing the material properties determine how the object will look. We will follow the following steps in choosing the material properties -
Decide on the diffuse and ambient colors.
Decide on the shininess depending on the type of material object that is being modeled such as silver, wood etc.
Decide whether the object would be giving off any light on its own.
The above values can be obtained by mere trial and error. We will look at how we go about doing this when we write a program.
Lighting
OpenGL has two types of lighting: global lighting or ambient lighting and individual light sources which have position and direction. The way an object is lit depends on the material of the object, all the lights and their properties, their positions and direction etc. This means lighting calculations are quite expensive and are hence turned off by OpenGL by default. So we have to turn on lighting before using it. Global lighting parameters can be set using the glLightModel function which specify the lighting model we would be using - such as an infinite viewer or a local viewer (for specular highlights), single sided or two sided lighting and the RGBA value for the ambient light. In addition to this we would be specifying the individual light sources.
1,设置灯光和材质属性
void CCY457OpenGLView::SetupLighting ()
{
//Material Properties
GLfloat matSpecular[] = { 1.0f, 0.0f, 0.0f, 1.0f};
GLfloat matShininess[] = { 50.0f};
GLfloat matAmbient[] = { 0.25f, 0.25f, 0.25f, 1.0f};
GLfloat matDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f};
glMaterialfv(GL_FRONT, GL_SPECULAR, matSpecular);
glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiffuse);
glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);
//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.0};
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 spotlight
GLfloat ambient2[] = { 1.0f,1.0f,0.0f};
GLfloat diffuse2[] = { 1.0f,0.0f,0.0f};
GLfloat position2[] = { 1.0f,0.0f,5.0f,1.0};
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,在初始化OpenGL时调用上述函数
BOOL CCY457OpenGLView::InitializeOpenGL()
{
//Get a DC for the Client Area
m_pDC = new CClientDC(this);
//Failure to Get DC
if(m_pDC == NULL)
{
MessageBox("Error Obtaining DC");
return FALSE;
}
//Failure to set the pixel format
if(!SetupPixelFormat())
{
return FALSE;
}
//Create Rendering Context
m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());
//Failure to Create Rendering Context
if(m_hRC == 0)
{
MessageBox("Error Creating RC");
return FALSE;
}
//Make the RC Current
if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE)
{
MessageBox("Error making RC Current");
return FALSE;
}
//Specify Black as the clear color
::glClearColor(0.0f,0.0f,0.0f,0.0f);
//Specify the back of the buffer as clear depth
::glClearDepth(1.0f);
//Enable Depth Testing
::glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
SetupLighting();
return TRUE;
}
来源:博客园 作者:Phinecos(洞庭散人)
关键字:
《基于MFC的OpenGL编程》Part 9 Lighting。
本文在第8篇文章的基础上,为其加入灯光效果。
Materials
OpenGL materials are description of what objects are made of. It basically specifies how much of light they reflect and this is what we would be seeing. OpenGL lighting goes a long way towards modeling real world lighting. In order achieve proper lighting effects in OpenGL we need to specify material properties. Material properties are of the following types - Diffuse and Ambient properties, Specular and Shininess Properties and Emissive Properties. These are the properties that specify how a surface dissipates light.
Diffuse and Ambient Properties
The diffuse and ambient reflective material properties are a type of reflective effect that is independent of the viewpoint. Diffuse lighting is light from a particular direction and ambient lighting is light that is coming from all directions. A particular light source can have both a diffuse and an ambient component. These material properties describe how the material reflects diffuse and ambient light falling on it.
Specular and Shininess Properties
The specular and the shininess properties of the surface describe the reflective effects that are affected by the position of the viewpoint. Specular light is reflected from a surface that produces the reflective highlights in a surface. The shininess is a value that describes how focussed the reflective properties are.
Emissive Property
This is the light that an object gives off by itself. A typical example of this is a light source itself.
Specifying a material property
In order to specify a material property we have to use the OpenGL function - glMaterial*(). We can individually specify the material properties using this function. Another way to go about this is by color tracking. This can be achieved by enabling color tracking and specifying what parameters of the material would be affected by the glColor command. This is an optimization feature that OpenGL provides and is useful in cases where we will be changing only particular properties of the material and not all of them. In this tutorial we will use glMaterial to specify the material properties as we will be individually setting each one of them.
Choosing the material properties determine how the object will look. We will follow the following steps in choosing the material properties -
Decide on the diffuse and ambient colors.
Decide on the shininess depending on the type of material object that is being modeled such as silver, wood etc.
Decide whether the object would be giving off any light on its own.
The above values can be obtained by mere trial and error. We will look at how we go about doing this when we write a program.
Lighting
OpenGL has two types of lighting: global lighting or ambient lighting and individual light sources which have position and direction. The way an object is lit depends on the material of the object, all the lights and their properties, their positions and direction etc. This means lighting calculations are quite expensive and are hence turned off by OpenGL by default. So we have to turn on lighting before using it. Global lighting parameters can be set using the glLightModel function which specify the lighting model we would be using - such as an infinite viewer or a local viewer (for specular highlights), single sided or two sided lighting and the RGBA value for the ambient light. In addition to this we would be specifying the individual light sources.
1,设置灯光和材质属性
void CCY457OpenGLView::SetupLighting ()
{
//Material Properties
GLfloat matSpecular[] = { 1.0f, 0.0f, 0.0f, 1.0f};
GLfloat matShininess[] = { 50.0f};
GLfloat matAmbient[] = { 0.25f, 0.25f, 0.25f, 1.0f};
GLfloat matDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f};
glMaterialfv(GL_FRONT, GL_SPECULAR, matSpecular);
glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiffuse);
glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);
//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.0};
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 spotlight
GLfloat ambient2[] = { 1.0f,1.0f,0.0f};
GLfloat diffuse2[] = { 1.0f,0.0f,0.0f};
GLfloat position2[] = { 1.0f,0.0f,5.0f,1.0};
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,在初始化OpenGL时调用上述函数
BOOL CCY457OpenGLView::InitializeOpenGL()
{
//Get a DC for the Client Area
m_pDC = new CClientDC(this);
//Failure to Get DC
if(m_pDC == NULL)
{
MessageBox("Error Obtaining DC");
return FALSE;
}
//Failure to set the pixel format
if(!SetupPixelFormat())
{
return FALSE;
}
//Create Rendering Context
m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());
//Failure to Create Rendering Context
if(m_hRC == 0)
{
MessageBox("Error Creating RC");
return FALSE;
}
//Make the RC Current
if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE)
{
MessageBox("Error making RC Current");
return FALSE;
}
//Specify Black as the clear color
::glClearColor(0.0f,0.0f,0.0f,0.0f);
//Specify the back of the buffer as clear depth
::glClearDepth(1.0f);
//Enable Depth Testing
::glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
SetupLighting();
return TRUE;
}
来源:博客园 作者:Phinecos(洞庭散人)
《基于MFC的OpenGL编程》Part 9 Lighting
《基于MFC的OpenGL编程》Part 10 Texture Mapping
《基于MFC的OpenGL编程》Part 1 A Primer
《基于MFC的OpenGL编程》Part 2 Setting up OpenGL on Windows
基于MFC下的OpenGL框架
基于VC++的OpenGL编程讲座之概述
基于VC++的OpenGL编程讲座之坐标变换
基于VC++的OpenGL编程讲座之概述
基于VC++的OpenGL编程讲座之基本图元
手把手教你搭建用MFC进行OpenGL编程的框架
如何让基于对话框的MFC程序启动后自动隐藏对话框--==>肯肯编程小屋<==
OpenGL的安装 - OpenGL
MFC ODBC 编程
MFC中基于对话框程序快捷键的实现
MFC中基于对话框程序快捷键的实现
如何让基于对话框的MFC程序启动后自动隐藏对话框 VC/MFC / 界面
MFC 对话框编程要点 - lesky的专栏 - CSDNBlog
VC知识库文章 - MFC程序员的WTL指南: Part I - ATL 界面类
VC知识库文章 - MFC程序员的WTL指南: Part II - WTL 界面基类
VC知识库文章 - MFC程序员的WTL指南: Part III - 工具条与状态条
VC知识库文章 - MFC程序员的WTL指南: Part IV - 对话框与控件
VC知识库文章 - MFC程序员的WTL指南: Part VII - 分隔窗口
2如何让基于对话框的MFC程序启动后自动隐藏对话框 VC/MFC / 界面 - 社区 community.csdn.net
基于Visual C++6.0的DLL编程实现