PDA

View Full Version : OpenGL Panel Component



hermes
09-26-2000, 07:52 AM
I never thought that building an openGL component would be so complicated. My Panel draws my openGL code but then draws the panel on top of it.

Any Ideas anyone, I tried recreating the context when I resize but that only did half of it.

NewROmancer
09-26-2000, 09:39 PM
If you want me to help you just tell me more about it. What do you mean an OpenGL panel? OpenGL are some functions that draw into the framebuffer, and don't have anything with the windows (except the CDC and RDC). So please be more specific...

NewROmancer

hermes
09-27-2000, 02:27 AM
here is what I'm trying to do:
First off, I'm working in Delphi
I've created a component derived from the TPanel. This component sets up the panel to be the rendering context for the openGL
When I try to draw the Image the panel draws it and then draws the visual panel over top of it (This is supposed to replace the visible panel with the openGL stuff. when I resize the component I can see it flicker in the background.

Hope this is abit clearer

hermes
09-27-2000, 02:28 AM
here's my code:
unit GLPanel;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, GL, GLU;

type
TGLPanel = class(TPanel)
private
{ Private declarations }
DC: HDC;
RC: HGLRC;
initialized: boolean;
FOnBeforeRender: TNotifyEvent;
FOnAfterRender: TNotifyEvent;
protected
{ Protected declarations }
procedure RenderImage; virtual;
public
{ Public declarations }
constructor Create(Aowner: TComponent); override;
destructor Destroy; override;
procedure CreateRC;
procedure RenderBMP;
procedure Resize;override;
published
{ Published declarations }
property OnBeforeRender: TNotifyEvent read FOnBeforeRender write FOnBeforeRender;
property OnAfterRender: TNotifyEvent read FOnAfterRender write FOnAfterRender;
end;


procedure Register;

implementation

procedure Register;
begin
RegisterComponents('My Tools', [TGLPanel]);
end;
constructor TGLPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
DC:=0;RC:=0;
initialized:=false;
Parent:=(AOwner as TWinControl);
DC := GetDC(Handle);
CreateRC;
initialized:=true;
RenderBMP;
end;
procedure TGLPanel.Resize;
begin
if (DC<>0) and (RC<>0)then RenderBMP;
end;
procedure TGLPanel.CreateRC;
var
PFD: TPixelFormatDescriptor;
PFI: Integer; { PixelFormatIndex }
NewRC: HGLRC;
begin
FillChar(PFD, SizeOf(PFD), 0);
with PFD do
begin
nSize := SizeOf(PFD); // Size of this structure
nVersion := 1; // Version of this structure
dwFlags := PFD_SUPPORT_OPENGL; // Support OpenGL calls
dwFlags := dwFlags or PFD_DRAW_TO_WINDOW; // Draw into a bitmap
dwFlags := dwFlags or PFD_DOUBLEBUFFER; // Double buffering?
iPixelType := PFD_TYPE_RGBA; // RGBA color mode
cColorBits := 24;
cDepthBits := 32; // Size of depth buffer
cStencilBits := 8; // Stencil buffer, too.
iLayerType := PFD_MAIN_PLANE; // Draw in main plane
end;
PFI := ChoosePixelFormat(DC, @PFD);
SetPixelFormat(DC, PFI, @PFD);
DescribePixelFormat(DC, PFI, SizeOf(TPixelFormatDescriptor), PFD);
NewRC := wglCreateContext(DC);
if NewRC = 0 then
raise Exception.Create('Invalid rendering context value');
RC := NewRC;
end;
destructor TGLPanel.Destroy;
begin
wglDeleteContext(RC);
inherited Destroy;
end;
procedure TGLPanel.RenderBMP;
begin
try
wglMakeCurrent(DC, RC);
if assigned(OnBeforeRender) then OnBeforeRender(Self);
RenderImage;
if assigned(OnAfterRender) then OnAfterRender(Self);
SwapBuffers(DC);
finally
wglMakeCurrent(DC, 0);
end;
end;

procedure TGLPanel.RenderImage;

begin
{ === perform OpenGL stuff here === }
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10,10,-10,10, -1.0, 1.0);
glViewport(0,0,width,height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.75,0.5,0.5,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.25,0.75,0.65);
glVertex3f(7.5,7.5,0);
glVertex3f(-7.5,7.5,0);
glVertex3f(-7.5,-7.5,0);
glVertex3f(7.5,-7.5,0);
glColor3f(0.75,0.65,0.25);
glVertex3f(5,5,0);
glVertex3f(-5,5,0);
glVertex3f(-5,-5,0);
glVertex3f(5,-5,0);
glColor3f(0.25,0.65,0.25);
glVertex3f(2.5,2.5,0);
glVertex3f(-2.5,2.5,0);
glVertex3f(-2.5,-2.5,0);
glVertex3f(2.5,-2.5,0);
glend;

// empty place holder for specialized controls
end;


end.

hermes
09-27-2000, 07:38 AM
I have fixed this. needed to use the paint procedure. I was being called after mine.

NewROmancer
09-27-2000, 11:08 PM
Sorry but I only worked in Delphi for 3 month and with my current skills I think I can't help you...

But maybe someone else can. Just try...

NewROmancer

Latombe
01-11-2002, 02:19 PM
I tried a similar component but only works in design time it will not respond nor draw during run time. Am I missing something?
Thanks for your help


Originally posted by hermes:
I have fixed this. needed to use the paint procedure. I was being called after mine.