procedure TForm1.FormCreate(Sender: TObject);
begin
glClearColor(1,1,1,1);
//设置刷新背景色
glClear(GL_COLOR_BUFFER_BIT);
//刷新背景
glBegin(GL_LINE_LOOP);
glColor3f(1,0,0);
//设置当前颜色
glVertex3f(-0.9,-0.9,0);
glVertex3f(0.9,-0.9,0);
glVertex3f(0.9,0.9,0);
glVertex3f(-0.9,0.9,0);
glEnd();
glFlush();
swapbuffers(canvas.Handle);
end;
begin
glClearColor(1,1,1,1);
//设置刷新背景色
glClear(GL_COLOR_BUFFER_BIT);
//刷新背景
glBegin(GL_LINE_LOOP);
glColor3f(1,0,0);
//设置当前颜色
glVertex3f(-0.9,-0.9,0);
glVertex3f(0.9,-0.9,0);
glVertex3f(0.9,0.9,0);
glVertex3f(-0.9,0.9,0);
glEnd();
glFlush();
swapbuffers(canvas.Handle);
end;
解决方案 »
- 求助!!!!!Delphi如何处理TabSheet中Form的KeyDown事件
- 关于用adoconnection连接 win2000 + sql2000的特殊问题
- 请问如何在DELPHI中实现SQL-SERVER备份的数据倒入功能
- 用TQuery如何创建一个临时表,完成之后如何用?
- 在线等待,马上解决马上给分,很基础的!(代码)
- ★Delphi的控制台程序中有没有方法直接判断按下的按键,就像是TP7.0中的readkey,而不用输入回车键?
- 急:请教如何操作word中的表格!请附源码!谢谢!
- 关于分辨率,和Delphi中Post速度慢的问题
- 弹出菜单的位置问题,简单,快进
- dbgrid如何实现像EXCEL那样整行复制粘贴呢?
- 求助:请专家看看这例风湿病!本市医院检测不出结果!
- 有关在Delphi中应用OpenGL载入Bmp位图
若没。请下载一个Delphi编写的OpenGL简单例程。然后照葫芦画瓢
PaintBox其实就是Form的一部分,它的Handle其实就是Form的Handle.
Delphi给大家开了个玩笑,没有给PaintBox一个Handle。
}
unit frmmain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OpenGL, ExtCtrls, StdCtrls, glAux;type
TAUX_RGBImageRec = record
sizeX, sizeY: GLint;
data : pointer;
end;
PAUX_RGBImageRec = ^TAUX_RGBImageRec;type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
m_hrc : HGLRC;
m_hdc : HDC;
m_hdl : HWND;
m_success : BOOL;
m_width,
m_height : Integer;
m_texture : array [0..0] of GLuint;
m_data : PBYTE; public
{ Public declarations }
function DrawGLScene: BOOL;
function InitGL: BOOL;
function LoadGLTextures: BOOL;
end;var
Form1: TForm1;
rtri: GLfloat; // Angle For The Triangle ( NEW )
rquad: GLfloat; // Angle For The Quad ( NEW )implementation{$R *.dfm}procedure glGenTextures(n: GLsizei; var textures: GLuint); stdcall; external opengl32;
procedure glBindTexture(target: GLenum; texture: GLuint); stdcall; external opengl32;function LoadBitmap( sName: String ):PAUX_RGBImageRec;
var
bmfh : BITMAPFILEHEADER;
bmih : BITMAPINFOHEADER;
air : TAUX_RGBImageRec;
fm : TFileStream;
begin
result := nil; fm := TFileStream.Create( sName, fmOpenRead );
fm.Read( bmfh, sizeof(bmfh) );
fm.Read( bmih, sizeof(bmih) );
if bmih.biBitCount <> 24 then
exit; Form1.m_data := AllocMem( bmih.biSizeImage );
fm.Position := bmfh.bfOffBits;
fm.Read( Form1.m_data^, bmih.biSizeImage );
fm.Free; air.sizeX := bmih.biWidth;
air.sizeY := bmih.biHeight;
air.data := Form1.m_data; result := @air;
end;function ChildWndProc( hWnd: HWND; msg: UINT; wParam: WPARAM; lParam:
LPARAM ):LRESULT; stdcall;
begin
result := DefWindowProc( hWnd, msg, wParam, lParam );
end;procedure TForm1.FormCreate(Sender: TObject);
var
pfd : TPixelFormatDescriptor;
PixelFormat : Integer;
wc : TWndClass;
begin
//ControlStyle := ControlStyle+[csOpaque];
m_success := FALSE;
m_width := 256;
m_height := 256; // 为OPENGL子窗体注册窗体类
FillChar( wc, sizeof(wc), 0 );
with wc do
begin
style := CS_HREDRAW or CS_VREDRAW or CS_OWNDC;
lpfnWndProc := @ChildWndProc;
hInstance := HInstance;
lpszClassName := 'YafeiChildWindowClass';
end; if Windows.RegisterClass( wc ) = 0 then
begin
MessageBox( 0,'Failed To Register The Window Class.','Error',MB_ICONERROR );
exit;
end; // 创建OPENGL支窗体
m_hdl := CreateWindow( 'YafeiChildWindowClass', 'YafeiChildWindowClass',
WS_CHILD or WS_VISIBLE, 32, 32, m_width, m_height,
Handle, 0, HInstance, nil );
if m_hdl = 0 then
begin
MessageBox( 0, 'Window creation error.','Error', MB_ICONERROR );
exit;
end; FillChar( pfd, sizeof(pfd), 0 );
with pfd do
begin
nSize := sizeof(TPixelFormatDescriptor);
nVersion := 1;
dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
iPixelType := PFD_TYPE_RGBA;
cColorBits := 24;
cDepthBits := 16;
iLayerType := PFD_MAIN_PLANE;
end; // 获得子窗体HDC
m_hdc := GetDC( m_hdl );
if m_hdc = 0 then
begin
MessageBox( 0,'Cannot create a device context.','Error', MB_ICONERROR );
exit;
end; PixelFormat := ChoosePixelFormat( m_hdc, @pfd );
if PixelFormat = 0 then
begin
MessageBox( Handle, 'ChoosePixelFormat failed.', 'Error', MB_ICONERROR );
exit;
end; // 设置子窗体HDC像素格式
if not SetPixelFormat( m_hdc, PixelFormat, @pfd ) then
begin
MessageBox( Handle, 'SetPixelFormat failed.', 'Error', MB_ICONERROR );
exit;
end; // 设置OPENGL着色上下文
m_hrc := wglCreateContext( m_hdc );
if m_hrc = 0 then
begin
MessageBox( Handle, 'CreateContext failed.', 'Error', MB_ICONERROR );
exit;
end; // 把OPENGL子窗体HDC设定为 OPENGL着色上下文
if not wglMakeCurrent( m_hdc, m_hrc ) then
begin
MessageBox( Handle, 'MakeCurrent failed.', 'Error', MB_ICONERROR );
exit;
end; // 初始化OPENGL
LoadGLTextures();
InitGL();
//LoadGLTextures(); m_success := TRUE;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
try
wglMakeCurrent( m_hdc, 0 );
wglDeleteContext( m_hrc );
ReleaseDC( m_hdl, m_hdc );
DestroyWindow( m_hdl );
except
;
end;
end;function TForm1.InitGL: BOOL;
begin
glViewport( 0, 0, m_width, m_height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, m_width / m_height, 0.1, 100.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity; glShadeModel( GL_SMOOTH );
glClearColor( 0.0, 0.0, 0.0, 0.5 );
glEnable( GL_TEXTURE_2D );
glClearDepth( 1.0 );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
result := TRUE;
end;function TForm1.LoadGLTextures: BOOL;
var
pimg : PTAUX_RGBImageRec;
begin
result := FALSE;
//pimg := LoadBitmap( 'E:\Backup\GLLEARN\Lesson06\Debug\Data\1.bmp' );
pimg := auxDIBImageLoadA( 'E:\Backup\GLLEARN\Lesson06\Debug\Data\1.bmp' ); if pimg = nil then
exit; glGenTextures( 1, m_texture[0] );
glBindTexture( GL_TEXTURE_2D, m_texture[0] );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, 3, pimg^.sizeX, pimg^.sizeY, 0,
GL_RGB, GL_UNSIGNED_BYTE, pimg^.data ); //FreeMem( pimg^.data );
//FreeMem( pimg );
end;function TForm1.DrawGLScene: BOOL;
begin
glClear( GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glTranslatef(0.0,0.0,-3.0); glBindTexture( GL_TEXTURE_2D, m_texture[0] ); glRotatef(rquad,1.0,0.0,0.0);
//glColor3f(0.5,0.5,1.0); glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0, 0.0, 1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0); glEnd(); rtri := rtri + 0.2;
rquad := rquad - 0.55;
result := TRUE;
end;procedure TForm1.FormPaint(Sender: TObject);
begin
DrawGLScene();
SwapBuffers( m_hdc );
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
DrawGLScene();
SwapBuffers( m_hdc );
end;end.
Left = 192
Top = 107
Width = 559
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
object Timer1: TTimer
Interval = 55
OnTimer = Timer1Timer
Left = 72
Top = 32
end
endunit Glaux;interfaceuses Windows,Opengl;type
TAUX_RGBImageRec= record
sizeX, sizeY: GLint;
data: pointer;
end;
PTAUX_RGBImageRec= ^TAUX_RGBImageRec;function auxDIBImageLoadA(const dibfile: PChar): PTAUX_RGBImageRec; stdcall;
procedure auxWireSphere(value: GLdouble);stdcall;
procedure auxSolidSphere(value: GLdouble);stdcall;
procedure auxWireCube(value: GLdouble);stdcall;
procedure auxSolidCube(value: GLdouble);stdcall;
procedure auxWireBox(value,value1,value2: GLdouble);stdcall;
procedure auxSolidBox(value,value1,value2: GLdouble);stdcall;
procedure auxWireTorus(value,value1: GLdouble);stdcall;
procedure auxSolidTorus(value,value1: GLdouble);stdcall;
procedure auxWireCylinder(value,value1: GLdouble);stdcall;
procedure auxSolidCylinder(value,value1: GLdouble);stdcall;
procedure auxWireIcosahedron(value: GLdouble);stdcall;
procedure auxSolidIcosahedron(value: GLdouble);stdcall;
procedure auxWireOctahedron(value: GLdouble);stdcall;
procedure auxSolidOctahedron(value: GLdouble);stdcall;
procedure auxWireTetrahedron(value: GLdouble);stdcall;
procedure auxSolidTetrahedron(value: GLdouble);stdcall;
procedure auxWireDodecahedron(value: GLdouble);stdcall;
procedure auxSolidDodecahedron(value: GLdouble);stdcall;
procedure auxWireCone(value,value1: GLdouble);stdcall;
procedure auxSolidCone(value,value1: GLdouble);stdcall;
procedure auxWireTeapot(value: GLdouble);stdcall;
procedure auxSolidTeapot(value: GLdouble);stdcall;const
glaux1 = 'glaux.dll';implementationfunction auxDIBImageLoadA; external glaux1;
procedure auxWireSphere;external glaux1;
procedure auxSolidSphere;external glaux1;
procedure auxWireCube;external glaux1;
procedure auxSolidCube;external glaux1;
procedure auxWireBox;external glaux1;
procedure auxSolidBox;external glaux1;
procedure auxWireTorus;external glaux1;
procedure auxSolidTorus;external glaux1;
procedure auxWireCylinder;external glaux1;
procedure auxSolidCylinder;external glaux1;
procedure auxWireIcosahedron;external glaux1;
procedure auxSolidIcosahedron;external glaux1;
procedure auxWireOctahedron;external glaux1;
procedure auxSolidOctahedron;external glaux1;
procedure auxWireTetrahedron;external glaux1;
procedure auxSolidTetrahedron;external glaux1;
procedure auxWireDodecahedron;external glaux1;
procedure auxSolidDodecahedron;external glaux1;
procedure auxWireCone;external glaux1;
procedure auxSolidCone;external glaux1;
procedure auxWireTeapot;external glaux1;
procedure auxSolidTeapot;external glaux1;
end.
从此类继承下来。一个最简单的OPENGL窗体就生成了unit U_Base;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OPenGL;type
TBaseForm = class(TForm)
procedure FormResize(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
//
public
h_RC: HGLRC; // Rendering Context(着色描述表)。
h_DC: HDC; // Device Context(设备描述表)
keys: array[0..255] of Boolean; // 用于键盘例程的数组
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function SetPixel: Boolean; //设置像素
function SetRCToDC: Boolean; //关联RC和DC
procedure ProcessKeys; virtual; //键盘处理函数
procedure glInit(); virtual; //初始化
procedure glDraw(); virtual; //绘图
procedure Idle(Sender: TObject; var Done: Boolean); virtual; //循环重画
end;implementation{$R *.dfm}{ TF_Base }constructor TBaseForm.Create(AOwner: TComponent);
begin
inherited;
if not SetRCToDC then
begin
Application.Terminate;
end;
glInit(); // 初始化新建的GL窗口
Application.OnIdle := Idle;
end;destructor TBaseForm.Destroy;
begin
wglMakeCurrent(h_DC, 0);
wglDeleteContext(h_RC);
inherited;
end;procedure TBaseForm.glDraw;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存
glLoadIdentity(); // 重置当前的模型观察矩阵
end;procedure TBaseForm.glInit;
begin
glClearColor(0.0, 0.0, 0.0, 0.0); // 黑色背景
//阴影平滑通过多边形精细的混合色彩,并对外部光进行平滑。
glShadeModel(GL_SMOOTH); // 启用阴影平滑
glClearDepth(1.0); // 设置深度缓存
glEnable(GL_DEPTH_TEST); // 启用深度测试
glDepthFunc(GL_LESS); // 所作深度测试的类型
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 真正精细的透视修正
end;procedure TBaseForm.Idle(Sender: TObject; var Done: Boolean);
begin
Done := FALSE;
glDraw(); // 重画屏幕
SwapBuffers(h_DC); // 交换缓存 (双缓存) if keys[VK_ESCAPE] = True then // 如果按下了ESC键
Close
else
ProcessKeys; // 检查键盘消息
end;procedure TBaseForm.ProcessKeys;
begin
end;function TBaseForm.SetPixel: Boolean;
var
pfd: TPIXELFORMATDESCRIPTOR; //像素结构
PixelFormat: GLuint; // 象素格式
begin
Result := True;
with pfd do
begin
nSize := SizeOf(TPIXELFORMATDESCRIPTOR); // 像素结构大小
nVersion := 1; // 版本号
dwFlags := PFD_DRAW_TO_WINDOW //支持窗口
or PFD_SUPPORT_OPENGL // 支持OpenGL
or PFD_DOUBLEBUFFER; // 支持双缓冲
iPixelType := PFD_TYPE_RGBA; //RGBA 格式
cColorBits := 32; // 选定色彩深度
cRedBits := 0; // 忽略的色彩位
cRedShift := 0; // 忽略的色彩位
cGreenBits := 0; // 忽略的色彩位
cGreenShift := 0; // 忽略的色彩位
cBlueBits := 0; // 忽略的色彩位
cBlueShift := 0; // 忽略的色彩位
cAlphaBits := 0; // 无Alpha缓存
cAlphaShift := 0; // 忽略Shift Bit
cAccumBits := 0; // 无聚集缓存
cAccumRedBits := 0; // 忽略聚集位
cAccumGreenBits := 0; // 忽略聚集位
cAccumBlueBits := 0; // 忽略聚集位
cAccumAlphaBits := 0; // 忽略聚集位
cDepthBits := 32; // 16位 Z-缓存 (深度缓存)
cStencilBits := 0; // 无模板缓存
cAuxBuffers := 0; // 无辅助缓存
iLayerType := PFD_MAIN_PLANE; // 主绘图层
bReserved := 0; // 保留
dwLayerMask := 0; // 忽略层遮罩
dwVisibleMask := 0; // 忽略层遮罩
dwDamageMask := 0; // 忽略层遮罩
end;
//为设备描述表(HDC)找到最匹配的象素格式
PixelFormat := ChoosePixelFormat(h_DC, @pfd);
if (PixelFormat = 0) then
begin
MessageBox(0, '找不到最匹配的象素格式', '错误', MB_OK or MB_ICONERROR);
Result := False;
Exit;
end;
//设置最匹配的象素格式为当前的像素格式.
if (not SetPixelFormat(h_DC, PixelFormat, @pfd)) then
begin
MessageBox(0, '设置当前的像素格式失败', '错误', MB_OK or MB_ICONERROR);
Result := False;
Exit;
end;
end;function TBaseForm.SetRCToDC: Boolean;
begin
Result := True;
h_DC := GetDC(Handle);
if SetPixel = False then
begin
MessageBox(0, '设置像素格式失败', '错误', MB_OK or MB_ICONERROR);
Result := False;
Exit;
end;
//创建着色描述表
h_RC := wglCreateContext(h_DC);
if (h_RC = 0) then
begin
MessageBox(0, '无法创建OpenGL 绘制描述表', '错误', MB_OK or MB_ICONERROR);
Result := False;
Exit;
end;
//已经取得了设备描述表和着色描述表。
//激活着色描述表
if (not wglMakeCurrent(h_DC, h_RC)) then
begin
MessageBox(0, '不能激活着色描述表', '错误', MB_OK or MB_ICONERROR);
Result := False;
Exit;
end;
end;procedure TBaseForm.FormResize(Sender: TObject);
begin
if (Height = 0) then // 防止高度为0,产生除0异常
Height := 1;
glViewport(0, 0, Width, Height); // 重置当前的视口(Viewport)
glMatrixMode(GL_PROJECTION); // 选择投影矩阵
glLoadIdentity(); // 重置投影矩阵
gluPerspective(45.0, Width / Height, 0.1, 100.0); // 计算窗口的外观比例
glMatrixMode(GL_MODELVIEW); // 选择模型观察矩阵
glLoadIdentity(); // 重置模型观察矩阵
end;procedure TBaseForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
keys[Key] := True;
end;procedure TBaseForm.FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
keys[Key] := False;
end;end.