• Skip to main content
  • Skip to primary sidebar

Anastasios Chondrogiannis

Software developer with a focus on iOS

Android OpenGL ES – Part 5: Perspective Projection

March 10, 2020 by Anastasios Chondrogiannis Leave a Comment

1. Create the transformation matrix

  • Open the main.cpp file.
  • Declare the following global variable:
/* code */

GLuint program, triangleVAO, triangleVBO, triangleIBO;
GLint uniformModel;
glm::mat4 projectionMatrix;

float currentAngle = 0.0f;

/* code */
  • Modify the Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnSurfaceChanged function as follows:
extern "C" JNIEXPORT void JNICALL Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnSurfaceChanged(JNIEnv * env, jobject obj, jint width, jint height) {
   glViewport(0, 0, width, height);

   projectionMatrix = glm::perspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);

   return;
}

2. Modify the vertex shader

  • Modify the vertex shader source code as follows:
static const GLchar vertexShaderSource[] =
   "#version 310 es\n"
   "layout (location = 0) in vec3 pos;\n"
   "out vec4 vColor;\n"
   "uniform mat4 model;\n"
   "uniform mat4 projection;\n"
   "void main()\n"
   "{\n"
   "gl_Position = projection * model * vec4(pos, 1.0);\n"
   "vColor = vec4(clamp(pos, 0.0, 1.0), 1.0);\n"
   "}\n";

3. Set the uniform variable

  • Declare the following global variable:
/* code */

GLuint program, triangleVAO, triangleVBO, triangleIBO;
GLint uniformModel, uniformProjection;
glm::mat4 projectionMatrix;

/* code */
  • Modify the createProgram function as follows:
/* code */

uniformModel = glGetUniformLocation(program, "model");
uniformProjection = glGetUniformLocation(program, "projection");

return;

/* code */
  • Modify the Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame as follows:
/* code */

glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 0.0f, -2.5f));
model = glm::rotate(model, glm::radians(currentAngle), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::scale(model, glm::vec3(0.4f, 0.4f, 0.4f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projectionMatrix));

/* code */

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