<OpenGL>Lighting

I. Hidden-Surface Removal

Purpose: In order to increase performance, it is very important to draw objects that are closer to the viewing position and to eliminate objects obscured by others nearer to the eye.

Depth buffer: a value associating a depth, or a distance, from the view plane (usually the near clipping plane), with each pixel on the window. Initially, the depth values for all pixels are set to the largest possible distance (usually the far clipping plane) using the glClear() command with GL_DEPTH_BUFFER_BIT.

To use depth buffer, you need to enable depth-buffering. Before drawing, each time you draw the scene, you need to clear the depth buffer and then draw the objects in the scene in any order.

To perform hidden-face removal, using the following code:

1 glutInitDisplayMode(GLUT_DEPTH | ...);    // enable depth-buffering
2 glEnable(GL_DEPTH_TEST);           // enable hidden-surface removal
3 ...
4 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
5 /* drawings here */

II. OpenGL Lighting

i. Four components: Ambient, Diffuse, Specular, Emissive (All four component are computed independently and then added together)

1. Ambient illumination is light that‘s been scattered so much by the environment that it‘s direction is impossible to determine---it seems to come from all directions.(Environment lighting)

2. Diffuse component is the light that comes from one direction, so it‘s brighter if it comes squarely down on a surface than if it barely glances off the surface.(Chalk, carpet etc.)

3. Specular light comes from a particular direction, and it tends to bounce off the surface in a preferred direction.(Mirror, shiny metal etc.)

4. Emissive color, which materials may have, simulates light originating from an object. It does not introduce any additional light into the overall scene.

ii. Functions

1. glMaterial*(GLenum face, GLenum pname, TYPE param)  // define materiall properties for the objects

face

Specifies which face or faces are being updated. Must be one of GL_FRONTGL_BACK, or GL_FRONT_AND_BACK.

pname

Specifies the single-valued material parameter of the face or faces that is being updated. Must be GL_SHININESS.

param

Specifies the value that parameter GL_SHININESS will be set to.

2. glLight*(GLenum light, GLenum pname, TYPE param);    // create, position and enbale one or more light sources

light

Specifies a light. The number of lights depends on the implementation, but at least eight lights are supported. They are identified by symbolic names of the form GL_LIGHT i, where i ranges from 0 to the value of GL_MAX_LIGHTS - 1.

pname

Specifies a light source parameter for lightGL_AMBIENTGL_DSEGL_SPECULARGL_POSITIONGL_SPOT_CUTOFFGL_SPOT_DIRECTIONGL_SPOT_EXPONENTGL_CONSTANT_ATTENUATION,GL_LINEAR_ATTENUATION, and GL_QUADRATIC_ATTENUATION are accepted.

params

Specifies a pointer to the value or values that parameter pname of light source light will be set to.

ps. Remember using glEnable(GL_LIGHTi) to turn on the light source

Lighting example code:

 1 void init(void)
 2 {
 3         GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
 4         GLfloat mat_shininess[] = {10.0};
 5         GLfloat light0_position[] = {1.73, 1.0, 1.0, 0.0};
 6         GLfloat light1_position[] = {-1.73, 1.0, 1.0, 0.0};
 7         GLfloat r_light[] = {1.0, 0.0, 0.0, 1.0};
 8         GLfloat g_light[] = {0.0, 1.0, 0.0, 1.0};
 9
10         glClearColor(0.0, 0.0, 0.0, 0.0);
11
12         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
13         glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
14         glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
15         glLightfv(GL_LIGHT0, GL_DIFFUSE, r_light);
16         glLightfv(GL_LIGHT0, GL_SPECULAR, r_light);
17         glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
18         glLightfv(GL_LIGHT1, GL_DIFFUSE, g_light);
19         glLightfv(GL_LIGHT1, GL_SPECULAR, g_light);
20
21         glEnable(GL_DEPTH_TEST);
22         glEnable(GL_LIGHTING);
23         glEnable(GL_LIGHT0);
24         glEnable(GL_LIGHT1);
25 }

iii. Spotlights

You can have a positional light source act as a spotlight---that is, by restricting the shape of the light it emits to a cone.

Its properties can be modified by:

1 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, Default=(0.0, 0.0, -1.0))    // light‘s direction
2 glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, Default=0.0)    // light‘s exponent
3 glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, Default=180.0)    // light‘s cutoff angle

ps. GL_SPOT_EXPONENT controls the light intensity concentration, given that light‘s intensity is highest in the center of the cone.

ps. the default cutoff angle is the radius but not diameter, the default 180.0 light covers 360.0 of total space.

时间: 2024-12-14 11:58:40

<OpenGL>Lighting的相关文章

OpenGL - Lighting

1 简单光照 OpenGL简单光照光源分为: 辐射光(Emitted Light)是最简单的一种光,它直接从物体发出并且不受任何光源影响. 环境光(Ambient Light)是由光源发出经环境多次散射而无法确定其方向的光,即似乎来自所有方向. 漫射光(Diffuse Light)来自一个方向,它垂直于物体时比倾斜时更明亮. 镜面光(Specular Light)来自特定方向并沿另一方向反射出去. 1.1 创建光源 光源有许多特性,如颜色.位置.方向等. 设置光源特性参数的函数: void gl

Color, Material, Lighting

[Color, Material, Lighting] The material and lighting parameters are used to control the built-in vertex lighting. Vertex lighting is the standard Direct3D/OpenGL lighting model that is computed for each vertex. material & parameters 参数用于控制vertex lig

图形世界分裂的两派——理清Direct3D和OpenGL的脉络

计算机三维图形是指将用数据描述的三维空间通过计算转换成二维图像并显示或打印出来的技术,API(Application Programming Interface)即"应用程序接口"是连接应用程序与操作系统.实现对计算机硬件控制的纽带,Direct3D和OpenGL是目前的两大3D图形 API,要在你的3D显卡上进行3D特效的制作.实现都必须通过它们(Vooodoo迷们肯定对Glide接口记忆尤深,可惜已随着3dfx的倒闭而作古,其它还有Heidi等接口).关于D3D和OpenGL的理论

EBU5405: 3D Graphics Programming Tools - Coursework 2019

EBU5405: 3D Graphics Programming Tools - Coursework 2019In this coursework you will develop an articulated and interactive object in the shape of acrawling animal using C and OpenGL. The main objectives of this project are to practice anddemonstrate:

OpenGL教程翻译 第十八课 漫反射光(Diffuse Lighting)

OpenGL教程翻译 第十七课 环境光(Ambient Lighting) 原文地址:http://ogldev.atspace.co.uk/(源码请从原文主页下载) Background 环境光和漫反射光的主要不同是,漫反射光的计算需要依靠光线方向而环境光完全忽略了它!当只有环境光时整个场景被均等照亮.漫反射光会使物体面对光的部分比背对光的部分更加明亮. 此外漫反射光还增加了一点新的计算,光线的入射角决定了表面的亮度.通过下面的图片来演示这个概念: 让我们假设两条光线的强度是一样的,而唯一不一

OpenGL教程翻译 第十七课 环境光(Ambient Lighting)

OpenGL教程翻译 第十七课 环境光(Ambient Lighting) 原文地址:http://ogldev.atspace.co.uk/(源码请从原文主页下载) Background 光照是3D图形学的最重要的研究领域之一.对光照得体的模拟会使渲染的场景增加很多视觉吸引力.之所以使用"模拟"这个词是因为你无法准确的模仿出光照在自然界中的样子.真正的光是由大量的称为"光子"的粒子组成的,并且同时表现出波和粒子的特性(光的"波粒二象性").如果

Mali GPU OpenGL ES 应用性能优化--基本方法

1. 常用优化工具 2. 常用优化方案 OpenGL ES优化的主要工作是在图形管道中找到影响性能的bottleneck,其bottleneck一般表现在以下几方面: ? 在应用程序代码中,如冲突检测     ? GPU与主内存间的数据传输     ? 在VP(Vertex Processor)中的顶点处理     ? 在FP(Fragment Processor)中的片断处理 可通过DS-5 Streamline来定位性能瓶颈(Locate bottleneck).为了获取更好的性能,可从以下

iOS开发——图形编程OC篇&amp;OpenGL ES2.0编程步骤

OpenGL ES2.0编程步骤 OpenGL ES (OpenGL for Embedded Systems) 是 OpenGL 三维图形 API 的子集,针对手机.PDA和游戏主机等嵌入式设备而设计.该API由Khronos集团定义推广,Khronos是一个图形软硬件行业协会,该协会主要关注图形和多媒体方面的开放标准. 1. 保存全局变量的数据结构 以下例子程序均基于Linux平台. 1 typedef struct _escontext 2 { 3 void* userData; // P

三维图像技术与OpenGL基础理论

英文原文:3D Graphics with OpenGL Basic Theory 中文译文:三维图像技术与OpenGL基础理论 1. 计算机图像硬件 1.1 GPU(图像处理单元) 如今,计算机拥有用来专门做图像处理显示的GPU模块,拥有独立的图像处理储存(显存). 1.2 像素和画面 任何图像显示都是基于栅格的格式.一个栅格既是一张二维的像素直角坐标网.像素具有两个属性:颜色和位置.颜色通常使用RGB(红绿蓝)来表示,典型的有用8位或者24位二进制位(真彩色)表示一种颜色.位置则用坐标(x,