如何使用OpenGL ES 2.0应用光源? [英] How do you apply light sources using OpenGL ES 2.0?

查看:348
本文介绍了如何使用OpenGL ES 2.0应用光源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找任何教程或示例代码,以解释和显示OpenGL ES 2.0中灯光的使用情况。我在网上看到的大多数材料都是指Opengl ES 1.1。

I am trying to find any tutorial or sample code which explains and shows the usage of lights in OpenGL ES 2.0. Most of the material I have seen online refers to Opengl ES 1.1.

任何人都可以提供有关如何在OpenGL ES 2.0中进行照明的任何链接或文档吗?

Can anyone provide any links or documentation on how to do lighting in OpenGL ES 2.0?

推荐答案

OpenGL ES 2.0的难点在于,由于您可以根据自己的需要编写自己的着色器,因此没有一种方法可以为对象提供照明。这将取决于您想要如何呈现您的内容。

The difficulty with OpenGL ES 2.0 is that since you can write your own shaders pretty much how you want, there's no one set way to provide lighting to an object. It will depend on how you want to present your content.

也就是说,您可以通过提供光线方向来复制OpenGL ES 1.1的固定功能照明功能并计算每个顶点的光强度。你可以在这个示例应用程序中看到一个例子。我的一个班级。

That said, you can replicate OpenGL ES 1.1's fixed function lighting capabilties by doing something like providing a light direction and calculating the light intensity at each vertex. You can see an example of this in this sample application I slapped together for one of my classes.

这里使用的顶点着色器如下:

The vertex shader used here is the following:

attribute vec4 position;
attribute vec4 normal;

varying float lightIntensity;

uniform mat4 modelViewProjMatrix;
uniform vec3 lightDirection;

void main()
{
    vec4 newNormal = modelViewProjMatrix * normal;
    vec4 newPosition = modelViewProjMatrix * position;
    gl_Position = newPosition;

    lightIntensity = max(0.0, dot(newNormal.xyz, lightDirection));
}

带有匹配的片段着色器:

with the matching fragment shader:

varying highp float lightIntensity;

void main()
{
    lowp vec4 yellow = vec4(1.0, 1.0, 0.0, 1.0);
    gl_FragColor = vec4((yellow * lightIntensity * 0.2).rgb, 1.0);
}

光线方向的点积和该顶点的法线用于计算从该顶点撞击观察者眼睛的光强度。然后在每个三角形的顶点之间插入此光强度,以获得每个片段的强度。

The dot product of the light direction and the normal at that vertex is used to calculate the light intensity hitting the eye of the viewer from that vertex. This light intensity is then interpolated between the vertices in each triangle to give you the intensity at each fragment.

我在开源中执行一些更高级的每像素照明< a href =http://sunsetlakesoftware.com/molecules =noreferrer> Molecules 应用程序,其中每个球体和圆柱体实际上是一个位于矩形内的光线跟踪冒名顶替者。对于每个像素,我计算虚拟对象上该点处的镜面反射和环境光照贡献,然后将其与在模型加载时计算的环境遮挡因子相结合。这远远超出了你可以用照明做的另一个极端,但你可以深入研究那里的代码或阅读关于其工作原理的小写意见

I do some more advanced per-pixel lighting in my open source Molecules application, where each sphere and cylinder is really a raytraced impostor residing within a rectangle. For each pixel, I calculate the specular and ambient lighting contribution at that point on the virtual object, and then combine that with an ambient occlusion factor calculated at the loading of the model. That's far on the other extreme of what you can do with lighting, but you can dig into the code there or read my little writeup on how this works.

这篇关于如何使用OpenGL ES 2.0应用光源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆