PDA

View Full Version : OpenGL - Initialising with XE2



AthenaOfDelphi
12-11-2011, 09:43 PM
Hi all,

Well, it's been a while since I posted anything other than site notices and such, but now I have a window in my studies so I'm getting down to some development... mainly because I've been asked to build a game (most likely web based using Cocos2d-Javascript).

I want a map editor (yes, I've looked at Tiled, the de facto tool for creating tilemaps for Cocos2d, but it's just not got the features I want). I want it to be cross platform so I can run it on both my PC and my Mac (am also learning Objective C for Mac and iOS development, with Cocos2d). So I figured I'd break out my copy of XE2 and take it for a spin. And well... that's where it's becoming a real pain in the posterior.

I sat down last night and started banging out a map editor with RadStudio 2009... it's been a while, but within an hour I had two independent OpenGL rendering areas (one for the map and one for the palette). Then, as I say, I thought I'd make use of the cross platform features of XE2 and build a PC and Mac version.

So, I've started again and set about creating an OpenGL rendering panel. It's based on TControl, much like TPaintBox. The plan is to have it initialise OpenGL if it's not already and when the paint event occurs, to activate the relevant rendering context etc.

In RS2009, I've got this code



fMapRC:=createRenderingContext(oglMap.canvasDC,[opDoubleBuffered],32,24,0,0,0,0);

...

activateRenderingContext(oglMap.canvasDC,fMapRC);


And this works a treat. In XE2 I have nothing.

I can't find a definition for createRenderingContext or activateRenderingContext. I also don't seem to be able to find a routine to get a device context from a handle.

Does anyone have any suggestions, example code etc. of how to create and manage a rendering context on both Windows and Mac OSx for XE2?

I've googled with lots of different search strings and so far I've drawn a complete blank.

Thanks in advance for any help/suggestions.

SilverWarior
13-11-2011, 12:31 AM
I don't have personal expirience with Delphi XE2 jet but as far as I know if you wan't to make a crossplatform application with Delphi XE2 you need to use FireMonkey components and not VCL.

LP
13-11-2011, 05:23 AM
I don't have personal expirience with Delphi XE2 jet but as far as I know if you wan't to make a crossplatform application with Delphi XE2 you need to use FireMonkey components and not VCL.
Actually, you can create cross-platform applications without FireMonkey: just use the console mode.



I can't find a definition for createRenderingContext or activateRenderingContext. I also don't seem to be able to find a routine to get a device context from a handle.
I am unfamiliar with createRenderingContext function, but on Windows you can use wglCreateContext and wglMakeCurrent. These seem to work just fine both on Win32 and Win64. In addition, there are some intermediary steps required for selecting palette (aka pixel format) and updating format descriptor of the form. Also make sure to use latest OpenGL headers.

P.S. If you *are* using FireMonkey, remember that on Mac OS platform, OpenGL rendering context is already created and initialized.

AthenaOfDelphi
13-11-2011, 10:17 AM
I don't have personal expirience with Delphi XE2 jet but as far as I know if you wan't to make a crossplatform application with Delphi XE2 you need to use FireMonkey components and not VCL.

I am using FireMonkey. TControl exists in both the VCL and FMX namespaces.

AthenaOfDelphi
13-11-2011, 10:33 AM
Hi Yuriy,


Actually, you can create cross-platform applications without FireMonkey: just use the console mode.

I am unfamiliar with createRenderingContext function, but on Windows you can use wglCreateContext and wglMakeCurrent. These seem to work just fine both on Win32 and Win64. In addition, there are some intermediary steps required for selecting palette (aka pixel format) and updating format descriptor of the form. Also make sure to use latest OpenGL headers.

Yeah, I looked into wglCreateContext and wglMakeCurrent. The problem is, I can't seem to find an equivalent when I enable the OS X build platform. If I recall correctly, for these two call I add WinAPI.OpenGL to the uses, but I can't seem to find an equivalent for Mac. Any suggestions?

To be honest, I'm amazed they elected to use Direct X for FireMonkey on Windows. Why not just use OpenGL on all platforms?


P.S. If you *are* using FireMonkey, remember that on Mac OS platform, OpenGL rendering context is already created and initialized.

I realised that, because it's used for the whole application interface. On the first stab at a Windows implementation I have a bunch of VCL controls and two OpenGL rendering contexts. One for the map and one for the palette. I'm trying to recreate this using FireMonkey, so for it to work, I need to create a couple of contexts myself. So the application created context will be used for the FMX controls etc. and the two I create will be there just as they are in Windows.

If I'm really honest, I'm actually finding this whole cross platform thing to be a quite disappointing. Lots of definitions have moved, things that are useful like 'Anchors' aren't there in FireMonkey, and so far my experience with the help is that it's sadly lacking.

Sascha Willems
13-11-2011, 11:05 AM
ActivateRenderingContext (and DeactivateRenderingContext) are helper functions from our dglOpenGL-Header to easily create and manage a rendercontext. So I guess you've been using our header back then, which is sadly missing (though I've been in contact with embarcadero to replace their badly outdated header with ours).

You can always grab the latest version here : http://wiki.delphigl.com/index.php/dglOpenGL.pas/en

The header works fine with XE2, even with 64 Bit and a lot of people use it to create cross-platform applications (inlcuding me ;) ). It's at least known to work with Win32 (and 64, at least during XE beta), Linux (32 and 64 Bit), Mac OS X and possibly more platforms.

And as for FireMonkey and DX : I was wondering the same thing to, but actually Embarcadero didn't really reply to my question on why they used DX instead of OpenGL. But the damage is done, so I'm pretty sure FireMonkey will be forgotten pretty soon. They could've made so much more out of it with using OpenGL and opening up access to the low-level API functions, but well, never try to understand company decissions...

AthenaOfDelphi
13-11-2011, 11:14 AM
ActivateRenderingContext (and DeactivateRenderingContext) are helper functions from our dglOpenGL-Header to easily create and manage a rendercontext. So I guess you've been using our header back then, which is sadly missing (though I've been in contact with embarcadero to replace their badly outdated header with ours).

You can always grab the latest version here : http://wiki.delphigl.com/index.php/dglOpenGL.pas/en

The header works fine with XE2, even with 64 Bit and a lot of people use it to create cross-platform applications (inlcuding me ;) ). It's at least known to work with Win32 (and 64, at least during XE beta), Linux (32 and 64 Bit), Mac OS X and possibly more platforms.

And as for FireMonkey and DX : I was wondering the same thing to, but actually Embarcadero didn't really reply to my question on why they used DX instead of OpenGL. But the damage is done, so I'm pretty sure FireMonkey will be forgotten pretty soon. They could've made so much more out of it with using OpenGL and opening up access to the low-level API functions, but well, never try to understand company decissions...

Hi Sascha,

I started out with your headers, because I use them on RS2009 and I find them nice and easy. The problem is, they rely on a number of definitions from the Windows unit, like HGLRC (if I recall correctly) and HDC. I couldn't find the equivalent definitions for building with Mac OSx, so I figured they wouldn't work.

Do you have some example code you could publish for doing this sort of thing in a nice cross platform compatible way in XE2?

Sascha Willems
13-11-2011, 11:23 AM
The headers should do fine with Linux/Mac OS (X). The only thing you can't use are the CreateRenderingContext, ActivateRenderingContext, etc. helper functions, as those only support windows. So if you're able to get a render context for linux / Mac then you can use the dglOpenGL.pas without problems.

As I don't own XE2, I can't help you there sadly. For my cross-platform stuff I use FPC and the TOpenGLControl from Lazarus, which gives me a valid rendering context for each of the platforms that I can then use our header with.

AthenaOfDelphi
13-11-2011, 11:39 AM
The headers should do fine with Linux/Mac OS (X). The only thing you can't use are the CreateRenderingContext, ActivateRenderingContext, etc. helper functions, as those only support windows. So if you're able to get a render context for linux / Mac then you can use the dglOpenGL.pas without problems.

As I don't own XE2, I can't help you there sadly. For my cross-platform stuff I use FPC and the TOpenGLControl from Lazarus, which gives me a valid rendering context for each of the platforms that I can then use our header with.

Thanks Sascha,

Guess it's time to download Lazarus and see about creating an XE2 version of TOpenGLControl from the source... I suspect that will be a major feat in it's own right given it will need to sit in the somewhat complex structure that is FireMonkey.

Or of course, I fall back to RS2009 and write a Windows only version and then port it, or alternatively use Lazarus... decisions decisions :-)

AthenaOfDelphi
13-11-2011, 11:54 AM
Actually, scratch the idea of porting TOpenGLControl to XE2... it's nasty. Just installed Lazarus and taken a peek at the source code :o

LP
13-11-2011, 04:42 PM
Yeah, I looked into wglCreateContext and wglMakeCurrent. The problem is, I can't seem to find an equivalent when I enable the OS X build platform. If I recall correctly, for these two call I add WinAPI.OpenGL to the uses, but I can't seem to find an equivalent for Mac. Any suggestions?
In Embarcadero's Delphi XE 2 folder you can look at "FMX.Context.Mac.pas", which has the initialization code for Mac.


To be honest, I'm amazed they elected to use Direct X for FireMonkey on Windows. Why not just use OpenGL on all platforms?
I've written about this before. The curious thing is that they use latest headers for Mac but outdated OpenGL headers on Windows. My guess about using DirectX is probably the compatibility. For OpenGL to work nicely you need to use latest video drivers, which inexperienced users will rarely have. For instance, my partner's relatives have Eee PC which has stock drivers in Windows XP and OpenGL applications run in software mode, while Direct3D has no problems (though it is still buggy somewhat).


If I'm really honest, I'm actually finding this whole cross platform thing to be a quite disappointing. Lots of definitions have moved, things that are useful like 'Anchors' aren't there in FireMonkey, and so far my experience with the help is that it's sadly lacking.
As I've said before, FireMonkey is no match for VCL or even LCL, and it seems to be in infant stages. However, it does have benefits, especially if they use it to its full potential.


And as for FireMonkey and DX : I was wondering the same thing to, but actually Embarcadero didn't really reply to my question on why they used DX instead of OpenGL. But the damage is done, so I'm pretty sure FireMonkey will be forgotten pretty soon. They could've made so much more out of it with using OpenGL and opening up access to the low-level API functions, but well, never try to understand company decissions...
Sorry, but you are not being objective. Your opinion seems to be biased towards OpenGL, which in no way better than DirectX and you can't do more with it. Both are just APIs to the underlying hardware, nothing more. In fact, on new Windows installation you'll most likely DO LESS with OpenGL, because on Vista/7 you are stuck with OpenGL 1.4 when using Microsoft drivers, while DX10/11 is still readily available.

I seriously doubt it that FireMonkey will be forgotten any time soon. ;) But I do think you should read the source code first (since documentation for FireMonkey is nonexistent) before making such immature claims.

You *do* have access to low-level API functions in FireMonkey, both in OpenGL and DirectX. First, you can use their TContext3D, which contains most low-level API functions wrapped. Look for "FMX.Context.DX9.pas" and "FMX.Context.Mac.pas" specifically. Second, you can just hook into Direct3D/OpenGL to use them directly, exactly as Asphyre does it.

AthenaOfDelphi
13-11-2011, 05:07 PM
Thanks for the pointers Yuriy, I'll have a look at that file and see where it can get me.