1. Add a directional light
1.1. Modify the fragment shader
- Open the main.cpp file.
- Modify the fragment shader source code as follows:
static const GLchar fragmentShaderSource[] = "#version 310 es\n" "precision mediump float;\n" "in vec4 vColor;\n" "in vec2 vTexCoords;\n" "out vec4 color;\n" "struct DirectionalLight\n" "{\n" "vec3 color;\n" "float intensity;\n" "};\n" "uniform sampler2D textureSampler;\n" "uniform DirectionalLight directionalLight;\n" "void main()\n" "{\n" "color = texture(textureSampler, vTexCoords);\n" "}\n";
Note
Directional light also has a direction. However, the direction is not used in ambient lighting. You will add it when you add diffuse lighting.
1.2. Set the uniform variables
- Declare the following global variables:
/* code */ GLint uniformModel, uniformProjection, uniformView; GLint uniformLightColor, uniformLightIntensity; glm::mat4 projectionMatrix; /* code */
- Modify the createProgram function as follows:
/* code */ uniformView = glGetUniformLocation(program, "view"); uniformLightColor = glGetUniformLocation(program, "directionalLight.color"); uniformLightIntensity = glGetUniformLocation(program, "directionalLight.intensity"); return; /* code */
- Modify the Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame function as follows:
/* code */ glUseProgram(program); glUniform3f(uniformLightColor, 1.0f, 1.0f, 1.0f); glUniform1f(uniformLightIntensity, 1.0f); currentAngle += angleStep; /* code */
2. Apply ambient lighting
- Modify the fragment shader source code as follows:
static const GLchar fragmentShaderSource[] = "#version 310 es\n" "precision mediump float;\n" "in vec4 vColor;\n" "in vec2 vTexCoords;\n" "out vec4 color;\n" "struct DirectionalLight\n" "{\n" "vec3 color;\n" "float intensity;\n" "};\n" "uniform sampler2D textureSampler;\n" "uniform DirectionalLight directionalLight;\n" "void main()\n" "{\n" "vec4 ambientColor = vec4(directionalLight.color, 1.0f) * directionalLight.intensity;\n" "color = texture(textureSampler, vTexCoords) * ambientColor;\n" "}\n";
Leave a Reply