brane
brane

Reputation: 51

Draw quad in Delphi and OpenGL - background is painted in black

I need help with drawing a simple quad (this is only a test for drawing textures later) in Delphi and OpenGL. The quad is drawn correctly, but the rest of the form is painted in black. I'll have a background image on the form so I want that rest of the form's canvas stay unchanged.

I'm trying to use scissor, but no luck. I'm open for any other suggestions. Maybe using OpenGL viewport or.....

Here is mu code.

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Winapi.OpenGL;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    { Private declarations }
    RC: HGLRC;
    DC: HDC;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  pfd: TPixelFormatDescriptor;
  PixelFormat: Integer;
begin
  DC := GetDC(Handle);

  ZeroMemory(@pfd, SizeOf(pfd));
  pfd.nSize := SizeOf(pfd);
  pfd.nVersion := 1;
  pfd.dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
  pfd.iPixelType := PFD_TYPE_RGBA;
  pfd.cColorBits := 24;
  pfd.cDepthBits := 16;
  pfd.iLayerType := PFD_MAIN_PLANE;

  PixelFormat := ChoosePixelFormat(DC, @pfd);
  SetPixelFormat(DC, PixelFormat, @pfd);

  RC := wglCreateContext(DC);
  wglMakeCurrent(DC, RC);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  wglMakeCurrent(0, 0);
  wglDeleteContext(RC);
  ReleaseDC(Handle, DC);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

  glEnable(GL_SCISSOR_TEST);
  glScissor(ClientWidth div 4, ClientHeight div 4, ClientWidth div 2, ClientHeight div 2);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-ClientWidth / 2, ClientWidth / 2, -ClientHeight / 2, ClientHeight / 2, -1, 1);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glTranslatef(0, 0, 0);
  glColor3f(1.0, 0.0, 0.0);
  glBegin(GL_QUADS);
    glVertex2f(-50, -50);
    glVertex2f(-50, 50);
    glVertex2f(50, 50);
    glVertex2f(50, -50);
  glEnd();

  glDisable(GL_SCISSOR_TEST);

  SwapBuffers(DC);
end;

end.

I think that this should work, but it doesn't. I've tried with ALPHA BLEND functions but I've must be doing something wrong. I don't want to use any background color and want to draw background image only once. Area of the quad will change.

Here are additional explanation what am I trying.

Screen sesolution is 1920x1080. Delphi form is covered whole screen. Form has a background. In the center of the form I need OpenGL window where I'll have animation with 60 FPS. The rest of the window is pretty much static. I'm trying not to draw the entire whole screen with every form.

enter image description here

Even if I set "glViewport (200, 150, 1520, 780);" area outside of OpenGL window is painted black.

Maybe this is wrong:

  RC := wglCreateContext(DC);
  wglMakeCurrent(DC, RC);

Maybe I could do it but I think that I'll have some other idipendent animation at the top of the window which will have different FPS (15-20 FPS). I'm not sure how that will work if I draw whole window every time.

Upvotes: 0

Views: 198

Answers (1)

Tom Brunberg
Tom Brunberg

Reputation: 21033

To define the back ground color, you can use glClearColor(r, g, b, a), where the parameters are of type single, and values are 0 .. 1.0.

The parameters r, g, b and a stand for red, green, blue and alpha.

By default, the background is black, as if `glClearColor(0, 0, 0, 1.0) would have been issued.

To get a fairly light background, try

glClearColor(0.9, 0.9, 0.9, 1.0);

as the first line in your FormPaint procedure.

The result looks like this:

enter image description here

You should absolutely get hold of some documentation, e.g. https://www.khronos.org/opengl/

Upvotes: 0

Related Questions