public
Last active

Example application for gl3n/glamour

  • Download Gist
build.rf
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
-version=Derelict3
-version=SDLImage # needed for glamour (otherwise DevIL would be used for texture loading, but SDL is more common)
-version=gl3n # enable gl3n support in glamour
 
-debug
 
-L-lDerelictSDL2
-L-lDerelictUtil
-L-lDerelictGLFW3
-L-lDerelictGL3
-L-ldl
 
-I../glamour/glamour
 
../glamour/glamour/gl.d
../glamour/glamour/shader.d
../glamour/glamour/vbo.d
../glamour/glamour/sampler.d
../glamour/glamour/texture.d
../glamour/glamour/util.d
 
-I../gl3n/gl3n/
 
../gl3n/gl3n/linalg.d
../gl3n/gl3n/util.d
../gl3n/gl3n/math.d
../gl3n/gl3n/plane.d
../gl3n/gl3n/aabb.d
../gl3n/gl3n/frustum.d
 
-ofglhw
glhw.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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
module glhw;
 
private {
import std.conv : to;
 
import glamour.gl;
import glamour.shader : Shader;
import glamour.vbo : Buffer;
import derelict.glfw3.glfw3;
 
import gl3n.linalg : mat4, vec2;
 
debug import std.stdio;
}
 
static this() {
DerelictGLFW3.load();
DerelictGL3.load();
 
if(!glfwInit()) {
throw new Exception("glfwInit failure: " ~ to!string(glfwErrorString(glfwGetError())));
}
}
 
GLFWwindow open_glfw_win(int width, int height) {
glfwWindowHint(GLFW_WINDOW_RESIZABLE, GL_FALSE);
 
auto window = glfwCreateWindow(width, height, "Hello World (I am the title!)", null, null);
 
if(!window) {
throw new Exception("I am sorry man, I am not able to initialize a window/create an OpenGL context :/.");
}
glfwMakeContextCurrent(window);
 
glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_CAPTURED);
 
glfwSwapInterval(0); // change this to 1 for vsync
 
return window;
}
 
immutable string shader_source = `
#version 130
 
vertex:
in vec4 position;
in vec4 color;
 
out vec4 v_color;
 
uniform vec2 offset;
uniform mat4 proj;
 
void main() {
vec4 camera_pos = position + vec4(offset, 0.0f, 0.0f);
gl_Position = proj * camera_pos;
v_color = color;
};
 
fragment:
in vec4 v_color;
 
out vec4 output_color;
 
void main() {
output_color = v_color;
}`;
 
 
const float[288] vertex_data = [
0.25f, 0.25f, -1.25f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.25f, -0.25f, -1.25f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
-0.25f, 0.25f, -1.25f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
 
0.25f, -0.25f, -1.25f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
-0.25f, -0.25f, -1.25f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
-0.25f, 0.25f, -1.25f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
 
0.25f, 0.25f, -2.75f, 1.0f,
0.8f, 0.8f, 0.8f, 1.0f,
-0.25f, 0.25f, -2.75f, 1.0f,
0.8f, 0.8f, 0.8f, 1.0f,
0.25f, -0.25f, -2.75f, 1.0f,
0.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 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
 
0.25f, -0.25f, -2.75f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
-0.25f, -0.25f, -1.25f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
0.25f, -0.25f, -1.25f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
0.25f, -0.25f, -2.75f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
-0.25f, -0.25f, -2.75f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
-0.25f, -0.25f, -1.25f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f
];
 
 
extern(C) {
void key_callback(GLFWwindow* window, int key, int state) {
void* user_ptr = glfwGetWindowUserPointer(window);
HelloWorld hw = cast(HelloWorld)user_ptr;
 
if(state == GLFW_PRESS) {
hw.on_key_down(key);
} else {
hw.on_key_up(key);
}
}
}
 
 
class HelloWorld {
Buffer vbo; // where we store our veertices
Shader shader; // actually it's a OpenGL program with attached shaders
 
GLFWwindow* window;
int width;
int height;
 
bool exit = false;
 
this(int width, int height) {
this.width = width;
this.height = height;
window = open_glfw_win(width, height);
 
auto glv = DerelictGL3.reload();
debug writefln("OpenGL Version: %s", glv);
// filename can be anything, this is used to identify the shader of compilation or linking fails
shader = new Shader("filename", shader_source);
//shader = new Shader("/tmp/foo.shader"); // this would load the shader from a file
 
vbo = new Buffer();
vbo.set_data(vertex_data);
 
glViewport(0, 0, width, height);
 
setup_proj();
 
glfwSetWindowUserPointer(window, cast(void *)this);
glfwSetKeyCallback(&key_callback);
}
 
void setup_proj() {
shader.bind();
 
mat4 perspective = mat4.perspective(width, height, 60, 0.2f, 3.0f);
shader.uniform("proj", perspective);
}
 
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
 
shader.bind(); // in GL: glUseProgram
 
GLuint position = shader.get_attrib_location("position");
GLuint color = shader.get_attrib_location("color");
 
float offset = ((glfwGetTime() % 20) - 10.0f) / 20.0f;
shader.uniform("offset", vec2(offset, offset));
 
// bind-arguments:
// location, type, size, offset, stride
vbo.bind(position, GL_FLOAT, 4, 0, 8*float.sizeof);
vbo.bind(color, GL_FLOAT, 4, 4*float.sizeof, 8*float.sizeof);
 
// 2 = vertices and color information
// 4 = 4 floats per vertex/color
glDrawArrays(GL_TRIANGLES, 0, vertex_data.length/2/4);
}
 
void run() {
while(!exit) {
display();
glfwSwapBuffers(window);
glfwPollEvents();
}
}
 
void on_key_down(int key) {
if(key == GLFW_KEY_ESCAPE) {
exit = true;
}
}
void on_key_up(int key) {}
 
}
 
void main() {
scope(exit) glfwTerminate();
 
auto hello_world = new HelloWorld(1000, 800);
hello_world.run();
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.