opengl学习指导实验 计算机图形学上机(4)

2019-04-13 22:52

// release the dc ReleaseDC(hwnd,hdc); return(0); break; case WM_DESTROY: { // kill the application, this sends a WM_QUIT message PostQuitMessage(0); // return success return(0); } break; default:break; } // end switch

// process any messages that we didn't take care of

return (DefWindowProc(hwnd, message, wParam, lParam)); } // end WinProc

// WINMAIN //////////////////////////////////////////////// int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) {

WNDCLASSEX winclass; // this will hold the class we create HWND hwnd; // generic window handle MSG message; // generic message

// first fill in the window class stucture

winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_DBLCLKS | CS_OWNDC |

CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW);

winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winclass.lpszMenuName = NULL;

winclass.lpszClassName = WINDOW_CLASS_NAME;

winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// save hinstance in global

15

hinstance_app = hinstance;

// register the window class

if (!RegisterClassEx(&winclass)) return(0);

// create the window

if (!(hwnd = CreateWindowEx(NULL, // extended style WINDOW_CLASS_NAME, // class \ WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,0, // initial x,y WINDOW_WIDTH, // initial width WINDOW_HEIGHT,// initial height NULL, // handle to parent NULL, // handle to menu hinstance,// instance of this application NULL))) // extra creation parms return(0);

// save main window handle main_window_handle = hwnd;

while(GetMessage(&message,NULL,0,0)) { // translate any accelerator keys TranslateMessage(&message); // send the message to the window proc DispatchMessage(&message); } // end while

// return to Windows like this return(message.wParam); } // end WinMain

///////////////////////////////////////////////////////////

void BoundaryFill4(HDC hdc,int x,int y,COLORREF boundarycolor,COLORREF newcolor) { COLORREF color; color=GetPixel(hdc,x,y); if((color != boundarycolor) && (color !=newcolor)) { SetPixel(hdc,x,y,newcolor); BoundaryFill4(hdc,x,y+1,boundarycolor,newcolor); BoundaryFill4(hdc,x,y-1,boundarycolor,newcolor);

16

BoundaryFill4(hdc,x-1,y,boundarycolor,newcolor); BoundaryFill4(hdc,x+1,y,boundarycolor,newcolor); } }

2、vc.net中建立Win32工程

更改项目中recursion.cpp文件代码: 1)声明函数

void BoundaryFill4(HDC,int,int,COLORREF,COLORREF);

2)修改LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)函数中代码,把visualc++6.0介绍的recursion.cpp中的WndProc函数体复制即可。 3)在recursion.cpp文件末尾添加BoundaryFill4函数。

17

实验四 二维图形的几何变换

一、实验学时 2学时 二、实验类型 设计型实验 三、实验目的和要求

1、掌握二维图形的基本几何变换,如平移、旋转、缩放、对称、错切变换; 2、掌握OpenGL中模型变换函数,实现简单的动画技术。 四、实验内容 1、下面的代码采用GLUT库,实现了一个矩形在窗口中匀速转动(单击鼠标右键停止转动),请修改代码,实现矩形在窗口内沿着水平线匀速移动。

/*

* double.c

* This is a simple double buffered program.

* Pressing the left mouse button rotates the rectangle. * Pressing the right mouse button stops the rotation. */

#include #include

static GLfloat spin = 0.0;

void display(void) {

glClear(GL_COLOR_BUFFER_BIT); glPushMatrix();

glRotatef(spin, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0);

glRectf(-10.0, -10.0, 10.0, 10.0); glPopMatrix();

glutSwapBuffers(); }

void spinDisplay(void) {

spin = spin + 2.0; if (spin > 360.0) spin = spin - 360.0; glutPostRedisplay(); }

void init(void) {

glClearColor (0.0, 0.0, 0.0, 0.0);

18

glShadeModel (GL_FLAT); }

void reshape(int w, int h) {

glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity();

glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }

void mouse(int button, int state, int x, int y) {

switch (button) {

case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(spinDisplay); break;

case GLUT_MIDDLE_BUTTON: case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(NULL); break; default: break; } } /*

* Request double buffer display mode. * Register mouse input callback functions */

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init ();

glutDisplayFunc(display); glutReshapeFunc(reshape);

19


opengl学习指导实验 计算机图形学上机(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:仪器分析习题(色谱)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: