Board index » cppbuilder » GetDC from Bitmap->Canvas for OpenGL

GetDC from Bitmap->Canvas for OpenGL

I'm attempting to write my first app using OpenGL, and haven't gotten
very far. I've downloaded the OpenGL for BCB tutorial from
http://www.cbuilder.dthomas.co.uk/tutorials/gfx_hardware.htm , and am
using the 'Bouncing Ball' demo as a starting point.

The modification I'm attempting to make to the demo program is this.
Instead of OpenGL rendering directly to the main form's canvas, I want
to render to an instantiated Graphics::TBitmap->Canvas. I then want to
take the TBitmap's canvas and BitBlt it to the canvas of a TImage I've
placed on the form.

I've instantiated the bitmap by declaring a pointer " Graphics::TBitmap
*GLSurface;" in the GLSkeleton.h file, and by inserting the lines

   GLSurface = new Graphics::TBitmap;
   GLSurface->Height = 200;
   GLSurface->Width = 200;
   GLSurface->Canvas->CopyMode = cmSrcCopy;

in the constructor function (see Note below).  delete GLSurface; is in
the OnDestroy handler.

The trouble comes in the FormShow event handler. The example contained
the code :

  hdc = GetDC(Handle);
 SetPixelFormatDescriptor();
 hrc = wglCreateContext(hdc);

 if(hrc == NULL)
     ShowMessage(":-)~ hrc == NULL");
if(wglMakeCurrent(hdc, hrc) == false)
     ShowMessage("Could not MakeCurrent");

What I tried was to substitute the Handle property from the instantiated
TBitmap into the GetDC call; that is, hdc =
GetDC(GLSurface->Canvas->Handle);. The result was the message 'hrc ==
NULL' popped up; also, nothing was drawn to the form or the image (as
expected).  Another thing I tried was changing the line to hdc =
GetDeviceContext(GLSurface->Canvas->Handle); . This resulted in the
following warning on compile:

[C++Warning] GLSkeleton.cpp(132): Temporary used for parameter
'WindowHandle' in call to '_fastcall
Controls::TWinControl::GetDeviceContext(void * &)'.

The program ran, however, and somewhat unexpectedly displayed the OpenGL
graphic on the Form's canvas, as did the original example program.

I next tried the same two permutations, but substituted
Image1->Canvas->Handle for GLSurface->Canvas->Handle  (Image1 being the
TImage component referred to above). The results were identical.

My question, then, is this: How can I tweak this code so OpenGL will
render it's image to the canvas of an instantiated bitmap, rather than
to the form's canvas? Or, alternatively, how about rendering it to the
canvas of a TImage?

A Note - I had to add the line of code  "     _control87(MCW_EM,
MCW_EM); " to the constructor function

 __fastcall TForm1::TForm1(TComponent* Owner)
 : TForm(Owner)

at the beginning of GLSkeleton.cpp in order to suppress floating point
math errors. Without it the program wouldn't run. (That is the
constructor call, isn't it ?)

 

Re:GetDC from Bitmap->Canvas for OpenGL


Quote
> What I tried was to substitute the Handle property from the instantiated
> TBitmap into the GetDC call; that is, hdc =
> GetDC(GLSurface->Canvas->Handle);. The result was the message 'hrc ==
> NULL' popped up; also, nothing was drawn to the form or the image (as

Try changing the PFD to be a single buffer by taking out the
PFD_DOUBLEBUFFER flag and change the PFD_DRAW_TO_WINDOW flag
to PFD_DRAW_TO_BITMAP. Take a look at the OpenGL help from
the SDK for more info.

Quote
> math errors. Without it the program wouldn't run. (That is the
> constructor call, isn't it ?)

Yes. That is the constructor and a good place to put the
control word mask call.
//jt

Other Threads