问题是由翻译写的,所以我不知道它是正确的。 我开始学习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
答案 0 :(得分:6)
我可以发现一些事情。
首先:你的法线不一致。改为
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);
其次:您正在GL_PROJECTION矩阵中转换多维数据集。 GL_PROJECTION矩阵应仅包含设置投影所需的内容。
你的立方体&灯应该由GL_MODELVIEW矩阵转换。
只有当窗口大小发生变化时,您的投影才会发生变化,因此可以进入FormResize事件。
procedure TForm2.FormResize(Sender: TObject);
var tmpBool:boolean;
begin
glViewport(0,0,ClientWidth,ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(45.0,ClientWidth/ClientHeight,NearClipping,FarClipping);
idleHandler(Sender, tmpBool);
end;
可以在SetupGL过程中设置GL_MODELVIEW矩阵。 注意gluLookAt()命令设置从(-2,3,-3)到(0,0,0)的视图。设置gluLookAt(0,3,-3,0,0,0,0,1,0);有一个'正面'的观点。
procedure TForm2.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);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
gluLookAt(-2, 3, -3, 0,0,0, 0,1,0);
uzglLightEnable;
LightPos[0]:=0;
LightPos[1]:=0;
LightPos[2]:=1;
LightPos[3]:=1;
end;
Render Proc变为。
procedure TForm2.Render;
var i:integer;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix;
glRotatef(XRot,1,0,0);
uzglCube;
if FTransformLights then
begin
glLightfv(GL_LIGHT0, GL_POSITION,@LightPos);
glPopMatrix;
end
else
begin
glPopMatrix;
glLightfv(GL_LIGHT0, GL_POSITION,@LightPos);
end;
SwapBuffers(dc);
XRot:=XRot+1;
end;
注意:我添加了一个私有表单变量FTransformLights,如果为true,则使用立方体旋转灯光,否则灯光在GL_MODELVIEW矩阵“弹出”回原始状态后转换为不变形(标识) )。
我添加了'x'键来切换灯是否旋转。 这是完整修改的列表。希望对其进行分类。
unit MainUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,dglOpenGL;
type
TFMainForm = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
FTransformLights:Boolean;
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);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
gluLookAt(-2, 3, -3, 0,0,0, 0,1,0);
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;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix;
glRotatef(XRot,1,0,0);
uzglCube;
if FTransformLights then
begin
glLightfv(GL_LIGHT0, GL_POSITION,@LightPos);
glPopMatrix;
end
else
begin
glPopMatrix;
glLightfv(GL_LIGHT0, GL_POSITION,@LightPos);
end;
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);
FTransformLights:=false;
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);
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;
'x':FTransformLights:=NOT FTransformLights;
end;
end;
end.