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);
分享到:
相关推荐
101 Helpful Hints for IELTS
Derived from years of experience working with data access developers, ADO.NET Examples and Best Practices for C# Programmers includes a set of techniques proven to drastically reduce overhead, ...
Guide and Hints for Samsung BLE API.三星BLE说明文档
提供了OpenGL最新的接口技术和开发内容,提供了最新的开发技巧和流程。
Preface Acknowledgments Trademarks Chapter 1. Architecture and Implementation Section 1.1.... Answers and Hints for Selected Exercises Chapter 1 Chapter 2 Chapter 3 Chapter 4 ...
Hints for Computer System Design - Microsoft.pdf Kruchten 2006 architecture 10 years.pdf On_the_Design_and_Development_of_Program.pdf Programmin_In_Large_versus_In_Small-Kron.pdf Software Architecture...
Hints优化.pdf
There is extensive coverage of new Java 9 features, such as the new layout of the modular JDK/JRE runtime image, new convenience factory methods for creating collections, the new spin-wait hints ...
Spec for Extended Window Manager Hints. version 1.4 draft2
Oracle的hints调整机制介绍 Oracle的hints调整机制介绍 Oracle的hints调整机制介绍
oracle hints的详细介绍,包括各种优化算法等。
hints 使用的好的話很好用。在进行sql执行过程中,由于有时候系统自动优化的方式并不是最优的。需要我们手工添加hint来提高查询效率。
Oracle Hints 用法大全,对于优化 SQL 很有帮助!
oracle HINTS用法,自己平时整理的一些关于ORACLE HINTS的用法
Six hints for using templates
maven-hints-3.1.4-sources.jar
and syntax extensions, ALTER TABLE improvements for online operations, and new query hints for query tuning. Chapter 5, JSON Support in SQL Server, explores the JSON support built into SQLServer. This...
解析Oracle Hints.解析Oracle Hints.
Hashtable, Object> hints = new Hashtable, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, CHARSET); hints.put...
has been thoroughly expanded and updated for OS X Mountain Lion. No other book has the same drive to help you boost productivity, save time, and simply do things the best way possible when using a Mac...