旋转式正方体和正四面体

更新时间: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函数赋予颜色,以及运用多个选择语句,实现消息转变。通过此实验,我加深了对课程的理解,增强了我的实践能力,受益匪浅。

本文来源:https://www.bwwdw.com/article/7lv4.html

Top