Autor Beitrag
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 19.10.06 08:23 
Hallo,

ich versuche momentan mir eigene Komponenten zu schreiben, die auf OpenGL basieren. Allerdings scheitert er bei mir bei der erzeugung vom OpenGL_Kontext. Bei den markierten Stellen hat er irgendwie Probleme. Ich verstehe den Grund aber noch nicht :( .

ausblenden volle Höhe Delphi-Quelltext
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:
150:
151:
152:
153:
154:
unit SBComponents;

interface

uses
  Messages, Windows, Controls, Classes, Math, dglOpenGL;


type
  TSBControl = class(TWinControl)
    private
      //OpenGL-Vars
      FNoGLParent: Boolean;

      FDC    : HDC;
      FRC    : HGLRC;
      FzNear : Double;
      FzFear : Double;
      FGLInit: Boolean;

      FOnSetOpenGLSetting :TNotifyEvent;

      procedure CreateGL;
      //Messages
      procedure WMPaint(var Msg: TWMPaint); message WM_PAINT;
    protected
      procedure SetParent(AParent: TWinControl); override;
      procedure SetOpenGLSetting; virtual;
      procedure Resize; override;
    public
      constructor Create(AOwner: TComponent); override;
      destructor Destroy; override;

      property OnSetOpenGLSetting: TNotifyEvent read FOnSetOpenGLSetting write FOnSetOpenGLSetting;
  end;


implementation

////////////////////////////////////////////////////////////////////////////////
//--TSBControl--
//public

constructor TSBControl.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FzNear     :=-1;
  FzFear     := 1;
  FNoGLParent:=true;
  FGLInit:=false;
end;

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

destructor TSBControl.Destroy;
begin
  if FNoGLParent then
  begin
    wglMakeCurrent(FDC, 0);
    if FRC<>0 then wglDeleteContext(FRC);
    ReleaseDC(Handle, FDC);
    FDC:=0;
  end;
  inherited Destroy;
end;

{******************************************************************************}
//protected

procedure TSBControl.SetParent(AParent: TWinControl);
begin
  inherited SetParent(AParent);
  if not FGLInit then CreateGL;
end;

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

procedure TSBControl.SetOpenGLSetting;
begin
  if FGLInit then
  begin
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glDepthMask(True);
    glClearDepth(1);
    glDisable(GL_BLEND);
    glDisable(GL_ALPHA_TEST);
    glEnable(GL_COLOR_MATERIAL);
    if Assigned(FOnSetOpenGLSetting) then FOnSetOpenGLSetting(Self);
  end;
end;

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

procedure TSBControl.Resize;
var
  CS: array[0..1] of Integer;
  Width, Height: Integer;
begin
  if FGLInit and FNoGLParent then
  begin
    glGetIntegerv(GL_MAX_VIEWPORT_DIMS, @CS);

    Width :=  Min(CS[0], ClientWidth);
    Height := Min(CS[1], ClientHeight);

    glViewport(0, 0, Width, Height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0, ClientWidth, ClientHeight, 0, FzNear, FzFear);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
  end;

  inherited Resize;
end;

{******************************************************************************}
//private
procedure TSBControl.CreateGL;
var
  ParentClass: TClass;
begin
  ParentClass:=Self.ClassType;
  while (ParentClass <> nil) and (ParentClass <> TStream) do ParentClass:=ParentClass.ClassParent;
  if ParentClass = nil then
  begin
    FDC:=GetDC(Handle);
    FRC:=wglCreateContext(FDC);
    wglMakeCurrent(FDC, FRC);

    SetOpenGLSetting;
    FNoGLParent:=true;
  end
  else
  begin
    FNoGLParent:=false;
  end;
  FGLInit:=true;
end;

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

procedure TSBControl.WMPaint(var Msg: TWMPaint);
begin
  glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT);
  SwapBuffers(FDC);
end;

end.


Test-Unit:

ausblenden volle Höhe Delphi-Quelltext
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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,

  SBComponents;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  test: TSBControl;

implementation

{$R *.dfm}

procedure TForm1.FormDestroy(Sender: TObject);
begin
  test.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  test:=TSBControl.Create(Self);
  test.Parent:=self;
  test.Left:=0;
  test.Top:=0;
  test.Height:=50;
  test.Width:=50;
  test.Show;
end;

end.

Sieht einer von euch das Problem?


Zuletzt bearbeitet von Heiko am Do 19.10.06 14:31, insgesamt 1-mal bearbeitet
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Do 19.10.06 09:59 
Titel: Re: Erzeugung bei schlägt fehl
probiers mal so:

ausblenden volle Höhe Delphi-Quelltext
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:
unit SBComponents;

interface

uses
  Messages, Windows, Controls, Classes, Math, dglOpenGL;


type
  TSBControl = class(TWinControl)
    private
      //OpenGL-Vars
      FNoGLParent: Boolean;

      FDC    : HDC;
      FRC    : HGLRC;
      FzNear : Double;
      FzFear : Double;
      FGLInit: Boolean; 
      FForm: TForm;

      FOnSetOpenGLSetting :TNotifyEvent;

      procedure CreateGL;
      //Messages
      procedure WMPaint(var Msg: TWMPaint); message WM_PAINT;
    protected
      procedure SetOpenGLSetting; virtual;
      procedure Resize; override;
    public
      constructor Create(AOwner: TForm); override;
      destructor Destroy; override;

      property OnSetOpenGLSetting: TNotifyEvent read FOnSetOpenGLSetting write FOnSetOpenGLSetting;
  end;


implementation

////////////////////////////////////////////////////////////////////////////////
//--TSBControl--
//public

constructor TSBControl.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FzNear     :=-1;
  FzFear     := 1;
  FNoGLParent:=true;
  FGLInit:=false;
  FForm := AOwner;
end;

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

destructor TSBControl.Destroy;
begin
  if FNoGLParent then
  begin
    wglMakeCurrent(FDC, 0);
    if FRC<>0 then wglDeleteContext(FRC);
    ReleaseDC(Handle, FDC);
    FDC:=0;
  end;
  inherited Destroy;
end;

procedure TSBControl.SetOpenGLSetting;
begin
  if FGLInit then
  begin
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glDepthMask(True);
    glClearDepth(1);
    glDisable(GL_BLEND);
    glDisable(GL_ALPHA_TEST);
    glEnable(GL_COLOR_MATERIAL);
    if Assigned(FOnSetOpenGLSetting) then FOnSetOpenGLSetting(Self);
  end;
end;

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

procedure TSBControl.Resize;
var
  CS: array[0..1] of Integer;
  Width, Height: Integer;
begin
  if FGLInit and FNoGLParent then
  begin
    glGetIntegerv(GL_MAX_VIEWPORT_DIMS, @CS);

    Width :=  Min(CS[0], ClientWidth);
    Height := Min(CS[1], ClientHeight);

    glViewport(0, 0, Width, Height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0, ClientWidth, ClientHeight, 0, FzNear, FzFear);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
  end;

  inherited Resize;
end;

{******************************************************************************}
//private
procedure TSBControl.CreateGL;
var
  ParentClass: TClass;
begin
  ParentClass:=Self.ClassType;
  while (ParentClass <> nil) and (ParentClass <> TStream) do ParentClass:=ParentClass.ClassParent;
  if ParentClass = nil then
  begin
    FDC:=GetDC(FForm.Handle);
    FRC:=wglCreateContext(FDC);
    wglMakeCurrent(FDC, FRC);
    SetOpenGLSetting;
    FNoGLParent:=true;
  end
  else
  begin
    FNoGLParent:=false;
  end;
  FGLInit:=true;
end;

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

procedure TSBControl.WMPaint(var Msg: TWMPaint);
begin
  glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT);
  SwapBuffers(FDC);
end;

end.


und parent brauchst du garnet. //€ aso bei ner kompo möglicherweise schon ;)


mfg
Heiko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 19.10.06 14:39 
Nun ja, ich will ja meine eigene Komponente schreiben und nicht auf TForm zugreifen. Von daher bringt mir das nix. Das Parent kann ja schließlich auch ein Panel sein.