Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

3DGraphicsFoundation/INSU/SolarSystem

From ZeroWiki
Revision as of 05:22, 7 February 2021 by imported>Unknown
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

static LPCTSTR lpszAppName = "Solar System";
static HINSTANCE hInstance;

GLfloat whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };

float xRot = 0.0f;
float yRot = 0.0f;

inline void glRGB(int x, int y, int z)
{
	glColor3ub((GLubyte)x, (GLubyte)y, (GLubyte)z);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// 템플릿 //
void SetDCPixelFormat(HDC hDC)
{
	int nPixelFormat;

	static PIXELFORMATDESCRIPTOR pfd = {
		sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
		1,                                                              // Version of this structure    
		PFD_DRAW_TO_WINDOW |                    // Draw to Window (not to bitmap)
		PFD_SUPPORT_OPENGL |					// Support OpenGL calls in window
		PFD_DOUBLEBUFFER,                       // Double buffered
		PFD_TYPE_RGBA,                          // RGBA Color mode
		24,                                     // Want 24bit color 
		0,0,0,0,0,0,                            // Not used to select mode
		0,0,                                    // Not used to select mode
		0,0,0,0,0,                              // Not used to select mode
		32,                                     // Size of depth buffer
		0,                                      // Not used to select mode
		0,                                      // Not used to select mode
		PFD_MAIN_PLANE,                         // Draw in main plane
		0,                                      // Not used to select mode
		0,0,0 };                                // Not used to select mode

	// Choose a pixel format that best matches that described in pfd
	nPixelFormat = ChoosePixelFormat(hDC, &pfd);

	// Set the pixel format for the device context
	SetPixelFormat(hDC, nPixelFormat, &pfd);
}

void ChangeSize(GLsizei w, GLsizei h)
{
	if(h == 0)
		h = 1;

	// Set Viewport to window dimensions
    glViewport(0, 0, w, h);

	// Calculate aspect ratio of the window
	GLfloat fAspect = (GLfloat)w/(GLfloat)h;

	// Set the perspective coordinate system
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// field of view of 45 degrees, near and far planes 1.0 and 425
	gluPerspective(45.0f, fAspect, 1.0, 425.0);

	// Modelview matrix reset
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

// 템플릿 끝 //


void SetupRC()
{
	glEnable(GL_DEPTH_TEST);
	glFrontFace(GL_CCW);
	glEnable(GL_CULL_FACE);

	glEnable(GL_LIGHTING);

	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, sourceLight);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glEnable(GL_LIGHT0);

	glEnable(GL_COLOR_MATERIAL);

	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void RenderScene()
{
	static float fFar = -300.0f;
	static float fMoonRot = 0.0f;
	static float fVenusRot = 0.0f;
	static float fEarthRot = 0.0f;
	static float fMercuryRot = 0.0f;
	static float fMarsRot = 0.0f;
	static float fJupiterRot = 0.0f;
	static float fSaturnRot = 0.0f;
	static float fUranusRot = 0.0f;
	static float fNeptuneRot = 0.0f;
	static float fPlutoRot = 0.0f;

	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glMatrixMode(GL_MODELVIEW);

	glPushMatrix();


// 태양
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glTranslatef(0.0f, 0.0f, fFar);	
	glRGB(255,100,0);
	auxSolidSphere(30.0f);		

	glRotatef(xRot, 1.0f, 0.0f, 0.0f);
	glRotatef(yRot, 0.0f, 1.0f, 0.0f);

// 수성
	
	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glRotatef(fMercuryRot, 0.0f, 1.0f, 0.0f);
	glRGB(0,0,255);
	glTranslatef(50.0f, 0.0f, 0.0f);
	auxSolidSphere(2.0f);
	glPopMatrix();
	
// 금성

	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glRotatef(fVenusRot, 1.0f, 1.0f, 0.0f);		
	glRGB(200,100,255);
	glTranslatef(0.0f, 0.0f, -70.0f);	
	auxSolidSphere(5.0f);		
	glPopMatrix();

	// 지구
	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glRotatef(fEarthRot, 1.0f, 0.0f, 0.0f);
	glRGB(0,255,255);
	glTranslatef(0.0f, 0.0f, -90.0f);	
	auxSolidSphere(6.0f);	
	
// 달

	glRGB(200,200,200);
	glRotatef(fMoonRot, 1.0f, 0.0f, 0.0f);	
	glTranslatef(0.0f, 0.0f, -8.0f);		
	auxSolidSphere(1.0f);		

	fMoonRot += 10.0f;
	if(fMoonRot > 360.0f)
		fMoonRot = 0.0f;

	glPopMatrix();
	
// 화성

	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glRotatef(fMarsRot, -1.0f, 1.0f, 0.0f);
	glRGB(255,0,0);
	glTranslatef(0.0f, 0.0f, -110.0f);
	auxSolidSphere(4.0f);
	glPopMatrix();

// 목성
	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glRotatef(fJupiterRot, 1.0f, -1.0f, 0.0f);
	glRGB(255,100,0);
	glTranslatef(0.0f, 0.0f, -130.0f);
	auxSolidSphere(10.0f);
	glPopMatrix();

// 토성
	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glRotatef(fSaturnRot, 0.0f, 0.0f, 1.0f);
	glRGB(255,100,0);
	glTranslatef(160.0f, 0.0f, 0.0f);
	auxSolidSphere(9.0f);
	glPopMatrix();

// 천왕성
	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glRotatef(fUranusRot, 0.0f, 1.0f, 0.0f);
	glRGB(255,50,0);
	glTranslatef(190.0f, 0.0f, 0.0f);
	auxSolidSphere(8.0f);
	glPopMatrix();

// 해왕성
	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glRotatef(fNeptuneRot, 1.0f, 0.0f, 0.0f);
	glRGB(0,255,255);
	glTranslatef(0.0f, 0.0f, -220.0f);
	auxSolidSphere(7.0f);
	glPopMatrix();

// 명왕성
	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glRotatef(fPlutoRot, 1.0f, 1.0f, 0.0f);
	glRGB(255,255,255);
	glTranslatef(0.0f, 0.0f, -240.0f);
	auxSolidSphere(5.0f);
	glPopMatrix();

	fMercuryRot += 10.0f;
	if(fMercuryRot > 360.0f)
		fMercuryRot = 0.0f;

	fVenusRot += 9.0f;
	if(fVenusRot > 360.0f)
		fVenusRot = 0.0f;

	fEarthRot += 8.0f;
	if(fEarthRot > 360.0f)
		fEarthRot = 0.0f;

	fMarsRot += 6.0f;
	if(fMarsRot > 360.0f)
		fMarsRot = 0.0f;

	fJupiterRot += 8.0f;
	if(fJupiterRot > 360.0f)
		fJupiterRot = 0.0f;

	fSaturnRot += 9.0f;
	if(fSaturnRot > 360.0f)
		fSaturnRot = 0.0f;

	fUranusRot += 10.0f;
	if(fUranusRot > 360.0f)
		fUranusRot = 0.0f;

	fNeptuneRot += 9.0f;
	if(fNeptuneRot > 360.0f)
		fNeptuneRot = 0.0f;

	fPlutoRot += 8.0f;
	if(fPlutoRot > 360.0f)
		fPlutoRot = 0.0f;

	glPopMatrix();
	glFlush();
}
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine,	int nCmdShow)
{
	MSG                     msg; 
	WNDCLASS        wc;      
	HWND            hWnd;    

	hInstance = hInst;

	wc.style                        = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc          = (WNDPROC) WndProc;
	wc.cbClsExtra           = 0;
	wc.cbWndExtra           = 0;
	wc.hInstance            = hInstance;
	wc.hIcon                        = NULL;
	wc.hCursor                      = LoadCursor(NULL, IDC_ARROW);
	
	wc.hbrBackground        = NULL;         
	wc.lpszMenuName         = NULL;
	wc.lpszClassName        = lpszAppName;

	if(RegisterClass(&wc) == 0)
		return FALSE;

	hWnd = CreateWindow(
				lpszAppName,
				lpszAppName,
				WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
				50, 50,
				800, 600,
				NULL,
				NULL,
				hInstance,
				NULL);

	if(hWnd == NULL)
		return FALSE;

	ShowWindow(hWnd,SW_SHOW);
	UpdateWindow(hWnd);

	while( GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HGLRC hRC; 
	static HDC hDC;   

	switch (message)
	{
		case WM_CREATE:
			hDC = GetDC(hWnd);              
			SetDCPixelFormat(hDC);          
			hRC = wglCreateContext(hDC);
			wglMakeCurrent(hDC, hRC);
			SetupRC();
			SetTimer(hWnd, 101, 50, NULL);
			break;

		case WM_DESTROY:
			KillTimer(hWnd, 101);
			wglMakeCurrent(hDC,NULL);
			wglDeleteContext(hRC);
			PostQuitMessage(0);
			break;

		case WM_SIZE:
			ChangeSize(LOWORD(lParam), HIWORD(lParam));
			break;

		case WM_PAINT:
			RenderScene();
			SwapBuffers(hDC);
			ValidateRect(hWnd,NULL);
			break;

		case WM_TIMER:
			InvalidateRect(hWnd, NULL, FALSE);
			break;
		
		case WM_KEYDOWN:
			switch(wParam)
			{
			case VK_UP :
				xRot += 5.0f;
				break;
			case VK_DOWN :
				xRot -= 5.0f;
				break;
			case VK_LEFT :
				yRot += 5.0f;
				break;
			case VK_RIGHT:
				yRot -= 5.0f;
				break;
			}
			break;

	default:  
	    return (DefWindowProc(hWnd, message, wParam, lParam));
	}

    return (0L);
}

//오..졸라 신기하다. 이런 기능 오또케 알쏘? 우와~
//상민이형 temp 페이지에 써있던걸--; format python도 된다. 자바는 안되더라