Skocz do zawartości

[Delphi][OpenGL]Światła


Brainer

Polecane posty

Witam. :)

 

Napotkałem mały problem przy próbie użycia świateł. Obudowałem światło taką klasą:

CODE{ .: TLight :. }

TLight = class(TObject)

private

{ Private declarations }

FIndex: GLenum;

FInfinite: Boolean;

FAmbient: TColorVector;

FConstAtt: Single;

FDiffuse: TColorVector;

FSpecular: TColorVector;

FSpotCutoff: Single;

FSpotDirection: TAffineVector;

FQuadraticAtt: Single;

FLinearAtt: Single;

FSpotExponent: Single;

FPos: TCoordinates4;

procedure SetAmbient(const Value: TColorVector);

procedure SetConstAtt(const Value: Single);

procedure SetDiffuse(const Value: TColorVector);

procedure SetInfinite(const Value: Boolean);

procedure SetLinearAtt(const Value: Single);

procedure SetQuadraticAtt(const Value: Single);

procedure SetSpecular(const Value: TColorVector);

procedure SetSpotCutoff(const Value: Single);

procedure SetSpotDirection(const Value: TAffineVector);

procedure SetSpotExponent(const Value: Single);

procedure SetPos(const Value: TCoordinates4);

public

{ Public declarations }

constructor Create();

destructor Destroy(); override;

 

procedure Enable();

procedure Disable();

 

property Ambient: TColorVector read FAmbient write SetAmbient;

property Diffuse: TColorVector read FDiffuse write SetDiffuse;

property Specular: TColorVector read FSpecular write SetSpecular;

property Position: TCoordinates4 read FPos write SetPos;

property SpotDirection: TAffineVector read FSpotDirection

write SetSpotDirection;

property SpotExponent: Single read FSpotExponent write SetSpotExponent;

property SpotCutoff: Single read FSpotCutoff write SetSpotCutoff;

property ConstAtt: Single read FConstAtt write SetConstAtt;

property LinearAtt: Single read FLinearAtt write SetLinearAtt;

property QuadraticAtt: Single read FQuadraticAtt write SetQuadraticAtt;

property Infinite: Boolean read FInfinite write SetInfinite;

end;

{...}

 

{ TLight }

 

constructor TLight.Create;

begin

inherited Create();

 

if (NumLights

begin

Inc(NumLights);

FIndex := NumLights - 1;

 

FAmbient := ColorVectorMake(0.0, 0.0, 0.0, 1.0);

FDiffuse := ColorVectorMake(1.0, 1.0, 1.0, 1.0);

FSpecular := ColorVectorMake(1.0, 1.0, 1.0, 1.0);

FPos := TCoordinates4.Create();

FPos.Z := 1.0;

FInfinite := True;

FSpotDirection := AffineVectorMake(0.0, 0.0, -1.0);

FSpotExponent := 0.0;

FSpotCutoff := 180.0;

FConstAtt := 1.0;

FLinearAtt := 0.0;

FQuadraticAtt := 0.0;

 

Log.LogStatus('Lights: ' + IntToStr(NumLights), 'Objects Unit');

end else

begin

Log.LogError('Cannot create more than eight lights!', 'Objects Unit');

Halt(1);

end;

end;

 

destructor TLight.Destroy;

begin

Log.LogStatus('Light destroyed', 'Objects Unit');

 

Disable();

FPos.Free();

 

inherited Destroy();

end;

 

procedure TLight.Disable;

begin

glDisable(FIndex);

end;

 

procedure TLight.Enable;

begin

glEnable(GL_LIGHTING);

glEnable(FIndex);

end;

 

procedure TLight.SetAmbient(const Value: TColorVector);

begin

FAmbient := Value;

glLightfv(FIndex, GL_AMBIENT, @FAmbient);

end;

 

procedure TLight.SetConstAtt(const Value: Single);

begin

FConstAtt := Value;

glLightf(FIndex, GL_CONSTANT_ATTENUATION, FConstAtt);

end;

 

procedure TLight.SetDiffuse(const Value: TColorVector);

begin

FDiffuse := Value;

glLightfv(FIndex, GL_DIFFUSE, @FDiffuse);

end;

 

procedure TLight.SetInfinite(const Value: Boolean);

begin

FInfinite := Value;

if Value then

FPos.W := 0.0

else

FPos.W := 1.0;

glLightfv(FIndex, GL_POSITION, @FPos.AsVector);

end;

 

procedure TLight.SetLinearAtt(const Value: Single);

begin

FLinearAtt := Value;

glLightf(FIndex, GL_LINEAR_ATTENUATION, FConstAtt);

end;

 

procedure TLight.SetPos(const Value: TCoordinates4);

begin

FPos.Assign(Value);

if FInfinite then

FPos.W := 0.0

else

FPos.W := 1.0;

glLightfv(FIndex, GL_POSITION, @FPos.AsVector);

end;

 

procedure TLight.SetQuadraticAtt(const Value: Single);

begin

FQuadraticAtt := Value;

glLightf(FIndex, GL_QUADRATIC_ATTENUATION, FConstAtt);

end;

 

procedure TLight.SetSpecular(const Value: TColorVector);

begin

FSpecular := Value;

glLightfv(FIndex, GL_SPECULAR, @FSpecular);

end;

 

procedure TLight.SetSpotCutoff(const Value: Single);

begin

FSpotCutoff := Value;

glLightf(FIndex, GL_SPOT_CUTOFF, FSpotCutoff);

end;

 

procedure TLight.SetSpotDirection(const Value: TAffineVector);

begin

FSpotDirection := Value;

glLightfv(FIndex, GL_SPOT_DIRECTION, @FSpotDirection);

end;

 

procedure TLight.SetSpotExponent(const Value: Single);

begin

FSpotExponent := Value;

glLightf(FIndex, GL_SPOT_EXPONENT, FSpotExponent);

end;

 

 

I teraz, gdy próbuję użyć tej klasy (czyli włączyć światło), scena jest strasznie ciemna. :( Oto kodzik, którego używam:

CODEprocedure TTestEngine.AdditionalInit;

begin

{ ... }

 

// OpenGL initialization

glShadeModel(GL_SMOOTH);

glClearDepth(1.0);

glEnable(GL_DEPTH_TEST);

glEnable(GL_CULL_FACE);

glEnable(GL_TEXTURE_2D);

glEnable(GL_COLOR_MATERIAL);

glDepthFunc(GL_LEQUAL);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

 

// Light setup

Light.Enable();

Light.Position.X := 6.0;

Light.Position.Y := 0.5;

Light.Position.Z := 0.0;

Light.SpotDirection := AffineVectorMake(-1.0, 0.0, -1.0);

Light.Ambient := ColorVectorMake(0.5, 0.5, 0.5, 1.0);

Light.Diffuse := ColorVectorMake(0.5, 0.5, 0.5, 1.0);

Light.Specular := ColorVectorMake(1.0, 1.0, 1.0, 1.0);

Light.SpotCutoff := 40.0;

Light.SpotExponent := 30.0;

Light.Infinite := False;

 

{ ... }

end;

 

Próbowałem też zdefiniować materiały (funkcja glMaterialfv) i też nic... :huh:

Ktoś wie o co chodzi? :unsure: Załączyłem też screena - może się przyda...

Obrazek

 

Pozdrawiam! :)

Link do komentarza
Udostępnij na innych stronach

Albo problem z parametrami, które trzeba dobrać zazwyczaj doświadczalnie albo nie policzyłeś/podałeś normalnych dla tego sześcianu i to najbardziej prawdopodobne bo jest jednolity wszędzie jak widzę z miniaturki.

 

Poza tym w tej swojej metodzie: Enable jest:

glEnable(FIndex);
gsbą)bs6Ś(Ą91ĄŃąŃĄ((%Ą91ĄŃ(%91ĄŃ(

To wygląda jak Int ? Tylko, żeby włączyć konkretne światło przekazujesz do glEnable: GL_LIGHT0 itd.

 

Ew. zmień kolor diffuse w porównaniu do ambient... lub inne takie.

Ot taka mini-strona moja po godzinach :)http://www.wnetrzekuchni.pl

Link do komentarza
Udostępnij na innych stronach

Zarchiwizowany

Ten temat jest archiwizowany i nie można dodawać nowych odpowiedzi.

×
×
  • Utwórz nowe...