My simpledisplay.d
module also supports the creation of OpenGL contexts. It uses an older version of OpenGL but covers the basic functionality, and the same principle can be used to add other OpenGL functions. Here, we'll create a colorful spinning pyramid.
Download simpledisplay.d
and color.d
from my Github repository and put them in your project folder. On Windows, you must also acquire opengl32.lib
and glu32.lib
from my Github repository.
Let's create an OpenGL window by executing the following steps:
simpledisplay
.SimpleWindow
object, passing OpenGlOptions.yes
to the constructor.window.redrawOpenGlScene
to a delegate that will be called to draw your scene. This function should use the gl*
family of functions to do the drawing and should not have to swap the OpenGL buffers. For our spinning pyramid, this function will set up our matrix, set up our viewport, and enable depth testing. Then, draw the pyramid.window.eventLoop
with a timeout and a timeout handler function (a delegate with no arguments) that calls redrawOpenGlSceneNow
to draw your new frame.dmd yourfile.d simpledisplay.d color.d
on Posix. On Windows, also add –version=with_opengl
to the compile command.import simpledisplay; void main() { auto window = new SimpleWindow(512, 512, "OpenGL Demo", OpenGlOptions.yes); float f = 0.0; window.redrawOpenGlScene = { glMatrixMode(GL_PROJECTION); glClearDepth(1.0f); glEnable (GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glViewport(0,0,window.width,window.height); // clear the screen glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT); glLoadIdentity(); glRotatef(f, 1, 0, 0); f += 4.5; glBegin(GL_TRIANGLES); // base of the pyramid glColor3f(1, 0, 0); glVertex3f(0.5, -0.5, 0); glColor3f(0, 1, 0); glVertex3f(0, 0.5, 0); glColor3f(0, 0, 1); glVertex3f(-0.5, -0.5, 0); // the other three sides connect to the top glColor3f(1, 1, 1); glVertex3f(0, 0, 0.5); glColor3f(0, 1, 0); glVertex3f(0, 0.5, 0); glColor3f(0, 0, 1); glVertex3f(-0.5, -0.5, 0); glColor3f(1, 0, 0); glVertex3f(0.5, -0.5, 0); glColor3f(1, 1, 1); glVertex3f(0, 0, 0.5); glColor3f(0, 0, 1); glVertex3f(-0.5, -0.5, 0); glColor3f(1, 1, 1); glVertex3f(0, 0, 0.5); glColor3f(1, 0, 0); glVertex3f(0.5, -0.5, 0); glColor3f(0, 1, 0); glVertex3f(0, 0.5, 0); glEnd(); }; window.eventLoop(50, delegate () { window.redrawOpenGlSceneNow(); }); }
Running the program will open a window with an animated spinning pyramid. The following is one frame from it:
The simpledisplay.d
module also includes bindings to OpenGL 1.1. It uses a delegate to redraw the scene so that it can automatically handle cases where your window was covered partially and exposed without needing you to work the whole message loop. The delegate is bracketed by code that prepares the OpenGL context, binding it to the correct window and then swapping the buffers when it is finished—automatically displaying your frame.
OpenGL is disabled by default on Windows because the necessary library files are not bundled with DMD. I created these libraries by running implib
, the tool from Digital Mars, on the opengl32.dll
and glu32.dll
files—as we learned about in Chapter 1, Core Tasks. On 32-bit Windows, the creation or conversion of import libraries is often necessary because DMD uses an old format that is no longer common. This limitation has been solved for 64-bit Windows.
simpledisplay.d
.3.139.97.202