1. Visualize interpolation
- Open the main.cpp file.
- 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"
"void main()\n"
"{\n"
"gl_Position = model * vec4(pos, 1.0);\n"
"vColor = vec4(clamp(pos, 0.0, 1.0), 1.0);\n"
"}\n";
- Modify the fragment shader source code as follows:
static const GLchar fragmentShaderSource[] =
"#version 310 es\n"
"precision mediump float;\n"
"in vec4 vColor;\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vColor;\n"
"}\n";
Info
You used the clamp function to convert any negative value of pos to a value between 0.0 and 1.0.
2. Create a rotation animation
- Delete the following global variable declarations:
bool movingRight = true; float movingOffset = 0.0f; float movingMaxOffset = 0.5f; float movingStep = 0.015f;
- Add the following global variable declarations in their place:
/* code */ float currentAngle = 0.0f; float angleStep = 1.0f; /* code */
- Delete the following lines of code from the Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame function:
movingRight ? movingOffset += movingStep : movingOffset -= movingStep;
if (abs(movingOffset) >= movingMaxOffset) {
movingRight = !movingRight;
}
model = glm::translate(model, glm::vec3(movingOffset, 0.0f, 0.0f));
- Still in Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame, make the following changes:
/* code */ glUseProgram(program); currentAngle += angleStep; if (currentAngle >= 360.0f) currentAngle -= 360.0f; glm::mat4 model = glm::mat4(1.0f); model = glm::rotate(model, glm::radians(movingOffset), glm::vec3(0.0f, 1.0f, 0.0f)); /* code */
Warning
Note that you need to change both the angle and the axis of the rotation!
3. Convert the triangle into a pyramid
- Declare one more global variable:
/* code */ GLuint program, triangleVAO, triangleVBO, triangleIBO; /* code */
- Modify the createTriangle function as follows:
void createTriangle() {
GLuint indices[] = {
0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 1
};
GLfloat vertices[] = {
0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f
};
glGenVertexArrays(1, &triangleVAO);
glBindVertexArray(triangleVAO);
glGenBuffers(1, &triangleIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glGenBuffers(1, &triangleVBO);
glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
glBufferData(GL_ARRAY_BUFFER, 15 * sizeof(GL_FLOAT), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
return;
}
- Delete the following line of code from the Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame function:
glDrawArrays(GL_TRIANGLES, 0, 3);
- Still in Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame, make the following changes:
/* code */ 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)); glBindVertexArray(triangleVAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleIBO); glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindVertexArray(0); glUseProgram(0); /* code */
4. Enable depth
- Modify the Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnSurfaceCreated function as follows:
/* code */ glClearColor(0.0, 1.0, 0.0, 1.0); glEnable(GL_DEPTH_TEST); createProgram(); /* code */
- Modify the Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame function as follows:
/* code */
extern "C" JNIEXPORT void JNICALL Java_dev_anastasioscho_glestriangle_NativeLibrary_nOnDrawFrame(JNIEnv * env, jobject obj) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
/* code */
Leave a Reply