News | About | Download | Documentation | Development | Links
This document explains how to use OpenGL in ClanLib.
OpenGL is already a crossplatform library - most distros even include GLUT, which removes the platform dependant initialization. Why use ClanLib then?
The answer is that ClanLib provides much more than simply its display support. You will get access to the 2D functionality, crossplatform sound, networking and a nice framework for it all.
In order to use OpenGL in ClanLib, you will have to link against clanGL, and initialize it. You do this by calling the functions in CL_SetupGL, accordingly to the system explained in the Getting Started document:
#include <ClanLib/application.h>
#include <ClanLib/core.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>
class MyApp : public CL_ClanApplication
{
public:
virtual int main(int argc, char **argv)
{
CL_SetupCore::init();
CL_SetupGL::init();
CL_SetupDisplay::init();
// Select the video mode
CL_DisplayWindow window("OpenGL", 640, 480);
CL_Display::begin_3d();
while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
{
glBegin(GL_TRIANGLES);
// ...
glEnd();
// Show the frame:
CL_Display::flip();
// Update input and other stuff:
CL_System::keep_alive();
}
CL_Display::end_3d();
CL_SetupGL::deinit();
CL_SetupDisplay::deinit();
CL_SetupCore::deinit();
return 0;
}
} my_app;
The important thing to note here is, that the OpenGL context is first created and select when a video mode is set. So do not make calls to OpenGL before that.
You can use the normal 2D gfx operations that ClanLib offers together with OpenGL. Note that ClanLib uses OpenGL to emulate them, so you cannot use them without telling ClanLib to switch modes. For this you use CL_Display::begin_3d() and CL_Display::end_3d(). Put all your custom OpenGL calls between these two calls.
ClanLib can help you with loading textures into an OpenGL texture. The class CL_Texture creates a texture handle and uses a surface provider to load the texture:
CL_Texture *texture1 = new CL_Texture("image", resources);
CL_Texture *texture2 = new CL_Texture("texture.tga");
To use the texture, you will have to bind it. To do that, call CL_Texture::bind().
texture1->bind(); glBegin(GL_QUADS); glColor3f(1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(10,10,10); glTexCoord2f(1.0f, 0.0f); glVertex3f(20,10,10); glTexCoord2f(1.0f, 1.0f); glVertex3f(20,10,20); glTexCoord2f(0.0f, 1.0f); glVertex3f(10,10,20); glEnd();