/**************************
* Includes
*
**************************/
#include <windows.h>
#include <gl/gl.h>
#include <math.h>
/**************************
* Function Declarations
*
**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
float minute=0;
float hour=0;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
//HWND CreateWindow(
//LPCTSTR lpClassName,LPCTSTR lpWindowName,
//DWORD dwStyle,
//int x,int y,int nWidth,int nHeight,
//HWND hWndParent,HMENU hMenu,HANDLE hlnstance,LPVOID lpParam);
hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
100, 100, 500, 530,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
/* OpenGL animation code goes here */
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix ();
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f,1.0f,0.0f);
for(int i=0;i<=390;i+=30)
{
float p=(float)(i*3.14/180);
glVertex3f((float)sin(p)/1.5,(float)cos(p)/1.5,0.0f);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.0f,0.0f,0.0f);
for(int i=0;i<=365;i+=5)
{
float p=(float)(i*3.14/180);
glVertex3f((float)sin(p)/1.6,(float)cos(p)/1.6,0.0f);
}
glEnd();
glBegin(GL_POINTS);
glColor3f(1.0f,1.0f,0.0f);
for(int i=0;i<=365;i+=30)
{
float p=(float)(i*3.14/180);
/*glBegin(GL_POLYGON);
glVertex3f((float)sin(p)/2.2,(float)cos(p)/2.2,0.0f);
glVertex3f((float)sin(p)/2.4,(float)cos(p)/2.4,0.0f);
glVertex3f((float)sin(p+0.02)/2.2,(float)cos(p+0.02)/2.2,0.0f);
glVertex3f((float)sin(p+0.02)/2.4,(float)cos(p+0.02)/2.4,0.0f);
glEnd();*/
glBegin(GL_LINES);
glVertex3f((float)sin(p)/2.0,(float)cos(p)/2.0,0.0f);
glVertex3f((float)sin(p)/1.8,(float)cos(p)/1.8,0.0f);
glEnd();
}
glEnd();
glPopMatrix ();
glPushMatrix ();//矩阵入栈,防止调用函数变换后改变现有矩阵
glRotatef (theta, 0.0f, 0.0f, 1.0f);//实现绕坐标轴旋转
/*glBegin (GL_TRIANGLES);
glColor3f (1.0f, 0.0f, 0.0f); glVertex2f (0.0f, 1.0f);
glColor3f (0.0f, 1.0f, 0.0f); glVertex2f (0.87f, -0.5f);
glColor3f (0.0f, 0.0f, 1.0f); glVertex2f (-0.87f, -0.5f);
glEnd ();*/
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f,-0.1f); glVertex2f(0.0f,0.5f);
glEnd();
glPopMatrix ();
glPushMatrix ();
glRotatef (theta/60, 0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f,-0.07f); glVertex2f(0.0f,0.4f);
glEnd();
glPopMatrix ();
glPushMatrix ();
glRotatef (theta/3600, 0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.0f,-0.05f); glVertex2f(0.0f,0.3f);
glEnd();
glPopMatrix ();
glColor3f(1.0f,1.0f,0.0f);
glBegin(GL_POLYGON);
glVertex2f(0.01f,0.0f);
glVertex2f(0.0f,0.01f);
glVertex2f(-0.01f,0.0f);
glVertex2f(0.0f,-0.01f);
glEnd();
SwapBuffers (hDC);
theta -= 6.0f;
Sleep (1000);
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
(dev-c++)opengl示例小程序:clock
5星 · 超过95%的资源 需积分: 27 106 浏览量
2010-04-23
09:53:15
上传
评论
收藏 12KB RAR 举报
量子人生
- 粉丝: 9
- 资源: 5
最新资源
- ZArchiver.Pro_0.9.5.apk
- vmware环境配置.mp4
- 548springboot + vue 大学生社团活动平台.zip(可运行源码+数据库文件+文档)
- 微信小程序 辩论倒计时小程序源码 作业设计demo 计算机专业参考
- 深入探究文件IO,嵌入式Linux
- 微信备忘录小程序源码 作业设计demo 计算机专业作业
- 微信小程序 仿百度小说小程序 看小说小程序 实现源码
- 锂电资料包-锂离子电池技术干货资料合集.zip
- EMC EMI资料大全电磁兼容电磁干扰设计资料工程师必备经验资料35个合集.zip
- 547springboot + vue 交流互动系统.zip(可运行源码+数据库文件+文档)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈