Jimmy Valavanis
11-06-2004, 02:04 PM
Does anyone know how can I have lights in DelphiX immediate mode? I tried
DXDraw.D3DDevice7.SetLight(0, light);
DXDraw.D3DDevice7.LightEnable(0, true);
and
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_LI GHTING, 1);
but it didn't work.
Can anyone post a working source code example, or a link to such an example?
The source code look like this:
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DXClass, DXDraws, DirectX, MMSystem, D3DUtils, ExtCtrls;
type
TMainForm = class(TDXForm)
DXTimer: TDXTimer;
DXImageList: TDXImageList;
Panel1: TPanel;
DXDraw: TDXDraw;
procedure DXTimerTimer(Sender: TObject; LagCount: Integer);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DXDrawInitializeSurface(Sender: TObject);
procedure DXDrawInitialize(Sender: TObject);
procedure DXDrawFinalize(Sender: TObject);
private
g_Vertices: array[0..64 * 64 - 1] of TD3DVertex;
g_Indexes: array[0..63 * 63 * 6 - 1] of word;
FTexture: array[0..0] of TDirect3DTexture2;
procedure MakeLandScape;
procedure AddLight;
procedure FrameMovie(Time: Double);
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.DXDrawInitialize(Sender: TObject);
var
i: Integer;
begin
for i:=Low(FTexture) to High(FTexture) do
FTexture[i] := TDirect3DTexture2.Create(DXDraw, DXImageList.Items[i].Picture.Graphic, False);
end;
procedure TMainForm.DXDrawFinalize(Sender: TObject);
var
i: Integer;
begin
for i:=Low(FTexture) to High(FTexture) do
begin
FTexture[i].Free;
FTexture[i] := nil;
end;
end;
procedure TMainForm.MakeLandScape;
var
n0: TD3DVector;
i, j, k: integer;
begin
// Define the normals for the landscape
n0 := MakeD3DVector( 0.0, 1.0, 0.0 );
randseed := 1;
i := 0;
for j := 0 to 63 do
g_Vertices[i * 64 + j] :=
MakeD3DVERTEX(
MakeD3DVECTOR(i - 32.0, random, j - 32.0),
n0, i - 32.0, j - 32.0);
for i := 1 to 63 do
begin
j := 0;
g_Vertices[i * 64 + j] :=
MakeD3DVERTEX(
MakeD3DVECTOR(i - 32.0, random, j - 32.0),
n0, i - 32.0, j - 32.0);
for j := 1 to 63 do
g_Vertices[i * 64 + j] :=
MakeD3DVERTEX(
MakeD3DVECTOR(i - 32.0,
(g_Vertices[i * 64 + j - 1].y + g_Vertices[(i - 1) * 64 + j].y) /2 + 0.5 - random,
j - 32.0),
n0, i/4, j/4);
end;
k := 0;
for i := 0 to 62 do
for j := 0 to 62 do
begin
g_Indexes[k] := i * 64 + j;
inc(k);
g_Indexes[k] := i * 64 + j + 1;
inc(k);
g_Indexes[k] := (i + 1) * 64 + j;
inc(k);
g_Indexes[k] := g_Indexes[k - 1];
inc(k);
g_Indexes[k] := g_Indexes[k - 3];
inc(k);
g_Indexes[k] := (i + 1) * 64 + j + 1;
inc(k);
end;
end;
procedure TMainForm.FrameMovie(Time: Double);
var matView, matRotate: TD3DMatrix;
begin
FilLChar(matView, SizeOf(matView), 0);
matView._11 := 0.75;
matView._22 := cos(-0.5);
matView._23 := sin(-0.5);
matView._32 := -sin(-0.5);
matView._33 := cos(-0.5);
matView._41 := 5.0;
matView._43 := 5.0;
matView._44 := 0.1;
DXDraw.D3DDevice7.SetTransform( D3DTRANSFORMSTATE_VIEW, matView);
// Set the world matrix to rotate along the y-axis, in sync with the
// timekey
FillChar(matRotate, SizeOf(matRotate), 0);
matRotate._11 := cos(-Time);
matRotate._13 := sin(Time);
matRotate._22 := 1.0;
matRotate._31 := -sin(Time);
matRotate._33 := cos(Time);
matRotate._44 := 1.0;
DXDraw.D3DDevice7.SetTransform( D3DTRANSFORMSTATE_WORLD, matRotate);
end;
procedure TMainForm.DXDrawInitializeSurface(Sender: TObject);
var
vp: TD3DViewport7;
mtrl: TD3DMaterial7;
matProj: TD3DMatrix;
begin
{ Viewport }
FillChar(vp, SizeOf(vp), 0);
vp.dwX := 0;
vp.dwY := 0;
vp.dwWidth := DXDraw.SurfaceWidth;
vp.dwHeight := DXDraw.SurfaceHeight;
vp.dvMinZ := 0.0;
vp.dvMaxZ := 1.0;
DXDraw.D3DDevice7.SetViewport(vp);
{ Material }
FillChar(mtrl, SizeOf(mtrl), 0);
mtrl.ambient.r := 1.0;
mtrl.ambient.g := 1.0;
mtrl.ambient.b := 1.0;
mtrl.ambient.a := 1.0;
mtrl.diffuse.r := 1.0;
mtrl.diffuse.g := 1.0;
mtrl.diffuse.b := 1.0;
mtrl.diffuse.a := 1.0;
mtrl.specular.r := 1.0;
mtrl.specular.g := 1.0;
mtrl.specular.b := 1.0;
mtrl.specular.a := 1.0;
DXDraw.D3DDevice7.SetMaterial(mtrl);
// Set the projection matrix. Note that the view and world matrices are
// set in the App_FrameMove() function, so they can be animated each
// frame.
FilLChar(matProj, SizeOf(matProj), 0);
matProj._11 := 1.0;
matProj._22 := 1.0;
matProj._33 := 1.0;
matProj._34 := 1.0;
matProj._43 := -1.0;
DXDraw.D3DDevice7.SetTransform( D3DTRANSFORMSTATE_PROJECTION, matProj );
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_DI THERENABLE, 1);
DXDraw.D3DDevice7.LightEnable(0, true);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_SP ECULARENABLE, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_DI FFUSEMATERIALSOURCE, 0);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_LI GHTING, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_SH ADEMODE, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_AM BIENT, $A0A0A0A0);
{ Make landscape }
MakeLandScape;
end;
procedure TMainForm.AddLight;
var light: TD3DLight7;
begin
FillChar(light, sizeof(light), chr(0));
light.dltType := D3DLIGHT_POINT;
light.dcvDiffuse.r := 1.0;
light.dcvDiffuse.g := 1.0;
light.dcvDiffuse.b := 1.0;
light.dcvSpecular.r := 1.0;
light.dcvSpecular.g := 1.0;
light.dcvSpecular.b := 1.0;
light.dcvAmbient.b := 1.0;
light.dcvAmbient.g := 1.0;
light.dcvAmbient.b := 1.0;
light.dvPosition := VectorMulS(MakeD3DVector(1,1,0), 4.5);
light.dvAttenuation1 := 0.4;
light.dvRange := D3DLIGHT_RANGE_MAX;
DXDraw.D3DDevice7.SetLight(0, light);
end;
procedure TMainForm.DXTimerTimer(Sender: TObject; LagCount: Integer);
var
r: TD3DRect;
begin
if not DXDraw.CanDraw then Exit;
{ Frame Movie }
FrameMovie(GetTickCount/1000);
{ Clear Screen }
r.x1 := 0;
r.y1 := 0;
r.x2 := DXDraw.SurfaceWidth;
r.y2 := DXDraw.SurfaceHeight;
if DXDraw.ZBuffer<>nil then
DXDraw.D3DDevice7.Clear(1, r, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, $000000, 1, 0)
else
DXDraw.D3DDevice7.Clear(1, r, D3DCLEAR_TARGET, $000000, 1, 0);
{ Draw Screen }
asm
FINIT
end;
AddLight;
DXDraw.D3DDevice7.BeginScene;
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_CU LLMODE, Ord(D3DCULL_NONE));
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_SP ECULARENABLE, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_DI FFUSEMATERIALSOURCE, 0);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_LI GHTING, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_SH ADEMODE, 1);
DXDraw.D3DDevice7.LightEnable(0, true);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_AM BIENT, $A0A0A0A0);
DXDraw.D3DDevice7.SetTexture( 0, FTexture[0].Surface.IDDSurface7);
DXDraw.D3DDevice7.DrawIndexedPrimitive(
D3DPT_TRIANGLELIST, D3DFVF_VERTEX,
g_Vertices[0], 64 * 64,
g_Indexes[0], 63 * 63 * 6, 0);
DXDraw.D3DDevice7.EndScene;
asm
FINIT
end;
{ Draw FrameRate }
with DXDraw.Surface.Canvas do
begin
try
Brush.Style := bsClear;
Font.Color := clWhite;
Font.Size := 12;
Textout(0, 0, 'FPS: '+inttostr(DXTimer.FrameRate));
if doHardware in DXDraw.NowOptions then
Textout(0, 14, 'Device: Hardware')
else
Textout(0, 14, 'Device: Software');
finally
Release; { Indispensability }
end;
end;
DXDraw.Flip;
end;
procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
{ Application end }
if Key=VK_ESCAPE then
Close;
{ Screen mode change }
if (ssAlt in Shift) and (Key=VK_RETURN) then
begin
DXDraw.Finalize;
if doFullScreen in DXDraw.Options then
begin
RestoreWindow;
DXDraw.Cursor := crDefault;
BorderStyle := bsSizeable;
DXDraw.Options := DXDraw.Options - [doFullScreen];
end else
begin
StoreWindow;
DXDraw.Cursor := crNone;
BorderStyle := bsNone;
DXDraw.Options := DXDraw.Options + [doFullScreen];
end;
DXDraw.Initialize;
end;
end;
end.
DXDraw.D3DDevice7.SetLight(0, light);
DXDraw.D3DDevice7.LightEnable(0, true);
and
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_LI GHTING, 1);
but it didn't work.
Can anyone post a working source code example, or a link to such an example?
The source code look like this:
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DXClass, DXDraws, DirectX, MMSystem, D3DUtils, ExtCtrls;
type
TMainForm = class(TDXForm)
DXTimer: TDXTimer;
DXImageList: TDXImageList;
Panel1: TPanel;
DXDraw: TDXDraw;
procedure DXTimerTimer(Sender: TObject; LagCount: Integer);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DXDrawInitializeSurface(Sender: TObject);
procedure DXDrawInitialize(Sender: TObject);
procedure DXDrawFinalize(Sender: TObject);
private
g_Vertices: array[0..64 * 64 - 1] of TD3DVertex;
g_Indexes: array[0..63 * 63 * 6 - 1] of word;
FTexture: array[0..0] of TDirect3DTexture2;
procedure MakeLandScape;
procedure AddLight;
procedure FrameMovie(Time: Double);
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.DXDrawInitialize(Sender: TObject);
var
i: Integer;
begin
for i:=Low(FTexture) to High(FTexture) do
FTexture[i] := TDirect3DTexture2.Create(DXDraw, DXImageList.Items[i].Picture.Graphic, False);
end;
procedure TMainForm.DXDrawFinalize(Sender: TObject);
var
i: Integer;
begin
for i:=Low(FTexture) to High(FTexture) do
begin
FTexture[i].Free;
FTexture[i] := nil;
end;
end;
procedure TMainForm.MakeLandScape;
var
n0: TD3DVector;
i, j, k: integer;
begin
// Define the normals for the landscape
n0 := MakeD3DVector( 0.0, 1.0, 0.0 );
randseed := 1;
i := 0;
for j := 0 to 63 do
g_Vertices[i * 64 + j] :=
MakeD3DVERTEX(
MakeD3DVECTOR(i - 32.0, random, j - 32.0),
n0, i - 32.0, j - 32.0);
for i := 1 to 63 do
begin
j := 0;
g_Vertices[i * 64 + j] :=
MakeD3DVERTEX(
MakeD3DVECTOR(i - 32.0, random, j - 32.0),
n0, i - 32.0, j - 32.0);
for j := 1 to 63 do
g_Vertices[i * 64 + j] :=
MakeD3DVERTEX(
MakeD3DVECTOR(i - 32.0,
(g_Vertices[i * 64 + j - 1].y + g_Vertices[(i - 1) * 64 + j].y) /2 + 0.5 - random,
j - 32.0),
n0, i/4, j/4);
end;
k := 0;
for i := 0 to 62 do
for j := 0 to 62 do
begin
g_Indexes[k] := i * 64 + j;
inc(k);
g_Indexes[k] := i * 64 + j + 1;
inc(k);
g_Indexes[k] := (i + 1) * 64 + j;
inc(k);
g_Indexes[k] := g_Indexes[k - 1];
inc(k);
g_Indexes[k] := g_Indexes[k - 3];
inc(k);
g_Indexes[k] := (i + 1) * 64 + j + 1;
inc(k);
end;
end;
procedure TMainForm.FrameMovie(Time: Double);
var matView, matRotate: TD3DMatrix;
begin
FilLChar(matView, SizeOf(matView), 0);
matView._11 := 0.75;
matView._22 := cos(-0.5);
matView._23 := sin(-0.5);
matView._32 := -sin(-0.5);
matView._33 := cos(-0.5);
matView._41 := 5.0;
matView._43 := 5.0;
matView._44 := 0.1;
DXDraw.D3DDevice7.SetTransform( D3DTRANSFORMSTATE_VIEW, matView);
// Set the world matrix to rotate along the y-axis, in sync with the
// timekey
FillChar(matRotate, SizeOf(matRotate), 0);
matRotate._11 := cos(-Time);
matRotate._13 := sin(Time);
matRotate._22 := 1.0;
matRotate._31 := -sin(Time);
matRotate._33 := cos(Time);
matRotate._44 := 1.0;
DXDraw.D3DDevice7.SetTransform( D3DTRANSFORMSTATE_WORLD, matRotate);
end;
procedure TMainForm.DXDrawInitializeSurface(Sender: TObject);
var
vp: TD3DViewport7;
mtrl: TD3DMaterial7;
matProj: TD3DMatrix;
begin
{ Viewport }
FillChar(vp, SizeOf(vp), 0);
vp.dwX := 0;
vp.dwY := 0;
vp.dwWidth := DXDraw.SurfaceWidth;
vp.dwHeight := DXDraw.SurfaceHeight;
vp.dvMinZ := 0.0;
vp.dvMaxZ := 1.0;
DXDraw.D3DDevice7.SetViewport(vp);
{ Material }
FillChar(mtrl, SizeOf(mtrl), 0);
mtrl.ambient.r := 1.0;
mtrl.ambient.g := 1.0;
mtrl.ambient.b := 1.0;
mtrl.ambient.a := 1.0;
mtrl.diffuse.r := 1.0;
mtrl.diffuse.g := 1.0;
mtrl.diffuse.b := 1.0;
mtrl.diffuse.a := 1.0;
mtrl.specular.r := 1.0;
mtrl.specular.g := 1.0;
mtrl.specular.b := 1.0;
mtrl.specular.a := 1.0;
DXDraw.D3DDevice7.SetMaterial(mtrl);
// Set the projection matrix. Note that the view and world matrices are
// set in the App_FrameMove() function, so they can be animated each
// frame.
FilLChar(matProj, SizeOf(matProj), 0);
matProj._11 := 1.0;
matProj._22 := 1.0;
matProj._33 := 1.0;
matProj._34 := 1.0;
matProj._43 := -1.0;
DXDraw.D3DDevice7.SetTransform( D3DTRANSFORMSTATE_PROJECTION, matProj );
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_DI THERENABLE, 1);
DXDraw.D3DDevice7.LightEnable(0, true);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_SP ECULARENABLE, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_DI FFUSEMATERIALSOURCE, 0);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_LI GHTING, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_SH ADEMODE, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_AM BIENT, $A0A0A0A0);
{ Make landscape }
MakeLandScape;
end;
procedure TMainForm.AddLight;
var light: TD3DLight7;
begin
FillChar(light, sizeof(light), chr(0));
light.dltType := D3DLIGHT_POINT;
light.dcvDiffuse.r := 1.0;
light.dcvDiffuse.g := 1.0;
light.dcvDiffuse.b := 1.0;
light.dcvSpecular.r := 1.0;
light.dcvSpecular.g := 1.0;
light.dcvSpecular.b := 1.0;
light.dcvAmbient.b := 1.0;
light.dcvAmbient.g := 1.0;
light.dcvAmbient.b := 1.0;
light.dvPosition := VectorMulS(MakeD3DVector(1,1,0), 4.5);
light.dvAttenuation1 := 0.4;
light.dvRange := D3DLIGHT_RANGE_MAX;
DXDraw.D3DDevice7.SetLight(0, light);
end;
procedure TMainForm.DXTimerTimer(Sender: TObject; LagCount: Integer);
var
r: TD3DRect;
begin
if not DXDraw.CanDraw then Exit;
{ Frame Movie }
FrameMovie(GetTickCount/1000);
{ Clear Screen }
r.x1 := 0;
r.y1 := 0;
r.x2 := DXDraw.SurfaceWidth;
r.y2 := DXDraw.SurfaceHeight;
if DXDraw.ZBuffer<>nil then
DXDraw.D3DDevice7.Clear(1, r, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, $000000, 1, 0)
else
DXDraw.D3DDevice7.Clear(1, r, D3DCLEAR_TARGET, $000000, 1, 0);
{ Draw Screen }
asm
FINIT
end;
AddLight;
DXDraw.D3DDevice7.BeginScene;
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_CU LLMODE, Ord(D3DCULL_NONE));
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_SP ECULARENABLE, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_DI FFUSEMATERIALSOURCE, 0);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_LI GHTING, 1);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_SH ADEMODE, 1);
DXDraw.D3DDevice7.LightEnable(0, true);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_AM BIENT, $A0A0A0A0);
DXDraw.D3DDevice7.SetTexture( 0, FTexture[0].Surface.IDDSurface7);
DXDraw.D3DDevice7.DrawIndexedPrimitive(
D3DPT_TRIANGLELIST, D3DFVF_VERTEX,
g_Vertices[0], 64 * 64,
g_Indexes[0], 63 * 63 * 6, 0);
DXDraw.D3DDevice7.EndScene;
asm
FINIT
end;
{ Draw FrameRate }
with DXDraw.Surface.Canvas do
begin
try
Brush.Style := bsClear;
Font.Color := clWhite;
Font.Size := 12;
Textout(0, 0, 'FPS: '+inttostr(DXTimer.FrameRate));
if doHardware in DXDraw.NowOptions then
Textout(0, 14, 'Device: Hardware')
else
Textout(0, 14, 'Device: Software');
finally
Release; { Indispensability }
end;
end;
DXDraw.Flip;
end;
procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
{ Application end }
if Key=VK_ESCAPE then
Close;
{ Screen mode change }
if (ssAlt in Shift) and (Key=VK_RETURN) then
begin
DXDraw.Finalize;
if doFullScreen in DXDraw.Options then
begin
RestoreWindow;
DXDraw.Cursor := crDefault;
BorderStyle := bsSizeable;
DXDraw.Options := DXDraw.Options - [doFullScreen];
end else
begin
StoreWindow;
DXDraw.Cursor := crNone;
BorderStyle := bsNone;
DXDraw.Options := DXDraw.Options + [doFullScreen];
end;
DXDraw.Initialize;
end;
end;
end.