Recent

Author Topic: OpenGL ES in GTK window  (Read 4167 times)

marius.maximus

  • New Member
  • *
  • Posts: 35
OpenGL ES in GTK window
« on: November 11, 2012, 07:48:38 pm »
How display "OpenGL ES" in form/panel (LINUX GTK2)?
Under windows this code works:

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, ctOpenGLES2xCanvas, Forms, Controls, Graphics,
  Dialogs, StdCtrls, ctOpenGLES2x, LCLType
  ,LCLIntf, LResources, ExtCtrls

  ;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    feglSurface: EGLSurface;
    feglContext: EGLContext;
    OpenGLES20Canvas11: TOpenGLES20Canvas;
     FHandle:HWND;
     eglWindow: EGLNativeWindowType;
     fdc : hDC;
     pi32ConfigAttribs: array[0..128] of EGLint;

     iMajorVersion: EGLint;
     iMinorVersion: EGLint;
     feglDisplay: EGLDisplay;
     feglConfig: EGLConfig;

     iConfigs: integer;

  public
    Procedure SwapBuffers;

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   i:integer;
begin
  InitGLES20;

  feglDisplay:= nil;
  feglConfig:= nil;
  feglSurface:= nil;
  feglContext:= nil;

  FHandle:= Panel1.Handle;

  eglWindow:= fHandle;

  fdc := GetDC(eglWindow);

  feglDisplay := eglGetDisplay(fdc);//EGL_DEFAULT_DISPLAY);

  if eglInitialize(feglDisplay, nil, nil) = 0  then
  begin
    MessageBox(0, 'eglInitialize() failed.', 'Error', MB_OK or MB_ICONEXCLAMATION);
    Exit;
  end;

  i := 0;      pi32ConfigAttribs[i] := EGL_RED_SIZE;
  i := i + 1;  pi32ConfigAttribs[i] := 5;
  i := i + 1;  pi32ConfigAttribs[i] := EGL_GREEN_SIZE;
  i := i + 1;  pi32ConfigAttribs[i] := 6;
  i := i + 1;  pi32ConfigAttribs[i] := EGL_BLUE_SIZE;
  i := i + 1;  pi32ConfigAttribs[i] := 5;
  i := i + 1;  pi32ConfigAttribs[i] := EGL_ALPHA_SIZE;
  i := i + 1;  pi32ConfigAttribs[i] := 0;
  i := i + 1;  pi32ConfigAttribs[i] := EGL_SURFACE_TYPE;
  i := i + 1;  pi32ConfigAttribs[i] := EGL_WINDOW_BIT;
  i := i + 1;  pi32ConfigAttribs[i] := EGL_NONE;


  if eglChooseConfig(feglDisplay, @pi32ConfigAttribs, @feglConfig, 1, @iConfigs) = 0 then
begin
          MessageBox(0, 'eglChooseConfig() failed.', 'Error', MB_OK or MB_ICONEXCLAMATION);
          Exit;
end;

  feglSurface := eglCreateWindowSurface(feglDisplay, feglConfig, eglWindow, nil);
  feglContext := eglCreateContext(feglDisplay, feglConfig, nil, nil);

  if eglMakeCurrent(feglDisplay, feglSurface, feglSurface, feglContext) = 0 then
begin
          MessageBox(0, 'eglMakeCurrent() failed.', 'Error', MB_OK or MB_ICONEXCLAMATION);
          Exit;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  glViewPort(0,0,Width,Height);
  glClearColor(0,1,0,1);
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

  SwapBuffers;
end;

Procedure TForm1.SwapBuffers;
begin
   eglSwapBuffers(feglDisplay, feglSurface);
end;

end.

I compiled this code for linux (gtk/gtk2)
And i have exception "Acces violation"
when call eglInitialize(....)


I tested this code with window created with   "XCreateWindow" and it works
but when I used TForm or TPanel (TForm or Tpanel   is GTK) i have exception .

If i would like use OpenGL ES inside my application I can't use TForm or Tpanel  for display GTK ?

« Last Edit: November 11, 2012, 08:39:04 pm by marius.maximus »

 

TinyPortal © 2005-2018