directory

  1. Practice (ambient light, diffuse light, specular light)
  2. data
  3. harvest

In the last article we learned to practice the basic concepts of care foundation and cube implementation. If you are not clear, you can look back first. In this article we come to the specific practice of light foundation

First, basic practice of lighting

Raw Cube effect

1.1 Ambient Light

Uniform vec3 lightColor; void main() { ... // Float ambientStrength = 0.3; ambient = ambientStrength * lightColor; . } // The source shader varying vec3 is available. void main() { vec4 color = texture2D(uTexture, v_texCoord); vec3 finalColor = ambient * vec3(color); Gl_FragColor = min (vec4 (finalColor, color. A), vec4 (1.0)); }Copy the code

Pass lightcolor into Render

GLES20. GlEnableVertexAttribArray (ulightcolorLoc) / / ambient light, xyz coordinate values GLES20. GlUniform3f (ulightcolorLoc, 1.0 f, f 1.0, 1.0 f)Copy the code

After adding ambient light, the effect is as follows

1.2 Diffuse Lighting

// Vertex normal vector attribute vec3 aNormal; Uniform mat4 uModelMatrix; Uniform vec3 lightPos; void main() { ... // Float diffuseStrength = 0.8; UnitNormal = normalize(uModelMatrix * vec4(aNormal, 1.0)); Vec3 lightDir = normalize(lightpos-fragpos); // lightDir = normalize(lightpos-fragpos); Float diff = Max (dot(unitNormal, lightDir), 0.0); diffuse = diffuseStrength * diff * lightColor; . }Copy the code

Render modified as follows

/ / set each vertex normal vector GLES20. GlEnableVertexAttribArray (aNormalCoorLoc) cubeLight.vertexArrayBuffer.position(CubeLight.POSITION_COMPONENT_COUNT+CubeLight.POSITION_TEXTURE_COUNT) GLES20.glVertexAttribPointer(aNormalCoorLoc,CubeLight.POSITION_NORMAL_COUNT,GLES20.GL_FLOAT,false,CubeLight.STRIDE, CubeLight. VertexArrayBuffer) / / setting GLES20 light glEnableVertexAttribArray (ulightPosLoc) GLES20. GlUniform3f (ulightPosLoc, 3.0 f, f 0.0, 1.0 f)Copy the code

Normal vectors are added to vertex data based on the previous article

Val vertexData = floatArrayOf(//position //texture coord //normal -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, F f 0.5, 0.5, 0.5 f to 1.0 f to 0.0 f to 0.0 f to 0.0 f to 1.0 f to 0.5 f to 0.5 f to 0.5 f to 1.0 f to 1.0 f to 0.0 f to 0.0 f to 1.0 f to 0.5 f to 0.5 f to 0.5 f, F 1.0, 1.0 f, f 0.0, 0.0, f - 1.0 f to 0.5 f, 0.5 f to 0.5 f, f 0.0, 1.0 f, f 0.0, 0.0, f - 1.0 f to 0.5 f, - 0.5 f to 0.5 f, f 0.0, 0.0, f 0.0 f, f, 0.0-1.0 f to 0.5 f to 0.5 f to 0.5 f to 0.0 f to 0.0 f to 0.0 f to 0.0 f to 1.0 f to 0.5 f to 0.5 f to 0.5 f to 1.0 f to 0.0 f to 0.0 f to 0.0 f to 1.0 f, 1.0 0.5 0.5 0.5 f, f, f, f, f 1.0, 0.0 f, f 0.0, 1.0 f, f 0.5, 0.5 f, f 0.5, 1.0 f, f 1.0, 0.0 f, f 0.0, 1.0 f to 0.5 f, f 0.5, 0.5, f 0.0 f, f, 1.0 to 0.0 f, f 0.0, 1.0, f - 0.5 f to 0.5 f, f 0.5, 0.0 f, f 0.0, 0.0 f, f 0.0, 1.0 f to 0.5 f, f 0.5, 0.5 f, f 1.0, 0.0 f to 1.0 f, 0.0 f, f, 0.0-0.5 f, 0.5 f to 0.5 f, f 1.0, 1.0 f to 1.0 f, f 0.0, 0.0, f - 0.5 f to 0.5 f, 0.5 f, f, 0.0 1.0 f to 1.0 f, f 0.0, 0.0, f 0.5 f to 0.5 f to 0.5 f to 0.0 f to 1.0 f to 1.0 f to 0.0 f to 0.0 f to 0.5 f to 0.5 f to 0.5 f to 0.0 f to 0.0 f to 1.0 f to 0.0 f to 0.0 f to 0.5 f to 0.5 f, 0.5 f, f, 1.0 0.0 f to 1.0 f, f 0.0, 0.0 f, f 0.5, 0.5 f, f 0.5, 1.0 f, f 0.0, 1.0 f, f 0.0, 0.0 f, f 0.5, 0.5 f to 0.5 f, f 1.0, 1.0, f F 1.0, 0.0 f, f 0.0, 0.5, f - 0.5 f to 0.5 f, f 0.0, 1.0 f, f 1.0, 0.0 f, f 0.0, 0.5, f - 0.5 f to 0.5 f, f 0.0, 1.0, f 1.0 f, f 0.0, 0.0, f 0.5 f to 0.5 f, f, 0.5 0.0 f, f 0.0, 1.0 f, f 0.0, 0.0 f, f 0.5, 0.5 f, f 0.5, 1.0 f, f 0.0, 1.0 f, f 0.0, 0.0, f - 0.5 f to 0.5 f, 0.5 f, 0.0 f, f, 1.0 0.0 f to 1.0 f, f 0.0, 0.5, f - 0.5 f to 0.5 f, f 1.0, 1.0, f 0.0 f to 1.0 f, f 0.0, 0.5 f to 0.5 f, f 0.5, 1.0 f, f 0.0, 0.0, f 1.0 f, f, 0.0 0.5 f to 0.5 f, f 0.5, 1.0 f, f 0.0, 0.0 f to 1.0 f, f 0.0, - 0.5 f to 0.5 f, f 0.5, 0.0 f, f 0.0, 0.0 f to 1.0 f, 0.0 f, 0.5 f to 0.5 f to 0.5 f to 0.0 f to 1.0 f to 0.0 f to 1.0 f to 0.0 f to 0.5 f to 0.5 f to 0.5 f to 0.0 f to 1.0 f to 0.0 f to 1.0 f to 0.0 f to 0.5 f to 0.5 f, 0.5 f, f 1.0, 1.0 f, f 0.0, 1.0 f, f 0.0, 0.5 f, f 0.5, 0.5 f, f 1.0, 0.0 f, f 0.0, 1.0 f, f 0.0, 0.5 f, f 0.5, 0.5 f, f 1.0, 0.0, f 0.0 f, f, 1.0 0.0 f to 0.5 f, 0.5 f, f, 0.5 0.0 f, f 0.0, 0.0 f, f 1.0, 0.0 f to 0.5 f, 0.5 f to 0.5 f, f 0.0, 1.0 f, f 0.0, 0.0 1.0 f, f)Copy the code

The effect of ambient light + diffuse light is as follows

We set the light source at positive 3 of the x axis and positive 1 of the Y axis, so the plane on the right side of the cube will have light effect, while the other sides will have little effect.

1.3 Specular Lighting

Float specularStrength = 0.9; float specularStrength = 0.9; Vec3 viewDir = normalize(viewpos-fragpos); vec3 viewDir = normalize(viewpos-fragpos); // Call reflectDir vec3 reflectDir = reflectDir (-lightdir, unitNormal); Float spec = POw (Max (dot(unitNormal, reflectDir), 0.0), 16.0); float spec = pow(Max (dot(unitNormal, reflectDir), 0.0), 16.0); specular = specularStrength * spec * lightColor; . / / set the light point GLES20 glEnableVertexAttribArray (ulightPosLoc) GLES20. GlUniform3f (ulightPosLoc, 3.0 f, f 0.0, 2.0 f) / / set the specular reflection point position GLES20. GlEnableVertexAttribArray (uViewPosLoc) GLES20. GlUniform3f (uViewPosLoc, 1.0 f, f 0.0, 3.0 f)Copy the code

The effect of ambient light + specular reflected light is as follows

Finally, let’s look at the effect of all three lights on the cube

The full code has been uploaded to github github.com/ayyb1988/me…

Second, the data

OpenGL Programming Guide

NDK OpenGL ES 3.0 development (9) : OpenGL ES case 03: COREANIMATION drawing cube + rotation

Three, harvest

On the basis of the previous article, practice of ambient light, diffuse reflection, specular reflection

Thank you for reading the next article we learn practical fluid, welcome to pay attention to the public account “audio and video development journey”, learn and grow together.

Welcome to communicate