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.mesh; 30 31 public import dlib.math.vector; 32 public import dlib.math.quaternion; 33 import derelict.opengl3.gl3; 34 35 /****** 36 * Vertex attribute storage class. For use in meshes 37 * 38 * Attribute will be used like this, when there is more then 1 attribute presented 39 * ----- 40 * glVertexAttribPointer(location, elementNum, elementType, normalized, elementNum*elementSize, cast(void*)offset); 41 * --- 42 * or like this, when there is only one attribute in the mesh 43 * ----- 44 * glVertexAttribPointer(location, elementNum, elementType, normalized, 0, null); 45 * --- 46 *****/ 47 48 class Attribute 49 { 50 uint location; 51 uint elementNum;///number of 52 uint elementType;///GL_FLOAT etc 53 uint elementSize;///in bytes 54 uint offset;///offset from the begining of buffer 55 bool normalized; 56 } 57 58 class Mesh 59 { 60 61 Vector3f position; 62 Quaternionf orientation; 63 ubyte[] meshData; 64 GLuint vao; 65 GLuint vbo; 66 67 68 this(){ 69 glGenBuffers( 1, &vbo ); 70 glGenVertexArrays(1, &vao); 71 } 72 ~this() 73 { 74 glDeleteBuffers(1, &vbo); 75 glDeleteVertexArrays(1, &vao); 76 } 77 78 void load(){ 79 glBindVertexArray(vao); 80 glBindBuffer(GL_ARRAY_BUFFER, vbo ); 81 glBufferData(GL_ARRAY_BUFFER, meshData.length, meshData.ptr, GL_STATIC_DRAW); 82 glEnableVertexAttribArray(0); 83 glEnableVertexAttribArray(1); 84 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*float.sizeof, null); 85 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*float.sizeof, cast(void*)(3*float.sizeof)); 86 glBindBuffer(GL_ARRAY_BUFFER,0); 87 glBindVertexArray(0); 88 } 89 90 void bind(){ 91 glBindVertexArray(vao); 92 } 93 94 void render(){ 95 glDrawArrays(GL_TRIANGLES, 0, meshData.length/24);//meshData.length/12); 96 } 97 98 } 99