Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module test;
- import std.string;
- import std.conv;
- import std.stdio;
- import derelict.opengl3.gl3;
- import derelict.glfw3.glfw3;
- import glamour.vao: VAO;
- import glamour.shader: Shader;
- import glamour.vbo: Buffer, ElementBuffer;
- class Renderer {
- private:
- uint width_, height_;
- float[] vertices;
- ushort[] indices;
- GLint position_;
- VAO vao_;
- Shader program_;
- Buffer vbo_;
- ElementBuffer ibo_;
- static immutable string example_program_src_ = `
- #version 120
- vertex:
- attribute vec2 position;
- void main(void)
- {
- gl_Position = vec4(position, 0, 1);
- }
- fragment:
- void main(void)
- {
- gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
- }
- `;
- public:
- this() {
- vertices = [ -0.3, -0.3, 0.3, -0.3, -0.3, 0.3, 0.3, 0.3];
- indices = [0, 1, 2, 3];
- vao_ = new VAO();
- vao_.bind();
- vbo_ = new Buffer(vertices);
- ibo_ = new ElementBuffer(indices);
- program_ = new Shader("example_program", example_program_src_);
- program_.bind();
- position_ = program_.get_attrib_location("position");
- }
- void draw() {
- glClearColor(1, 0.9, 0.8, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- vbo_.bind();
- glEnableVertexAttribArray(position_);
- glVertexAttribPointer(position_, 2, GL_FLOAT, GL_FALSE, 0, null);
- ibo_.bind();
- glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, null);
- glDisableVertexAttribArray(position_);
- }
- void close() {
- ibo_.remove();
- vbo_.remove();
- program_.remove();
- vao_.remove();
- }
- }
- class Application {
- private GLFWwindow* window;
- private OnDraw on_draw_;
- alias void delegate() OnDraw;
- @property
- onDraw(OnDraw on_draw) {
- assert(on_draw);
- on_draw_ = on_draw;
- }
- this() {
- DerelictGL3.load();
- DerelictGLFW3.load();
- glfwInit();
- window = glfwCreateWindow(1280, 720, "Hello World", null, null);
- glfwMakeContextCurrent(window);
- GLVersion gl = DerelictGL3.reload();
- }
- void run() {
- assert(on_draw_);
- auto run = true;
- while (!glfwWindowShouldClose(window)) {
- glClearColor(0f, 0f, 0f, 0f);
- glfwPollEvents();
- glClear(GL_COLOR_BUFFER_BIT);
- on_draw_();
- glfwSwapBuffers(window);
- }
- glfwTerminate();
- }
- }
- void main() {
- auto app = new Application();
- auto renderer = new Renderer();
- scope(exit)
- renderer.close();
- app.onDraw = &renderer.draw;
- app.run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement