Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Do Mär 28, 2024 14:28

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 6 Beiträge ] 
Autor Nachricht
BeitragVerfasst: Mi Okt 27, 2010 06:50 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Im working on an application to render Minecraft Levels. The lighting information is stored in a Byte Array with each 4-bits belonging to a different Block in the world. Im wondering if the following should extract the 4-bits correctly:

Code:
function GetLight(Data : Pointer; ID : Integer) : Byte;
begin
  if Odd(ID) then
  Result := Byte(Pointer(Cardinal(Data) + (ID-1) div 2*SizeOf(Byte))^)
  else
  Result := Byte(Pointer(Cardinal(Data) + (ID) div 2*SizeOf(Byte))^);

  if Odd(ID) then
  begin
    Result := Result shl 4;
    Result := Result shr 4;
  end
  else
  Result := Result shr 4;
end;


Problem is that it doesn't seem to get the correct information as parts of the level which should be bright are completely black.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
BeitragVerfasst: Mi Okt 27, 2010 08:55 
Offline
DGL Member
Benutzeravatar

Registriert: Sa Aug 18, 2007 18:47
Beiträge: 694
Wohnort: Köln
Programmiersprache: Java
hi,

i would try this approach:
Code:
function GetLight(Data : Pointer; ID : Integer) : Byte;
var
  index:integer;
  data:byte;
  bits:byte;
begin
  index := ID div 2;
  data := Byte(Pointer(Cardinal(Data) + index )^)
  if Odd(ID) then
    bits := data and 15; // mask lower 4 bits
  else
    bits := (data shr 4) and 15; // move upper 4 bits down
  Result := bits;
end;


maybe the second "and 15" isn't needed. but i'm not sure if the "shr" cuts off or rotates the bits. i'm betting 80% on cuting off. :)
and i'm not sure of the pointer-moving part. maybe i've broken it. another reason why i hate pointers...

_________________
Es werde Licht.
glEnable(GL_LIGHTING);
Und es ward Licht.


Zitat aus einem Java Buch: "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off"

on error goto next


Nach oben
 Profil  
Mit Zitat antworten  
BeitragVerfasst: Mi Okt 27, 2010 17:10 
Offline
DGL Member
Benutzeravatar

Registriert: Di Mai 18, 2004 16:45
Beiträge: 2621
Wohnort: Berlin
Programmiersprache: Go, C/C++
Shr and shl are base on the processor shifting operation and both drop the overflow and fill with 0.
So you can remove the "and 15".

_________________
"Wer die Freiheit aufgibt um Sicherheit zu gewinnen, der wird am Ende beides verlieren"
Benjamin Franklin

Projekte: https://github.com/tak2004


Nach oben
 Profil  
Mit Zitat antworten  
BeitragVerfasst: Mi Okt 27, 2010 19:24 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Thanks but it looks like either both of our code is wrong or both is right (Gives the same results).

P.S You can't have Data as a Parameter and as a Local Variable :P .

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
BeitragVerfasst: Mi Okt 27, 2010 22:08 
Offline
DGL Member
Benutzeravatar

Registriert: Di Apr 29, 2008 18:56
Beiträge: 1213
Programmiersprache: Delphi/FPC
Hey,

maybe this way?!
Code:
function GetLight(Data: Pointer; ID: Integer): Byte;
var
  Index: Integer;
begin
  Index := ID div 2;
  inc(PByte(Data), Index);
  if Odd(ID) then
    result := PByte(Data)^ and $0F
  else
    result := PByte(Data)^ shr 4;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  ByteArr: array of Byte;
  i: Integer;
  str: String;
begin
  SetLength(ByteArr, 4);
  for i := 0 to High(ByteArr) do
    ByteArr[i] := i+1;
  str := '';
  for i := 0 to 2*Length(ByteArr)-1 do
    str := str + IntToStr(GetLight(@ByteArr[0], i));
  Caption := str;
end;


this code sets the Caption to '01020304'. so if you doesn't get the right results with this code, you must check your input data.

greetings, Bergmann.

_________________
Aktuelle Projekte: BumpMapGenerator, Massive Universe Online
Auf meiner Homepage gibt auch noch paar Projekte und Infos von mir.


Nach oben
 Profil  
Mit Zitat antworten  
BeitragVerfasst: Mi Okt 27, 2010 23:10 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
All 3 seem to give the same results. I don't think there should be any way the input is wrong.

Code:
Light    := TNBT_Container(Data.Data).FindProperty('BlockLight');
 SkyLight := TNBT_Container(Data.Data).FindProperty('SkyLight');
 Blocks   := TNBT_Container(Data.Data).FindProperty('Blocks');

 SetLength(FBlocks,16,128,16);

 for X := 0 to 15 do
 for Z := 0 to 15 do
 for Y := 0 to 127 do
 begin
  BlockType := Byte(Pointer(Cardinal(Blocks.Data) + (Y + ( Z * 128 + ( X * 128 * 16 ) )))^);

  FBlocks[X,Y,Z].Block           := BlockArray[BlockType];
  FBlocks[X,Y,Z].Data.CanFall    := False;
  FBlocks[X,Y,Z].Data.FallOffset := 0;
  FBlocks[X,Y,Z].Data.Light      := Max(GetLight(Light.Data,(Y + ( Z * 128 + ( X * 128 * 16 ) ))),GetLight(SkyLight.Data,(Y + ( Z * 128 + ( X * 128 * 16 ) ))));
  if FBlocks[X,Y,Z].Data.Light > 15 then
  FBlocks[X,Y,Z].Data.Light := 15;
 end;

As far as i understand Blocks, SkyLight and BlockLight are all stored in X,Z,Y format.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 6 Beiträge ] 
Foren-Übersicht » English » English Programming Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 9 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
cron
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.052s | 19 Queries | GZIP : On ]