1 program SimpleLoadFromFile;
6 {$IFDEF UNIX}{$IFDEF UseCThreads}
9 Classes, Windows, SysUtils, dglOpenGL, glBitmap, Helper;
12 oglWindow: TOpenGLWindow;
13 running: Boolean = true;
17 function WindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
24 result := DefWindowProc(hWnd, Msg, wParam, lParam);
30 glColor4f(1, 1, 1, 1);
32 glTexCoord2f(0, 0); glVertex2f(100, 100);
33 glTexCoord2f(1, 0); glVertex2f(700, 100);
34 glTexCoord2f(1, 1); glVertex2f(700, 500);
35 glTexCoord2f(0, 1); glVertex2f(100, 500);
40 { function to generate texture data }
41 procedure GenerateTextureFunc1(var FuncRec: TglBitmapFunctionRec);
43 g1, g2, g3, g4: Single;
45 g1 := (sin(FuncRec.Position.X / 25) + 1) / 2; // generator function 1: large sinus on x position (0.0 to 1.0)
46 g2 := (sin(FuncRec.Position.Y / 25) + 1) / 2; // generator function 2: large sinus on y position (0.0 to 1.0)
47 g3 := FuncRec.Position.X / FuncRec.Size.X; // generator function 3: linear fade on x position (0.0 to 1.0)
48 g4 := FuncRec.Position.Y / FuncRec.Size.Y; // generator function 4: linear fade on y position (0.0 to 1.0)
50 FuncRec.Dest.Data.r := Trunc(g1 * FuncRec.Dest.Range.r);
51 FuncRec.Dest.Data.g := Trunc(g2 * FuncRec.Dest.Range.g);
52 FuncRec.Dest.Data.b := Trunc(g3 * FuncRec.Dest.Range.b);
53 FuncRec.Dest.Data.a := Trunc(g4 * FuncRec.Dest.Range.a);
56 { function to generate texture data }
57 procedure GenerateTextureFunc2(var FuncRec: TglBitmapFunctionRec);
61 x := FuncRec.Position.X / FuncRec.Size.X;
62 y := FuncRec.Position.Y / FuncRec.Size.Y;
63 if (x < 0.05) or (x > 0.95) or (y < 0.05) or (y > 0.95) then
65 FuncRec.Dest.Data := FuncRec.Dest.Range;
66 end else if (y < 0.333) then begin
67 FuncRec.Dest.Data := glBitmapRec4ui(0, 0, 0, 0);
68 end else if (y < 0.666) then begin
69 FuncRec.Dest.Data := glBitmapRec4ui(FuncRec.Dest.Range.r, 0, 0, 0);
71 FuncRec.Dest.Data := glBitmapRec4ui(FuncRec.Dest.Range.r, FuncRec.Dest.Range.g, 0, 0);
76 oglWindow := CreateOpenGLWindow('TextureFromFunction', 800, 600, @WindowProc);
78 tex := TglBitmap2D.Create; // create texture object
79 data := TglBitmapData.Create; // create texture data object
81 data.LoadFromFunc( // generate texture data using either GenerateTextureFunc1 or GenerateTextureFunc2
82 glBitmapSize(512, 512),
85 //@GenerateTextureFunc2
87 tex.UploadData(data); // upload data to video card
89 FreeAndNil(data); // after upload is done, the data object could be freed to save memory
92 while running and ProgressMesages do begin
94 SwapBuffers(oglWindow.DC);
98 DestroyOpenGLWindow(oglWindow);