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 */
Leave a Reply