public
Created

Derelict3 + GLFW3 Example

  • Download Gist
buffer.d
D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
module buffer;
 
import derelict.opengl3.gl3;
 
class VertexArray
{
GLuint vao;
alias vao this;
 
this()
{
glGenVertexArrays(1, &vao);
}
 
void bind()
{
glBindVertexArray(vao);
}
 
void bind(GLuint loc, GLenum type, int components, int stride, int offset = 0, bool normalize = false)
{
bind();
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, components, type,
normalize, stride, cast(void*)offset);
}
}
 
class Buffer
{
GLuint buffer;
alias buffer this;
 
this(T)(T[] data)
{
glGenBuffers(1, &buffer);
setData(data);
}
 
void bind()
{
glBindBuffer(GL_ARRAY_BUFFER, buffer);
}
 
 
void setData(T)(T[] data)
{
bind();
glBufferData(GL_ARRAY_BUFFER,
T.sizeof * data.length,
data.ptr, GL_STATIC_DRAW);
}
 
}
shader.d
D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
module shader;
 
import std.file,
std.regex,
std.algorithm,
std.conv,
std.stdio;
import std.array;
import std.string : splitLines;
import derelict.opengl3.gl3;
 
class Shader
{
GLuint vertShader,
fragShader,
geomShader,
program;
alias program this;
 
this(string filename)
{
buildShaders(filename.readText());
}
 
void buildShaders(string src)
{
string[] directives;
string[][string] parts;
 
auto partMatch = regex(`^(\w+):`);
string curType;
foreach (line; src.splitLines())
{
if (line.startsWith("#"))
directives ~= line;
 
else {
auto m = line.match(partMatch);
 
if (m)
curType = m.captures[1];
 
else
parts[curType] ~= line;
}
}
 
// Get shader objects
string source;
if (auto s = "vertex" in parts)
{
vertShader = glCreateShader(GL_VERTEX_SHADER);
source = (directives ~ *s).join("\n");
const(char*) p = source.ptr;
glShaderSource(vertShader, 1, &p, null);
vertShader.compileShader();
}
 
if (auto s = "fragment" in parts)
{
fragShader = glCreateShader(GL_FRAGMENT_SHADER);
source = (directives ~ *s).join("\n");
const(char*) p = source.ptr;
glShaderSource(fragShader, 1, &p, null);
fragShader.compileShader();
}
 
//TODO geometry shader
 
// Link into a program
program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
 
// Check the program
int ret, logLen;
glGetProgramiv(program, GL_LINK_STATUS, &ret);
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLen);
 
if (logLen > 1)
{
char[] log = new char[](logLen);
glGetProgramInfoLog(program, logLen, null, log.ptr);
writeln("Program link log:\n", to!string(log));
}
 
glDeleteShader(vertShader);
glDeleteShader(fragShader);
}
 
void bind()
{
glUseProgram(program);
}
 
}
private void compileShader(GLuint shader)
{
glCompileShader(shader);
 
// Check Vertex Shader
int ret, logLen;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ret);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLen);
 
if (logLen > 1)
{
char[] log = new char[](logLen);
glGetShaderInfoLog(shader, logLen, null, log.ptr);
writeln("Shader compile log:\n", to!string(log));
}
 
}
simple.glsl
GLSL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#version 330
 
vertex:
layout(location=0) in vec3 position;
 
//uniform mat4 projection;
//uniform mat4 view;
//uniform mat4 model;
 
void main()
{
//gl_Position = projection * view * model * vec4(position, 0);
gl_Position = vec4(position, 1);
}
 
fragment:
out vec3 color;
void main()
{
color = vec3(0.5, 0.6, 0.8);
}
test1.d
D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 
import derelict.opengl3.gl3;
import derelict.glfw3.glfw3;
 
import buffer, shader;
 
pragma(lib, "DerelictGLFW3");
pragma(lib, "DerelictGL3");
pragma(lib, "DerelictUtil");
pragma(lib, "dl");
 
void main(string[] args)
{
DerelictGL3.load();
DerelictGLFW3.load();
 
if (!glfwInit())
throw new Exception("Failed to initialize GLFW3");
scope(exit) glfwTerminate();
 
glfwWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
auto window = glfwCreateWindow(800, 600, GLFW_WINDOWED, "GLASS", null);
assert(window !is null);
glfwMakeContextCurrent(window);
 
auto vers = DerelictGL3.reload();
 
auto shader = new Shader("simple.glsl");
shader.bind();
 
float[] rawVerts = [-1, -1, 0,
1, -1, 0,
0, 1, 0];
auto vertices = new Buffer(rawVerts);
auto vertArray = new VertexArray();
vertArray.bind(0, GL_FLOAT, 3, 3 * float.sizeof);
 
while (true) {
 
glfwPollEvents();
if ( glfwGetKey( window, GLFW_KEY_ESCAPE ) == GLFW_PRESS )
break;
 
glClear(GL_COLOR_BUFFER_BIT);
 
glDrawArrays(GL_TRIANGLES, 0, cast(int)rawVerts.length);
 
glfwSwapBuffers(window);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.