Jump to content

Przyspieszenie dzialania programu


Guest

Recommended Posts

Witam! chodzi o to ze napisalem "gierke", a raczej ciagle ja pisze ;p bazujacej na GOL, no i wyciaga mi ledwo 21-32 FPS'y :/ prubowalem juz z TThread (chyba tak sie to pisalo) i z grupowaniem kodu na odpowiedznie procedury ale ciagle bez zmian sad.gif wiec moze wy mi poradzicie cos ?

Link to comment
Share on other sites

Musisz pobawić sie w optymlizację kodu, starać się przyspieszyć najważniejsze procki/funkcje, sprawdzić, co jest nie potrzebne. Wychwyć najsłabszą cześć kodu i skup się na niej. Może za często wykonujesz jakieś złożone obliczenia itp.?..popatrz co można by zrobić lepiej, możę nie potrzebnie ładujesz ogromne dane z plików do pamięci? Napisz może coś więcej o projekcie, albo zapodaj procki/fun, które wiesz, że można by przyspieszyć, ale nie wiesz jak.
Link to comment
Share on other sites

1. Rysowania elementów stałych (nieruchomych) np: Góry, Drzewa, kamienie (nie wiem czy dobre przykłady do Towjego projektu dałem) umieść w listach.

Coś takiego:

KOD type

Tengine=class

private

procedure rob_Drzewa;

public

procedure Listy();

procedure RenderSko;

end;



var

Engine:Tengine;

Drzewko:Gluint; // lista wyswietlania drzew

{***************************************************************************
}

procedure Tengine.rob_Drzewa;

var i:integer;

begin

(...)

end;



{***************************************************************************
}

procedure Tengine.Listy;

begin

drzewko:=glGenLists(4);

glNewList(drzewko,GL_COMPILE);

Rob_Drzewa;

glEndList();

(...)

end;

I rysowanie wywołujesz nie poprzez rob_Drzewa tylko poprzez Listy. smile.gif

2. Gdy masz wiele textur w programie to rób tak aby zmieniac je przy rysowania jak najmniej razy. Tzn. włączasz texture1, rysujesz wszystko co z niej korzysta (nieważne czy jest to na pierwszym planie czy całkiem w tle), następnie textura2 i to samo.
Link to comment
Share on other sites

To moze zapodam wiekszym kawalkiem kodu ale ostrzegam wiekszosc ludzi mowi ze strasznie wyglada moj kod zrudlowy tongue.gif

KODunit mapper;



interface



uses Windows, Messages, SysUtils, OpenGl, Gl, Glu;





procedure Lmap;

procedure Tmap;

var

XX, XX2, YY, YY2, i, licz, MX, PX, MZ, PZ, ii, jj, kk, m, przesow: integer;

implementation



uses Render;

procedure Lmap;

begin



if pier1 = true then

begin

for ii := 0 to 7 do

for jj := 0 to 7 do

for kk := 0 to 3 do

Tchoinka[ii, jj, kk] := random(64)+64;

pier1 := False;

end;



if (X >= -50) and (X <= -40)then

begin

PX := 1;

MX := -50;

end;



if (X >= -40) and (X <= -30)then

begin

PX := 2;

MX := -40;

end;



if (X >= -30) and (X <= -20)then

begin

PX := 3;

MX := -30;

end;



if (X >= -20) and (X <= -10)then

begin

PX := 4;

MX := -20;

end;

if (X >= -10) and (X <= 0) then

begin

PX := 5;

MX := -10;

end;

if (X >= 0) and (X <= 10) then

begin

PX := 6;

MX := 0;

end;

if (X >= 10) and (X <= 20) then

begin

PX := 7;

MX := 10;

end;

if (X >= 20) and (X <= 30) then

begin

PX := 8;

MX := 20;

end;

if (X >= 30) and (X <= 40) then

begin

PX := 9;

MX := 30;

end;

if (X >= 40) and (X <= 50) then

begin

PX := 10;

MX := 40;

end;

if (X >= 50) and (X <= 60) then

begin

PX := 11;

MX := 50;

end;

if (X >= 60) and (X <= 70) then

begin

PX := 12;

MX := 60;

end;

if (X >= 70) and (X <= 80) then

begin

PX := 13;

MX := 70;

end;

if (X >= 80) and (X <= 90) then

begin

PX := 14;

MX := 80;

end;

if (X >= 90) and (X <= 100)then

begin

PX := 15;

MX := 90;

end;

if (X >= 100) and (X <= 110)then

begin

PX := 16;

MX := 100;

end;

if (X >= 110) and (X <= 120)then

begin

PX := 17;

MX := 110;

end;

if (X >= 120) and (X <= 130)then

begin

PX := 18;

MX := 120;

end;

if (X >= 130) and (X <= 140)then

begin

PX := 19;

MX := 130;

end;

if (X >= 140) and (X <= 150)then

begin

PX := 20;

MX := 140;

end;

if (X >= 150) and (X <= 160)then

begin

PX := 21;

MX := 150;

end;

if (X >= 160) and (X <= 170)then

begin

PX := 22;

MX := 160;

end;









if (Z >= -40) and (Z <= -30)then

begin

PZ := 19;

MZ := -40;

end;

if (Z >= -30) and (Z <= -20)then

begin

PZ := 18;

MZ := -30;

end;

if (Z >= -20) and (Z <= -10)then

begin

PZ := 17;

MZ := -20;

end;

if (Z >= -10) and (Z <= 0) then

begin

PZ := 16;

MZ := -10;

end;

if (Z >= 0) and (Z <= 10) then

begin

PZ := 15;

MZ := 0;

end;

if (Z >= 10) and (Z <= 20) then

begin

PZ := 14;

MZ := 10;

end;

if (Z >= 20) and (Z <= 30) then

begin

PZ := 13;

MZ := 20;

end;

if (Z >= 30) and (Z <= 40) then

begin

PZ := 12;

MZ := 30;

end;

if (Z >= 40) and (Z <= 50) then

begin

PZ := 11;

MZ := 40;

end;

if (Z >= 50) and (Z <= 60) then

begin

PZ := 10;

MZ := 50;

end;

if (Z >= 60) and (Z <= 70) then

begin

PZ := 9;

MZ := 60;

end;

if (Z >= 70) and (Z <= 80) then

begin

PZ := 8;

MZ := 70;

end;

if (Z >= 80) and (Z <= 90) then

begin

PZ := 7;

MZ := 80;

end;

if (Z >= 90) and (Z <= 100)then

begin

PZ := 6;

MZ := 90;

end;

if (Z >= 100) and (Z <= 110)then

begin

PZ := 5;

MZ := 100;

end;

if (Z >= 110) and (Z <= 120)then

begin

PZ := 4;

MZ := 110;

end;

if (Z >= 120) and (Z <= 130)then

begin

PZ := 3;

MZ := 120;

end;

if (Z >= 130) and (Z <= 140)then

begin

PZ := 2;

MZ := 130;

end;

if (Z >= 140) and (Z <= 150)then

begin

PZ := 1;

MZ := 140;

end;



for m := 0 to 8 do

begin

if M =0 then

begin

PX := PX-1;

PZ := PZ-1;

end;

if M =1 then

begin

PX := PX-1

end;

if M =2 then

begin

PX := PX-1;

PZ := PZ+1;

end;

if M =3 then

begin

PZ := PZ-1;

end;

if M =4 then

begin



end;

if M =5 then

begin

PZ := PZ+1;

end;

if M =6 then

begin

PX := PX+1;

PZ := PZ-1;

end;

if M =7 then

begin

PX := PX+1;

end;

if M =8 then

begin

PX := PX+1;

PZ := PZ+1;

end;



assignfile(TF,ExtractFilePath('mapper')+'mapyTWG'+IntToStr(PZ)+'_'+IntToStr(PX)+'.map');

if not fileExists(ExtractFilePath('mapper')+'mapyTWG'+IntToStr(PZ)+'_'+IntToStr(PX)+'.map')then

begin

for YY := 0 to 9 do

for XX := 0 to 9 do

mapa[m][YY][XX]:= 0;

end else

begin

reset(TF);

licz:=-1;

while not(eof(TF)) do

begin

inc(licz);

readln(TF,str);

for I := 1 to length(str) do

begin

if str[I]='0' then mapa[m][licz][I-1]:= 0;

if str[I]='1' then mapa[m][licz][I-1]:= 1;

if str[I]='2' then mapa[m][licz][I-1]:= 2;

end;

end;

closefile(TF);

end;

if M =0 then

begin

PX := PX+1;

PZ := PZ+1;

end;

if M =1 then

begin

PX := PX+1

end;

if M =2 then

begin

PX := PX+1;

PZ := PZ-1;

end;

if M =3 then

begin

PZ := PZ+1;

end;

if M =4 then

begin



end;

if M =5 then

begin

PZ := PZ-1;

end;

if M =6 then

begin

PX := PX-1;

PZ := PZ+1;

end;

if M =7 then

begin

PX := PX-1;

end;

if M =8 then

begin

PX := PX-1;

PZ := PZ-1;

end;



end;

{

for YY := 0 to 9 do

for XX := 0 to 9 do

begin

if (x+cos(t)/20 >= XX+MX) and (x+cos(t)/20 <= XX+1+MX)then

if (z+sin(t)/20 >= YY+MZ) and (z+sin(t)/20 <= YY+1+MZ) then

begin

if mapa[4,XX,YY] <> 0 then

przeszkoda_P := false;

if mapa[4,XX,YY] = 0 then

przeszkoda_P := true;

end;

if (x-cos(t)/20 >= XX+MX) and (x-cos(t)/20 <= XX+1+MX)then

if (z-sin(t)/20 >= YY+MZ) and (z-sin(t)/20 <= YY+1+MZ) then

begin

if mapa[4,XX,YY] <> 0 then

przeszkoda_T := false;

if mapa[4,XX,YY] = 0 then

przeszkoda_T := true;

end;

end; }



end;











procedure Tmap;

begin

for m := 0 to 8 do

begin

if M =0 then

begin

PX := PX-1;

PZ := PZ-1;

end;

if M =1 then

begin

PX := PX-1;

end;

if M =2 then

begin

PX := PX-1;

PZ := PZ+1;

end;

if M =3 then

begin

PZ := PZ-1;

end;

if M =4 then

begin



end;

if M =5 then

begin

PZ := PZ+1;

end;

if M =6 then

begin

PX := PX+1;

PZ := PZ-1;

end;

if M =7 then

begin

PX := PX+1;

end;

if M =8 then

begin

PX := PX+1;

PZ := PZ+1;

end;

assignfile(TF2,ExtractFilePath('mapper')+'mapyTPG'+IntToStr(PZ)+'_'+IntToStr(PX)+'_T.map');

if FileExists(ExtractFilePath('mapper')+'mapyTPG'+IntToStr(PZ)+'_'+IntToStr(PX)+'_T.map')then

begin

reset(TF2);

licz:=-1;

while not(eof(TF2)) do

begin

inc(licz);

readln(TF2,str);

for I := 1 to length(str) do

begin





if M =0 then

begin

MX := MX-10;

MZ := MZ+10;

end;

if M =1 then

begin

MX := MX-10

end;

if M =2 then

begin

MX := MX-10;

MZ := MZ-10;

end;

if M =3 then

begin

MZ := MZ+10;

end;

if M =4 then

begin



end;

if M =5 then

begin

MZ := MZ-10;

end;

if M =6 then

begin

MX := MX+10;

MZ := MZ+10;

end;

if M =7 then

begin

MX := MX+10;

end;

if M =8 then

begin

MX := MX+10;

MZ := MZ-10;

end;



if str[I]='T' then

begin



glEnable(GL_TEXTURE_2D);

glTexImage2D(GL_TEXTURE_2D,0,3,32,32,0,GL_RGB, GL_UNSIGNED_BYTE,@Ttrawa);

glcolor3f(0.1,0.4,0.1);

glbegin(GL_QUADS);

glTexCoord2f(0,0); glVertex3f(0+MX+licz*1,0,0+MZ+(I-1)*1);

glTexCoord2f(0,1); glVertex3f(0+MX+licz*1,0,1+MZ+(I-1)*1);

glTexCoord2f(1,1); glVertex3f(1+MX+licz*1,0,1+MZ+(I-1)*1);

glTexCoord2f(1,0); glVertex3f(1+MX+licz*1,0,0+MZ+(I-1)*1);

glEnd();

glPopAttrib;

end;



if str[I]= 'W' then

begin

glEnable(GL_TEXTURE_2D);

glTexImage2D(GL_TEXTURE_2D,0,4,8,8,0,GL_RGB, GL_UNSIGNED_BYTE,@Tpiach);

glcolor3f(0,0,0.35);

glbegin(GL_QUADS);

glTexCoord2f(0,0); glVertex3f(0+MX+licz*1,0,0+MZ+(I-1)*1);

glTexCoord2f(0,1); glVertex3f(0+MX+licz*1,0,1+MZ+(I-1)*1);

glTexCoord2f(1,1); glVertex3f(1+MX+licz*1,0,1+MZ+(I-1)*1);

glTexCoord2f(1,0); glVertex3f(1+MX+licz*1,0,0+MZ+(I-1)*1);

glEnd();

end;



if str[I]= 'P' then

begin

glEnable(GL_TEXTURE_2D);

glTexImage2D(GL_TEXTURE_2D,0,4,8,8,0,GL_RGB, GL_UNSIGNED_BYTE,@Tpiach);

glcolor3f(0.1,0.4,0.1);

glbegin(GL_QUADS);

glcolor3f(1,1,0);

glTexCoord2f(0,0); glVertex3f(0+MX+licz*1,0,0+MZ+(I-1)*1);

glTexCoord2f(0,1); glVertex3f(0+MX+licz*1,0,1+MZ+(I-1)*1);

glTexCoord2f(1,1); glVertex3f(1+MX+licz*1,0,1+MZ+(I-1)*1);

glTexCoord2f(1,0); glVertex3f(1+MX+licz*1,0,0+MZ+(I-1)*1);

glEnd();



glPopAttrib;

end;

if m = 4 then

if (x+cos(t)/20 >= licz+MX) and (x+cos(t)/20 <= licz+1+MX)then

if (z+sin(t)/20 >= (I-1)+MZ) and (z+sin(t)/20 <= (I-1)+1+MZ) then

begin

if str[I]= 'W'then

przeszkoda_P := false;

if str[I]<> 'W' then

//przeszkoda_P := true;

end;

if (x-cos(t)/20 >= licz+MX) and (x-cos(t)/20 <= licz+1+MX)then

if (z-sin(t)/20 >= (I-1)+MZ) and (z-sin(t)/20 <= (I-1)+1+MZ) then

begin

if str[I]= 'W'then

przeszkoda_T := false;

if str[I]<> 'W' then

//przeszkoda_T := true;

end;



if M =0 then

begin

MX := MX+10;

MZ := MZ-10;

end;

if M =1 then

begin

MX := MX+10

end;

if M =2 then

begin

MX := MX+10;

MZ := MZ+10;

end;

if M =3 then

begin

MZ := MZ-10;

end;

if M =4 then

begin



end;

if M =5 then

begin

MZ := MZ+10;

end;

if M =6 then

begin

MX := MX-10;

MZ := MZ-10;

end;

if M =7 then

begin

MX := MX-10;

end;

if M =8 then

begin

MX := MX-10;

MZ := MZ+10;

end;







end;

end;

closefile(TF2);

end;

if M =0 then

begin

PX := PX+1;

PZ := PZ+1;

end;

if M =1 then

begin

PX := PX+1

end;

if M =2 then

begin

PX := PX+1;

PZ := PZ-1;

end;

if M =3 then

begin

PZ := PZ+1;

end;

if M =4 then

begin



end;

if M =5 then

begin

PZ := PZ-1;

end;

if M =6 then

begin

PX := PX-1;

PZ := PZ+1;

end;

if M =7 then

begin

PX := PX-1;

end;

if M =8 then

begin

PX := PX-1;

PZ := PZ-1;

end;

end;



end;





end.

To jest kod do wczytywania mapy terenu w stylu Drzewa, krzaki itd. nastepnie jest wczytywanie podloza czyli trawa piach itd. i odrazu jest wyswietlanie tego :> Wiem mozna bylo to napisac docej i zapewne polowa rzeczy jest do kitu pomyslana no ale mam nadzieje ze pomozecie mi to zoptymalizowac :>

// EDIT by HNB nie źrudło tylko źródło! jakie byki ;(
// kod wygląda naprawdę strasznie...stosuj wcięcia, spójrz na swój kod:

[code=Delphi] if m = 4 then
if (x+cos(t)/20 >= licz+MX) and (x+cos(t) HXJÓV
][BY
[
K H
KLJJÓVH[B6Bó#fCłÓłFV\0Ś&Vv\0Śb7G%ÓuŃĄ4)}@ą4)Ńm%tąŃĄ
//przeszkoda_P := true;
end;
if (x-cos(t)/20 >= liczÓV
H[

K HXJÓV
][BY
[
KfwCłÓćB6Bó#fCłÓłFV\0Ś&V4)Ńm%tŃĄ4)}Pą4)Ńm%tlt;> 'W' then
//przeszkoda_T := true;
end;*hŻ ĘĘk)Ź~ćj0j|ŚŚź^bpvbBFV\0bś62Bó#fwCłĆ7ćBś62źąąŹ5`ŃĄ4(ĄĄźĄ$Z) and (z+sin(t)/20 <= (I-1)+1+MZ) then
begin
(YWOH [BWH[NBb7G%fCfwCurrFV\0'ŚW7ŚśFŁG'VS4(4(4)ĄĄźą5`ĄĄ0 <= licz+1+MX)then
if (z-sin(t)/20 >= (I-1)+MZ) k
[
K H
KLJJJÓVH[BY[BYÓurwFV\0'ŚW7ŚśFBŁf6S\0b7G%aŃĄ4(ź}PŃ4( [/code]

Poza tym, nie stosuj znaczników code tylko syntax="delphi"
Iskar

Link to comment
Share on other sites

masz rację, że wygląda strasznie bo nie ma wcięć ! :twisted:
Jakiś czas temu ( w wakację) bawiłem się w OGL-a i wsumie rysowanie glVertex-em to sobie odpuść już na starcie to się nadaje by narysować jakieś menu albo coś... Skorzystaj z tablic wierzchołków, list wyświetlania i lepiej z VBO jeszcze tongue.gif.
Jak załapiesz t.wierzchołków to dodanie do nich VBO które da ci dość przyzwoitego kopa to jest kwesja około 20-30 linijek.
Przykładowy tutorial o Vertex buffer object: http://steinsoft.net/index.php?site=Progra...ets/OpenGL/no11Jest dość krótki i prosty.

Link to comment
Share on other sites

Ten post powyżej to mój ale zapomniałem się lognąć tongue.gif.
Pozatym jak już koledzy powiedzieli unikaj częstych zmian stanu maszyny OpenGL. Tak to się chyba nazywa "fachowo" czyli np. unikaj częstego: glBindTexture(). Pogrupuj trójkąty które korzystają z tej samej textury i rysuj najpierw je a potem te które korzystają z drugiej i tak dalej.

PS. wrzuć ten kod na jakiś link bo trzeba strasznie dużo przewijać. Pozatym jak po warunku if masz wykonywaną tylko jedną instrukcję to nie musisz dawać begin end; biggrin.gif tylko piszesz:
if (Warunek1) and (warunek cos tam 2) then Instrukcja.

Zaoszczędzasz 2-3 linijki biggrin.gif

Edit1: Po co w linijce: [code=Delphi] glVertex3f(1+MX+licz*1,0,0+MZ+(I-1)*1);fŹYĄzó{=&Śźr'\"'ł')^)zlz7Źśjez{6r6('x,ł8%Uę{hóĘZ4ćn*mjH'Źx'Ź^w*Zjnn'hłjhrr b\"3jho)jśvxŁ{ (w87Źś6vVŚ'jsŻ7ŁŻ<Ź\"zg3ĘĄb(3ui'
if str[I]<> 'W' then
//przeszkoda_T := true;
[/code]
to zakomentuj też warunek a nie tylko "skutek"
I jeszcze glEnable(GL_TEXTURE_2D); tam gzieś masz pare razy to wrzuć go gdzieś w GLInit czy coś podobnego a nie wywołujesz go co chwilę. Ustaw na starcie programu, że używasz tekstur 2d i już nigdzie tego nie wpisuj.

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

Link to comment
Share on other sites

[code=Delphi]
if (X >= -50) and (X <= -40)then
begin
PX := 1;
VHMLB[BBY
HM
H[
HL
][B&VvŁ#ŁÓCŚVćCŚbfwCłÓ3ćB`ąŃĄ4)4(A`4(5`4)4(4)X >= -20) and (X <= -10)then
begin
PX := 4;
MX :=LB[BY
HLL
H[
H
H[BY[BŁSŁÓŚVćCŚbfwCłćBfCłŃĄ4)4(A`4(5`4)4)Ą`nd (X <= 20) then
begin
PX := 7;
MX := 10;
end;
if
H
H[
H
H[BY[BHBV#ŚVćCŚbfwCł3ćBfCłCFVŚ&VvA`4(5`4)4)Ą`Ą`ą then
begin
PX := 10;
MX := 40;
end;
if (X >= 50) [
H

H[BY[BHLNBVH
LB[BbfwCłcćBfCłsFVŚ&VvŁ#`4)4)Ą`Ą`ąŃĄ4)
PX := 13;
MX := 70;
end;
if (X >= 80) and (X <HL
H[BY[BHMBVHB[BY
HćBfCłFVŚ&VvŁSŁŚV4)Ą`Ą`ąŃĄ4)4(A`
MX := 100;
end;
if (X >= 110) and (X <= 120)then
Y[BHMBVHLLB[BY
HL
H[
fCł3FVŚ&VvŁŁ#ŚVćCŚbĄ`ąŃĄ4)4(A`4(5`130;
end;
if (X >= 140) and (X <= 150)then
begin
HBVHMB[BY
HML
H[
HcFVŚ&VvŁ#ŁSŚVćCŚbfwCłĄ`ąŃĄ4)4(A`4(5`4); [/code]
Pozostałe bloki IF też można zastąpić funkcjami liniowymi (czyli tak jak zrobiłem to przed chwilą).

// używaj formatowania kodu źródłowego No dobrze... zapomnialem. Przepraszam tongue.gif

Pozdrawiam,

vo7 (;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...