You are on page 1of 6

/* * Contoh Win32 OpenGL program. * Animasi Bola Berputar pada sumbu x dan y */ #include <windows.h> #include <GL/glut.

h> /* Definisi dan Prototype Windows */ CHAR szAppName[]="Win OpenGL"; HWND ghWnd; HDC ghDC; HGLRC ghRC; #define SWAPBUFFERS SwapBuffers(ghDC) #define WIDTH #define HEIGHT 300 200

LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM); BOOL bSetupPixelFormat(HDC); /* Definisi dan Prototype OpenGL */ GLfloat latitude, longitude, latinc, longinc; GLdouble radius, z;

#define GLOBE 1 #define CYLINDER 2 #define CONE 3 GLvoid resize(GLsizei, GLsizei); GLvoid initializeGL(GLsizei, GLsizei); GLvoid drawScene(GLvoid); void polarView( GLdouble, GLdouble, GLdouble, GLdouble, GLdouble); static void initLights( void ) // Inisialisasi cahaya dan meterial { // definisi atau inisialisasi variable cahaya GLfloat light_ambient[] = {1.0, 1.0, 0.0, 1.0}; GLfloat light_diffuse[] = {0.8, 0.8, 0.8, 1.0}; GLfloat light_position[] = {1.0, 1.0, 2.0, 1.0}; // definisi atau inisialisasi variable material GLfloat mat_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat mat_shininess[] = {50.0}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // menghidupkan atau mengaktifkan cahaya // cahaya di set 0

// memanggil inisialisasi cahaya

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_POSITION,light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); // memanggil inisialisasi material glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASS wndclass; wndclass.style wndclass.lpfnWndProc wndclass.cbClsExtra wndclass.cbWndExtra wndclass.hInstance wndclass.hIcon wndclass.hCursor wndclass.hbrBackground wndclass.lpszMenuName wndclass.lpszClassName = = = = = = = = = = 0; (WNDPROC)MainWndProc; 0; 0; hInstance; LoadIcon (hInstance, szAppName); LoadCursor (NULL,IDC_ARROW); (HBRUSH)(COLOR_WINDOW+1); szAppName; szAppName;

if (!RegisterClass (&wndclass) ) return FALSE; ghWnd = CreateWindow (szAppName, "Animasi Bola", WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL); if (!ghWnd) return FALSE; ShowWindow (ghWnd, nCmdShow); UpdateWindow (ghWnd); while (1) { while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE) { if (GetMessage(&msg, NULL, 0, 0) ) { TranslateMessage(&msg); DispatchMessage(&msg); } else { return TRUE; } }

drawScene(); } } /* prosedur utama untuk window */ LONG WINAPI MainWndProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LONG lRet = 1; PAINTSTRUCT ps; RECT rect; switch (uMsg) { case WM_CREATE: ghDC = GetDC(hWnd); if (!bSetupPixelFormat(ghDC)) PostQuitMessage (0); ghRC = wglCreateContext(ghDC); wglMakeCurrent(ghDC, ghRC); GetClientRect(hWnd, &rect); initializeGL(rect.right, rect.bottom); break; case WM_PAINT: BeginPaint(hWnd, &ps); EndPaint(hWnd, &ps); break; case WM_SIZE: GetClientRect(hWnd, &rect); resize(rect.right, rect.bottom); break; case WM_CLOSE: if (ghRC) wglDeleteContext(ghRC); if (ghDC) ReleaseDC(hWnd, ghDC); ghRC = 0; ghDC = 0; DestroyWindow (hWnd); break; case WM_DESTROY: if (ghRC) wglDeleteContext(ghRC); if (ghDC) ReleaseDC(hWnd, ghDC); PostQuitMessage (0); break;

case WM_KEYDOWN: switch (wParam) { case VK_LEFT: longinc += 0.5F; break; case VK_RIGHT: longinc -= 0.5F; break; case VK_UP: latinc += 0.5F; break; case VK_DOWN: latinc -= 0.5F; break; case 'P': z += 0.1F; break; case 'L': z -= 0.1F; break; } default: lRet = DefWindowProc (hWnd, uMsg, wParam, lParam); break; } return lRet; } BOOL bSetupPixelFormat(HDC hdc) { PIXELFORMATDESCRIPTOR *ppfd; HPALETTE m_hPalette; int pixelformat; PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER, PFD_TYPE_RGBA,24,0, 0, 0, 0, 0, 0, 0,0, 0,0, 0, 0, 0,32,0,0,PFD_MAIN_PLANE,0,0, 0, 0 }; int i; struct { WORD VersionNumber; WORD NumberOfEntries; PALETTEENTRY PalEntryArr[256]; } MyPalette = { 0x300, 256}; pfd.cColorBits = GetDeviceCaps(ghDC,BITSPIXEL); ppfd = &pfd; pixelformat = ChoosePixelFormat(hdc, ppfd); SetPixelFormat(hdc, pixelformat, ppfd); DescribePixelFormat(hdc,pixelformat,sizeof(pfd),&pfd); if(pfd.dwFlags & PFD_NEED_PALETTE) { for(i=0;i<256;i++) { MyPalette.PalEntryArr[i].peRed=((i&7)<<5); MyPalette.PalEntryArr[i].peGreen=((i&0x38)*4); MyPalette.PalEntryArr[i].peBlue=(i&0xc0); }

m_hPalette = CreatePalette ((LOGPALETTE*) &MyPalette); SelectPalette(hdc,m_hPalette,0); RealizePalette(hdc); } return TRUE; } /* Kode OpenGL */ GLvoid resize( GLsizei width, GLsizei height ) { GLfloat aspect; glViewport( 0, 0, width, height ); aspect = (GLfloat) width / height; glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 45.0, aspect, 2.0, 7.0 ); glMatrixMode( GL_MODELVIEW ); } GLint ell; GLvoid createObjects() { GLUquadricObj *quadObj; glNewList(CONE, GL_COMPILE); glPushMatrix (); quadObj = gluNewQuadric (); gluQuadricDrawStyle (quadObj, GLU_FILL); gluQuadricNormals (quadObj, GLU_SMOOTH); //glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (GLfloat[]){2.0,0.2,5.0,1.0}); //gluSphere(quadObj, 2.0,50,50); //glPopMatrix (); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (GLfloat[]){0.0,0.0,1.0,1.0}); gluCylinder (quadObj, 0.3, 0.3, 0.6, 13, 2); // membuat sisi paralon glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (GLfloat[]){2.0,5.0,1.0,1.0}); gluQuadricOrientation(quadObj,GLU_INSIDE); gluDisk(quadObj,0.0,0.3,13,2); //buat bawah gambar paralon gluQuadricOrientation(quadObj,GLU_OUTSIDE); glTranslatef(0.0,0.0,0.6); // paralon gluDisk(quadObj,0.0,0.3,13,2); // membuat atas paralon glPopMatrix (); glEndList(); }

GLvoid initializeGL(GLsizei width, GLsizei height) { GLfloat maxObjectSize, aspect; GLdouble near_plane, far_plane; glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glMatrixMode( GL_PROJECTION ); aspect = (GLfloat) width / height; gluPerspective( 45.0, aspect, 2.0, 7.0 ); glMatrixMode( GL_MODELVIEW ); near_plane = 3.0; far_plane = 7.0;

maxObjectSize = 3.0F; radius = near_plane + maxObjectSize/2.0; latitude = 0.0F; longitude = 0.0F; latinc = 0.0F; longinc = 0.0F; createObjects(); initLights(); } void polarView(GLdouble radius, GLdouble z, GLdouble twist, GLdouble latitude, GLdouble longitude) { glTranslated(0.0, 0.0, -radius); glTranslated(0.0, 0.0, -z); glRotated(-twist, 0.0, 0.0, 1.0); glRotated(-latitude, 1.0, 0.0, 0.0); glRotated(longitude, 0.0, 0.0, 1.0); }

GLvoid drawScene(GLvoid) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); latitude += latinc; longitude += longinc; polarView( radius, z, 0, latitude, longitude ); glCallList(CONE); glTranslatef(0.8F, -0.65F, 1.0F); glRotatef(30.0F, 1.0F, 0.5F, 1.0F); glCallList(CYLINDER); glPopMatrix(); SWAPBUFFERS; }

You might also like