我开始学习opengl,在了解了light之后开始阅读fog,但是当我尝试将fog输入到程序中时却没有看到。问题是什么?
PS这里是代码:
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);
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;
XPos,ZPos:single;
LightPos:TGLArrayf4=(0,0,1,1);
BlueArray:TGLArrayf4;
GreenArray:TGLArrayf4=(0,1,0,0);
FogStart:TGLArrayf4=(0,0,-5,0);
FogEnd: TGLArrayf4=(0,0,5,0);
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);
glColor3f(1,0,0);glVertex3fv(@FrontDownLeft);
glColor3f(0,1,0);glVertex3fv(@FrontDownRight);
glColor3f(0,0,1);glVertex3fv(@FrontUpRight);
glColor3f(0.686,0.847,1);glVertex3fv(@FrontUpLeft);
// End of front
glEnd;
glBegin(GL_QUADS);
// Beginning of left
glNormal3fv(@LeftNormal);
glColor3f(1,0,0);glVertex3fv(@FrontDownLeft);
glColor3f(0,1,0);glVertex3fv(@FrontUpLeft);
glColor3f(0,0,1);glVertex3fv(@BackUpLeft);
glColor3f(0.686,0.847,1);glVertex3fv(@BackDownLeft);
// End of left
glEnd;
glBegin(GL_QUADS);
// Beginning of right
glNormal3fv(@RightNormal);
glColor3f(1,0,0);glVertex3fv(@FrontDownRight);
glColor3f(0,1,0);glVertex3fv(@FrontUpRight);
glColor3f(0,0,1);glVertex3fv(@BackUpRight);
glColor3f(0.686,0.847,1);glVertex3fv(@BackDownRight);
// End of right
glEnd;
glBegin(GL_QUADS);
// Beginning of up
glNormal3fv(@UpNormal);
glColor3f(1,0,0);glVertex3fv(@FrontUpRight);
glColor3f(0,1,0);glVertex3fv(@FrontUpLeft);
glColor3f(0,0,1);glVertex3fv(@BackUpLeft);
glColor3f(0.686,0.847,1);glVertex3fv(@BackUpRight);
// End of up
glEnd;
glBegin(GL_QUADS);
// Beginning of down
glNormal3fv(@DownNormal);
glColor3f(1,0,0);glVertex3fv(@FrontDownRight);
glColor3f(0,1,0);glVertex3fv(@FrontDownLeft);
glColor3f(0,0,1);glVertex3fv(@BackDownLeft);
glColor3f(0.686,0.847,1);glVertex3fv(@BackDownRight);
// End of down
glEnd;
glBegin(GL_QUADS);
// Beginning of back
glNormal3fv(@BackNormal);
glColor3f(1,0,0);glVertex3fv(@BackDownLeft);
glColor3f(0,1,0);glVertex3fv(@BackDownRight);
glColor3f(0,0,1);glVertex3fv(@BackUpRight);
glColor3f(0.686,0.847,1);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;
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_START,1);
glFogf(Gl_FOG_END,50);
glFogfv(GL_FOG_COLOR, @GreenArray);
glFogF(GL_FOG_DENSITY,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
gluLookAt(-2, 3, -4, 0,0,0, 0,1,0);
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_MODELVIEW);
glPushMatrix;
glRotatef(XRot,1,0.5,0.5);
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);
idleHandler(Sender, tmpBool);
end;
procedure TFMainForm.FormDestroy(Sender: TObject);
begin
DeactivateRenderingContext;
DestroyRenderingContext(hrc);
ReleaseDC(Handle,dc);
end;
end.