⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wgl应用.cpp

📁 经典 C++代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		glDisable(GL_FOG);
	}

	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//update transformations
	if(mode==MoveObject)
	{
		glPushMatrix();
		glLoadIdentity();
		glRotatef(angle,axis[0],axis[1],axis[2]);
		glMultMatrixf((GLfloat*)objectXform);
		glGetFloatv(GL_MODELVIEW_MATRIX,(GLfloat*)objectXform);
		glPopMatrix();
	}

	if(textureEnabled)glEnable(GL_TEXTURE_2D);
	if(drawOutlines)glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
	#if defined(GL_SGI_cull_vertex)
		if(useVertexCull)
			glEnable(GL_CULL_VERTEX_SGI);
	#endif
	if(useFaceCull)
		glEnable(GL_CULL_FACE);
	if(useLighting)
		glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);

	glPushMatrix();
	glTranslatef(xOffset,yOffset,0.0F);
	glMultMatrixf((GLfloat*)objectXform);

	(*drawObject[objectIndex])();

	glPopMatrix();

	glDisable(GL_TEXTURE_2D);
	glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
	#if defined(GL_SGI_cull_vertex)
	glDisable(GL_CULL_VERTEX_SGI);
	#endif
	glDisable(GL_CULL_FACE);
	glDisable(GL_LIGHTING);
	glDisable(GL_DEPTH_TEST);

	glFlush();
	SwapBuffers(hDC);
}

void idleredraw(void)
{
	if(!redrawContinue)idleFunc=NULL;
	doRedraw();
}

void redraw(void)
{
	if(!idleFunc)
		idleFunc=idleredraw;
}
BOOL trackingMotion=FALSE;
float lastPos[3];
DWORD lastTime;
int startX,startY;
void ptov(int x,int y,int width,int height,float v[3])
{
	float d,a;
	//project x,y onto a hemi-sphere centered within width,height
	v[0]=(2.0F*x-width)/width;
	v[1]=(height-2.0F*y)/height;
	d=(float)sqrt(v[0]*v[0]+v[1]*v[1]);
	v[2]=(float)cos((M_PI/2.0F)*((d<1.0F)?d:1.0F));
	a=1.0F/(float)sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
	v[0]*=a;
	v[1]*=a;
	v[2]*=a;
}

void startMotion(DWORD time,int button,int x,int y)
{
	if(button==1)mode=MoveObject;
	else return;

	trackingMotion=TRUE;
	redrawContinue=FALSE;
	startX=x;
	startY=y;
	ASSERT(startY);
	ptov(x,y,winWidth,winHeight,lastPos);
}

void stopMotion(DWORD time,int button,int x,int y)
{
	if(button==1&&mode==MoveObject)trackingMotion=FALSE;
	else return;

	if(startX!=x||startY!=y)redrawContinue=TRUE;
	else
	{
		angle=0.0F;
		redrawContinue=FALSE;
	}

	if(!redrawContinue)mode=MoveNone;
	redraw();
}

void trackMotion(DWORD time,int x,int y)
{
	if(trackingMotion)
	{
		float curPos[3],dx,dy,dz;

		ptov(x,y,winWidth,winHeight,curPos);

		dx=curPos[0]-lastPos[0];
		dy=curPos[1]-lastPos[1];
		dz=curPos[2]-lastPos[2];
		angle=90.0F*(float)sqrt(dx*dx+dy*dy+dz*dz);

		axis[0]=lastPos[1]*curPos[2]-lastPos[2]*curPos[1];
		axis[1]=lastPos[2]*curPos[0]-lastPos[0]*curPos[2];
		axis[2]=lastPos[0]*curPos[1]-lastPos[1]*curPos[0];

		lastPos[0]=curPos[0];
		lastPos[1]=curPos[1];
		lastPos[2]=curPos[2];
		redraw();
	}
}

void setupPalette(HDC hDC)
{
	PIXELFORMATDESCRIPTOR pfd;
	LOGPALETTE *pPal;
	int pixelFormat=GetPixelFormat(hDC);
	int paletteSize;

	DescribePixelFormat(hDC,pixelFormat,sizeof(PIXELFORMATDESCRIPTOR),&pfd);
	if(!(pfd.dwFlags &PFD_NEED_PALETTE||pfd.iPixelType==PFD_TYPE_COLORINDEX))
	{
		return;
	}

	paletteSize=1 << pfd.cColorBits;
	pPal=(LOGPALETTE*)
	malloc(sizeof(LOGPALETTE)+paletteSize*sizeof(PALETTEENTRY));
	pPal->palVersion=0x300;
	pPal->palNumEntries=paletteSize;

	//start with a copy of the curent system palette
	(void)GetSystemPaletteEntries(hDC,0,paletteSize,&pPal->palPalEntry[0]);

	{
		//fill in an RGBA color palette
		int redMask=(1 << pfd.cRedBits)-1;
		int greenMask=(1 << pfd.cGreenBits)-1;
		int blueMask=(1 << pfd.cBlueBits)-1;
		int i;
		for(i=0;i<paletteSize;++i)
		{
			pPal->palPalEntry[i].peRed=(((i>>pfd.cRedShift)&redMask)*255)/redMask;
			pPal->palPalEntry[i].peGreen=(((i>>pfd.cGreenShift)&greenMask)*255)/greenMask;
			pPal->palPalEntry[i].peBlue=(((i>>pfd.cBlueShift)&blueMask)*255)/blueMask;
			pPal->palPalEntry[i].peFlags=0;
		}
	}
	hPalette=CreatePalette(pPal);
	free(pPal);
	if(hPalette)
	{
		SelectPalette(hDC,hPalette,FALSE);
		RealizePalette(hDC);
	}
}

void setupPixelformat(HDC hDC)
{
	PIXELFORMATDESCRIPTOR pfd=
	{
		sizeof(PIXELFORMATDESCRIPTOR), //size of this pfd
		1,                             //version num
		PFD_DRAW_TO_WINDOW|            //support window
		PFD_SUPPORT_OPENGL,            //support OpenGL
		PFD_TYPE_RGBA,                 //color type
		8,                             //8-bit color depth
		0,0,0,0,0,0,                   //color bits(ignored)
		0,                             //no alpha buffer
		0,							   //alpha bits(ignored)
		0,							   //no accumulation buffer
		0,0,0,0,					   //accum bits(ignored)
		0,							   //depth buffer(filled below)
		0,							   //no stencil buffer
		0,							   //no auxiliary buffer
		PFD_MAIN_PLANE,				   //main layer
		0,							   //reserved
		0,0,0,						   //no layer,visible,damage masks
	};
	int SelectedPixelFormat;
	BOOL retVal;
	if(doubleBuffered)pfd.dwFlags|=PFD_DOUBLEBUFFER;
	if(depthBuffered)pfd.cDepthBits=16;
	SelectedPixelFormat=ChoosePixelFormat(hDC,&pfd);
	if(SelectedPixelFormat==0)
	{
		MessageBox(WindowFromDC(hDC),"ChoosePixelFormat failed\n","Error",
			MB_ICONERROR|MB_OK);
		exit(1);
	}
	retVal=SetPixelFormat(hDC,SelectedPixelFormat,&pfd);
	if(retVal!=TRUE)
	{
		MessageBox(WindowFromDC(hDC),"SetPixelFormat failed\n","Error",
			MB_ICONERROR|MB_OK);
		exit(1);
	}
}

LRESULT APIENTRY WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	switch(message)
	{
		case WM_CREATE:
			hDC=GetDC(hWnd);
			setupPixelformat(hDC);
			setupPalette(hDC);
			hGLRC=wglCreateContext(hDC);
			wglMakeCurrent(hDC,hGLRC);
			init();
			return 0;
		case WM_DESTROY:
			if(hGLRC)
			{
				wglMakeCurrent(NULL,NULL);
				wglDeleteContext(hGLRC);
			}
			idleFunc=NULL;
			ReleaseDC(hWnd,hDC);
			PostQuitMessage(0);
			return 0;
		case WM_SIZE:
			if(hGLRC)
			{
				winWidth=(int)LOWORD(lParam);
				winHeight=(int)HIWORD(lParam);
				resize();
				return 0;
			}
		case WM_PALETTECHANGED:
			if(hPalette!=NULL&&(HWND)wParam!=hWnd)
			{
				UnrealizeObject(hPalette);
				SelectPalette(hDC,hPalette,FALSE);
				RealizePalette(hDC);
				redraw();
				return 0;
			}
			break;
		case WM_QUERYNEWPALETTE:
			if(hPalette!=NULL)
			{
				UnrealizeObject(hPalette);
				SelectPalette(hDC,hPalette,FALSE);
				RealizePalette(hDC);
				redraw();
				return TRUE;
			}
		case WM_PAINT:
			if(hGLRC)
			{
				PAINTSTRUCT ps;
				BeginPaint(hWnd,&ps);
				redraw();
				EndPaint(hWnd,&ps);
				return 0;
			}
			break;
		case WM_LBUTTONDOWN:
			if(hGLRC)
			{
				int x=((int)LOWORD(lParam) << 16) >> 16;
				int y=((int)HIWORD(lParam) << 16) >> 16;
				SetCapture(hWnd);
				startMotion(0,1,x,y);
				return 0;
			}
			break;
		case WM_LBUTTONUP:
			if(hGLRC)
			{
				int x=((int)LOWORD(lParam) << 16) >> 16;
				int y=((int)HIWORD(lParam) << 16) >> 16;
				ReleaseCapture();
				stopMotion(0,1,x,y);
				return 0;
			}
			break;
		case WM_MOUSEMOVE:
			if(hGLRC)
			{
				int x=((int)LOWORD(lParam) << 16) >> 16;
				int y=((int)HIWORD(lParam) << 16) >> 16;
				trackMotion(0,x,y);
				break;
			}
		case WM_CHAR:
			switch((int)wParam)
			{
				case VK_ESCAPE:
					DestroyWindow(hWnd);
					return 0;
				case VK_SPACE:
					objectIndex=objectIndex<NUM_OBJECTS-1?++objectIndex:0;
					redraw();
					return 0;
				case 'a':
					useVertexArray=!useVertexArray;
					redraw();
					return 0;
				case 'c':
					useFaceCull=!useFaceCull;
					redraw();
					return 0;
				case 'f':
					useFog=!useFog;
					redraw();
					return 0;
				case 'h':
					halfObject=!halfObject;
					redraw();
					return 0;
				case 'l':
					useLighting=!useLighting;
					redraw();
					return 0;
				case 'o':
					perspectiveProj=!perspectiveProj;
					redraw();
					return 0;
				case 'p':
					drawOutlines=!drawOutlines;
					redraw();
					return 0;
				case 'v':
					useVertexCull=!useVertexCull;
					redraw();
					return 0;
				case 'i':
					yOffset +=Y_OFFSET_STEP;
					redraw();
					return 0;
				case 'j':
					xOffset -=X_OFFSET_STEP;
					redraw();
					return 0;
				case 'k':
					xOffset +=X_OFFSET_STEP;;
					redraw();
					return 0;
				case 'm':
					yOffset -=Y_OFFSET_STEP;;
					redraw();
					return 0;
				case 'r':
					textureReplace=!textureReplace;
					redraw();
					return 0;
				case 't':
					textureEnabled=!textureEnabled;
					redraw();
					return 0;
				case 'x':
					useVertexLocking=!useVertexLocking;
					redraw();
					return 0;
				default:
					break;
			}
			break;
		case WM_KEYDOWN:
			switch((int)wParam)
			{
			    case VK_DOWN:
					--objectNumMajor;
					redraw();
					break;
				case VK_UP:
					++objectNumMajor;
					redraw();
					break;
				case VK_LEFT:
					--objectNumMinor;
					redraw();
					break;
				case VK_RIGHT:
					++objectNumMinor;
					redraw();
					break;
				default:
					break;
			}
			if(hGLRC)
				redraw();
			return 0;
		default:
			break;
	}
	//Deal with any unprocessed messages
	long k=DefWindowProc(hWnd,message,wParam,lParam);
	ASSERT(k);
	return DefWindowProc(hWnd,message,wParam,lParam);
}

int APIENTRY WinMain(HINSTANCE hCurrentInst,HINSTANCE hPreviousInst,
					 PSTR lpszCmdLine, int nCmdShow)
{
	static TCHAR szAppName[] = TEXT ("OpenGL") ;
	WNDCLASS wndClass;
	HWND hWnd;
	MSG msg;
	//Define and register the window class
	wndClass.style=CS_HREDRAW|CS_VREDRAW;
	wndClass.lpfnWndProc=WndProc;
	wndClass.cbClsExtra=0;
	wndClass.cbWndExtra=sizeof(LPVOID);
	//ASSERT(hCurrentInst);
	wndClass.hInstance=hCurrentInst;
	wndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndClass.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
	wndClass.lpszMenuName=NULL;
	wndClass.lpszClassName=szAppName;
    if (!RegisterClass (&wndClass))
    {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
    }
	winWidth=GetSystemMetrics(SM_CYSCREEN)/3;
	winHeight=GetSystemMetrics(SM_CYSCREEN)/3;
	hWnd=CreateWindow(
		szAppName,TEXT ("WGL 应用"),
		WS_OVERLAPPEDWINDOW,
		winX,winY,winWidth,winHeight,NULL,NULL,hCurrentInst,NULL);
	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);

	while(1)
	{
		while(idleFunc&&
			PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)==FALSE)
		{
			(*idleFunc)();
		}
		if(GetMessage(&msg,NULL,0,0)!=TRUE)break;
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -