Changeset 172 for trunk/dglut

Show
Ignore:
Timestamp:
12/26/07 22:57:14 (1 year ago)
Author:
FeepingCreature
Message:
  • Went back to the simple blob
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dglut/dglut_test.d

    r171 r172  
    3131 
    3232void setupLights() { 
    33   glEnable(GL_DEPTH_TEST); 
    3433  glEnable(GL_LIGHTING); 
    3534  with (Light[0]) { 
    3635    enable; 
    37     position([100, -100, 10], true); 
     36    position(lightpos.parts, true); 
    3837    ambient=[0, 0, 0, 1]; 
    3938    diffuse=[1, 1, 1, 1]; 
    4039    specular=[1, 1, 1, 1]; 
    4140  } 
    42   with (Light[1]) { 
     41  /*with (Light[1]) { 
    4342    enable; 
    4443    position([10, 50, 70], true); 
     
    4645    diffuse=[2, 1, 1, 1]; 
    4746    specular=[1, 1, 1, 1]; 
    48   } 
     47  }*/ 
    4948} 
    5049 
    5150import std.string: toStringz; 
    5251import dglut.ext; 
    53 FrameBuffer test; 
     52FrameBuffer test; Texture target; 
     53float[32] cameraMatrix, lightMatrix; // projection, view 4x4 
     54 
     55vec3f campos, lightpos; 
    5456void main(string[] args) { 
    5557  int c_argc=args.length; 
     
    6264  auto win = glutCreateWindow("Triangle\0".ptr); 
    6365   
     66  campos=vec3f(0, 5, -10); 
     67  lightpos=vec3f(100, -100, 10); 
     68   
    6469  writefln(Extensions.igrep("framebuffer_object")); 
    65   auto test_rb=new RenderBuffer; 
    66   auto tex=new Texture(128, 128); 
    67   glChecked("CreateFramebuffer", test=new FrameBuffer); 
    68   glChecked("AttachTextureAndRenderBuffer", 
    69     test.With(test_rb.attach(GL_DEPTH_ATTACHMENT_EXT, GL_DEPTH_COMPONENT, tex)), 
    70     test.attach(GL_DEPTH_ATTACHMENT_EXT, test_rb) 
    71   ); 
    72   test.checkState; 
    73   writefln(test, " created!"); 
     70  target=new Texture(1024, 1024); 
     71  glChecked("CreateFramebuffer", test=new FrameBuffer, test.attach(GL_COLOR_ATTACHMENT0_EXT, GL_RGBA, target)); 
     72  writefln(test, " created - ", test.width, ":", test.height); 
    7473   
    7574  MatrixMode.Projection; 
     
    7776  gluPerspective(45, 1.0, 0.1, 100); 
    7877  MatrixMode.Modelview; 
     78  Texture.enable; 
     79  glEnable(GL_DEPTH_TEST); 
    7980 
    8081  glutDisplayFunc=&display; 
     
    109110    fps=0; 
    110111  } 
    111   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    112   glClearColor(0, 0, 0, 0); 
    113112  glLoadIdentity; 
    114113  static float f=0; 
     
    118117  Rotate(f*0.2, 0, 1, 0); 
    119118  Rotate(f*0.1, 1, 1, 0); 
    120   glChecked("RenderToFramebuffer", test.With(renderScene(f))); 
     119 
     120  glChecked("RenderToFramebuffer", MatrixScope(test.Using(renderScene(f)))); 
    121121  glChecked("RenderToScreen", renderScene(f)); 
    122   writefln("Done"); 
     122   
    123123  glFlush; 
    124124  glutSwapBuffers(); 
     
    126126 
    127127void renderScene(float t) { 
     128  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    128129  setupLights(); 
    129130  auto points=[ 
     
    165166  } 
    166167   
    167   Texture.enable; 
    168168  static Texture tex; 
    169169  glChecked("DisplayGenerateTexture", { 
     
    184184  })); 
    185185  //writefln(cube.length, " triangles rendered"); 
    186   glChecked("DisplayRenderCube", { 
    187     int height=-5; 
    188     auto floorpoints=[vec3f(-100, height, -100), vec3f(-100, height, 100), vec3f(100, height, 100), vec3f(100, height, -100)]; 
    189     Triangle[] floor=[Triangle(floorpoints[0], floorpoints[1], floorpoints[2]), Triangle(floorpoints[0], floorpoints[2], floorpoints[3])]; 
    190     times(6, { floor=subdivide(floor); }); 
    191     with (Material.FrontAndBack) emission=[2f, 1f, 1f, 1f]; 
    192     Triangles={ foreach (tri; floor) with (tri) { 
    193       Normal(0, 1, 0); 
    194       TexCoord(0, 0); Vertex(x); 
    195       TexCoord(1, 0); Vertex(y); 
    196       TexCoord(1, 1); Vertex(z); 
    197     }}; 
    198   }); 
    199186} 
  • trunk/dglut/ext.d

    r171 r172  
    6868); 
    6969 
     70const GLenum GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT=0x8CD0, 
     71  GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT=0x8CD1; 
     72 
    7073class FrameBuffer : glExtension!("GL_EXT_framebuffer_object") { 
    71   static void checkState() { 
     74  void checkState() { 
     75    auto old=bound(); scope(exit) bind(old); bind; 
     76    alias ExtFunc!(void function(GLenum, GLenum, GLenum, int *), "GetFramebufferAttachmentParameterivEXT") getpar; 
     77    foreach (where, obj; attachedObjects) { 
     78      int res, res2; 
     79      getpar(GL_FRAMEBUFFER_EXT, where, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT, &res); 
     80      getpar(GL_FRAMEBUFFER_EXT, where, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT, &res2); 
     81      if (!res || !res2) throw new Exception("Weird .. no draw buffer attached?"); 
     82    } 
     83     
    7284    auto res=ExtFunc!(GLenum function(GLenum target), "CheckFramebufferStatusEXT")(GL_FRAMEBUFFER_EXT); 
    7385    if (res==GL_FRAMEBUFFER_COMPLETE_EXT) return; 
     
    89101  static void bind(uint whar) { ExtFunc!(void function(GLenum, uint), "BindFramebufferEXT")(GL_FRAMEBUFFER_EXT, whar); } 
    90102  static uint bound() { uint res; glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, cast(int*)&res); return res; } 
     103  bool isBound() { return bound==fb; } 
    91104  void bind() { bind(fb); } 
    92   void With(void delegate()[] dgs...) { 
    93     checkState; 
     105  void Using(void delegate()[] dgs...) { 
    94106    glPushAttrib(GL_VIEWPORT_BIT); scope(exit) glPopAttrib; 
    95107    glViewport(0, 0, width, height); 
    96108    auto old=bound(); scope(exit) bind(old); bind; 
    97     writefln("Viewport limited to ", width, ":", height, "; bound to ", bound, " from ", old)
     109    checkState
    98110    foreach (dg; dgs) dg(); 
    99111  } 
    100    
    101   RenderBuffer[Attachment] attachedObjects; // for the sake of the GC 
    102   int width, height; 
    103    
    104   alias ExtFunc!(void function(GLenum, Attachment, GLenum, uint), "FramebufferRenderbufferEXT") FramebufferRenderbuffer; 
    105   void attach(Attachment where, RenderBuffer rb) { 
    106     if (!width && !height) { 
    107       width=rb.width; height=rb.height; 
    108     } else { 
    109       if (width!=rb.width) throw new Exception("All attachments to an FBO must have the same width and height!"); 
    110       if (height!=rb.height) throw new Exception("All attachments to an FBO must have the same height and width!"); 
    111     } 
    112     attachedObjects[where]=rb; 
    113     auto old=bound(); scope(exit) bind(old); bind; 
    114     FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, where, GL_RENDERBUFFER_EXT, rb.rb); 
    115   } 
    116   void detach(Attachment where) { 
    117     if (!attached(where)) throw new Exception("Trying to detach from unused attachment point!"); 
    118     FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, where, GL_RENDERBUFFER_EXT, 0); 
    119     attachedObjects.remove(where); 
    120     if (!attachedObjects.length) { width=0; height=0; } 
    121   } 
    122   RenderBuffer attached(Attachment where) { auto res=where in attachedObjects; if (res) return *res; return null; } 
    123 } 
    124  
    125 import std.stdio; 
    126 class RenderBuffer : glExtension!("GL_EXT_framebuffer_object") { 
    127   uint rb; 
    128   int width, height; 
    129   this() { ExtFunc!(void function(int, uint *), "GenRenderbuffersEXT")(1, &rb); } 
    130   ~this() { free; } 
    131   void free() { ExtFunc!(void function(int, uint *), "DeleteRenderbuffersEXT")(1, &rb); } 
    132    
    133   static uint bound() { uint res; glGetIntegerv(GL_RENDERBUFFER_BINDING_EXT, cast(int*)&res); return res; } 
    134   static void bind(uint what) { ExtFunc!(void function(GLenum, uint), "BindRenderbufferEXT")(GL_RENDERBUFFER_EXT, what); } 
    135   void bind() { bind(rb); } 
    136112  void With(void delegate()[] dgs...) { 
    137113    auto old=bound(); scope(exit) bind(old); bind; 
     
    139115  } 
    140116   
     117  Texture[Attachment] attachedObjects; // for the sake of the GC 
     118  int width, height; 
     119   
     120  alias ExtFunc!(void function(GLenum, Attachment, GLenum, uint), "FramebufferRenderbufferEXT") FramebufferRenderbuffer; 
    141121  alias ExtFunc!(void function(GLenum target, Attachment, GLenum target, uint texture, int level), "FramebufferTexture2DEXT") 
    142122    glfbtex2d; 
    143    
    144   Texture[Attachment] attachedObjects; 
    145123  void attach(Attachment where, PixelFormat component, Texture tex, int level=0) { 
    146     if (where in attachedObjects) throw new Exception("Attachment point already in use!"); 
    147     if (!FrameBuffer.bound) throw new Exception("Please bind a frame buffer before attaching a texture. 
    148   \"The error INVALID_OPERATION is generated if FramebufferRenderbufferEXT 
    149   or FramebufferTexture{1D|2D|3D}EXT is called while the 
    150   value of FRAMEBUFFER_BINDING_EXT is zero.\" --FBO extension spec"); 
     124    auto old=bound(); scope(exit) bind(old); bind; 
    151125    if (width || height) { 
    152126      if (width!=tex.width || height!=tex.height) 
    153         throw new Exception("All textures attached to a render buffer must be the same size!"); 
     127        throw new Exception("All textures attached to a frame buffer must be the same size!"); 
    154128    } else { width=tex.width; height=tex.height; } 
    155     glChecked("RenderBufAttachReserveStorage", storage(component, width, height)); 
    156     glChecked("RenderBufBindTexture", { 
     129    glChecked("FrameBufBindTexture", { 
    157130      auto prev=tex.bound; scope(exit) tex.bind(prev); 
    158131      tex.bind; 
    159       glChecked("RenderBufAttachTexture", glfbtex2d(GL_FRAMEBUFFER_EXT, where, GL_TEXTURE_2D, tex.me, level)); 
    160     }); 
    161     attachedObjects[where]=tex; 
    162   } 
    163   Texture attached(Attachment where) { auto res=where in attachedObjects; return res?*res:null; } 
    164   void detach(Attachment where, int level=0) { 
    165     if (!attached(where)) throw new Exception("Trying to detach from unused attachment point!"); 
    166     glfbtex2d(GL_FRAMEBUFFER_EXT, where, GL_TEXTURE_2D, 0, level); 
    167     attachedObjects.remove(where); 
    168     if (!attachedObjects.length) { width=0; height=0; } 
    169   } 
    170    
    171   private void storage(PixelFormat component, int width, int height) { 
    172     auto old=bound(); scope(exit) bind(old); bind; 
    173     glChecked("Allocate renderbuffer storage", { 
    174       ExtFunc!(void function(GLenum, GLenum, int, int), "RenderbufferStorageEXT")(GL_RENDERBUFFER_EXT, component, width, height); 
     132      glChecked("FrameBufAttachTexture", glfbtex2d(GL_FRAMEBUFFER_EXT, where, GL_TEXTURE_2D, tex.me, level)); 
    175133    }); 
    176134  } 
     135  void detach(Attachment where, int level=0) { 
     136    glfbtex2d(GL_FRAMEBUFFER_EXT, where, GL_TEXTURE_2D, 0, level); 
     137    width=0; height=0; 
     138  } 
    177139} 
  • trunk/dglut/opengl.d

    r171 r172  
    651651    else glChecked("TexMinFilter", glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); 
    652652    glChecked("TexParams", glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR), 
    653       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP), 
    654       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP
     653      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT), 
     654      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT
    655655    ); 
    656656    if (mipmap) {