在TForm中应用OpenGL

简介: 一 新建一工程,设保存为OpenGLApp.dpr,窗口为Form1 二 在Form1中加入Panel1和Button1,unit1.pas代码为 unit Unit1; interface uses Windows,messages,Sysutils,classes,graphics,c...

一 新建一工程,设保存为OpenGLApp.dpr,窗口为Form1

二 在Form1中加入Panel1和Button1,unit1.pas代码为

unit Unit1;

interface

uses

Windows,messages,Sysutils,classes,graphics,controls,Forms,Dialogs,OpenGL,Exectrs,Stdctrls;

type

TForm1=class(TForm)

Panel1:TPanel;

Button1:TButton;

procedure FormCreate(Sender:TObject);

procedure PanelResize(Sender:TObject);

procedure FormDestroy(Sender:TObject);

procedure FormKeyPress(Sender:TObject;var key:char);

procedure Button1Click(Sender:TObject);

private

rc:HGLRC;

dc:HDC;

ElapsedTime,AppStart,LastTime:dword;

procedure glDraw;

procedure Idle(Sender:TObject;var Done:boolean);

public

end;

var

Form1:TForm1;

implementation

{$R *.DFM}

procedure TForm1.glDraw()

begin

glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glTranslatef(0,0,-4);

glRotatef(ElapsedTime/10,0,1,0);

glBegin(GL_TRIANGLES);

glColor3f(1,00); glVertex(-1,-1,0);

glColor3f(0,10); glVertex(1,-1,0);

glColor3f(0,0,1); glVertex(0,1,0);

glEnd()

end;

//OpenGL初始化

procedure glInit()

begin

glClearColor(0.0,0.0,0.0,0.0);

glShadeModal(GL_SMOOTH);

glClearDepth(1.0);

glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LESS);

glHint(GL_DEPSPECTIVE_CORRECTION_HINT,GL_NICEST);

end;

procedure TForm1.FormCreate(Sender:TObject);

var pfd:TPIXELFormatDescriptor;

pf:integer;

begin

dc :=GetDC(Panel1.Handle);

pfd.nSize :=sizeof(pfd); //让Panel1获取句柄

pfd.dwFlags:=PFD_DRAW_TO_WINOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER or 0);

pfd.ColorBits :=32;

pf :=ChoosePixelFormat(dc,@pfd);

SetPixelFormat(dc,pf,@pfd);

rc :=wglCreateContext(dc);

wglMakeCurrent(dc,rc);

glInit;

PanelResize(Sender);

AppStart :=GetTickCount();

Application.OnIdle :=Idle;

end;

procedure TForm1.FormDestroy(Sender:TObject);

begin

wglMakeCurrent(0,0);

wglDeleteContext(rc);

end;

procedure TForm1.Idle(Sender:TObject;var Done:Boolean);

begin

Done :=False;

LastTime :=ElapsedTime;

ElapsedTime :=GetTickCount()-AppStart;

glDraw();

SwapBuffers(DC);

end;

procedure TForm1.Resize(Sender:TObject);

begin

glVewport(0,0,Penel1.Width,panel1.Height);

glMatrixMod(GL_PROJECTION);

glLoadIdentity();

gluPerspective(45,0,Panel1.Width/Panel1.Height,1,0,500.0);

glMatrixMode(GL_MODEVIEW);

end;

procedure TForm1.FormKeyPress(Sender:TObject;var Key:char);

begin

if key=#27 then

Close;

end;

procedure TForm1.Button1Click(Sender:TObject);

begin

Close;

end;

end.

相关文章
|
数据可视化 Unix API
什么是OpenGL
OpenGL(Open Graphics Library,译为“开放式图形库”) 是用于渲染2D、3D矢量图形的跨语言、跨平台的应用程序编程接口(API)。这个接口由近350个不同的函数调用组成,用来绘制从简单的图形元件到复杂的三维景象。OpenGL常用于CAD、虚拟现实、科学可视化程序和电子游戏开发。
334 0
什么是OpenGL
|
存储 API 开发者
一、了解 OpenGL 常见专有名词
了解 OpenGL 常见专有名词
174 0
|
编译器 异构计算
GLKit详解 (下) 与 OpenGL GLSL 初探 (3) (11)
GLKViewController关于更新方法的补充
125 0
GLKit详解 (下) 与 OpenGL GLSL 初探 (3) (11)
|
Android开发
OpenGL 中的颜色混合和使用
因为 Latex 公式显示有问题,建议在微信公众号点击阅读原文获得更好的阅读体验
459 0
OpenGL 中的颜色混合和使用
|
C语言 Windows
|
Web App开发 前端开发 Android开发