《基于MFC的OpenGL编程》Part 9 Lighting

本文介绍如何在OpenGL中实现真实感照明效果,包括设置材质属性如漫反射、环境光、高光及发光特性,并通过示例代码展示如何配置光源。
本文在 第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,设置灯光和材质属性

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> void CCY457OpenGLView::SetupLighting()
{
// MaterialProperties
GLfloatmatSpecular[] = { 1.0f , 0.0f , 0.0f , 1.0f };
GLfloatmatShininess[]
= { 50.0f };
GLfloatmatAmbient[]
= { 0.25f , 0.25f , 0.25f , 1.0f };
GLfloatmatDiffuse[]
= { 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);
// LightingParameters
// EnableLighting
glEnable(GL_LIGHTING);
// Specifyasingledirectionallight
GLfloatambient1[] = { 0.5f , 0.5f , 0.5f };
GLfloatdiffuse1[]
= { 0.5f , 0.5f , 0.5f };
GLfloatspecular1[]
= { 1.0f , 0.0f , 0.0f };
GLfloatposition1[]
= { 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);
// Specifyasinglepositionalspotlight
GLfloatambient2[] = { 1.0f , 1.0f , 0.0f };
GLfloatdiffuse2[]
= { 1.0f , 0.0f , 0.0f };
GLfloatposition2[]
= { 1.0f , 0.0f , 5.0f , 1.0 };
GLfloatdirection2[]
= { 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时调用上述函数

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> BOOLCCY457OpenGLView::InitializeOpenGL()
{
// GetaDCfortheClientArea
m_pDC = new CClientDC( this );
// FailuretoGetDC
if (m_pDC == NULL)
{
MessageBox(
" ErrorObtainingDC " );
return FALSE;
}
// Failuretosetthepixelformat
if ( ! SetupPixelFormat())
{
return FALSE;
}
// CreateRenderingContext
m_hRC = ::wglCreateContext(m_pDC -> GetSafeHdc());
// FailuretoCreateRenderingContext
if (m_hRC == 0 )
{
MessageBox(
" ErrorCreatingRC " );
return FALSE;
}
// MaketheRCCurrent
if (::wglMakeCurrent(m_pDC -> GetSafeHdc(),m_hRC) == FALSE)
{
MessageBox(
" ErrormakingRCCurrent " );
return FALSE;
}
// SpecifyBlackastheclearcolor
::glClearColor( 0.0f , 0.0f , 0.0f , 0.0f );
// Specifythebackofthebufferascleardepth
::glClearDepth( 1.0f );
// EnableDepthTesting
::glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
SetupLighting();
return TRUE;
}

(Kriging_NSGA2)克里金模型结合多目标遗传算法求最优因变量及对应的最佳自变量组合研究(Matlab代码实现)内容概要:本文介绍了克里金模型(Kriging)与多目标遗传算法NSGA-II相结合的方法,用于求解最优因变量及其对应的最佳自变量组合,并提供了完整的Matlab代码实现。该方法首先利用克里金模型构建高精度的代理模型,逼近复杂的非线性系统响应,减少计算成本;随后结合NSGA-II算法进行多目标优化,搜索帕累托前沿解集,从而获得多个最优折衷方案。文中详细阐述了代理模型构建、算法集成流程及参数设置,适用于工程设计、参数反演等复杂优化问题。此外,文档还展示了该方法在SCI一区论文中的复现应用,体现了其科学性与实用性。; 适合人群:具备一定Matlab编程基础,熟悉优化算法和数值建模的研究生、科研人员及工程技术人员,尤其适合从事仿真优化、实验设计、代理模型研究的相关领域工作者。; 使用场景及目标:①解决高计算成本的多目标优化问题,通过代理模型降低仿真次数;②在无法解析求导或函数高度非线性的情况下寻找最优变量组合;③复现SCI高水平论文中的优化方法,提升科研可信度与效率;④应用于工程设计、能源系统调度、智能制造等需参数优化的实际场景。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现过程,重点关注克里金模型的构建步骤与NSGA-II的集成方式,建议自行调整测试函数或实际案例验证算法性能,并配合YALMIP等工具包扩展优化求解能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值