1. Tutorial 10.
Vertex Animation
Banyak obyek sebetulnya bukan benda rigid. Sebagai contoh adalah bendera atau selembar kertas.
Program 14 memberi ilustrasi tentang bagaimana membuat suatu bendera berkibar. Program 14
melakukan ilusi berkibar dengan merubah posisi relatif
suatu vertex terhadap koordinat bendanya. Cara
yang lebih canggih mencakup proses pemodelan dinamika benderanya.
TUGAS: Terangkan bagaimana cara bekerjanya animasi bendera tersebut.
// Vertex Animation
// Program ini menampilkan animasi pada tiap vertex
// Image Loadernya menggunakan library tambahan yaitu SOIL (Simple
OpenGL Image Library)
#include <math.h> // Math
Library Header File
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <GL/glut.h>
#include <SOIL/SOIL.h>
float points[45][45][3]; // The Array for
the points on the grid of our "Wave"
int wiggle_count = 0; // Counter used
to control how fast flag waves
GLfloat xRot; // X rotation
GLfloat yRot; // y rotation
GLfloat zRot; // z rotation
GLfloat hold; // Temporarily
holds a floating value
GLuint texture; // Storage for
one texture
void init(void);
GLuint LoadGLTextures(const char* filename);
void myTimeOut(int id);
void myKeyboard(unsigned char key, int x, int y);
void resize(int width, int height);
2. void renderScene(void);
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Vertex Animation");
glutDisplayFunc(renderScene);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
glutTimerFunc(100, myTimeOut, 0);
init();
glutMainLoop();
return 0;
}
GLuint LoadGLTextures(const char* filename)
// Load Bitmaps And Convert To Textures
{
GLuint tex_2d;
/* load an image file directly as a new OpenGL texture */
tex_2d = SOIL_load_OGL_texture(filename,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS |
SOIL_FLAG_INVERT_Y |
SOIL_FLAG_NTSC_SAFE_RGB |
SOIL_FLAG_COMPRESS_TO_DXT);
/* check for an error during the load process */
if(tex_2d == 0)
{
printf( "SOIL loading error: '%s'n", SOIL_last_result() );
}
glBindTexture(GL_TEXTURE_2D, tex_2d);
// Typical Texture Generation Using Data From The Bitmap
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
return tex_2d; // Return
3. Success
}
void init(void)
{
texture = LoadGLTextures("Pirateflag.png"); // Load
the texture image
glEnable(GL_TEXTURE_2D); // Enable
Texture Mapping
glShadeModel(GL_SMOOTH); // Enable
Smooth Shading
glClearColor(1.0f, 0.0f, 0.0f, 0.5f); // Red
Background (Dangerous!!!!)
glClearDepth(1.0f); // Depth
Buffer Setup
glEnable(GL_DEPTH_TEST); //
Enables Depth Testing
glDepthFunc(GL_LEQUAL); // Type
of depth testing to do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really
nice perspective calculation
glPolygonMode(GL_BACK, GL_FILL); // Back
face is solid
glPolygonMode(GL_FRONT, GL_LINE); // Front
face is made of line
for(int x = 0; x < 45; ++x)
{
for(int y = 0; y < 45; ++y)
{
points[x][y][0] = (float)((x/5.0f) - 4.5f);
points[x][y][1] = (float)((y/5.0f) - 4.5f);
points[x][y][2] = (float)(sin((((x / 5.0f) * 40.0f) /
360.0f) * M_PI * 2.0f));
}
}
}
void myKeyboard(unsigned char key, int x, int y)
// Keyboard event
{
switch(key)
{
case 27:
// Exit if 'Esc' key is pressed
exit(0);
break;
4. }
}
void myTimeOut(int id)
{
glutPostRedisplay();
// Request redisplay
glutTimerFunc(100, myTimeOut, 0);
// request next timer event
}
void renderScene(void)
// Here where we do all the drawing
{
int x, y;
float float_x, float_y, float_xb, float_yb;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Clear the screen and depth buffer
glLoadIdentity();
// Reset the view
glTranslatef(0.0f, 0.0f, -12.0f);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glRotatef(zRot, 0.0f, 0.0f, 1.0f);
// glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
for(x = 0; x < 44; ++x)
{
for(y = 0; y < 44; ++y)
{
float_x = float(x)/44.0f;
float_y = float(y)/44.0f;
float_xb = float(x+1)/44.0f;
float_yb = float(y+1)/ 44.0f;
glTexCoord2f(float_x, float_y);
glVertex3f(points[x][y][0], points[x][y][1],
points[x][y][2]);
glTexCoord2f(float_x, float_yb);
glVertex3f(points[x][y+1][0], points[x][y+1][1],
points[x][y+1][2]);
glTexCoord2f(float_xb, float_yb);
glVertex3f(points[x+1][y+1][0], points[x+1][y+1][1],