问题是译者写的,不知道拼写是否正确。我开始学习Opengl,遇到了以下问题:当我旋转立方体时,灯光也随之旋转。我试图在每一帧中改变光的坐标,但它没有帮助。告诉我:出了什么问题。如果不麻烦,有可能,会导致修改版本。
代码:
unit MainUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,dglOpenGL,dglut;
type
TFMainForm = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
procedure SetupGL;
procedure uzglLightEnable;
procedure IdleHandler(Sender : TObject; var Done : Boolean);
public
procedure Render;
end;
var
FMainForm: TFMainForm;
dc:hdc;
hrc:hglrc;
XRot,YRot,ZRot:Single;
LightPos:TGLArrayf4;
BlueArray:TGLArrayf4;
GreenArray:TGLArrayf4;
myTex: glUint;
FrontNormal: TGLvectorf3=(0,0,1);
BackNormal: TGLvectorf3 =(0,0,-1);
LeftNormal: TGLvectorf3 =(-1,0,0);
RightNormal: TGLvectorf3=(1,0,0);
UpNormal: TGLvectorf3 =(0,1,0);
DownNormal: TGLvectorf3 =(0,-1,0);
FrontDownLeft :TGLvectorf3 = (-1,-1,-1);
FrontDownRight:TGLvectorf3 = (1,-1,-1);
FrontUpLeft :TGLvectorf3 = (-1,1,-1);
FrontUpRight :TGLvectorf3 = (1,1,-1);
BackDownLeft :TGLvectorf3 = (-1,-1,1);
BackDownRight:TGLvectorf3 = (1,-1,1);
BackUpLeft :TGLvectorf3 = (-1,1,1);
BackUpRight :TGLvectorf3 = (1,1,1);
const
NearClipping = 0.1;
FarClipping = 200;
procedure uzglTriangle(x1,y1,z1,x2,y2,z2,x3,y3,z3:single);
procedure uzglCube;
implementation
{$R *.dfm}
procedure uzglTriangle(x1,y1,z1,x2,y2,z2,x3,y3,z3:single);
begin
glBegin(GL_TRIANGLES);
glColor3f(0.3,0,0);glVertex3f(x1,y1,z1);
glColor3f(0.25,1,0);glVertex3f(x2,y2,z2);
glColor3f(0.7,0,1);glVertex3f(x3,y3,z3);
glEnd;
end;
procedure uzglCube;
begin
glBegin(GL_QUADS);
// Beginning of front
glNormal3fv(@FrontNormal);
glVertex3fv(@FrontDownLeft);
glVertex3fv(@FrontDownRight);
glVertex3fv(@FrontUpRight);
glVertex3fv(@FrontUpLeft);
// End of front
glEnd;
glBegin(GL_QUADS);
// Beginning of left
glNormal3fv(@LeftNormal);
glVertex3fv(@FrontDownLeft);
glVertex3fv(@FrontUpLeft);
glVertex3fv(@BackUpLeft);
glVertex3fv(@BackDownLeft);
// End of left
glEnd;
glBegin(GL_QUADS);
// Beginning of right
glNormal3fv(@RightNormal);
glVertex3fv(@FrontDownRight);
glVertex3fv(@FrontUpRight);
glVertex3fv(@BackUpRight);
glVertex3fv(@BackDownRight);
// End of right
glEnd;
glBegin(GL_QUADS);
// Beginning of up
glNormal3fv(@UpNormal);
glVertex3fv(@FrontUpRight);
glVertex3fv(@FrontUpLeft);
glVertex3fv(@BackUpLeft);
glVertex3fv(@BackUpRight);
// End of up
glEnd;
glBegin(GL_QUADS);
// Beginning of down
glNormal3fv(@DownNormal);
glVertex3fv(@FrontDownRight);
glVertex3fv(@FrontDownLeft);
glVertex3fv(@BackDownLeft);
glVertex3fv(@BackDownRight);
// End of down
glEnd;
glBegin(GL_QUADS);
// Beginning of back
glNormal3fv(@BackNormal);
glVertex3fv(@BackDownLeft);
glVertex3fv(@BackDownRight);
glVertex3fv(@BackUpRight);
glVertex3fv(@BackUpLeft);
// End of back
glEnd;
end;
procedure TFMainForm.SetupGL;
begin
glClearColor(0.3,0.4,0.7,0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_SMOOTH);
glDepthFunc(GL_LEQUAL);
glEnable(GL_TEXTURE_2D);
uzglLightEnable;
LightPos[0]:=0;
LightPos[1]:=0;
LightPos[2]:=1;
LightPos[3]:=1;
end;
procedure TFMainForm.uzglLightEnable;
begin
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
end;
procedure TFMainForm.Render;
var i:integer;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(45.0,ClientWidth/ClientHeight,NearClipping,FarClipping);
glTranslatef(0,0,-5);
glPushMatrix;
glRotatef(XRot,1,0,0);
uzglCube;
glPopMatrix;
glLightfv(GL_LIGHT0, GL_POSITION,@LightPos);
SwapBuffers(dc);
XRot:=XRot+1;
end;
procedure TFMainForm.IdleHandler(Sender : TObject; var Done : Boolean);
begin
Render;
Sleep(25);
Done:=false;
end;
procedure TFMainForm.FormCreate(Sender: TObject);
begin
dc:=GetDC(Handle);
if not InitOpenGL then
begin
ShowMessage('Печалька... Инициализация провалилась');
Application.Terminate;
end;
hrc:=CreateRenderingContext (dc,
[opDoubleBuffered],
32,
24,
8,
0,
0,
0);
ActivateRenderingContext(dc,hrc);
SetupGL;
Application.OnIdle:=IdleHandler;
FMainForm.OnResize(self);
end;
procedure TFMainForm.FormResize(Sender: TObject);
var tmpBool:boolean;
begin
glViewport(0,0,ClientWidth,ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(45.0,ClientWidth/ClientHeight,NearClipping,FarClipping);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
idleHandler(Sender, tmpBool);
end;
procedure TFMainForm.FormDestroy(Sender: TObject);
begin
DeactivateRenderingContext;
DestroyRenderingContext(hrc);
ReleaseDC(Handle,dc);
end;
procedure TFMainForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
case key of
'q':LightPos[0]:=LightPos[0]+0.1;
'a':LightPos[0]:=LightPos[0]-0.1;
'w':LightPos[1]:=LightPos[1]+0.1;
's':LightPos[1]:=LightPos[1]-0.1;
'e':LightPos[2]:=LightPos[2]+0.1;
'd':LightPos[2]:=LightPos[2]-0.1;
end;
end;
end.
dglOpenGL:http ://wiki.delphigl.com/index.php/dglOpenGL.pas/en