我想尝试一下OpenGL ES 2.0编程指南中的照明示例.在着色器中,他们制作了两个结构.
struct directional_light
{
vec3 direction; // normalized light direction in eye space
vec3 halfplane; // normalized half-plane vector
vec4 ambient_color;
vec4 diffuse_color;
vec4 specular_color;
};
struct material_properties
{
vec4 ambient_color;
vec4 diffuse_color;
vec4 specular_color;
float specular_exponent;
};
Run Code Online (Sandbox Code Playgroud)
他们还根据这些结构制作了两件制服.
uniform material_properties u_material_properties;
uniform directional_light u_directional_light;
Run Code Online (Sandbox Code Playgroud)
问题是,我不知道如何将自己的结构传递给实际的着色器.
我想在我的主代码中创建相同的结构,并将对象传递到着色器.这怎么可能?
关心尼克拉斯
我正在关注OpenGL ES 2.0的教程,并将它与我发现的GLSL照明教程相结合,使用developer.apple.com上的一个方便的犹他茶壶.
经过大量的摆弄和实验,我在屏幕上适度地绘制了茶壶,使用照明教程中的"香椿阴影"旋转所有三个轴.由于我简单地将整个顶点列表绘制为三角形条带,因此几何体中存在一些毛刺(如果你在teapot.h文件中看到嵌入了'-1',我应该开始新的三角形条带,但这是只测试数据,与我的问题无关).
我真的很困惑的是如何在场景中定位灯光.在我的Objective-C代码中,我有一个包含{0,1,0} 的float 3向量,并将其传递到着色器中,然后计算光的强度.
为什么灯光也会在场景中移动?我的意思是光线的作用就好像是用一根看不见的棍子贴在茶壶上,无论茶壶面向什么方向,它总是指向它的同一侧.
这是顶点着色器
attribute vec4 Position;
attribute vec4 SourceColor;
attribute vec3 Normal;
uniform mat4 Projection;
uniform mat4 Modelview;
varying vec3 normal;
void main(void) {
normal = Normal;
gl_Position = Projection * Modelview * Position;
}
Run Code Online (Sandbox Code Playgroud)
'Position'由Obj-C代码设置并且是对象的顶点,'Normal'是来自顶点数组(VBO)的法线列表,'Projection'和'Modelview'的计算如下:
(CC3GLMatrix来自Cocos3D库,在上面链接的GLES教程中提到)
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:1 andFar:100];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
[modelView …Run Code Online (Sandbox Code Playgroud) 是否可以在不声明曲面法线的情况下声明对象上的光照?
我愿意做的步骤是:
1. enable lighting
2. define surface material
3. define lighting position
4. define lighting parms : ambient....
Run Code Online (Sandbox Code Playgroud)
这还不够,还是我必须放在哪里呢?