You are on page 1of 3

// Vertex Animation // Program ini menampilkan animasi pada tiap vertex // Image Loadernya menggunakan library tambahan yaitu

SOIL (SimpleOpenGL Image L ibrary) #include <math.h> // MathLibrary Header File #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <GL/glut.h> #include <SOIL.h> float points[45][45][3]; // The Array forthe points on the grid of our "Wave" int wiggle_count = 0; // Counter usedto control how fast flag waves GLfloat xRot; // X rotation GLfloat yRot; // y rotation GLfloat zRot; // z rotation GLfloat hold; // Temporarilyholds a floating value GLuint texture; // Storage forone 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); 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; // ReturnSuccess } void init(void) { texture = LoadGLTextures("6.discus1.jpg"); // Loadthe texture image glEnable(GL_TEXTURE_2D); // EnableTexture Mapping glShadeModel(GL_SMOOTH); // EnableSmooth Shading glClearColor(1.0f, 0.0f, 0.0f, 0.5f); // RedBackground (Dangerous!!!!) glClearDepth(1.0f); // DepthBuffer Setup glEnable(GL_DEPTH_TEST); //Enables Depth Testing glDepthFunc(GL_LEQUAL); // Typeof depth testing to do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Reallynice perspective cal culation glPolygonMode(GL_BACK, GL_FILL); // Backface is solid glPolygonMode(GL_FRONT, GL_LINE); // Frontface 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; } } 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], points[x+1][y+1][2]); glTexCoord2f(float_xb, float_y); glVertex3f(points[x+1][y][0], points[x+1][y][1], points[x+1][y][2]); } } glEnd(); if(wiggle_count == 1) { for(y = 0; y < 45; ++y) { hold = points[0][y][2]; for(x = 0; x < 44; ++x) { points[x][y][2] = points[x+1][y][2]; } points[44][y][2] = hold; } wiggle_count = 0; } wiggle_count++; xRot = 0.3f; yRot = 0.2f; zRot = 0.4f; glFlush(); glutSwapBuffers(); } // Resize and initialize window void resize(int width, int height) { glViewport(0, 0, width, height); // Reset current viewport glMatrixMode(GL_PROJECTION); // Select projection Matrix glLoadIdentity(); // Reset projection Matrix gluPerspective(45.0, (GLdouble)width / (GLdouble)height, 1.0, 300.0); // Calculate aspect ratio of the window glMatrixMode(GL_MODELVIEW); // Select modelview matrix glLoadIdentity(); // Reset modelview matrix }

You might also like