旋转式正方体和正四面体
更新时间:2023-05-24 17:19:01 阅读量: 实用文档 文档下载
一、设计目的
设计一个程序使得运行后生成一个旋转的正方体和一个可旋转的正四面体,旋转过程中伴随着颜色的变化。
二、算法描述
运用多个glVertex3f函数赋予颜色,以及运用多个选择语句,实现消息转变。根据题目要求,分别选择正方体和正四面体,分别作左上右下旋转和水平逆时针(从上方看)旋转。正方体的六个面采用不同的颜色,正四面体的三个可见面则采用多色分布镶嵌。 程序要运用多个函数有:
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)(调整和初始化GL窗口); int InitGL(GLvoid)(初始化);
int DrawGLScene(GLvoid)(GL场景绘制);
GLvoid KillGLWindow(GLvoid) (选择正确方式选择窗口或关闭窗口);AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);(调整窗口大小来创建合适的窗口); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)(主函数)
三、程序代码
#include <windows.h> #include <gl\gl.h>
#include <gl\glu.h> #include <gl\glaux.h>
HDC
//需要用到的各个头文件
hDC=NULL;
HGLRC HWND
hRC=NULL; hWnd=NULL;
HINSTANCE hInstance; bool keys[256]; bool active=TRUE;
GLfloat rtri; GLfloat rquad;
bool fullscreen=TRUE;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//调整和初始化GL窗口
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) {
if (height==0) {
height=1;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity();
}
int InitGL(GLvoid) { }
//初始化
glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.5f); glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); return TRUE;
int DrawGLScene(GLvoid) {
// GL场景绘制
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(-1.5f,0.0f,-6.0f); glRotatef(rtri,0.0f,1.0f,0.0f); glBegin(GL_TRIANGLES); glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,0.0f,1.0f);
glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f,-1.0f, -1.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f, -1.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f(-1.0f,-1.0f, -1.0f); glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); glEnd(); glLoadIdentity(); glTranslatef(1.5f,0.0f,-7.0f); glRotatef(rquad,1.0f,1.0f,1.0f); glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f, 1.0f,-1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); glColor3f(1.0f,0.5f,0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f( 1.0f,-1.0f,-1.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); glColor3f(1.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); glColor3f(1.0f,0.0f,1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); glVertex3f( 1.0f,-1.0f,-1.0f);
glEnd(); rtri+=0.2f;
rquad-=0.15f; return TRUE;
}
GLvoid KillGLWindow(GLvoid) {
//选择正确方式选择窗口或关闭窗口
if (fullscreen) { ChangeDisplaySettings(NULL,0); }
ShowCursor(TRUE);
if (hRC) { if (!wglMakeCurrent(NULL,NULL))
{
MessageBox(NULL,"Release
Of
DC
And
RC
Failed.","SHUTDOWN
ERROR",MB_OK | MB_ICONINFORMATION); }
if (!wglDeleteContext(hRC)) {
Context
Failed.","SHUTDOWN
MessageBox(NULL,"Release Rendering ERROR",MB_OK | MB_ICONINFORMATION);
}
if (hDC && !ReleaseDC(hWnd,hDC)) {
MessageBox(NULL,"Release
Device
}
hRC=NULL;
Context
Failed.","SHUTDOWN
ERROR",MB_OK | MB_ICONINFORMATION); hDC=NULL; }
if (hWnd && !DestroyWindow(hWnd)) { MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
if (!UnregisterClass("OpenGL",hInstance)) {
hWnd=NULL;
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); }
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) //创建绘图窗口 {
GLuint
PixelFormat;
WNDCLASS wc; }
hInstance=NULL;
DWORD dwExStyle; DWORD dwStyle; RECT WindowRect; WindowRect.left=(long)0;
WindowRect.right=(long)width; WindowRect.top=(long)0; WindowRect.bottom=(long)height; fullscreen=fullscreenflag; hInstance
= GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra wc.cbWndExtra wc.hInstance wc.hIcon wc.hCursor
= 0; = 0;
= hInstance;
= LoadIcon(NULL, IDI_WINLOGO); = LoadCursor(NULL, IDC_ARROW);
= NULL;
wc.hbrBackground = NULL; wc.lpszMenuName
wc.lpszClassName = "OpenGL"; if (!RegisterClass(&wc))
The
Window
MessageBox(NULL,"Failed To Register Class.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; }
if (fullscreen) {
DEVMODE dmScreenSettings;
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); dmScreenSettings.dmSize=sizeof(dmScreenSettings); dmScreenSettings.dmPelsWidth = width; dmScreenSettings.dmPelsHeight = height; dmScreenSettings.dmBitsPerPel = bits;
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
if
(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) {
}
fullscreen=FALSE;
else {
Will
Now
MessageBox(NULL,"Program
Close.","ERROR",MB_OK|MB_ICONSTOP);
if (fullscreen) {
}
}
}
return FALSE;
dwExStyle=WS_EX_APPWINDOW; dwStyle=WS_POPUP;
ShowCursor(FALSE); } else
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; dwStyle=WS_OVERLAPPEDWINDOW;
} AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); //调整窗口大小来创建合适的窗口 if (!(hWnd=CreateWindowEx( dwExStyle, "OpenGL",
title,
dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
0, 0,
WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, NULL, NULL, hInstance,
NULL)))
{
KillGLWindow(); MessageBox(NULL,"Window
Error.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } static PIXELFORMATDESCRIPTOR pfd=
{ sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA,
bits, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16,
0, 0,
PFD_MAIN_PLANE,
Creation
0, 0, 0, 0 };
if (!(hDC=GetDC(hWnd))) { KillGLWindow();
MessageBox(NULL,"Can't
Create
A
Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; }
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) {
KillGLWindow();
MessageBox(NULL,"Can't Find PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE;
}
if(!SetPixelFormat(hDC,PixelFormat,&pfd))
{
KillGLWindow();
MessageBox(NULL,"Can't Set PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } if (!(hRC=wglCreateContext(hDC)))
{
KillGLWindow(); MessageBox(NULL,"Can't Create A Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE;
}
if(!wglMakeCurrent(hDC,hRC))
{ KillGLWindow();
MessageBox(NULL,"Can't Activate
The
Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
ShowWindow(hWnd,SW_SHOW); SetForegroundWindow(hWnd);
SetFocus(hWnd);
ReSizeGLScene(width, height);
if (!InitGL())
GL
Device
A
Suitable
The
GL Rendering
GL Rendering
{
KillGLWindow(); MessageBox(NULL,"Initialization
Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; }
LRESULT CALLBACK WndProc( {
HWND hWnd,
}
return TRUE;
UINT uMsg, WPARAM wParam, LPARAM
lParam)
switch (uMsg) { case WM_ACTIVATE:
{ }
//监视窗口激活消息
if ((LOWORD(wParam) != WA_INACTIVE) && !((BOOL)HIWORD(wParam)))
active=TRUE;
else active=FALSE; return 0;
case WM_SYSCOMMAND: {
switch (wParam) { case SC_SCREENSAVE:
case SC_MONITORPOWER: return 0;
}
break;
}
case WM_CLOSE: { }
PostQuitMessage(0); return 0;
case WM_KEYDOWN: {
keys[wParam] = TRUE; return 0;
}
}
case WM_KEYUP: { keys[wParam] = FALSE; return 0; }
case WM_SIZE: { }
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); return 0;
return DefWindowProc(hWnd,uMsg,wParam,lParam);
int WINAPI WinMain( HINSTANCE hInstance, {
LPSTR int
HINSTANCE hPrevInstance,
lpCmdLine, nCmdShow)
MSG BOOL msg; done=FALSE;
if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)//选择窗口大小—全屏或者否
{ }
fullscreen=FALSE;
if (!CreateGLWindow("NeHe's Solid Object Tutorial",640,480,16,fullscreen)) //创建opengl窗口 {
} {
return 0;
while(!done)
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
if (msg.message==WM_QUIT) { }
done=TRUE;
}
}
}
{ }
TranslateMessage(&msg); DispatchMessage(&msg);
else { }
if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) {
done=TRUE;
} else {
SwapBuffers(hDC);
}
if (keys[VK_F1]) { }
keys[VK_F1]=FALSE; KillGLWindow();
fullscreen=!fullscreen;
if (!CreateGLWindow("NeHe's Solid Object Tutorial",640,480,16,fullscreen)) { }
return 0;
KillGLWindow();
//关闭窗口
return (msg.wParam); //退出程序
四、运行结果
五、总结
为了做这个课程设计,借阅了一些资料并不断完善最终实现实验目标。此实验运用多个glVertex3f函数赋予颜色,以及运用多个选择语句,实现消息转变。通过此实验,我加深了对课程的理解,增强了我的实践能力,受益匪浅。
正在阅读:
旋转式正方体和正四面体05-24
激进与变异论包世臣书学思想的两面性07-07
孩子们应该经历的七大类冒险游戏10-01
快乐的元宵节的小学生二年级作文250字06-13
概率论分赌注问题10-23
《食品化学》习题集1及答案04-09
犯罪现场勘查的意义05-13
文化大革命的历史教训01-21
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 正四面体
- 正方体
- 旋转式
- 全国2016年自学考试学前比较教育必考试题和答案
- Aefkgkw中国摄影通史
- 2012年水利部在京事业单位公开招聘工作人员简章
- 县粮安工程建设规划
- 听觉诱发电位在临床麻醉监测中的应用
- 村级计生工作基本知识
- 高二 第七章 现代生物理论进化的由来
- 巧用ERP总账系统中的辅助核算功能
- 第六章 疾病预防与控制法律制度
- 发动机排气管毕业设计
- 继电保护原理基础_第三章
- S2200ME-PAF交换机配置说明(24+2新)
- 浅谈如何_善待_韩剧提高韩语听说能力
- 大学英语(二)-开放英语(1602)
- 道路交通安全综合整治“三年行动”和大会战实施方案2012.9.12
- 对20世纪以来美国中学数学教育目标变迁的再思考
- 社会行政期末复习资料 分类型整理
- 单元4售后服务-客户回访
- 酒店前台及前台服务培训教案
- 用判别式法求函数值域的几点思考