Advertisement
Guest User

Untitled

a guest
Feb 7th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.84 KB | None | 0 0
  1. module test;
  2.  
  3. import std.string;
  4. import std.conv;
  5. import std.stdio;
  6.  
  7. import derelict.opengl3.gl3;
  8. import derelict.glfw3.glfw3;
  9.  
  10. import glamour.vao: VAO;
  11. import glamour.shader: Shader;
  12. import glamour.vbo: Buffer, ElementBuffer;
  13.  
  14. class Renderer {
  15. private:
  16.     uint width_, height_;
  17.  
  18.     float[] vertices;
  19.     ushort[] indices;
  20.     GLint position_;
  21.  
  22.     VAO vao_;
  23.     Shader program_;
  24.     Buffer vbo_;
  25.     ElementBuffer ibo_;
  26.  
  27.     static immutable string example_program_src_ = `
  28.        #version 120
  29.        vertex:
  30.        attribute vec2 position;
  31.        void main(void)
  32.        {
  33.            gl_Position = vec4(position, 0, 1);
  34.        }
  35.        fragment:
  36.        void main(void)
  37.        {
  38.            gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  39.        }
  40.        `;
  41.  
  42. public:
  43.     this() {
  44.         vertices = [ -0.3, -0.3,  0.3, -0.3,  -0.3, 0.3,  0.3, 0.3];
  45.         indices = [0, 1, 2, 3];
  46.  
  47.         vao_ = new VAO();
  48.         vao_.bind();
  49.  
  50.         vbo_ = new Buffer(vertices);
  51.        
  52.         ibo_ = new ElementBuffer(indices);
  53.  
  54.         program_ = new Shader("example_program", example_program_src_);
  55.         program_.bind();
  56.         position_ = program_.get_attrib_location("position");
  57.     }
  58.  
  59.     void draw() {
  60.         glClearColor(1, 0.9, 0.8, 1);
  61.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  62.        
  63.         vbo_.bind();
  64.         glEnableVertexAttribArray(position_);
  65.      
  66.         glVertexAttribPointer(position_, 2, GL_FLOAT, GL_FALSE, 0, null);
  67.      
  68.         ibo_.bind();
  69.      
  70.         glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, null);
  71.      
  72.         glDisableVertexAttribArray(position_);
  73.     }
  74.  
  75.     void close() {
  76.         ibo_.remove();
  77.         vbo_.remove();
  78.         program_.remove();
  79.         vao_.remove();
  80.     }
  81. }
  82.  
  83. class Application {
  84.     private GLFWwindow* window;
  85.     private OnDraw on_draw_;
  86.    
  87.     alias void delegate() OnDraw;
  88.    
  89.     @property
  90.     onDraw(OnDraw on_draw) {
  91.         assert(on_draw);
  92.         on_draw_ = on_draw;
  93.     }
  94.  
  95.     this() {
  96.         DerelictGL3.load();
  97.         DerelictGLFW3.load();
  98.        
  99.         glfwInit();
  100.  
  101.         window = glfwCreateWindow(1280, 720, "Hello World", null, null);
  102.         glfwMakeContextCurrent(window);
  103.  
  104.         GLVersion gl = DerelictGL3.reload();
  105.     }
  106.  
  107.     void run() {
  108.         assert(on_draw_);
  109.         auto run = true;
  110.         while (!glfwWindowShouldClose(window)) {
  111.             glClearColor(0f, 0f, 0f, 0f);
  112.  
  113.             glfwPollEvents();
  114.             glClear(GL_COLOR_BUFFER_BIT);
  115.  
  116.             on_draw_();
  117.  
  118.             glfwSwapBuffers(window);
  119.         }
  120.  
  121.         glfwTerminate();
  122.     }
  123. }
  124.  
  125. void main() {
  126.    
  127.     auto app = new Application();
  128.  
  129.     auto renderer = new Renderer();
  130.     scope(exit)
  131.         renderer.close();
  132.  
  133.     app.onDraw = &renderer.draw;
  134.     app.run();
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement