Skip to content

Commit ceafff6

Browse files
committed
add new example and fix r3d
1 parent 6f35aac commit ceafff6

File tree

10 files changed

+1069
-86
lines changed

10 files changed

+1069
-86
lines changed

examples/extra/r3d/resize/r3d_resize.lpr

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@
44

55
uses
66
{$IFDEF LINUX} cthreads,{$ENDIF}
7-
Classes, SysUtils, CustApp, raylib, r3d;
7+
Classes, SysUtils, CustApp, raylib, r3d, rlGl, math;
88

99
type
1010
{ TRayApplication }
1111
TRayApplication = class(TCustomApplication)
1212
protected
1313
procedure DoRun; override;
14+
private
15+
sphere: TModel;
16+
camera: TCamera;
17+
materials: array[0..4] of TMaterial;
1418
public
1519
constructor Create(TheOwner: TComponent); override;
1620
destructor Destroy; override;
21+
procedure Init;
22+
procedure Update;
23+
procedure Draw;
24+
procedure Close;
1725
end;
1826

19-
const AppTitle = 'raylib - basic window';
27+
const AppTitle = '[r3d] - resize example';
2028

2129
{ TRayApplication }
2230

@@ -25,7 +33,7 @@ constructor TRayApplication.Create(TheOwner: TComponent);
2533
inherited Create(TheOwner);
2634

2735
InitWindow(800, 600, AppTitle); // for window settings, look at example - window flags
28-
36+
Init;
2937
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
3038
end;
3139

@@ -35,11 +43,10 @@ procedure TRayApplication.DoRun;
3543
while (not WindowShouldClose) do // Detect window close button or ESC key
3644
begin
3745
// Update your variables here
38-
46+
Update;
3947
// Draw
4048
BeginDrawing();
41-
ClearBackground(RAYWHITE);
42-
DrawText('Congrats! You created your first window!', 190, 200, 20, LIGHTGRAY);
49+
Draw;
4350
EndDrawing();
4451
end;
4552

@@ -49,7 +56,7 @@ procedure TRayApplication.DoRun;
4956

5057
destructor TRayApplication.Destroy;
5158
begin
52-
// De-Initialization
59+
close;
5360
CloseWindow(); // Close window and OpenGL context
5461

5562
// Show trace log messages (LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR...)
@@ -58,6 +65,88 @@ destructor TRayApplication.Destroy;
5865
inherited Destroy;
5966
end;
6067

68+
procedure TRayApplication.Init;
69+
var i: integer;
70+
light: TR3D_Light;
71+
begin
72+
R3D_Init(GetScreenWidth(), GetScreenHeight(), R3D_FLAG_NONE);
73+
SetWindowState(FLAG_WINDOW_RESIZABLE);
74+
SetTargetFPS(60);
75+
76+
sphere := LoadModelFromMesh(GenMeshSphere(0.5, 64, 64));
77+
UnloadMaterial(sphere.materials[0]);
78+
79+
for i := 0 to 4 do
80+
begin
81+
materials[i] := LoadMaterialDefault();
82+
materials[i].maps[MATERIAL_MAP_ALBEDO].color := ColorFromHSV(i / 5 * 330, 1.0, 1.0);
83+
materials[i].maps[MATERIAL_MAP_OCCLUSION].value := 1;
84+
materials[i].maps[MATERIAL_MAP_ROUGHNESS].value := 1;
85+
materials[i].maps[MATERIAL_MAP_METALNESS].value := 0;
86+
end;
87+
88+
camera.Create(Vector3Create(0, 2, 2), Vector3Create(0, 0, 0), Vector3Create(0, 1, 0), 60, 0);
89+
90+
light := R3D_CreateLight(R3D_LIGHT_DIR);
91+
92+
R3D_SetLightDirection(light, Vector3Create( 0, 0, -1 ));
93+
R3D_SetLightActive(light, true);
94+
95+
end;
96+
97+
procedure TRayApplication.Update;
98+
var keep, linear: boolean;
99+
begin
100+
UpdateCamera(@camera, CAMERA_ORBITAL);
101+
102+
if IsKeyPressed(KEY_R) then
103+
begin
104+
keep := R3D_HasState(R3D_FLAG_ASPECT_KEEP);
105+
if (keep) then R3D_ClearState(R3D_FLAG_ASPECT_KEEP)
106+
else R3D_SetState(R3D_FLAG_ASPECT_KEEP);
107+
end;
108+
109+
if (IsKeyPressed(KEY_F)) then
110+
begin
111+
linear := R3D_HasState(R3D_FLAG_BLIT_LINEAR);
112+
if (linear) then R3D_ClearState(R3D_FLAG_BLIT_LINEAR)
113+
else R3D_SetState(R3D_FLAG_BLIT_LINEAR);
114+
end;
115+
end;
116+
117+
procedure TRayApplication.Draw;
118+
var keep, linear: boolean; i: integer;
119+
begin
120+
keep := R3D_HasState(R3D_FLAG_ASPECT_KEEP);
121+
linear := R3D_HasState(R3D_FLAG_BLIT_LINEAR);
122+
123+
if (keep) then
124+
ClearBackground(BLACK);
125+
126+
127+
R3D_Begin(camera);
128+
rlPushMatrix();
129+
for i := 0 to 4 do
130+
begin
131+
sphere.materials[0] := materials[i];
132+
R3D_DrawModel(sphere, Vector3Create( i - 2, 0, 0 ), 1.0);
133+
end;
134+
rlPopMatrix();
135+
R3D_End();
136+
137+
if keep then DrawText('(R)esize mode: KEEP', 10, 10, 20, BLACK) else
138+
DrawText('(R)esize mode: EXPAND', 10, 10, 20, BLACK);
139+
140+
if linear then DrawText('(F)ilter mode: LINEAR', 10, 40, 20, BLACK) else
141+
DrawText('(F)ilter mode: NEAREST', 10, 40, 20, BLACK);
142+
end;
143+
144+
procedure TRayApplication.Close;
145+
begin
146+
UnloadModel(sphere);
147+
R3D_Close();
148+
end;
149+
61150
var
62151
Application: TRayApplication;
63152
begin
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CONFIG>
3+
<ProjectOptions>
4+
<Version Value="12"/>
5+
<General>
6+
<Flags>
7+
<MainUnitHasCreateFormStatements Value="False"/>
8+
<MainUnitHasTitleStatement Value="False"/>
9+
<MainUnitHasScaledStatement Value="False"/>
10+
</Flags>
11+
<SessionStorage Value="InProjectDir"/>
12+
<Title Value="r3d_skybox"/>
13+
<UseAppBundle Value="False"/>
14+
<ResourceType Value="res"/>
15+
</General>
16+
<BuildModes>
17+
<Item Name="Default" Default="True"/>
18+
</BuildModes>
19+
<PublishOptions>
20+
<Version Value="2"/>
21+
<UseFileFilters Value="True"/>
22+
</PublishOptions>
23+
<RunParams>
24+
<FormatVersion Value="2"/>
25+
</RunParams>
26+
<RequiredPackages>
27+
<Item>
28+
<PackageName Value="ray4laz"/>
29+
</Item>
30+
</RequiredPackages>
31+
<Units>
32+
<Unit>
33+
<Filename Value="r3d_skybox.lpr"/>
34+
<IsPartOfProject Value="True"/>
35+
</Unit>
36+
</Units>
37+
</ProjectOptions>
38+
<CompilerOptions>
39+
<Version Value="11"/>
40+
<Target>
41+
<Filename Value="../../../../binary/r3d_skybox"/>
42+
</Target>
43+
<SearchPaths>
44+
<IncludeFiles Value="$(ProjOutDir)"/>
45+
<UnitOutputDirectory Value="../../../../temp/$(TargetCPU)-$(TargetOS)"/>
46+
</SearchPaths>
47+
<Linking>
48+
<Debugging>
49+
<DebugInfoType Value="dsDwarf3"/>
50+
</Debugging>
51+
</Linking>
52+
</CompilerOptions>
53+
<Debugging>
54+
<Exceptions>
55+
<Item>
56+
<Name Value="EAbort"/>
57+
</Item>
58+
<Item>
59+
<Name Value="ECodetoolError"/>
60+
</Item>
61+
<Item>
62+
<Name Value="EFOpenError"/>
63+
</Item>
64+
</Exceptions>
65+
</Debugging>
66+
</CONFIG>
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
program r3d_skybox;
2+
3+
{$mode objfpc}{$H+}
4+
5+
uses
6+
{$IFDEF LINUX} cthreads,{$ENDIF}
7+
Classes, SysUtils, CustApp, raylib, r3d, raymath;
8+
9+
const
10+
MATERIALS_ROWS = 7;
11+
MATERIALS_COLS = 7;
12+
13+
type
14+
{ TRayApplication }
15+
TRayApplication = class(TCustomApplication)
16+
protected
17+
procedure DoRun; override;
18+
private
19+
sphere: TMesh;
20+
skybox: TR3D_Skybox;
21+
camera: TCamera;
22+
materials: array[0..MATERIALS_ROWS * MATERIALS_COLS - 1] of TMaterial;
23+
public
24+
constructor Create(TheOwner: TComponent); override;
25+
destructor Destroy; override;
26+
procedure Init;
27+
procedure Update;
28+
procedure Draw;
29+
procedure Close;
30+
end;
31+
32+
const AppTitle = '[r3d] - skybox example';
33+
34+
{ TRayApplication }
35+
36+
constructor TRayApplication.Create(TheOwner: TComponent);
37+
begin
38+
inherited Create(TheOwner);
39+
40+
InitWindow(800, 600, AppTitle); // for window settings, look at example - window flags
41+
Init;
42+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
43+
end;
44+
45+
procedure TRayApplication.DoRun;
46+
begin
47+
48+
while (not WindowShouldClose) do // Detect window close button or ESC key
49+
begin
50+
// Update your variables here
51+
update;
52+
// Draw
53+
BeginDrawing();
54+
Draw;
55+
EndDrawing();
56+
end;
57+
58+
// Stop program loop
59+
Terminate;
60+
end;
61+
62+
destructor TRayApplication.Destroy;
63+
begin
64+
Close;
65+
CloseWindow(); // Close window and OpenGL context
66+
inherited Destroy;
67+
end;
68+
69+
procedure TRayApplication.Init;
70+
var x, y, i: integer;
71+
begin
72+
R3D_Init(GetScreenWidth(), GetScreenHeight(), R3D_FLAG_NONE);
73+
SetTargetFPS(60);
74+
75+
sphere := GenMeshSphere(0.5, 64, 64);
76+
77+
for x := 0 to MATERIALS_ROWS - 1 do
78+
for y := 0 to MATERIALS_COLS - 1 do
79+
begin
80+
i := y * MATERIALS_ROWS + x;
81+
materials[i] := LoadMaterialDefault();
82+
R3D_SetMaterialOcclusion(@materials[i], nil, 1.0);
83+
R3D_SetMaterialMetalness(@materials[i], nil, x / MATERIALS_ROWS);
84+
R3D_SetMaterialRoughness(@materials[i], nil, y / MATERIALS_COLS);
85+
R3D_SetMaterialAlbedo(@materials[i], nil, ColorFromHSV((x/MATERIALS_ROWS) * 360, 1, 1));
86+
end;
87+
88+
skybox := R3D_LoadSkybox('resources/sky/skybox1.png', CUBEMAP_LAYOUT_AUTO_DETECT);
89+
R3D_EnableSkybox(skybox);
90+
91+
camera.Create(Vector3Create(0, 0, 5), Vector3Create(0, 0, 0), Vector3Create(0, 1, 0), 60, 0);
92+
93+
DisableCursor();
94+
95+
end;
96+
97+
procedure TRayApplication.Update;
98+
begin
99+
UpdateCamera(@camera, CAMERA_FREE);
100+
end;
101+
102+
procedure TRayApplication.Draw;
103+
var x, y: integer;
104+
begin
105+
R3D_Begin(camera);
106+
for x := 0 to 6 do
107+
for y := 0 to 6 do
108+
R3D_DrawMesh(sphere, materials[y * 7 + x], MatrixTranslate(x - 3, y - 3, 0.0));
109+
R3D_End();
110+
end;
111+
112+
procedure TRayApplication.Close;
113+
begin
114+
UnloadMesh(sphere);
115+
R3D_UnloadSkybox(skybox);
116+
R3D_Close();
117+
end;
118+
119+
var
120+
Application: TRayApplication;
121+
begin
122+
Application:=TRayApplication.Create(nil);
123+
Application.Title:=AppTitle;
124+
Application.Run;
125+
Application.Free;
126+
end.
127+

0 commit comments

Comments
 (0)