• Skip to main content
  • Skip to primary sidebar

Anastasios Chondrogiannis

Software developer with a focus on iOS

Android OpenGL ES – Part 8: Ambient Lighting

March 24, 2020 by Anastasios Chondrogiannis Leave a Comment

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";

Filed Under: Android Development

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Let’s connect!

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Recent Posts

  • Android OpenGL ES – Part 10: Specular lighting
  • Android OpenGL ES – Part 9: Diffuse lighting
  • Android OpenGL ES – Part 8: Ambient Lighting
  • Android OpenGL ES – Part 7: Textures
  • Android OpenGL ES – Part 6: Camera View

Archives

  • March 2020
  • February 2020
  • November 2019
  • October 2019
  • July 2019

Categories

  • Android Development
  • Machine Learning

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in