RecentChanges
  FrontPage
TitleIndex  |  RecentChanges
 

모자돌리기


수건돌리기라는 놀이는 육체적인 놀이인데 반해 모자돌리기는 정신적인 놀이이다. 모자를 가져다 놓고 머리속에서 마구 돌려보는것이다.. 일차원에서 돌리고... 이차원에서 돌리고.. 삼차원에서 돌리고.. 사차원에서 돌리고..

사람의 머리가 둥그렇기때문에 모자는 둥그런줄 알았는데.... 전혀 둥그런 구석이라고는 찾아볼 수도 없었다..

모자를 돌릴 다른 방법이 있을까요?

  • 모기의 모 짜를 90도 돌려보면 머기가 된다. 모기는 장난가들의 신성시하는 신비의 동물 개구리의 머기 이다. :) --유상윤

  • 90도 돌리니까.. ㅣ자가 되는데여? quho

  • "ㅣ" 그렇게 되나요?? 위에 보세요... 유상윤

  • 90도가 꼭 2차원상에서 돌리란 법이 있던가요? -.- 쫑아

  • 그럼 다른 차원상에서 돌리면 어떤식으로 보이나요?? 궁금합니다. ;; --유상윤

  • 모를 돌리면 머가 되기도 하고 |이 되기도 하고 ㅡ이 되기도 하는 법이지요. ㅏㅁ 도 되고 기타등등 많습니다. 90도라.. - 쫑아
  • 3차원이니 다양하잖아요... 여러가지 모양이.. --유상윤

  • 삼차원에서 모자 돌리기 코드입니다... 일단 돌려보고 계속 싸우는게 좋을듯 하네요...quho


#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <math.h>

#define PI 3.141592

static float 	l_vtx, l_vty, l_vtz; //translation for view
static float 	l_vrx, l_vry, l_vrz; //rotation for view


static void draw_mo() {
	GLfloat x, y , z;
	x = l_vtx;
	y = l_vty;
	z = l_vtz+5.0f;
	glPushAttrib(GL_CURRENT_BIT);
	glColor3f(0.0f,0.0f,1.0f);
	glBegin(GL_LINES);


	glVertex3f(x+-0.5f, y+0.5f,z+0.0f);
	glVertex3f(x+ 0.5f, y+0.5f,z+0.0f);

	glVertex3f(x+ 0.5f, y+0.5f,z+0.0f);
	glVertex3f(x+ 0.5f, y+0.0f,z+0.0f);

	glVertex3f(x+ 0.5f, y+0.0f,z+0.0f);
	glVertex3f(x+-0.5f, y+0.0f,z+0.0f);

	glVertex3f(x+-0.5f, y+0.0f,z+0.0f);
	glVertex3f(x+-0.5f, y+0.5f,z+0.0f);

	glVertex3f(x+ 0.0f, y+0.0f,z+0.0f);
	glVertex3f(x+ 0.0f, y+-0.3f,z+0.0f);

	glVertex3f(x+-0.6f,y+-0.3f,z+0.0f);
	glVertex3f(x+ 0.6f,y+-0.3f,z+0.0f);

    glEnd();
	glPopAttrib();
}



HGLRC SetUpOpenGL(HWND hwnd)
{
	static PIXELFORMATDESCRIPTOR pfd={
		sizeof(PIXELFORMATDESCRIPTOR),	//strcut size
		1,								//Version number
		PFD_DRAW_TO_WINDOW|				//draw to a window
		PFD_DOUBLEBUFFER|				//double buffer for animation
		PFD_SUPPORT_OPENGL,				//use OpenGL
		PFD_TYPE_RGBA,					//RGBA pixel values
		24,								//24-bit color
		0,0,0,							//RGB bits & shift sizes.
		0,0,0,							//Don't care about them
		0,0,							//No alpha buffer info
		0,0,0,0,0,						//No accumulation buffer
		32,								//32-bit depth buffer
		0,								//No stencil buffer
		0,								//No auxiliary buffers
		PFD_MAIN_PLANE,					//Layer type
		0,								//Reserved(must be 0)
		0,								//No layer mask
		0,								//No visible mask
		0								//No damage mask
	};

	int nMyPixelFormatID;
	HDC hdc;
	HGLRC hrc;

	hdc=GetDC(hwnd);
	nMyPixelFormatID=ChoosePixelFormat(hdc,&pfd);

	SetPixelFormat(hdc,nMyPixelFormatID,&pfd);

	hrc=wglCreateContext(hdc);
	ReleaseDC(hwnd,hdc);

	return hrc;
}

static void Draw(void){
          glDisable(GL_LIGHTING);
	  draw_mo();
          glEnable(GL_LIGHTING);
          glEnable(GL_LIGHT0);
}

static void View(void){
	//glTranslatef(l_vtx,l_vty,l_vtz);
	glTranslatef(0.0f,0.0f,-5.0f);
	glRotatef(l_vrx,1.0f,0.0f,0.0f);
	glRotatef(l_vry,0.0f,1.0f,0.0f);
	glRotatef(l_vrz,0.0f,0.0f,1.0f);
	
	
}

static void Light(void){
    GLfloat	afAmbient[] ={0.2f,0.2f,0.2f,1.0f},
    afDiffuse[] ={1.0f,1.0f,1.0f,1.0f},
    afSpecular[]={1.0f,1.0f,1.0f,1.0f},
    afPosition[]={2.0f,2.0f,2.0f,0.0f};
        
    glLightfv(GL_LIGHT0,GL_AMBIENT,afAmbient);
    glLightfv(GL_LIGHT0,GL_DIFFUSE,afDiffuse);
    glLightfv(GL_LIGHT0,GL_SPECULAR,afSpecular);
    glLightfv(GL_LIGHT0,GL_POSITION,afPosition);
        
    glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1.0f);
    glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.1f);
   	glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.0f);
        
    glEnable(GL_LIGHTING);
}

void DrawOpenGLScene()   {
	//start
	glEnable(GL_DEPTH_TEST);
	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
              
	//setting
	glMatrixMode(GL_MODELVIEW);
	glColor3f(0.8f,1.0f,0.8f);
	glLoadIdentity();
	
	//view and draw
	View();
	Light();
	Draw();
	
	//end
	glFlush();
}

static void CtrlDeg(float *pfX,float *pfY,float *pfZ){
		//-360..+360
	  while(*pfX>=360.0f) *pfX-=360.0f;
	  while(*pfX<=-360.0f)*pfX+=360.0f;
	  while(*pfZ>=360.0f) *pfZ-=360.0f;
	  while(*pfZ<=-360.0f)*pfZ+=360.0f;

		//-179.99..+180
	  if(*pfX>180.0)   *pfX=-360.0f+*pfX;
	  if(*pfX<=-180.0) *pfX=360.0f+*pfX;
	  if(*pfZ>180.0)   *pfZ=-360.0f+*pfZ;
	  if(*pfZ<=-180.0) *pfZ=360.0f+*pfZ;
}


static void KeyIn(	
		  HWND hwnd,
		  int iVirKey,     //virtual key
		  int iScanCode,   //scan code(8bit)
		  int iCount,      //repeat count(16bit)
		  BOOL bExtKey,    //extended key(indicated pad/arrow)
		  BOOL bAltKey,    //alt key or hangle key
		  BOOL bPrevDown,  //prev state      :down(1) up(0)
		  BOOL bCurDown    //transition state:down(0) up(1)
){
	BOOL bPaint=FALSE;
	GLfloat fObjDist=0.1f;
	GLfloat fViewDist=0.25f;
	GLfloat fDeg=5.0f;

    //rotation
    if(iVirKey=='U'){bPaint=TRUE;l_vrx-=fDeg;}
    if(iVirKey=='J'){bPaint=TRUE;l_vrx+=fDeg;}
    if(iVirKey=='I'){bPaint=TRUE;l_vrz-=fDeg;}
    if(iVirKey=='Y'){bPaint=TRUE;l_vrz+=fDeg;}
	if(iVirKey=='H'){bPaint=TRUE;l_vry-=fDeg;}
    if(iVirKey=='K'){bPaint=TRUE;l_vry+=fDeg;}
	CtrlDeg(&l_vrx, &l_vry, &l_vrz);

	
	// translation
	if(iVirKey=='D'){bPaint=TRUE;l_vtx-=fObjDist;}
    if(iVirKey=='G'){bPaint=TRUE;l_vtx+=fObjDist;}
    if(iVirKey=='R'){bPaint=TRUE;l_vtz-=fObjDist;}
    if(iVirKey=='F'){bPaint=TRUE;l_vtz+=fObjDist;}
	if(iVirKey=='E'){bPaint=TRUE;l_vty-=fObjDist;}
    if(iVirKey=='T'){bPaint=TRUE;l_vty+=fObjDist;}



	if(bPaint)
		InvalidateRect(hwnd,NULL,FALSE);
}


LONG WINAPI WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
  HDC hdc;
  static HGLRC hrc; //Note this is STATIC !!!
  PAINTSTRUCT ps;
  GLdouble gldAspect;
  GLsizei glnWidth,glnHeight;

  switch(msg){
	case WM_CREATE:
		//create a rendering context
		hrc=SetUpOpenGL(hwnd);

		//view
		l_vtx=0.0f;
		l_vty=0.0f;
		l_vtz=-5.0f;

		l_vrx=20.0f;
		l_vry=-30.0f;
		l_vrz=0.0f;
		return 0;
	case WM_SIZE:
		//redefine the viewing volume and viewport

		hdc=GetDC(hwnd);
		wglMakeCurrent(hdc,hrc);

		glnWidth =(GLsizei)LOWORD(lParam);
		glnHeight=(GLsizei)HIWORD(lParam);
		gldAspect=(GLdouble)glnWidth/(GLdouble)glnHeight;

		//projection matrix
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();

		//perspective-view matrix
		gluPerspective(
			30.0,           //Field-of-view angle
			gldAspect,      //Aspect ratio of view volume
			1.0,            //Distance to near clipping plane
			20.0            //Distance to far clipping plane
		);

		glViewport(0,0,glnWidth,glnHeight);
		wglMakeCurrent(NULL,NULL);
		ReleaseDC(hwnd,hdc);
		return 0;

    case WM_PAINT:
		hdc=BeginPaint(hwnd,&ps);
			wglMakeCurrent(hdc,hrc);
				DrawOpenGLScene();
				SwapBuffers(hdc);
			wglMakeCurrent(NULL,NULL);
		EndPaint(hwnd,&ps);
		return 0;

    case WM_DESTROY:
		wglDeleteContext(hrc);
		PostQuitMessage(0);

    case WM_KEYDOWN:
		KeyIn(
			hwnd,
			wParam,                           //virtual key
			HIWORD(lParam)&0xFF,              //scan code(8bit)
			LOWORD(lParam),                   //repeat count(16bit)
			((0x01000000&lParam)?TRUE:FALSE), //extended key(indicated pad/arrow)
			((0x20000000&lParam)?TRUE:FALSE), //alt key or hangle key
			((0x40000000&lParam)?TRUE:FALSE), //prev state      :down(1) up(0)
			((0x80000000&lParam)?FALSE:TRUE)  //transition state:down(0) up(1)
		);
		return 0;
	}

	return DefWindowProc(hwnd,msg,wParam,lParam);
}


int WINAPI WinMain(	HINSTANCE hInstance,
  					HINSTANCE hPrevInstance,
  					LPSTR lpszCmdLine,
  					int nCmdShow)	
{
	static char 	szAppName[]="OpenGL App";
	static char 	szTitle[]="rotate mo";
	WNDCLASS 		wc;
	MSG      		msg;
	HWND     		hwnd;

	wc.style=		  CS_HREDRAW|CS_VREDRAW;//Class style(s).
	wc.lpfnWndProc=   (WNDPROC)WndProc;     //Window Procedure
	wc.cbClsExtra=    0;                    //No per-class extra data.
	wc.cbWndExtra=    0;                    //No per-window extra data.
	wc.hInstance=     hInstance;            //Owner of this class
	wc.hIcon=         NULL;                 //Icon name
	wc.hCursor=  LoadCursor(NULL,IDC_ARROW);//Cursor
	wc.hbrBackground=(HBRUSH)(COLOR_WINDOW);//Default color
	wc.lpszMenuName=  NULL;                 //Menu from .RC
	wc.lpszClassName= szAppName;            //Name to register as
	RegisterClass(&wc);
  
	hwnd=CreateWindow(
			szAppName, 			//app name
			szTitle,        	//Text for window title bar
			WS_OVERLAPPEDWINDOW //Window style
			//|WS_CLIPCHILDREN
    		//| WS_CLIPSIBLINGS
    		,
    		CW_USEDEFAULT,
			0,
			CW_USEDEFAULT,
			0,
    		NULL,				//no parent window
    		NULL,				//Use the window class menu.
    		hInstance,			//This instance owns this window
    		NULL				//We don't use any extra data
	);
	if(!hwnd){return(0);}

	ShowWindow(hwnd,nCmdShow);        //Show the window
	UpdateWindow(hwnd);               //Sends WM_PAINT message

	while(GetMessage(&msg,NULL,0,0)){
		TranslateMessage(&msg);         //Translates messages
		DispatchMessage(&msg);          //then dispatches
	}

	return(msg.wParam);
}

어흑, 어려운 코드다. 무슨말일까... 저도
호호.. 윈도용 코드군요 -.- 궁금하면 컴파일! & 실행! - 쫑아





Powered by MoniWiki
xhtml1 | css2 | rss