• Skip to main content
  • Skip to primary sidebar

Anastasios Chondrogiannis

Software developer with a focus on iOS

Android OpenGL ES – Part 6: Camera View

March 12, 2020 by Anastasios Chondrogiannis Leave a Comment

1. Reset the pyramid position

  • Open the main.cpp file.
  • Modify the Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame function as follows:
/* code */

glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians(currentAngle), glm::vec3(0.0f, 1.0f, 0.0f));

/* code */

2. Remove the rotation animation

  • Delete the following line of code from Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame:
model = glm::rotate(model, glm::radians(currentAngle), glm::vec3(0.0f, 1.0f, 0.0f));

3. Modify the vertex shader

  • Modify the vertex shader source code as follows:
/* code */

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"
   "uniform mat4 view;\n"
   "void main()\n"
   "{\n"
   "gl_Position = projection * view * model * vec4(pos, 1.0);\n"
   "vColor = vec4(clamp(pos, 0.0, 1.0), 1.0);\n"
   "}\n";

/* code */

4. Set the uniform variable

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

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

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

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

return;

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

glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projectionMatrix));

glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 2.5f);
glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f);

glm::mat4 viewMatrix = glm::lookAt(cameraPosition, cameraTarget, worldUp);
glUniformMatrix4fv(uniformView, 1, GL_FALSE, glm::value_ptr(viewMatrix));

glBindVertexArray(triangleVAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleIBO);

/* code */

Note

cameraTarget is the position of the object the camera is looking at (in this case the pyramid).

worldUp is a vector pointing upwards at the world (for example this could be the sky in our world).

5. Move the camera around the pyramid

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

glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projectionMatrix));

float camX = sin(glm::radians(currentAngle)) * 2.5f;
float camZ = cos(glm::radians(currentAngle)) * 2.5f;

glm::vec3 cameraPosition = glm::vec3(camX, 0.0f, camZ);
glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);

/* code */

6. References

  • https://learnopengl.com/Getting-started/Camera

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