各位高手帮我看看这个OpenGL程序,为什么显示效果这么差? ( 积分: 100 )

  • 主题发起人 主题发起人 modico
  • 开始时间 开始时间
M

modico

Unregistered / Unconfirmed
GUEST, unregistred user!
显示效果很差,各位大侠帮我看看为什么?

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OpenGL, ExtCtrls;

type
pGLArrayf3=^TGLArrayf3;

TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
DC:HDC;
RC:HGLRC;
xg,yg,zg,xp,yp,zp:GLfloat;

hangNo,lieNo:integer;
p:pGLArrayf3;

procedure Draw;
procedure drawmodel(p:pGLArrayf3);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure setupPixelFormat(DC:HDC); //象素设置过程
const
pfd:TPIXELFORMATDESCRIPTOR = (
nSize:sizeof(TPIXELFORMATDESCRIPTOR); // 大小
nVersion:1; // 版本
dwFlags:PFD_SUPPORT_OPENGL or PFD_DRAW_TO_WINDOW or PFD_DOUBLEBUFFER; // 支持 double buffer
iPixelType:PFD_TYPE_RGBA; // 颜色类型 RGB+Alpha
cColorBits:24; // 色深
cRedBits:0; cRedShift:0; // color bits
cGreenBits:0;
cGreenShift:0;
cBlueBits:0;
cBlueShift:0;
cAlphaBits:0;
cAlphaShift:0;// 不使用 alpha buffer
cAccumBits:0;// 不使用 accumulation buffer
cAccumRedBits:0;
cAccumGreenBits:0;
cAccumBlueBits:0;
cAccumAlphaBits:0;
cDepthBits:16; // depth buffer
cStencilBits:0;
cAuxBuffers:0;
iLayerType:PFD_MAIN_PLANE;
bReserved:0;
dwLayerMask:0;
dwVisibleMask:0;
dwDamageMask:0;// 具体含义大家可以参考 MSDN
);
var pixelFormat:integer;
begin
pixelFormat := ChoosePixelFormat(DC, @pfd);//选择象素格式
if (pixelFormat = 0) then exit;
if (SetPixelFormat(DC, pixelFormat, @pfd) <> TRUE) then //设置格式
exit;
end;

procedure GLInit;//初始化完成坐标转换建立过程,设置光照;
const
light0_position:TGLArrayf4=( 80.0, 80.0, 160.0, 0.0);
light1_position:TGLArrayf4=( -80.0, -80.0, 160.0, 0.0);
l0ambient: TGLArrayf4=( 1, 0.3, 0.3, 0.3);
l1ambient: TGLArrayf4=( 1, 1, 1, 1);
begin
glMatrixMode(GL_PROJECTION);//设置物体对象
glFrustum(-0.1, 0.1, -0.1, 0.1, 0.1, 25.0); //设置左右下上进远距离
glMatrixMode(GL_MODELVIEW);//设置观察者

glEnable(GL_DEPTH_TEST);//使用 depth buffer
glEnable(GL_NORMALIZE);
glShadeModel(GL_FLAT);
glCullFace(GL_BACK);

// 设置光照
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, @light0_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, @l0ambient);
glEnable(GL_LIGHT0);

glLightfv(GL_LIGHT1, GL_POSITION, @light1_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, @l1ambient);
glEnable(GL_LIGHT1);

glEnable(GL_POLYGON_SMOOTH);
end;

function getNormal(p1,p2,p3:TGLArrayf3):TGLArrayf3;//法向量计算函数
var a,b:TGLArrayf3;
begin
// 得到两个向量a 和 b
a[0]:=p2[0]-p1[0]; a[1]:=p2[1]-p1[1]; a[2]:=p2[2]-p1[2];
b[0]:=p3[0]-p1[0]; b[1]:=p3[1]-p1[1]; b[2]:=p3[2]-p1[2];
// 计算叉积
result[0]:=a[1]*b[2]-a[2]*b[1];
result[1]:=a[2]*b[0]-a[0]*b[2];
result[2]:=a[0]*b[1]-a[1]*b[0];
end;

procedure TForm1.Draw;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

glLoadIdentity;

glTranslatef(xp,0,0);glTranslatef(0,yp,0);glTranslatef(0,0,zp-12);
glRotated(xg,1,0,0);glRotated(yg,0,1,0);glRotated(zg,0,0,1);
drawmodel(p);

//glTranslatef(2,0,0);
//drawmodel(p);

SwapBuffers(wglGetCurrentDC);
end;


procedure TForm1.drawmodel(p: pGLArrayf3);
var
i , j : integer;
n, a1, a2, a3, a4, ha1 ,ha2: TGLArrayf3;
pp,pt:pGLArrayf3;
begin
pp:=p;pt:=p;//inc(pp,lieNo);

glBegin(GL_POLYGON);
a1:=pp^;inc(pp);a2:=pp^;inc(pp);a3:=pp^;inc(pp);n:=getNormal(a1,a2,a3);glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);
for i:=1 to lieNo-3 do begin
a1:=pp^;inc(pp);glVertex3fv(@a1);
end;
glEnd;

glBegin(GL_TRIANGLES);
for i:=2 to hangNo do begin
ha1:=pt^;ha2:=pp^;
for j:=2 to lieNo do begin
a1:=pt^;a2:=pp^;inc(pp);inc(pt);a3:=pp^;a4:=pt^;
n := getNormal(a1,a2,a3);
glNormal3fv(@n);
glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);

n := getNormal(a1,a3,a4);
glNormal3fv(@n);
glVertex3fv(@a1); glVertex3fv(@a3); glVertex3fv(@a4);
end;

a1:=pt^;a2:=pp^;a3:=ha2;
n := getNormal(a1,a2,a3);
glNormal3fv(@n);
glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);
a1:=pt^;a2:=ha2;a3:=ha1;
n := getNormal(a1,a2,a3);
glNormal3fv(@n);
glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);

inc(pp);inc(pt);
end;
glEnd;


glBegin(GL_POLYGON);
a1:=pt^;inc(pt);a2:=pt^;inc(pt);a3:=pt^;inc(pt);
n:=getNormal(a1,a2,a3);
glNormal3fv(@n);glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);
for i:=1 to lieNo-3 do begin
a1:=pt^;inc(pt);glVertex3fv(@a1);
end;
glEnd;

end;

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
f:TextFile;
pp:pGLArrayf3;
begin
DC:=GetDC(Handle);//获取一个DC
SetupPixelFormat(DC);//设置DC的象素格式
RC:=wglCreateContext(DC); //建立一个着色环境RC
wglMakeCurrent(DC, RC);//将建立的RC激活
GLInit;//初始化OpenGL



AssignFile(f,'../m/d.dat');Reset(f);
Readln(f,hangNo,lieNO);
GetMem(p,sizeof(TGLArrayf3)*hangNo*lieNo);pp:=p;
for i:=1 to hangNo*lieNo do begin
read(f,pp^[0],pp^[1],pp^[2]);
Inc(pp);
end;
CloseFile(f);
xg:=0;yg:=120;zg:=0;xp:=0;yp:=0;zp:=0;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
wglMakeCurrent(0,0);//取消当前RC和当前DC
wglDeleteContext(RC);//删除RC
ReleaseDC(Handle, DC);//释放DC

FreeMem(p,sizeof(TGLArrayf3)*hangNo*lieNo);
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case key of
81 : xg:=xg+1;
87 : xg:=xg-1;
65 : yg:=yg+1;
83 : yg:=yg-1;
90 : zg:=zg+1;
88 : zg:=zg-1;


37 : xp:=xp+0.1;
39 : xp:=xp-0.1;
38 : yp:=yp+0.1;
40 : yp:=yp-0.1;
188: zp:=zp+0.1;
190: zp:=zp-0.1;
end;

Caption:=format(' xg=%.4f yg=%.4f zg=%.4f xp=%.4f yp=%.4f zp=%.4f',[xg,yg,zg,xp,yp,zp]);

Draw;
end;

end.
 
显示效果很差,各位大侠帮我看看为什么?

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OpenGL, ExtCtrls;

type
pGLArrayf3=^TGLArrayf3;

TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
DC:HDC;
RC:HGLRC;
xg,yg,zg,xp,yp,zp:GLfloat;

hangNo,lieNo:integer;
p:pGLArrayf3;

procedure Draw;
procedure drawmodel(p:pGLArrayf3);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure setupPixelFormat(DC:HDC); //象素设置过程
const
pfd:TPIXELFORMATDESCRIPTOR = (
nSize:sizeof(TPIXELFORMATDESCRIPTOR); // 大小
nVersion:1; // 版本
dwFlags:PFD_SUPPORT_OPENGL or PFD_DRAW_TO_WINDOW or PFD_DOUBLEBUFFER; // 支持 double buffer
iPixelType:PFD_TYPE_RGBA; // 颜色类型 RGB+Alpha
cColorBits:24; // 色深
cRedBits:0; cRedShift:0; // color bits
cGreenBits:0;
cGreenShift:0;
cBlueBits:0;
cBlueShift:0;
cAlphaBits:0;
cAlphaShift:0;// 不使用 alpha buffer
cAccumBits:0;// 不使用 accumulation buffer
cAccumRedBits:0;
cAccumGreenBits:0;
cAccumBlueBits:0;
cAccumAlphaBits:0;
cDepthBits:16; // depth buffer
cStencilBits:0;
cAuxBuffers:0;
iLayerType:PFD_MAIN_PLANE;
bReserved:0;
dwLayerMask:0;
dwVisibleMask:0;
dwDamageMask:0;// 具体含义大家可以参考 MSDN
);
var pixelFormat:integer;
begin
pixelFormat := ChoosePixelFormat(DC, @pfd);//选择象素格式
if (pixelFormat = 0) then exit;
if (SetPixelFormat(DC, pixelFormat, @pfd) <> TRUE) then //设置格式
exit;
end;

procedure GLInit;//初始化完成坐标转换建立过程,设置光照;
const
light0_position:TGLArrayf4=( 80.0, 80.0, 160.0, 0.0);
light1_position:TGLArrayf4=( -80.0, -80.0, 160.0, 0.0);
l0ambient: TGLArrayf4=( 1, 0.3, 0.3, 0.3);
l1ambient: TGLArrayf4=( 1, 1, 1, 1);
begin
glMatrixMode(GL_PROJECTION);//设置物体对象
glFrustum(-0.1, 0.1, -0.1, 0.1, 0.1, 25.0); //设置左右下上进远距离
glMatrixMode(GL_MODELVIEW);//设置观察者

glEnable(GL_DEPTH_TEST);//使用 depth buffer
glEnable(GL_NORMALIZE);
glShadeModel(GL_FLAT);
glCullFace(GL_BACK);

// 设置光照
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, @light0_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, @l0ambient);
glEnable(GL_LIGHT0);

glLightfv(GL_LIGHT1, GL_POSITION, @light1_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, @l1ambient);
glEnable(GL_LIGHT1);

glEnable(GL_POLYGON_SMOOTH);
end;

function getNormal(p1,p2,p3:TGLArrayf3):TGLArrayf3;//法向量计算函数
var a,b:TGLArrayf3;
begin
// 得到两个向量a 和 b
a[0]:=p2[0]-p1[0]; a[1]:=p2[1]-p1[1]; a[2]:=p2[2]-p1[2];
b[0]:=p3[0]-p1[0]; b[1]:=p3[1]-p1[1]; b[2]:=p3[2]-p1[2];
// 计算叉积
result[0]:=a[1]*b[2]-a[2]*b[1];
result[1]:=a[2]*b[0]-a[0]*b[2];
result[2]:=a[0]*b[1]-a[1]*b[0];
end;

procedure TForm1.Draw;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

glLoadIdentity;

glTranslatef(xp,0,0);glTranslatef(0,yp,0);glTranslatef(0,0,zp-12);
glRotated(xg,1,0,0);glRotated(yg,0,1,0);glRotated(zg,0,0,1);
drawmodel(p);

//glTranslatef(2,0,0);
//drawmodel(p);

SwapBuffers(wglGetCurrentDC);
end;


procedure TForm1.drawmodel(p: pGLArrayf3);
var
i , j : integer;
n, a1, a2, a3, a4, ha1 ,ha2: TGLArrayf3;
pp,pt:pGLArrayf3;
begin
pp:=p;pt:=p;//inc(pp,lieNo);

glBegin(GL_POLYGON);
a1:=pp^;inc(pp);a2:=pp^;inc(pp);a3:=pp^;inc(pp);n:=getNormal(a1,a2,a3);glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);
for i:=1 to lieNo-3 do begin
a1:=pp^;inc(pp);glVertex3fv(@a1);
end;
glEnd;

glBegin(GL_TRIANGLES);
for i:=2 to hangNo do begin
ha1:=pt^;ha2:=pp^;
for j:=2 to lieNo do begin
a1:=pt^;a2:=pp^;inc(pp);inc(pt);a3:=pp^;a4:=pt^;
n := getNormal(a1,a2,a3);
glNormal3fv(@n);
glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);

n := getNormal(a1,a3,a4);
glNormal3fv(@n);
glVertex3fv(@a1); glVertex3fv(@a3); glVertex3fv(@a4);
end;

a1:=pt^;a2:=pp^;a3:=ha2;
n := getNormal(a1,a2,a3);
glNormal3fv(@n);
glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);
a1:=pt^;a2:=ha2;a3:=ha1;
n := getNormal(a1,a2,a3);
glNormal3fv(@n);
glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);

inc(pp);inc(pt);
end;
glEnd;


glBegin(GL_POLYGON);
a1:=pt^;inc(pt);a2:=pt^;inc(pt);a3:=pt^;inc(pt);
n:=getNormal(a1,a2,a3);
glNormal3fv(@n);glVertex3fv(@a1); glVertex3fv(@a2); glVertex3fv(@a3);
for i:=1 to lieNo-3 do begin
a1:=pt^;inc(pt);glVertex3fv(@a1);
end;
glEnd;

end;

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
f:TextFile;
pp:pGLArrayf3;
begin
DC:=GetDC(Handle);//获取一个DC
SetupPixelFormat(DC);//设置DC的象素格式
RC:=wglCreateContext(DC); //建立一个着色环境RC
wglMakeCurrent(DC, RC);//将建立的RC激活
GLInit;//初始化OpenGL



AssignFile(f,'../m/d.dat');Reset(f);
Readln(f,hangNo,lieNO);
GetMem(p,sizeof(TGLArrayf3)*hangNo*lieNo);pp:=p;
for i:=1 to hangNo*lieNo do begin
read(f,pp^[0],pp^[1],pp^[2]);
Inc(pp);
end;
CloseFile(f);
xg:=0;yg:=120;zg:=0;xp:=0;yp:=0;zp:=0;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
wglMakeCurrent(0,0);//取消当前RC和当前DC
wglDeleteContext(RC);//删除RC
ReleaseDC(Handle, DC);//释放DC

FreeMem(p,sizeof(TGLArrayf3)*hangNo*lieNo);
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case key of
81 : xg:=xg+1;
87 : xg:=xg-1;
65 : yg:=yg+1;
83 : yg:=yg-1;
90 : zg:=zg+1;
88 : zg:=zg-1;


37 : xp:=xp+0.1;
39 : xp:=xp-0.1;
38 : yp:=yp+0.1;
40 : yp:=yp-0.1;
188: zp:=zp+0.1;
190: zp:=zp-0.1;
end;

Caption:=format(' xg=%.4f yg=%.4f zg=%.4f xp=%.4f yp=%.4f zp=%.4f',[xg,yg,zg,xp,yp,zp]);

Draw;
end;

end.
 
可以这样生成一个模型

procedure TForm1.Button1Click(Sender: TObject);
const
hangNo=150;
lieNo=100;
var
i,j:integer;
f:TextFile;
r1,r2,r3:real;
begin
AssignFile(f,'d.dat');
Rewrite(f);
Writeln(f,hangNO,' ',lieNo);
for i:=0 to hangNo-1 do
//for j:=lieNo-1 downto 0 do begin
for j:=0 to lieNo-1 do begin
r3:=(8/(hangNo-1)*i);r3:=sqrt(r3);
r1:=r3*sin(360/(lieNo)*j/57.3);
r2:=r3*cos(360/(lieNo)*j/57.3);
r3:=1+i/20;
Writeln(f,format(' %.8f %.8f %.8f',[r1,r2,r3]));
end;
CloseFile(f);
end;
 
后退
顶部