~cpp #include <windows.h> #include <gl\gl.h> #include <gl\glu.h> #include <gl\glaux.h> #include <stdio.h> #include <cmath> HGLRC hRC=NULL; HDC hDC=NULL; HWND hWnd = NULL; HINSTANCE hInstance; GLUquadricObj *obj; GLfloat xrot=0.0f; GLfloat yrot=0.0f; GLfloat o1_rot = 0.0f; GLfloat o2_rot1 = 0.0f; GLfloat o2_rot2 = 0.0f; //수성 GLfloat o3_rot = 0.0f; //금성 GLfloat o4_rot = 0.0f;//지구 GLfloat o5_rot = 0.0f;//화성 GLfloat o6_rot = 0.0f;//목성 GLfloat o7_rot = 0.0f;//토성 GLfloat o8_rot = 0.0f;//천왕성 GLfloat o9_rot = 0.0f;//해왕성 GLfloat o10_rot = 0.0f;//명왕성 GLfloat distance1 = 2.0f;//수성 GLfloat distance2 = 3.2f;//금성 GLfloat distance3 = 4.2f;//지구 GLfloat distance4 = 5.2f;//화성 GLfloat distance5 = 6.2f;//목성 GLfloat distance6 = 7.2f;//토성 GLfloat distance7 = 8.2f;//천왕성 GLfloat distance8 = 9.2f;//해왕성 GLfloat distance9 = 10.2f;//명왕성 float ambientLight[] = {0.5f,0.5f,0.5f,1.0f}; float diffuseLight[] = {0.25f,0.25f,0.25f,1.0f}; float lightPosition[] = {0.0f,0.0f,0.0f,1.0f}; float matAmbient[] = {1.0f,1.0f,1.0f,1.0f}; float matDiff[] = {1.0f,1.0f,1.0f,1.0f}; bool keys[256]; bool active=TRUE; bool fullscreen=TRUE; LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); GLvoid ReSizeGLScene(GLsizei width, GLsizei height) { if(height==0) height=1; glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); } int InitGL(GLvoid) { glClearColor(0.0f,0.0f,0.0f,0.0f); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glEnable(GL_LIGHTING); glMaterialfv(GL_FRONT,GL_AMBIENT,matAmbient); glMaterialfv(GL_FRONT,GL_DIFFUSE,matDiff); glLightfv(GL_LIGHT0,GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); glLightfv(GL_LIGHT0,GL_POSITION,lightPosition); glEnable(GL_LIGHT0); glClearDepth(1.0f); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); obj = gluNewQuadric(); gluQuadricNormals(obj, GLU_SMOOTH); gluQuadricOrientation(obj, GLU_OUTSIDE); gluQuadricTexture(obj,GL_FALSE); // glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); return TRUE; } void CorrectOfCoordinate(float distance,float x,float y, float z) { float absolute = float(sqrt(x*x+y*y+z*z)); float cosin = x/absolute; float sin = y/absolute; glTranslatef(-distance*cosin*cosin,-distance*sin*cosin,0.0f); } int DrawGLScene(GLvoid) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f,0.0f,-22.0f); glRotatef(yrot,1.0f,0.0f,0.0f); glRotatef(xrot,0.0f,1.0f,0.0f); glPushMatrix(); glRotatef(o1_rot,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.5f,0.0f,0.0f); gluSphere(obj,1.0f,24,24); gluQuadricDrawStyle(obj,GLU_LINE); glColor3f(1.0f,1.0f,1.0f); gluCylinder(obj,1.0f,1.0f,0.1,24,24); glPopMatrix(); glPushMatrix(); glRotatef(o2_rot1,0.0f,1.0f,0.0f); glTranslatef(distance1,0.0f,0.0f); glRotatef(o2_rot2,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.3f,0.5f,0.8f); gluSphere(obj,0.4f,12,12); glPopMatrix(); glPushMatrix(); CorrectOfCoordinate(distance2,1.0f,1.0f,0.0f); glRotatef(o3_rot,1.0f,1.0f,0.0f); glTranslatef(distance2,0,0.0f); glRotatef(o3_rot,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.2f,0.1f,0.4f); gluSphere(obj,0.2f,12,12); glPopMatrix(); glPushMatrix(); CorrectOfCoordinate(distance3,1.0f,1.0f,0.0f); glRotatef(o4_rot,0.2f,0.2f,0.0f); glTranslatef(distance3,0.0f,0.0f); glRotatef(o4_rot,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.3f,0.5f,0.8f); gluSphere(obj,0.4f,12,12); glPopMatrix(); glPushMatrix(); CorrectOfCoordinate(distance4,0.1f,1.2f,0.0f); glRotatef(o5_rot,0.1f,1.2f,0.0f); glTranslatef(distance4,0.0f,0.0f); glRotatef(o5_rot,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.2f,0.7f,0.7f); gluSphere(obj,0.4f,12,12); glPopMatrix(); glPushMatrix(); CorrectOfCoordinate(distance5,0.07f,0.06f,0.0f); glRotatef(o6_rot,0.07f,0.06f,0.0f); glTranslatef(distance5,0.0f,0.0f); glRotatef(o6_rot,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.7f,0.8f,0.4f); gluSphere(obj,0.4f,12,12); glPopMatrix(); glPushMatrix(); CorrectOfCoordinate(distance6,0.0f,1.0f,0.0f); glRotatef(o7_rot,0.0f,1.0f,0.0f); glTranslatef(distance6,0.0f,0.0f); glRotatef(o7_rot,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.0f,0.5f,0.2f); gluSphere(obj,0.4f,12,12); glPopMatrix(); glPushMatrix(); CorrectOfCoordinate(distance7,0.7f,1.0f,0.0f); glRotatef(o8_rot,0.7f,1.0f,0.0f); glTranslatef(distance7,0.0f,0.0f); glRotatef(o8_rot,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.0f,0.5f,0.8f); gluSphere(obj,0.4f,12,12); glPopMatrix(); glPushMatrix(); CorrectOfCoordinate(distance8,0.1f,1.0f,0.0f); glRotatef(o9_rot,0.1f,1.0f,0.0f); glTranslatef(distance8,0.0f,0.0f); glRotatef(o9_rot,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.7f,0.8f,1.0f); gluSphere(obj,0.9f,12,12); glPopMatrix(); glPushMatrix(); CorrectOfCoordinate(distance2,0.3f,1.0f,0.0f); glRotatef(o10_rot,0.3f,1.0f,0.0f); glTranslatef(distance9,0.0f,0.0f); glRotatef(o10_rot,0.0f,1.0f,0.0f); gluQuadricDrawStyle(obj,GLU_FILL); glColor3f(0.3f,0.5f,1.0f); gluSphere(obj,0.7f,12,12); glPopMatrix(); o1_rot+=2.5f; if(o1_rot>359.9f) o1_rot=0.0f; o2_rot1+=1.05f; if(o2_rot1>359.95f) o2_rot1=0.0f; o2_rot2+=4.0f; if(o2_rot2>358.0f) o2_rot2=0; o3_rot+=1.2f; o4_rot+=1.8f; o5_rot+=2.6f; o6_rot+=3.9f; o7_rot+=0.6f; o8_rot+=1.3f; o9_rot+=3.4f; o10_rot+=3.8f; return TRUE; } GLvoid KillGLWindow(GLvoid) { if(fullscreen) { ChangeDisplaySettings(NULL,0); ShowCursor(TRUE); } if(hRC) { if(!wglMakeCurrent(NULL,NULL)) { MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); } if(!wglDeleteContext(hRC)) { MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); } hRC = NULL; if(hDC && !ReleaseDC(hWnd,hDC)) { MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); hDC = NULL; } if(hWnd && !DestroyWindow(hWnd)) { MessageBox(NULL,"Could Not Release hWnd","SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); hWnd = NULL; } if(!UnregisterClass("OpenGL",hInstance)) { MessageBox(NULL,"Could Not Unregister Class","SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); hInstance = NULL; } } } BOOL CreateGLWindow(char *title, int width, int height, int bits, bool fullscreenflag) { GLuint PixelFormat; WNDCLASS wc; DWORD dwExStyle; DWORD dwStyle; RECT WindowRect; WindowRect.left=(long)0; WindowRect.right=(long)width; WindowRect.top=(long)0; WindowRect.bottom=(long)height; fullscreen = fullscreenflag; hInstance = GetModuleHandle(NULL); wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc=(WNDPROC)WndProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=hInstance; wc.hIcon = LoadIcon(NULL,IDI_WINLOGO); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hbrBackground=NULL; wc.lpszMenuName=NULL; wc.lpszClassName="OpenGL"; if(!RegisterClass(&wc)) { MessageBox(NULL,"Failed To Register The Window Class." ,"ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } if(fullscreen) { DEVMODE dmScreenSettings; memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); dmScreenSettings.dmPelsWidth = width; dmScreenSettings.dmPelsHeight = height; dmScreenSettings.dmBitsPerPel=bits; dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) { if(MessageBox(NULL,"The Requested FUllscreen Mode is Not Supported By\n Your video Card. Use Windowed Mode Instead?","NeHeGl",MB_YESNO| MB_ICONEXCLAMATION)==IDYES){ fullscreen=FALSE; } else { MessageBox(NULL,"Program Will Now Close","ERROR",MB_OK|MB_ICONSTOP); return FALSE; } } } if(fullscreen) { dwExStyle=WS_EX_APPWINDOW; dwStyle=WS_POPUP; ShowCursor(FALSE); } else { dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; dwStyle=WS_OVERLAPPEDWINDOW; } AdjustWindowRectEx(&WindowRect,dwStyle,FALSE,dwExStyle); if(!(hWnd=CreateWindowEx(dwExStyle, "OpenGL", title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle, 0,0, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, NULL, NULL, hInstance, NULL))) { KillGLWindow(); MessageBox(NULL,"Window Creation Error", "Error",MB_OK|MB_ICONEXCLAMATION); return FALSE; } static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, bits, 0,0,0,0,0,0, 0, 0, 0, 0,0,0,0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; if(!(hDC=GetDC(hWnd))) { KillGLWindow(); MessageBox(NULL,"Can't Creat A GL Device Context","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } if(!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) { KillGLWindow(); MessageBox(NULL,"Can't Find A Suitable PixelFormat" ,"ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } if(!SetPixelFormat(hDC,PixelFormat,&pfd)) { KillGLWindow(); MessageBox(NULL,"Can't Set The PixelFormat" ,"ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } if(!(hRC=wglCreateContext(hDC))) { KillGLWindow(); MessageBox(NULL,"Cant't Create A GL Rendering Context" ,"ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } if(!wglMakeCurrent(hDC,hRC)) { KillGLWindow(); MessageBox(NULL,"Can't Activate The GL Rendering Context" ,"ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } ShowWindow(hWnd,SW_SHOW); SetForegroundWindow(hWnd); SetFocus(hWnd); ReSizeGLScene(width,height); if(!InitGL()) { KillGLWindow(); MessageBox(NULL,"Initialization Failed" ,"ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_ACTIVATE: { if(!HIWORD(wParam)) active=TRUE; else active = FALSE; return 0; } case WM_SYSCOMMAND: { switch(wParam) { case SC_SCREENSAVE: case SC_MONITORPOWER: return 0; } break; } case WM_CLOSE: { PostQuitMessage(0); return 0; } case WM_KEYDOWN: { keys[wParam] = TRUE; return 0; } case WM_KEYUP: { keys[wParam] = FALSE; return 0; } case WM_SIZE: { ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); return 0; } } return DefWindowProc(hWnd,uMsg,wParam,lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; BOOL done=FALSE; if(MessageBox(NULL,"Would you like to run in FullScreen mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO) { fullscreen=FALSE; } if(!CreateGLWindow("namsang's OpenGL FrameWork",604,480,16,fullscreen)) { return 0; } while(!done) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message==WM_QUIT) done=TRUE; else { TranslateMessage(&msg); DispatchMessage(&msg); } } if((active && !DrawGLScene()) || keys[VK_ESCAPE]) done=TRUE; else SwapBuffers(hDC); if(keys[VK_F1]) { keys[VK_F1]=FALSE; KillGLWindow(); fullscreen=!fullscreen; if(!CreateGLWindow("namsang's OPenGL FrameWork" ,640,480,16,fullscreen)) { return 0; } } if(keys[VK_LEFT]) yrot+=0.2f; if(keys[VK_RIGHT]) yrot-=0.2f; if(keys[VK_UP]) xrot+=0.2f; if(keys[VK_DOWN]) xrot-=0.2f; if(keys[VK_RETURN]) { xrot=0; yrot=0; } } KillGLWindow(); return (msg.wParam); }