Skip to content

@jwhear /buffer.d
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Derelict3 + GLFW3 Example
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);
}
}
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));
}
}
#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);
}
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.