project2.dpr
来自「Delphi7编程80例(完全版)」· DPR 代码 · 共 594 行 · 第 1/2 页
DPR
594 行
program Project2;
{$APPTYPE CONSOLE}
uses Windows, Messages,OpenGL;
var
hParentWnd: hWnd; // 窗口句柄
h_DC: hDC; // 设备环境的句柄
h_RC: hGLRC; // 渲染环境的句柄
WndClass: TWndClass; // 窗口类型
iMessage: TMsg; // 屏幕的长度和宽度
PWndWidth, PWndHeight: Integer; // 旋转的星体
Angle: Integer;
// 处理父窗口消息的方法
function MainWndProc(hWnd:hWnd; iMessage: uInt; wParam: wParam; lParam: lParam): lResult; export; stdcall;
begin
Result := 0;
case iMessage of
WM_DESTROY: PostQuitMessage(0); // 通知窗口程序即将退出
else
Result := DefWindowProc(hWnd, iMessage, wParam, lParam);
end;
end;
// 创建自定义的窗口
procedure PrepareWindow;
begin
if (hPrevInst = 0) then
begin
WndClass.style := CS_HREDRAW or CS_VREDRAW;
WndClass.lpfnWndProc := @MainWndProc; // 指向处理窗口消息的过程
WndClass.cbClsExtra := 0;
WndClass.cbWndExtra := 0;
WndClass.hInstance := GetModuleHandle(nil);
WndClass.hIcon := 0;
WndClass.hCursor := LOADCURSOR(0, IDC_ARROW);
WndClass.hBrBackground := 0; //opengl场景不需要背景
WndClass.lpszMenuName := nil;
WndClass.lpszClassName := 'glClass';
if RegisterClass(WndClass) = 0 then
begin
MessageBox(hParentWnd, '窗口类型不能被注册!', nil, MB_OK or MB_ICONSTOP or MB_TASKMODAL);
Halt;
end;
end;
PWndWidth := Round(GetSystemMetrics(SM_CXSCREEN)/2); //设置演示窗口的宽度
PWndHeight := Round(GetSystemMetrics(SM_CYSCREEN)/2); // 设置严实窗口的长度
hParentWnd := CreateWindow('glClass', 'Delphi中演示OPENGL动画', WS_SYSMENU, 0, 0, PWndWidth, PWndHeight, 0, 0, hInstance, nil);
end;
// 绘制场景
procedure DrawScene;
begin
Angle := Angle+Random(20);
glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT); // 清除帧缓冲区,为绘制新的帧做准备
glPushMatrix; // 保存当前矩阵
glTranslatef(Random(2)/10, Random(2)/10,Random(2)-6);
{将视点后移使全部的场景可见,前面两个数值分别是X,Y的坐标值,最后的数值决定对象大小}
// 转动对象的视点,后面三个数字表示旋转幅度,值越大幅度越大
glRotatef(Angle, 1, 1, 1);
// glRotatef(Angle,0,0,0); //很强的视觉冲击效果
//以下是定义组成星体的点元素
glBegin(GL_TRIANGLES);
glNormal3f(-0.74116, -0.579615, 0.338716);
glVertex3f(-0.475525, -0.142578, 0.154755);
glVertex3f(-0.293854, -0.451569, 0.0235291);
glVertex3f(-0.344055, -0.147278, 0.434387);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.410835, -0.910633, -0.0442978);
glVertex3f(-0.293854, -0.451569, 0.0235291);
glVertex3f(-0.0111389, -0.565186, -0.262848);
glVertex3f(0.168762, -0.656647, -0.0511475);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.569974, -0.805453, 0.162401);
glVertex3f(0.168762, -0.656647, -0.0511475);
glVertex3f(0.417633, -0.475372, -0.0255432);
glVertex3f(0.253418, -0.528687, 0.286377);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.949311, -0.3094, -0.0554997);
glVertex3f(0.417633, -0.475372, -0.0255432);
glVertex3f(0.471161, -0.290833, -0.138733);
glVertex3f(0.531952, -0.152008, 0.127167);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.9759, 0.0404478, 0.214439);
glVertex3f(0.531952, -0.152008, 0.127167);
glVertex3f(0.533997, 0.20929, 0.0497131);
glVertex3f(0.472351, 0.0888977, 0.352966);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.672389, 0.730092, -0.121896);
glVertex3f(0.533997, 0.20929, 0.0497131);
glVertex3f(0.425964, 0.25, -0.302368);
glVertex3f(0.306183, 0.404633, -0.0369263);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.292631, 0.954815, -0.051914);
glVertex3f(0.306183, 0.404633, -0.0369263);
glVertex3f(0.0134583, 0.491821, -0.083374);
glVertex3f(0.131439, 0.474701, 0.266785);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.29624, 0.930867, -0.21384);
glVertex3f(0.0134583, 0.491821, -0.083374);
glVertex3f(-0.131439, 0.40448, -0.262848);
glVertex3f(-0.293854, 0.415649, 0.0107727);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.704095, 0.703107, 0.0994509);
glVertex3f(-0.475525, 0.224731, 0.0743408);
glVertex3f(-0.344055, 0.324249, 0.301544);
glVertex3f(-0.293854, 0.415649, 0.0107727);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.986932, -0.0344614, -0.157411);
glVertex3f(-0.425293, -0.0452881, -0.181488);
glVertex3f(-0.475525, -0.142578, 0.154755);
glVertex3f(-0.475525, 0.224731, 0.0743408);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.319147, -0.514182, -0.796091);
glVertex3f(-0.267548, -0.15448, -0.425323);
glVertex3f(0.191071, -0.337128, -0.491211);
glVertex3f(-0.0111389, -0.565186, -0.262848);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.156126, -0.100546, 0.982606);
glVertex3f(-0.317749, -0.0470581, 0.448822);
glVertex3f(-0.344055, -0.147278, 0.434387);
glVertex3f(-0.0487366, -0.464478, 0.448853);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.77365, -0.250923, -0.58181);
glVertex3f(0.191071, -0.337128, -0.491211);
glVertex3f(0.312347, 0, -0.475342 );
glVertex3f(0.471161, -0.290833, -0.138733);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.376298, -0.37407, 0.847627);
glVertex3f(-0.0487366, -0.464478, 0.448853);
glVertex3f(0.253418, -0.528687, 0.286377);
glVertex3f(0.212646, -0.201538, 0.448853);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.511356, 0.31956, -0.797744);
glVertex3f(0.312347, 0, -0.475342 );
glVertex3f(0.131378, 0.249969, -0.491211);
glVertex3f(0.425964, 0.25, -0.302368);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.407016, -0.0631076, 0.911238);
glVertex3f(0.212646, -0.201538, 0.448853);
glVertex3f(0.472351, 0.0888977, 0.352966);
glVertex3f(0.212646, 0.267761, 0.481354);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.270432, 0.623807, -0.733302);
glVertex3f(0.131378, 0.249969, -0.491211);
glVertex3f(-0.267548, 0.15448, -0.425323);
glVertex3f(-0.131439, 0.40448, -0.262848);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.187651, 0.74153, 0.644143);
glVertex3f(0.212646, 0.267761, 0.481354);
glVertex3f(0.131439, 0.474701, 0.266785);
glVertex3f(-0.0811768, 0.360779, 0.459869);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.839618, 0, -0.543177 );
glVertex3f(-0.425293, -0.0452881, -0.181488);
glVertex3f(-0.267548, 0.15448, -0.425323);
glVertex3f(-0.267548, -0.15448, -0.425323);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.523449, 0.281854, 0.804089);
glVertex3f(-0.344055, 0.324249, 0.301544);
glVertex3f(-0.317749, -0.0470581, 0.448822);
glVertex3f(-0.0811768, 0.360779, 0.459869);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.889618, -0.190557, 0.415053);
glVertex3f(-0.447205, 0.0761719, 0.315887);
glVertex3f(-0.475525, -0.142578, 0.154755);
glVertex3f(-0.344055, -0.147278, 0.434387);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.672801, -0.631467, 0.385471);
glVertex3f(0.00344849, -0.631836, 0.247131);
glVertex3f(-0.344055, -0.147278, 0.434387);
glVertex3f(-0.293854, -0.451569, 0.0235291);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.879473, -0.438944, -0.183999);
glVertex3f(-0.361786, -0.280945, -0.0588074);
glVertex3f(-0.293854, -0.451569, 0.0235291);
glVertex3f(-0.475525, -0.142578, 0.154755);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.703044, -0.516037, -0.489321);
glVertex3f(-0.361786, -0.280945, -0.0588074);
glVertex3f(-0.0111389, -0.565186, -0.262848);
glVertex3f(-0.293854, -0.451569, 0.0235291);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.17191, -0.842719, -0.510169);
glVertex3f(0.282532, -0.491699, -0.285278);
glVertex3f(0.168762, -0.656647, -0.0511475);
glVertex3f(-0.0111389, -0.565186, -0.262848);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.42147, -0.892735, -0.159332);
glVertex3f(0.00344849, -0.631836, 0.247131);
glVertex3f(-0.293854, -0.451569, 0.0235291);
glVertex3f(0.168762, -0.656647, -0.0511475);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.333159, -0.906305, 0.260032);
glVertex3f(0.00344849, -0.631836, 0.247131);
glVertex3f(0.168762, -0.656647, -0.0511475);
glVertex3f(0.253418, -0.528687, 0.286377);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.764525, -0.567613, 0.305478);
glVertex3f(0.473358, -0.170807, 0.400909);
glVertex3f(0.253418, -0.528687, 0.286377);
glVertex3f(0.417633, -0.475372, -0.0255432);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.586159, -0.768494, -0.256583);
glVertex3f(0.282532, -0.491699, -0.285278);
glVertex3f(0.417633, -0.475372, -0.0255432);
glVertex3f(0.168762, -0.656647, -0.0511475);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.705977, -0.593585, -0.386334);
glVertex3f(-0.361786, -0.280945, -0.0588074);
glVertex3f(-0.267548, -0.15448, -0.425323);
glVertex3f(-0.0111389, -0.565186, -0.262848);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.141872, -0.760405, -0.633764);
glVertex3f(0.282532, -0.491699, -0.285278);
glVertex3f(-0.0111389, -0.565186, -0.262848);
glVertex3f(0.191071, -0.337128, -0.491211);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.27854, -0.381434, -0.881433);
glVertex3f(0.00231934, 0, -0.577454 );
glVertex3f(0.191071, -0.337128, -0.491211);
glVertex3f(-0.267548, -0.15448, -0.425323);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.679685, 0.0732876, 0.729834);
glVertex3f(-0.447205, 0.0761719, 0.315887);
glVertex3f(-0.344055, -0.147278, 0.434387);
glVertex3f(-0.317749, -0.0470581, 0.448822);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.694999, -0.631368, 0.34402);
glVertex3f(0.00344849, -0.631836, 0.247131);
glVertex3f(-0.0487366, -0.464478, 0.448853);
glVertex3f(-0.344055, -0.147278, 0.434387);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(-0.17093, -0.110087, 0.979114);
glVertex3f(-0.0549011, 0, 0.5 );
glVertex3f(-0.317749, -0.0470581, 0.448822);
glVertex3f(-0.0487366, -0.464478, 0.448853);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.796179, -0.46695, -0.384781);
glVertex3f(0.282532, -0.491699, -0.285278);
glVertex3f(0.471161, -0.290833, -0.138733);
glVertex3f(0.417633, -0.475372, -0.0255432);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.968681, -0.225635, -0.103661);
glVertex3f(0.525299, -0.042511, -0.17334);
glVertex3f(0.531952, -0.152008, 0.127167);
glVertex3f(0.471161, -0.290833, -0.138733);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.902522, -0.39741, 0.16589);
glVertex3f(0.473358, -0.170807, 0.400909);
glVertex3f(0.417633, -0.475372, -0.0255432);
glVertex3f(0.531952, -0.152008, 0.127167);
glEnd;
glBegin(GL_TRIANGLES);
glNormal3f(0.976342, 0.0429096, 0.21193);
glVertex3f(0.473358, -0.170807, 0.400909);
glVertex3f(0.531952, -0.152008, 0.127167);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?