-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexmain.pas
150 lines (133 loc) · 4.56 KB
/
texmain.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
unit texmain;
{$mode objfpc}{$H+}
interface
uses
gltex, glcorearb, gl_core_utils, OpenGLContext, Classes, SysUtils, FileUtil, Forms,
Controls, Graphics, Dialogs, ExtCtrls, gl_core_matrix, Types;
type
{ TGLForm1 }
TGLForm1 = class(TForm)
GLbox: TOpenGLControl;
procedure FormShow(Sender: TObject);
procedure GLboxMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure GLboxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure GLboxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure GLboxMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure GLboxPaint(Sender: TObject);
private
public
end;
var
GLForm1: TGLForm1;
implementation
{$R *.lfm}
var
gPositionX: single = 0;
gPositionY: single = 0;
gZoom : single = 1;
gMouseY : integer = -1;
gMouseX : integer = -1;
gGLTexBG, gGLTex: TGLTex;
procedure InitGL(var GLcontrol: TOpenGLControl);
begin
GLcontrol.MakeCurrent();
if not Load_GL_version_3_3_CORE() then begin
GLcontrol.ReleaseContext;
{$IFNDEF Windows} writeln('Unable to load OpenGL');{$ENDIF}
showmessage('Unable to load OpenGL 3.3');
halt();
end;
GLcontrol.ReleaseContext;
if GLErrorStr <> '' then begin
showmessage(GLErrorStr);
GLErrorStr := '';
end;
end;
procedure TGLForm1.GLboxMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
if WheelDelta = 0 then exit;
if WheelDelta > 0 then
gZoom := gZoom + 0.1
else
gZoom := gZoom - 0.1;
if gZoom < 0.1 then gZoom := 0.1;
if gZoom > 10 then gZoom := 10;
//GLBox.MakeCurrent(false);
gGLTex.updateVbo(gPositionX,gPositionY,gZoom, GLBox);
//GLBox.ReleaseContext;
GLBox.Invalidate;
end;
procedure TGLForm1.FormShow(Sender: TObject);
var
fnm, fishfnm, coralfnm: string;
begin
fnm := extractfilepath(paramstr(0));
{$IFDEF DARWIN}
fnm := extractfilepath(ExcludeTrailingPathDelimiter(fnm))+'Resources/';
{$ENDIF}
fishfnm := fnm + 'fish.png'; //freeware icon from http://www.softicons.com/holidays-icons/scuba-diving-icons-by-diveandgo.com/fish-icon
if not fileexists(fishfnm) then
showmessage('Error: did not find image '+fishfnm);
coralfnm := fnm + 'coral.png'; //freeware icon from http://www.softicons.com/holidays-icons/scuba-diving-icons-by-diveandgo.com/fish-icon
if not fileexists(coralfnm) then
showmessage('Error: did not find image '+coralfnm);
//OSX has two modes:
// NSOpenGLProfileLegacy provides support for OpenGL 2.1/GLSL1.2 and earlier
// NSOpenGLProfileVersion3_2Core provides support for AT LEAST OpenGL 3.2/GLSL3.2 CORE
// NSOpenGLProfileVersion4_1Core provides support for AT LEAST OpenGL 4.1/GLSL4.1 CORE
//NOTE: CORE support removes deprecated LEGACY features
// In other words, Core OpenGL3.2 is NOT a superset of OpenGL2.1
// Functions like gl_FragColor, glTranslate etc. do not exist in CORE OpenGL
// Therefore, CORE is similar to embedded OpenGL, and old tutorials will not work
{$IFDEF LCLCarbon}
Error: Carbon only supports Legacy OpenGL. Solution: compile to the Cocoa widgetset (Project/ProjectOptions/Additions&Overrides)
{$ENDIF}
InitGL (GLBox);
gGLTexBG := TGLTex.Create(coralfnm, GLBox);
gGLTex := TGLTex.Create(fishfnm, GLBox);
GLBox.OnPaint:= @GLboxPaint;
end;
procedure TGLForm1.GLboxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
gMouseY := Y;
gMouseX := X;
end;
procedure TGLForm1.GLboxMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
if gMouseY < 0 then exit; //mouse is not down
if (X <> gMouseX) or (Y <> gMouseY) then begin
gPositionX := gPositionX + (X - gMouseX);
gPositionY := gPositionY + (gMouseY - Y);
gGLTex.updateVbo(gPositionX,gPositionY,gZoom, GLBox);
end;
gMouseY := Y;
gMouseX := X;
GLBox.Invalidate;
end;
procedure TGLForm1.GLboxMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
gMouseY := -1; //released
end;
procedure TGLForm1.GLboxPaint(Sender: TObject);
begin
nglMatrixMode(nGL_PROJECTION);
nglLoadIdentity();
nglOrtho (0, GLBox.ClientWidth, 0, GLBox.ClientHeight, 0.1, 40);
glClearColor(0.3, 0.5, 0.8, 1.0); //Set blue background
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gGLTexBG.DrawTex;
gGLTex.DrawTex;
GLbox.SwapBuffers;
end;
end.