Changeset 172 for trunk/dglut
- Timestamp:
- 12/26/07 22:57:14 (1 year ago)
- Files:
-
- trunk/dglut/dglut_test.d (modified) (9 diffs)
- trunk/dglut/ext.d (modified) (3 diffs)
- trunk/dglut/opengl.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/dglut/dglut_test.d
r171 r172 31 31 32 32 void setupLights() { 33 glEnable(GL_DEPTH_TEST);34 33 glEnable(GL_LIGHTING); 35 34 with (Light[0]) { 36 35 enable; 37 position( [100, -100, 10], true);36 position(lightpos.parts, true); 38 37 ambient=[0, 0, 0, 1]; 39 38 diffuse=[1, 1, 1, 1]; 40 39 specular=[1, 1, 1, 1]; 41 40 } 42 with (Light[1]) {41 /*with (Light[1]) { 43 42 enable; 44 43 position([10, 50, 70], true); … … 46 45 diffuse=[2, 1, 1, 1]; 47 46 specular=[1, 1, 1, 1]; 48 } 47 }*/ 49 48 } 50 49 51 50 import std.string: toStringz; 52 51 import dglut.ext; 53 FrameBuffer test; 52 FrameBuffer test; Texture target; 53 float[32] cameraMatrix, lightMatrix; // projection, view 4x4 54 55 vec3f campos, lightpos; 54 56 void main(string[] args) { 55 57 int c_argc=args.length; … … 62 64 auto win = glutCreateWindow("Triangle\0".ptr); 63 65 66 campos=vec3f(0, 5, -10); 67 lightpos=vec3f(100, -100, 10); 68 64 69 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); 74 73 75 74 MatrixMode.Projection; … … 77 76 gluPerspective(45, 1.0, 0.1, 100); 78 77 MatrixMode.Modelview; 78 Texture.enable; 79 glEnable(GL_DEPTH_TEST); 79 80 80 81 glutDisplayFunc=&display; … … 109 110 fps=0; 110 111 } 111 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);112 glClearColor(0, 0, 0, 0);113 112 glLoadIdentity; 114 113 static float f=0; … … 118 117 Rotate(f*0.2, 0, 1, 0); 119 118 Rotate(f*0.1, 1, 1, 0); 120 glChecked("RenderToFramebuffer", test.With(renderScene(f))); 119 120 glChecked("RenderToFramebuffer", MatrixScope(test.Using(renderScene(f)))); 121 121 glChecked("RenderToScreen", renderScene(f)); 122 writefln("Done");122 123 123 glFlush; 124 124 glutSwapBuffers(); … … 126 126 127 127 void renderScene(float t) { 128 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 128 129 setupLights(); 129 130 auto points=[ … … 165 166 } 166 167 167 Texture.enable;168 168 static Texture tex; 169 169 glChecked("DisplayGenerateTexture", { … … 184 184 })); 185 185 //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 });199 186 } trunk/dglut/ext.d
r171 r172 68 68 ); 69 69 70 const GLenum GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT=0x8CD0, 71 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT=0x8CD1; 72 70 73 class 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 72 84 auto res=ExtFunc!(GLenum function(GLenum target), "CheckFramebufferStatusEXT")(GL_FRAMEBUFFER_EXT); 73 85 if (res==GL_FRAMEBUFFER_COMPLETE_EXT) return; … … 89 101 static void bind(uint whar) { ExtFunc!(void function(GLenum, uint), "BindFramebufferEXT")(GL_FRAMEBUFFER_EXT, whar); } 90 102 static uint bound() { uint res; glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, cast(int*)&res); return res; } 103 bool isBound() { return bound==fb; } 91 104 void bind() { bind(fb); } 92 void With(void delegate()[] dgs...) { 93 checkState; 105 void Using(void delegate()[] dgs...) { 94 106 glPushAttrib(GL_VIEWPORT_BIT); scope(exit) glPopAttrib; 95 107 glViewport(0, 0, width, height); 96 108 auto old=bound(); scope(exit) bind(old); bind; 97 writefln("Viewport limited to ", width, ":", height, "; bound to ", bound, " from ", old);109 checkState; 98 110 foreach (dg; dgs) dg(); 99 111 } 100 101 RenderBuffer[Attachment] attachedObjects; // for the sake of the GC102 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); }136 112 void With(void delegate()[] dgs...) { 137 113 auto old=bound(); scope(exit) bind(old); bind; … … 139 115 } 140 116 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; 141 121 alias ExtFunc!(void function(GLenum target, Attachment, GLenum target, uint texture, int level), "FramebufferTexture2DEXT") 142 122 glfbtex2d; 143 144 Texture[Attachment] attachedObjects;145 123 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; 151 125 if (width || height) { 152 126 if (width!=tex.width || height!=tex.height) 153 throw new Exception("All textures attached to a renderbuffer must be the same size!");127 throw new Exception("All textures attached to a frame buffer must be the same size!"); 154 128 } else { width=tex.width; height=tex.height; } 155 glChecked("RenderBufAttachReserveStorage", storage(component, width, height)); 156 glChecked("RenderBufBindTexture", { 129 glChecked("FrameBufBindTexture", { 157 130 auto prev=tex.bound; scope(exit) tex.bind(prev); 158 131 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)); 175 133 }); 176 134 } 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 } 177 139 } trunk/dglut/opengl.d
r171 r172 651 651 else glChecked("TexMinFilter", glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); 652 652 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) 655 655 ); 656 656 if (mipmap) {
