PDA

View Full Version : Curious OpenGL -GLUT +Linux +Windows + MAC Can it be done?



jdarling
08-07-2010, 02:43 PM
So, because I'm a gluten for punishment, I decided to re-visit a few things I have on my list of "can this be done yet" and I came to Cross Platform OpenGL without using GLUT, SDL, or something else for Windowed and Fullscreen environments.

I've read the posts, I've searched the web, I've checked and re-checked the books... And fact is, I can't find an answer.

So, my question is, does anyone have a VERY basic example of how to do an OpenGL app in FPC or Lazarus that will work in Windows, Linux, and MAC that ONLY uses the GL and GLU units that ship with FreePascal?

If not, how close can you come? Lots of examples using the Windows unit, but that breaks Linux, examples for Linux using GLUT but that doesn't install so well on MAC, etc etc etc...

- Jeremy

Stoney
08-07-2010, 03:56 PM
You can use TOpenGLControl which ships with Lazarus but is not installed by default. It can be found in /components/opengl.

Just put the OpenGLControl on your form, add gl and glu to your uses clause, add a timer. Declare Angle as Single as a private variable and add this code in your OnTimer-Event:

glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, double(width) / height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0, -0.0,-6.0);
glRotatef(30,1,0,0);
glRotatef(Angle,0,1,0);
Inc(Angle);
If Angle = 360
Then Angle:= 0;

glBegin(GL_QUADS);
glColor3f(0.0,1.0,0.0); // Set The Color To Green
glVertex3f( 1.0, 1.0,-1.0); // Top Right Of The Quad (Top)
glVertex3f(-1.0, 1.0,-1.0); // Top Left Of The Quad (Top)
glVertex3f(-1.0, 1.0, 1.0); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0, 1.0, 1.0); // Bottom Right Of The Quad (Top)
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,0.5,0.0); // Set The Color To Orange
glVertex3f( 1.0,-1.0, 1.0); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0,-1.0, 1.0); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0,-1.0,-1.0); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0,-1.0,-1.0); // Bottom Right Of The Quad (Bottom)
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0); // Set The Color To Red
glVertex3f( 1.0, 1.0, 1.0); // Top Right Of The Quad (Front)
glVertex3f(-1.0, 1.0, 1.0); // Top Left Of The Quad (Front)
glVertex3f(-1.0,-1.0, 1.0); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0,-1.0, 1.0); // Bottom Right Of The Quad (Front)
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,1.0,0.0); // Set The Color To Yellow
glVertex3f( 1.0,-1.0,-1.0); // Bottom Left Of The Quad (Back)
glVertex3f(-1.0,-1.0,-1.0); // Bottom Right Of The Quad (Back)
glVertex3f(-1.0, 1.0,-1.0); // Top Right Of The Quad (Back)
glVertex3f( 1.0, 1.0,-1.0); // Top Left Of The Quad (Back)
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,1.0); // Set The Color To Blue
glVertex3f(-1.0, 1.0, 1.0); // Top Right Of The Quad (Left)
glVertex3f(-1.0, 1.0,-1.0); // Top Left Of The Quad (Left)
glVertex3f(-1.0,-1.0,-1.0); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0,-1.0, 1.0); // Bottom Right Of The Quad (Left)
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,0.0,1.0); // Set The Color To Violet
glVertex3f( 1.0, 1.0,-1.0); // Top Right Of The Quad (Right)
glVertex3f( 1.0, 1.0, 1.0); // Top Left Of The Quad (Right)
glVertex3f( 1.0,-1.0, 1.0); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0,-1.0,-1.0); // Bottom Right Of The Quad (Right)
glEnd();

OpenGLControl1.SwapBuffers;


When you start the application, you should see a rotating cube. It works on Windows, Linux and Mac OS almost perfectly*.


* That means on Mac OS X the OpenGL Control will always override other controls on the form and I've heard there might be some bugs when using GTK2. (I've never tested Linux + TOpenGLControl myself.)


P.S.: What is wrong with SDL or why aren't you considering it? :)

jdarling
08-07-2010, 04:55 PM
Nothing wrong with SDL, and I have an entire toolkit built around its use. As I said in the subject, more of a curiosity than anything. This more comes down to I want to port a few projects to Android and maintain the same code base across all platform. Since Android supports OpenGL ES I figured that OpenGL was the best way to go.

The problem I've read about with TOpenGLControl is you can't go fullscreen. I'm also not sure if you can use it with FPC without including all the other stuff required by Lazarus. Since I plan on porting to Android a pure FPC solution would be best.

- Jeremy

Stoney
08-07-2010, 05:23 PM
SDL will support Android very soon, see: http://forums.libsdl.org/viewtopic.php?t=6420
If you want to support as many platforms as possible, SDL is the best choice - at least in my experience. I have the same codebase in my projects and can target Windows, Linux, Mac OS X and GP2X (and iDevices if that would be allowed).

If you just want to target the big Three (Win, Linux, Mac) SFML (http://www.sfml-dev.org/) is also quite good. (I've started on SFML Pascal bindings some time ago, I think I should finish them.)

As for Lazarus, TOpenGLControl and fullscreen: You can always set Borderstyle of your form to bsNone, WindowState to wsMaximized and the align of the OpenGLControl to alClient, but that's not really a good solution to emulate fullscreen.

jdarling
08-07-2010, 07:16 PM
Guessing I didn't do a good job of explaining what I'm looking for... I already know about SDL, SFML, <other 3rd party libraries>... And I really don't care about them, I just want to know if anyone has done the following:

2D is fine as most ES machines don't have processors sufficient to handle full 3D
Fullscreen and windowed modes, opengl basic libraries only (GL.pas and GLu.pas). Fullscreen only on appropriate implementations (IE: ES devices)
Can use compiler directives, but should only use basic ones (IFDEF Windows, Linux). I'm not even all that interested in Mac/iPhone since 3.3.1 basically still kills iPhone development and I don't have a MAC yet :).


- Jeremy

noeska
08-07-2010, 08:38 PM
Currently i have a basic bare bones linux example only dependend on x and opengl.
And i have an barebones win32 example only dependen on opengl (and windows ofcourse)
Also i have an opengles linux example for the beagleboard (but that one cannot read mouse and keyboard events)
Both have an glinit and an gldraw. And all can use glsl.
Plan is to make this an small framework.
I leave osx to others as i dont have access to that platform.
If you want i can post the examples here.