Getting GtkD working with OpenGL

Chalix via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 6 03:49:40 PDT 2016


On Tuesday, 4 October 2016 at 17:00:34 UTC, Mike Wey wrote:
> Replace "import glgdk.GLContext;" with "import gdk.GLContext;"

Hey Mike, you've been a great help so far! Thank you :)

I thought now I am ready to draw something, but I struggle with 
the GLContext and GdkGlContext. Where do I get a valid Context, 
or how do I make it valid? My program crashes always with 
segmentation fault, as soon as I call a GL function except 
glClear.

It must be a problem with the Context... Btw, what exactly is a 
GLContext and what is it used for? I am not used to it, since 
neither Qt nor glut uses or needs a GLContext-like structure.

Would be great, if you see the error again... I think I am only 
missing an initialization function in my "initGL". I didn't find 
anything on the Internet. Here my code:

import gtk.Main;
import gtk.MainWindow;
import gtk.GLArea;
import gdk.GLContext;
import derelict.opengl3.gl;
import std.functional : toDelegate;

GLContext initGL(GLArea area) {
	DerelictGL3.load();

	/* Error happens somewhere here: */
	GdkGLContext *gdkCon; /* where do I get a valid "struct 
GdkGLContext"?*/
	GLContext context = new GLContext(gdkCon); /*needs GdkGLContext 
pointer for Initialization*/

	context.realize();     /*which of these two functions do I need 
really, and why?*/
	context.makeCurrent();
	/* Runtime Warnings: */
	/* Gdk-CRITICAL **: gdk_gl_context_realize:assertion 
'GDK_IS_GL_CONTEXT (context)' failed */
	/*same for ...make_current...*/
	return context;
}
bool renderGL(GLContext context, GLArea area)
{
	glClearColor (0, 0, 0, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW); /*Here the program crashes with 
Segmentation Fault (Some pointer is NULL)*/
	glLoadIdentity();
	glColor3f(1.0, 1.0, 0.0);
	glBegin(GL_TRIANGLES);
		glVertex3f(0, 0, -2);
		glVertex3f(1, 0, -2);
		glVertex3f(0, 1, -2);
	glEnd();
	return true;
}
void main(string[] args) {
	Main.init(args);
	MainWindow win = new MainWindow("Hello GL");
	GLArea area = new GLArea();
	area.addOnCreateContext(toDelegate(&initGL));
	area.addOnRender(toDelegate(&renderGL));
         //area.addOnResize(toDelegate(&resizeGL));
	win.add(area);
	win.showAll();
	Main.run();
}



More information about the Digitalmars-d-learn mailing list