OpenGL lighting hints for new programmers

本文提供了OpenGL照明设置的实用建议,包括光源、材质和表面法线的基本原则。通过遵循这些简化规则,初学者可以更容易地理解和实现光照效果。

Lighting in OpenGL is hard because you have to get light sources, materials, and surface normals all correct. If any one of them is wrong, it doesn't work at all.

As always, the key to successful learning is to keep things simple and change only one value at a time.

I'm going to give a number of 'rules' to follow. These are in fact useful simplifications, not commandments. Experienced OpenGL programmers don't follow these rules, and you will easily find contradictory opinions. I can only say that in my experience they work well to start with, and strongly urge you not to break these rules until you know exactly why you want to break them and what the implications are.

 

Lights

Don't go crazy with lights. Most scenes can be rendered quite comfortably with just one.

The light values for ambient, diffuse, etc are RGB colors. Use only white lights (R=G=B) and the fourth (alpha) value should always be 1.

Ambient light can be either specified globally (my preference) or on your primary light source only. Intensity should be in the 0.1 to 0.3 range, and never have multiple ambient lights.

Diffuse lighting is what photographers call 'soft' lighting. It should always be present, intensity 1.

Specular light is 'hard' lighting that creates highlights, sparkles, etc. Your primary light source always has specular intensity 1. If you need secondary lights as the equivalent of a photographers backlighting or extra flash, set the specular to 0 on those.

Lights have a position (x,y,z,,1) or a direction (x,y,z,0). Directional lights are easier to work with.

For a directional light the coordinates are a vector, the direction from which the light comes. As always, try to use only the values 0 or 1 at each of (x,y,z). The vectors (0,1,0) and (0,0,1) are good values for directional lighting.

 

Materials

You can make something appear yellow by either coloring it yellow and shining a white light on it, or coloring it white and shining a yellow light on it. The first is easier, so lights are set up once and rarely altered. Materials are where most of your design will be done.

Ambient and diffuse material values should be identical. OpenGL allows you to specify both at the same time for this reason.

Very roughly, all materials are either:

  • matte, with high diffuse color and low specular
  • plastic, with high diffuse color and white specular,
  • or metal, with low diffuse and high specular color

When something is described as being a particular color, that means the diffuse color for matte and plastic surfaces, the specular for metals.

OpenGL has the capability to set material values from the current vertex color values through use of glColorMaterial. While this is very useful to experienced programmers, I don't recommend trying to use it straight away. Using glColorMaterial does not mean you don't have to set up materials for your objects, it just adds one more thing that can go wrong.

 

Surface normals

Surface normals are essential for lighting, and always irritating to calculate.

Where possible, use the glu_ and glut_ primitives such as spheres and cylinders. These library functions calculate the surface normals for you. They are therefore good for testing your own lights and materials because at least you can't get the normals wrong.

A surface normal is a vector perpendicular to the polygon (triangle, quadrangle, whatever) being lit. It is in local object coordinates, before any transformations. If you rotate or translate say, a cylinder, the surface normals get transformed with it so you don't have to worry about it.

When learning to calculate normals, start with a cube that you construct yourself. The top face of a cube has surface normal (0, 1, 0), straight up, and all the others are equally easy. You can write these values directly into an array within the program code.

Once you have a light source and suitable material working properly with the cube, comment out the hard-coded normals. Every 3D programmer should have a copy of the function that takes three vertices as parameters, derives a pair of vectors, and does a cross product to return the surface normal. Test this on the vertices of your cube: since you know what the answers should be, you'll quickly discover any bugs and how to use the function correctly.

Once you can calculate the surface normal for a single cube face, you can do so for any piece of OpenGL geometry.

The surface normal, as the name indicates, has to be a normal or unit length vector, otherwise the lighting calculations won't work. If you use glScale anywhere, surface normals may no longer be correct because the scaling will shorten or lengthen the vector. At startup, write
so OpenGL will check and if necessary renormalise all your surface normals. In Olden Times this could slow your program down significantly, but these days it doesn't matter.  

        glEnable(GL_NORMALIZE);
【最优潮流】直流最优潮流(OPF)课设(Matlab代码实现)内容概要:本文档主要围绕“直流最优潮流(OPF)课设”的Matlab代码实现展开,属于电力系统优化领域的教学与科研实践内容。文档介绍了通过Matlab进行电力系统最优潮流计算的基本原理与编程实现方法,重点聚焦于直流最优潮流模型的构建与求解过程,适用于课程设计或科研入门实践。文中提及使用YALMIP等优化工具包进行建模,并提供了相关资源下载链接,便于读者复现与学习。此外,文档还列举了大量与电力系统、智能优化算法、机器学习、路径规划等相关的Matlab仿真案例,体现出其服务于科研仿真辅导的综合性平台性质。; 适合人群:电气工程、自动化、电力系统及相关专业的本科生、研究生,以及从事电力系统优化、智能算法应用研究的科研人员。; 使用场景及目标:①掌握直流最优潮流的基本原理与Matlab实现方法;②完成课程设计或科研项目中的电力系统优化任务;③借助提供的丰富案例资源,拓展在智能优化、状态估计、微电网调度等方向的研究思路与技术手段。; 阅读建议:建议读者结合文档中提供的网盘资源,下载完整代码与工具包,边学习理论边动手实践。重点关注YALMIP工具的使用方法,并通过复现文中提到的多个案例,加深对电力系统优化问题建模与求解的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值