1 /* 2 Copyright (c) 2013 Andrey Penechko 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license the "Software" to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module anchovy.graphics.texrectarray; 30 31 import derelict.opengl3.gl3; 32 33 import anchovy.core.types; 34 35 struct TexVertex 36 { 37 this(int a, int b, int c, int d) 38 { 39 x = cast(short) a; 40 y = cast(short) b; 41 s = cast(ushort) c; 42 t = cast(ushort) d; 43 } 44 45 align(1): 46 short x, y; 47 ushort s, t; 48 } 49 50 51 class TexRectArray 52 { 53 TexVertex[] vertieces; 54 this() 55 { 56 glGenVertexArrays(1, &vao); 57 glGenBuffers(1,&vbo); 58 } 59 60 void close() 61 { 62 glDeleteVertexArrays(1, &vao); 63 glDeleteBuffers(1,&vbo); 64 } 65 66 TexRectArray appendQuad(Rect geometryQuad, Rect textureQuad) 67 { 68 alias geometryQuad gq; 69 alias textureQuad tq; 70 TexVertex[4] vert; 71 vert[0] = TexVertex(gq.x, gq.y + gq.height, tq.x, tq.y + tq.height);//lower left 72 vert[1] = TexVertex(gq.x, gq.y, tq.x, tq.y);//upper left 73 vert[2] = TexVertex(gq.x + gq.width, gq.y, tq.x + tq.width, tq.y);//upper right 74 vert[3] = TexVertex(gq.x + gq.width, gq.y+ gq.height, tq.x + tq.width, tq.y + tq.height);//lower right 75 76 vertieces ~= vert[0]; 77 vertieces ~= vert[1]; 78 vertieces ~= vert[2]; 79 vertieces ~= vert[0]; 80 vertieces ~= vert[2]; 81 vertieces ~= vert[3]; 82 return this; 83 } 84 85 void load() 86 { 87 glBindVertexArray(vao); 88 glBindBuffer(GL_ARRAY_BUFFER, vbo ); 89 glBufferData(GL_ARRAY_BUFFER, vertieces.length * TexVertex.sizeof, vertieces.ptr, GL_STATIC_DRAW); 90 glEnableVertexAttribArray(0); 91 glEnableVertexAttribArray(1); 92 glVertexAttribPointer(0, 2, GL_SHORT, GL_FALSE, 4*short.sizeof, null); 93 glVertexAttribPointer(1, 2, GL_UNSIGNED_SHORT, GL_FALSE, 4*short.sizeof, cast(void*)(2*short.sizeof)); 94 95 glBindBuffer(GL_ARRAY_BUFFER,0); 96 glBindVertexArray(0); 97 } 98 99 void bind() 100 { 101 glBindVertexArray(vao); 102 } 103 104 void draw() 105 { 106 glDrawArrays(GL_TRIANGLES, 0, cast(uint)vertieces.length); 107 } 108 109 void unbind() 110 { 111 glBindVertexArray(0); 112 } 113 114 uint vao, vbo; 115 }