OpenGL之 小小太阳系 堆栈操作

  首先  需要建立win32 Application 应用空工程     

添加 两个文件  opengl.h       lission.cpp

 

opengl.h的源码如下

 

 


#include"gl/glut.h"

int InitGL()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glClearColor(0.0,1.0,0.0,1.0);

//	glShadeModel(GL_SMOOTH);


		GLfloat light_ambient[]={0.0,0.0,0.0,1.0};   //环境强度
	GLfloat light_diffuse[]={0.5,0.5,0.0,1.0};   //散射强度
	GLfloat light_specular[]={0.0,0.0,0.0,1.0};  //镜面强度
	GLfloat light_position[]={-10.0,0.0,10.0,1.0};    //位置  z=1是为方向光源
	GLfloat spot_direction[]={0.0,0.0,-1.0};    //聚光灯方向向量

	glLightfv(GL_LIGHT1,GL_AMBIENT,light_ambient);  //光的环境强度
	glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse);  //光的散射强度
	glLightfv(GL_LIGHT1,GL_SPECULAR,light_specular);//都安的镜面强度
	glLightfv(GL_LIGHT1,GL_POSITION,light_position);//光的位置

	glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,1.0);//光的常量衰减因子
    glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0.001);  //光的线性衰减因子
	glLightf(GL_LIGHT1,GL_QUADRATIC_ATTENUATION,0.0001);//光的二次衰减因子

	glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,180);          
	glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,spot_direction);
	glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,2.0);
 
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT1);
	glEnable(GL_DEPTH_TEST);

	return true;

}

int ReSizeGLScene(GLsizei w,GLsizei h)
{
	if(h==0)
		h=1;

	glViewport(0,0,w,h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0,(GLdouble)w/(GLdouble)h,1.0,100.0);

	gluLookAt(-10.0,10.0,20.0,
		      0.0,0.0,0.0,
			  0.0,1.0,0.0);



 return true;


}
GLdouble rtri=0;
GLdouble ytri=0;
GLdouble ztri=0;
GLdouble ytri_=0;

int DrawGLScene()
{

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 
		glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

      //glPushMatrix();

glPushMatrix();
   
	
    glTranslatef(0.0,0.0,0.0);

	glutSolidSphere(2.0,30,20);

	
glPopMatrix();


	
 glPushMatrix();
   glRotatef(rtri,0.0,1.0,0.0);
   glTranslatef(5.0,0.0,0.0);
   glutSolidSphere(1.0,30,20);

         glPushMatrix();
		   
		   glRotatef(ytri,1.0,0.0,0.0);
	       glTranslatef(0,1.5,0.0);
		   glutSolidSphere(0.3,30,20);
		  
         glPopMatrix();

		
 
 glPopMatrix();


 glPushMatrix();
    glRotatef(ztri,0.0,1.0,0.0);
    glTranslatef(10.0,0.0,0.0);
	glutSolidSphere(1.0,20,20);

	   glPushMatrix();
		   glRotatef(ytri_,0.0,0.0,1.0);
		   glTranslatef(2.0,0.0,0.0);
		   glutSolidSphere(0.4,20,20);
         glPopMatrix();
glPopMatrix();



     

	glPushMatrix();
	
  //glutSolidTorus(0.1,0.8,50,50);

glPopMatrix();
//if(rtri<=300)
rtri+=1.0;
ytri+=5.0;
ytri_+=2.0;
ztri+=0.8;

return true;
}

lession.cpp的源码如下

#include <windows.h>		
//#include<gl/glut.h>

#include"opengl.h"

HDC			hDC=NULL;		// Private GDI Device Context
HGLRC		hRC=NULL;		// Permanent Rendering Context
HWND		hWnd=NULL;		// Holds Our Window Handle
HINSTANCE	hInstance;		// Holds The Instance Of The Application

		
bool	active=TRUE;		// Window Active Flag Set To TRUE By Default



LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);	// Declaration For WndProc



GLvoid KillGLWindow(GLvoid)								// Properly Kill The Window
{
	

	if (hRC)											// Do We Have A Rendering Context?
	{
		wglMakeCurrent(NULL,NULL);				// Are We Able To Release The DC And RC Contexts?
		wglDeleteContext(hRC);					// Are We Able To Delete The RC
       ReleaseDC(hWnd,hDC);				// Are We Able To Release The DC

       DestroyWindow(hWnd);			// Are We Able To Destroy The Window?
	UnregisterClass("OpenGL",hInstance);		// Are We Able To Unregister Class
	
    }
}

/*	This Code Creates Our OpenGL Window.  Parameters Are:					*
 *	title			- Title To Appear At The Top Of The Window				*
 *	width			- Width Of The GL Window Or Fullscreen Mode				*
 *	height			- Height Of The GL Window Or Fullscreen Mode			*
 *	bits			- Number Of Bits To Use For Color (8/16/24/32)			*
 *	fullscreenflag	- Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)	*/
 
BOOL CreateGLWindow(char* title, int width, int height, int bits)
{
	GLuint		PixelFormat;			// Holds The Results After Searching For A Match
	WNDCLASS	wc;						// Windows Class Structure
	DWORD		dwExStyle;				// Window Extended Style
	DWORD		dwStyle;				// Window Style
	RECT		WindowRect;				// Grabs Rectangle Upper Left / Lower Right Values
	WindowRect.left=(long)0;			// Set Left Value To 0
	WindowRect.right=(long)width;		// Set Right Value To Requested Width
	WindowRect.top=(long)0;				// Set Top Value To 0
	WindowRect.bottom=(long)height;		// Set Bottom Value To Requested Height

		

	hInstance			= GetModuleHandle(NULL);				// Grab An Instance For Our Window
	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
	wc.lpfnWndProc		= (WNDPROC) WndProc;					// WndProc Handles Messages
	wc.cbClsExtra		= 0;									// No Extra Window Data
	wc.cbWndExtra		= 0;									// No Extra Window Data
	wc.hInstance		= hInstance;							// Set The Instance
	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
	wc.hbrBackground	= NULL;									// No Background Required For GL
	wc.lpszMenuName		= NULL;									// We Don't Want A Menu
	wc.lpszClassName	= "OpenGL";								// Set The Class Name

	if (!RegisterClass(&wc))									// Attempt To Register The Window Class
	{
		MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;											// Return FALSE
	}
	

	dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			// Window Extended Style
	dwStyle=WS_OVERLAPPEDWINDOW;							// Windows Style


	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);		// Adjust Window To True Requested Size

	// Create The Window
	if (!(hWnd=CreateWindowEx(	dwExStyle,							// Extended Style For The Window
								"OpenGL",							// Class Name
								title,								// Window Title
								dwStyle |							// Defined Window Style
								WS_CLIPSIBLINGS |					// Required Window Style
								WS_CLIPCHILDREN,					// Required Window Style
								0, 0,								// Window Position
								WindowRect.right-WindowRect.left,	// Calculate Window Width
								WindowRect.bottom-WindowRect.top,	// Calculate Window Height
								NULL,								// No Parent Window
								NULL,								// No Menu
								hInstance,							// Instance
								NULL)))								// Dont Pass Anything To WM_CREATE
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
	{
		sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
		1,											// Version Number
		PFD_DRAW_TO_WINDOW |						// Format Must Support Window
		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
		PFD_DOUBLEBUFFER,							// Must Support Double Buffering
		PFD_TYPE_RGBA,								// Request An RGBA Format
		bits,										// Select Our Color Depth
		0, 0, 0, 0, 0, 0,							// Color Bits Ignored
		0,											// No Alpha Buffer
		0,											// Shift Bit Ignored
		0,											// No Accumulation Buffer
		0, 0, 0, 0,									// Accumulation Bits Ignored
		16,											// 16Bit Z-Buffer (Depth Buffer)  
		0,											// No Stencil Buffer
		0,											// No Auxiliary Buffer
		PFD_MAIN_PLANE,								// Main Drawing Layer
		0,											// Reserved
		0, 0, 0										// Layer Masks Ignored
	};
	
	hDC=GetDC(hWnd);							// Did We Get A Device Context?
	

	PixelFormat=ChoosePixelFormat(hDC,&pfd);
	SetPixelFormat(hDC,PixelFormat,&pfd);
    hRC=wglCreateContext(hDC);			
    wglMakeCurrent(hDC,hRC);


	
	ShowWindow(hWnd,SW_SHOW);						// Show The Window
	SetForegroundWindow(hWnd);						// Slightly Higher Priority
	SetFocus(hWnd);									// Sets Keyboard Focus To The Window
	ReSizeGLScene(width, height);					// Set Up Our Perspective GL Screen

    InitGL();								// Initialize Our Newly Created GL Window
	

	return TRUE;									// Success
}


LRESULT CALLBACK WndProc(	HWND	hWnd,			// Handle For This Window
							UINT	uMsg,			// Message For This Window
							WPARAM	wParam,			// Additional Message Information
							LPARAM	lParam)			// Additional Message Information
{
	switch (uMsg)									// Check For Windows Messages
	{
		case WM_ACTIVATE:							// Watch For Window Activate Message
		{
			if (!HIWORD(wParam))					// Check Minimization State
			{
				active=TRUE;						// Program Is Active
			}
			else
			{
				active=FALSE;						// Program Is No Longer Active
			}

			return 0;								// Return To The Message Loop
		}

		case WM_SYSCOMMAND:							// Intercept System Commands
		{
			switch (wParam)							// Check System Calls
			{
				case SC_SCREENSAVE:					// Screensaver Trying To Start?
				case SC_MONITORPOWER:				// Monitor Trying To Enter Powersave?
				return 0;							// Prevent From Happening
			}
			break;									// Exit
		}

		case WM_CLOSE:								// Did We Receive A Close Message?
		{
			PostQuitMessage(0);						// Send A Quit Message
			return 0;								// Jump Back
		}

	

	
		case WM_SIZE:								// Resize The OpenGL Window
		{
			ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
			return 0;								// Jump Back
		}
		
	}

	// Pass All Unhandled Messages To DefWindowProc
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance
					HINSTANCE	hPrevInstance,		// Previous Instance
					LPSTR		lpCmdLine,			// Command Line Parameters
					int			nCmdShow)			// Window Show State
{
	MSG		msg;									// Windows Message Structure
	int	done=0;								// Bool Variable To Exit Loop

	

	// Create Our OpenGL Window
	if (!CreateGLWindow("NeHe's Rotation Tutorial",640,480,16))
	{
		return 0;									// Quit If Window Was Not Created
	}

	while(!done)									// Loop That Runs While done=FALSE
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	// Is There A Message Waiting?
		{
			if (msg.message==WM_QUIT)				// Have We Received A Quit Message?
			{
				done=TRUE;							// If So done=TRUE
			}
			else									// If Not, Deal With Window Messages
			{
				TranslateMessage(&msg);				// Translate The Message
				DispatchMessage(&msg);				// Dispatch The Message
			}
		}
		else										// If There Are No Messages
		{
			// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
//			if ((active && !DrawGLScene()) )	// Active?  Was There A Quit Received?
		        DrawGLScene();
				SwapBuffers(hDC);					// Swap Buffers (Double Buffering)
		

		/*	if (keys[VK_F1])						// Is F1 Being Pressed?
			{
				keys[VK_F1]=FALSE;					// If So Make Key FALSE
				KillGLWindow();						// Kill Our Current Window
							// Toggle Fullscreen / Windowed Mode
				// Recreate Our OpenGL Window
				if (!CreateGLWindow("NeHe's Rotation Tutorial",640,480,16))
				{
					return 0;						// Quit If Window Was Not Created
				}
			}      */
		}
	}

	// Shutdown
	KillGLWindow();									// Kill The Window
	return (msg.wParam);							// Exit The Program
}

最终效果如下   注意他是在转动的






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值