一、简单光照原理

平行光(正常光)

  光照效果=   环境颜色 + 漫反射颜色 + 镜面反射颜色

点光源

  光照效果=   环境颜色 + (漫反射颜色 + 镜面反射颜色)× 衰减因子

聚光灯

  光照效果=   环境颜色 + (漫反射颜色 + 镜面反射颜色)× 衰减因子 × 聚光灯效果

二、IOS光照

1、导入系统库

  • GLKit
  • OpenGLES
  • CoreGraphics
  • QuartzCore

2、光照类

#import <GLKit/GLKit.h>
//基础光
@interface BaseLight : NSObject{
@public GLKVector4 Color;
float AmbientIntensity;//周围无光照的区域亮度
float DiffuseIntensity;//漫反射
float SpecularIntensity;//镜面反射
}
@end
//点光源
@interface PointLight : BaseLight{
@public GLKVector3 DestPos;
GLKVector3 SourcePos;//光照源位置
float Shininess;
struct
{
float Constant;
float Linear;
float Exp;
} Attenuation;
}
@end
//聚光灯
@interface SpotLight : PointLight{
@public GLKVector3 Direction;
float Cutoff;//最小夹角cos值
float Exponent;//聚光灯角度
}
@end

3、实现光源属性槽位获取及更新

@interface BaseLight : NSObject{
@public GLKVector4 Color;
float AmbientIntensity;//周围无光照的区域亮度
float DiffuseIntensity;//漫反射
float SpecularIntensity;//镜面反射
}
@end
@interface PointLight : BaseLight{
@public GLKVector3 DestPos;
GLKVector3 SourcePos;//光照源位置
float Shininess;
struct
{
float Constant;
float Linear;
float Exp;
} Attenuation;
}
@end @interface SpotLight : PointLight{
@public GLKVector3 Direction;
float Cutoff;//最小夹角cos值
float Exponent;//聚光灯角度
}
@end

在实际项目中调用,初始化,设置,然后更新。。。。

4、GLSL实现

//基础光
struct BaseLight
{
vec4 Color;
float AmbientIntensity;
float DiffuseIntensity;
float SpecularIntensity;
};
//点光源
struct PointLight{
BaseLight Base; vec3 SourcePos;
vec3 DestPos;
float Shininess; //亮度
struct
{
float Constant;
float Linear;
float Exp;
} Attenuation;
};
//聚光灯
struct SpotLight{
PointLight Base;
vec3 Direction;
float Cutoff;
float Exponent;
}; vec3 vPos;
vec3 vEyePos; uniform SpotLight u_spotLight; //基础光计算
vec4 CalcLightInternal(BaseLight Light, vec3 LightDirection, vec3 Normal)
{
float DiffuseFactor = dot(Normal, LightDirection);
vec4 DiffuseColor = vec4(, , , );
vec4 SpecularColor = vec4(, , , );
if (DiffuseFactor > 0.0) {
DiffuseColor = Light.Color * Light.DiffuseIntensity * DiffuseFactor;
vec3 VertexToEye = normalize(vEyePos - vPos);
vec3 LightReflect = normalize(reflect(LightDirection, Normal));
vec3 H = normalize(LightDirection + VertexToEye);
float SpecularFactor = max(0.0, dot(Normal, H));
SpecularFactor = pow(SpecularFactor, 10.0);
if (SpecularFactor > 0.0) {
SpecularColor = Light.Color *
Light.SpecularIntensity * SpecularFactor;
}
}
return DiffuseColor + SpecularColor;
} //pointlight 点光源计算
vec4 CalcPointLight(PointLight l,vec3 Normal)
{
vec3 LightDirection = normalize(l.SourcePos-l.DestPos);
float Distance =length(vPos - l.DestPos);
vec4 Color = CalcLightInternal(l.Base, LightDirection, Normal);
//衰减因子
float Attenuation = l.Attenuation.Constant +l.Attenuation.Linear * Distance +
l.Attenuation.Exp* Distance * Distance;
Color=Color*l.Shininess/Attenuation;
return Color;
}
//spotlight实现
vec4 CalcSpotLight(SpotLight l, vec3 Normal)
{
vec3 LightToPixel = normalize(vPos - l.Base.DestPos);
vec3 LightDirection = normalize(LightToPixel-l.Direction);
//聚光灯因子
float SpotFactor =pow(max(0.0, dot(LightToPixel, LightDirection)),l.Exponent);
if (SpotFactor > l.Cutoff) {
vec4 Color = CalcPointLight(l.Base, Normal);
return Color * clamp((1.0 - (1.0 - SpotFactor) * 1.0/(1.0 - l.Cutoff)),0.0,1.0);
}
else {
return vec4(0.0,0.0,0.0,0.0);
}
} void main()
{
lowp vec4 topColor = texture2D(Texture, TexCoordOut); vec3 vNormal=vec3(, , );
vec3 N = normalMatrix * vNormal;
vPos=vec3(TexCoordOut,0.0) ; //取光照点
vEyePos=vec3(0.0,0.0,1.0);//观察点
vec4 AmbientColor = u_spotLight.Base.Base.Color * u_spotLight.Base.Base.AmbientIntensity; lowp vec4 lightColor =CalcSpotLight(u_spotLight,N)+CalcSpotLight(u_spotLight1,N)+AmbientColor;
gl_FragColor =lightColor* topColor; }

OpenGL ES2.0光照的更多相关文章

  1. Cocos2d-x中使用OpenGL ES2.0编写shader

    这几天在看子龙山人的关于OpenGL的文章,先依葫芦画瓢,能看到些东西,才能慢慢深入了解,当入门文章不错,但是其中遇到的一些问题,折腾了一些时间,为了方便和我一样的小白们,在这篇文章中进行写补充. O ...

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

    OpenGL ES2.0编程步骤 OpenGL ES (OpenGL for Embedded Systems) 是 OpenGL 三维图形 API 的子集,针对手机.PDA和游戏主机等嵌入式设备而设 ...

  3. Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤

    原文地址: Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤 - 网络资源是无限的 - 博客频道 - CSDN.NET http://blog.csdn.net/fen ...

  4. OpenGL ES2.0入门详解

    引自:http://blog.csdn.net/wangyuchun_799/article/details/7736928  1.决定你要支持的OpenGL ES的版本.目前,OpenGL ES包含 ...

  5. OPENGL ES2.0如何不使用glActiveTexture而显示多个图片

    https://www.oschina.net/question/253717_72107 用opengl es 2.0显示多个图片的话,我只会一种方式,先将图片生成纹理,然后用下面的方式渲染 // ...

  6. Android +NDK+eclipse+opengl ES2.0 开启深度測试

    參考:https://www.opengl.org/discussion_boards/showthread.php/172736-OpenGL-ES-Depth-Buffer-Problem 环境: ...

  7. OpenGL ES2.0 基本编程

    1. EGL OpenGL ES命令须要一个rendering context和一个drawing surface. Rendering Context: 保存当前的OpenGL ES状态. Draw ...

  8. OpenGL ES2.0 入门经典例子

    原文链接地址:http://www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorial 免责申明(必读!):本博客提供的所有教程的翻译原稿 ...

  9. OpenGL ES2.0基础入门

    1.OpenGL ES 1.x渲染管线(又称为渲染流水线) (1).基本处理: 基本处理主要是设定3D空间中物体的顶点坐标.顶点对应的颜色.顶点的纹理坐标等属性,并且指定绘制方式. 常见的绘制方式有: ...

随机推荐

  1. lua代码的加载

    lua代码的加载 Openresty是什么 OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,通过把lua嵌入到Nginx中,使得我们可以用轻巧的lua语言进行nginx的 ...

  2. 201521123121 《JAVA程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...

  3. 201521123069 《Java程序设计》 第9周学习总结

    1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. (1)使用try...catch语句捕获异常(try块后可跟一个或多个catch块,注意子类异常要放在父类异常前面, ...

  4. 笔记2 linux多线程 读写锁

    //read write lock #include<stdio.h> #include<unistd.h> #include<pthread.h> struct ...

  5. linux 下怎样查找一个文件夹在哪个目录下?

    如果只显示所在目录的路径: find 目录 -type d -name "查询目录名" -printf "%h\n" 如果同时显示目录名称和所在目录的路径: f ...

  6. 时间效率:最小的K个数

    输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. import java.util.ArrayList; import jav ...

  7. String类的源码分析

    之前面试的时候被问到有没有看过String类的源码,楼主当时就慌了,回来赶紧补一课. 1.构造器(构造方法) String类提供了很多不同的构造器,分别对应了不同的字符串初始化方法,此处从源码中摘录如 ...

  8. 这家IT教育公司太拼了:毕业生找不到工作就全额退学费!

    乐橙谷为了让更多的学生有工作,有高薪工作,已经决定尝试一种更刺激的游戏规则:完成课时的学员如果毕业找不到工作,公司将全额退还学费.这家公司秉承着自己的使命:以尊重的文化,敬畏的心态去努力帮助每个学生实 ...

  9. 再起航,我的学习笔记之JavaScript设计模式26(解释器模式)

    解释器模式 概念介绍 解释器模式(Interpreter):给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 获取元素在页面中的路径 我们都知道获取一个 ...

  10. C++ 虚函数 、纯虚函数、接口的实用方法和意义

    也许之前我很少写代码,更很少写面向对象的代码,即使有写多半也很容易写回到面向过程的老路上去.在写面向过程的代码的时候,根本不管什么函数重载和覆盖,想到要什么功能就变得法子的换个函数名字,心里想想:反正 ...