Created
June 11, 2013 07:14
-
-
Save nlguillemot/5754984 to your computer and use it in GitHub Desktop.
gtkD with opengl3 using derelict3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gdk.Event; | |
import gtk.DrawingArea; | |
import gtk.Main; | |
import gtk.MainWindow; | |
import gtk.Widget; | |
import gtk.Button; | |
import gtk.VBox; | |
import glgdk.GLConfig; | |
import glgdk.GLContext; | |
import glgdk.GLdInit; | |
import glgdk.GLWindow; | |
import glgtk.GLCapability; | |
import gtkglc.glgdktypes; | |
import derelict.opengl3.gl3; | |
import std.file; | |
import std.exception; | |
import std.conv; | |
import std.stdio; | |
class SimpleGL : DrawingArea | |
{ | |
GLfloat width; | |
GLfloat height; | |
GLuint vbo,vao; | |
GLuint fs,vs,sp; | |
bool initialized; | |
/** need to include the mixin to add GL capabilities to this widget */ | |
mixin GLCapability; | |
this() | |
{ | |
super(640, 480); | |
DerelictGL3.load(); | |
setGLCapability(); // set the GL capabilities for this widget | |
} | |
void initGL() | |
{ | |
DerelictGL3.reload(); | |
resizeGL(null); | |
static immutable GLfloat vertices[9] = [ | |
-1.0f, 1.0f, -1.0f, | |
-1.0f, 0.0f, -1.0f, | |
0.0f, 0.0f, -1.0f | |
]; | |
glGenBuffers(1, &vbo); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, vertices.sizeof, vertices.ptr, GL_STATIC_DRAW); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glGenVertexArrays(1, &vao); | |
glBindVertexArray(vao); | |
glEnableVertexAttribArray(0); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, null); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glBindVertexArray(0); | |
string fsrc = readText("simple.frag"); | |
string vsrc = readText("simple.vert"); | |
GLint status; | |
char infoLog[512]; | |
const char* fsrc_c = fsrc.ptr; | |
fs = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(fs, 1, &fsrc_c, null); | |
glCompileShader(fs); | |
glGetShaderiv(fs, GL_COMPILE_STATUS, &status); | |
if (!status) | |
{ | |
GLsizei len; | |
glGetShaderInfoLog(fs, infoLog.sizeof, &len, infoLog.ptr); | |
throw new Exception(to!string(infoLog[0 .. len])); | |
} | |
const char* vsrc_c = vsrc.ptr; | |
vs = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vs, 1, &vsrc_c, null); | |
glCompileShader(vs); | |
glGetShaderiv(vs, GL_COMPILE_STATUS, &status); | |
if (!status) | |
{ | |
GLsizei len; | |
glGetShaderInfoLog(vs, infoLog.sizeof, &len, infoLog.ptr); | |
throw new Exception(to!string(infoLog[0 .. len])); | |
} | |
sp = glCreateProgram(); | |
glAttachShader(sp, vs); | |
glAttachShader(sp, fs); | |
glLinkProgram(sp); | |
glGetProgramiv(sp, GL_LINK_STATUS, &status); | |
if (!status) | |
{ | |
GLsizei len; | |
glGetProgramInfoLog(sp, infoLog.sizeof, &len, infoLog.ptr); | |
throw new Exception(to!string(infoLog[0 .. len])); | |
} | |
initialized = true; | |
} | |
void destroyGL() | |
{ | |
glDeleteBuffers(1, &vbo); | |
glDeleteVertexArrays(1, &vao); | |
glDeleteShader(fs); | |
glDeleteShader(vs); | |
glDeleteProgram(sp); | |
} | |
bool drawGL() | |
{ | |
if (!initialized) | |
return true; | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glBindVertexArray(vao); | |
glUseProgram(sp); | |
glDrawArrays(GL_TRIANGLES,0,3); | |
glUseProgram(0); | |
glBindVertexArray(0); | |
return true; | |
} | |
bool resizeGL(Event event = null) { | |
GLfloat w; | |
GLfloat h; | |
if ( event is null || event.type != GdkEventType.CONFIGURE ) | |
{ | |
w = getWidth(); | |
h = getHeight(); | |
} | |
else | |
{ | |
w = event.configure.width; | |
h = event.configure.height; | |
} | |
width = w; | |
height = h; | |
glViewport (0, 0, cast(int)w, cast(int)h); //Adjust the viewport according to new window dimensions | |
return true; | |
} | |
} | |
class MyWindow : MainWindow | |
{ | |
this() | |
{ | |
super("My Window!"); | |
VBox box = new VBox(false,2); | |
SimpleGL simpleGL = new SimpleGL(); | |
box.add(simpleGL); | |
Button meowButton = new Button("Meow!"); | |
box.add(meowButton); | |
add(box); | |
showAll(); | |
} | |
} | |
void main(string[] args) | |
{ | |
Main.init(args); | |
GLdInit.init(args); | |
new MyWindow(); | |
Main.run(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 | |
out vec4 color; | |
void main() | |
{ | |
color = vec4(0,1,0,1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 | |
layout(location = 0) in vec3 position; | |
void main() | |
{ | |
gl_Position = vec4(position,1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment