MPMA Framework 0.4
|
00001 00002 //Luke Lenhart, 2010 00003 //See /docs/License.txt for details on how this code may be used. 00004 00005 #pragma once 00006 00007 #include "../base/Setup.h" 00008 00009 #ifdef MPMA_COMPILE_GFX 00010 00011 #include "../base/Types.h" 00012 #include "../base/ReferenceCount.h" 00013 #include "../gfxsetup/GL.h" 00014 #include "../gfxsetup/Extensions.h" 00015 00016 #include <vector> 00017 00018 namespace GFX 00019 { 00021 inline GLsizei GetGLTypeSize(GLenum type) 00022 { 00023 GLsizei componentBytes=4; 00024 if (type==GL_SHORT) componentBytes=2; 00025 else if (type==GL_DOUBLE) componentBytes=8; 00026 return componentBytes; 00027 } 00028 00030 enum VertexComponentMeaning 00031 { 00032 VertexPosition, 00033 VertexNormal, 00034 VertexColor, 00035 VertexTextureCoordinate0, 00036 VertexTextureCoordinate1, 00037 VertexTextureCoordinate2, 00038 VertexTextureCoordinate3, 00039 VertexTextureCoordinate4, 00040 VertexTextureCoordinate5, 00041 VertexTextureCoordinate6, 00042 VertexTextureCoordinate7, 00043 VertexTextureCoordinate8, 00044 VertexTextureCoordinate9, 00045 VertexTextureCoordinate10, 00046 VertexTextureCoordinate11, 00047 VertexTextureCoordinate12, 00048 VertexTextureCoordinate13, 00049 VertexTextureCoordinate14, 00050 VertexTextureCoordinate15, 00051 VertexPadding 00052 }; 00053 00055 struct VertexComponentFormat 00056 { 00058 VertexComponentMeaning Meaning; 00060 GLenum Type; 00062 GLuint Count; 00063 00064 nuint AutoOffset; //computed automatically when added to a InterleavedVertexFormat 00065 00067 inline VertexComponentFormat(): Meaning(VertexPadding), Type(GL_FLOAT), Count(0) {} 00069 inline VertexComponentFormat(VertexComponentMeaning meaning, GLenum type, GLuint count): Meaning(meaning), Type(type), Count(count) {} 00070 }; 00071 00073 struct InterleavedVertexFormat 00074 { 00076 std::vector<VertexComponentFormat> Components; 00078 GLenum Usage; 00079 00080 GLsizei CurrentVertexSize; //updated automatically as components are added with the << operator 00081 00083 inline InterleavedVertexFormat(): Usage(GL_STATIC_DRAW), CurrentVertexSize(0) {} 00084 00086 inline InterleavedVertexFormat& operator<<(const VertexComponentFormat &newEntry) 00087 { 00088 Components.push_back(newEntry); 00089 Components.back().AutoOffset=CurrentVertexSize; 00090 CurrentVertexSize+=GetGLTypeSize(newEntry.Type)*newEntry.Count; 00091 return *this; 00092 } 00093 00095 inline GLsizei GetVertexSize() const 00096 { 00097 return CurrentVertexSize; 00098 } 00099 }; 00100 00101 struct VertexBufferData 00102 { 00103 GLuint object; 00104 00105 InterleavedVertexFormat format; 00106 00107 inline VertexBufferData(): object(0) 00108 {} 00109 }; 00110 00112 class VertexBuffer: public MPMA::ReferenceCountedData<VertexBufferData> 00113 { 00114 public: 00115 virtual ~VertexBuffer(); 00116 00118 bool Create(); 00120 void Free(); 00121 00123 inline operator GLuint() const { return Data().object; } 00124 00126 bool LoadInterleaved(const InterleavedVertexFormat &format, const void *bytes, nuint vertexCount); 00127 00129 void BindAndSetState(); 00131 void Unbind(); 00132 }; 00133 00135 class AutoBindVertexBuffer 00136 { 00137 public: 00139 inline AutoBindVertexBuffer(VertexBuffer &vbToBind): vb(vbToBind) { vb.BindAndSetState(); } 00141 inline ~AutoBindVertexBuffer() { vb.Unbind(); } 00142 00143 private: 00144 VertexBuffer &vb; 00145 00146 //prevent copying 00147 AutoBindVertexBuffer(const AutoBindVertexBuffer &o); 00148 bool operator=(const AutoBindVertexBuffer &o); 00149 }; 00150 00151 struct IndexBufferData 00152 { 00153 GLuint object; 00154 00155 inline IndexBufferData(): object(0) 00156 {} 00157 }; 00158 00160 class IndexBuffer: public MPMA::ReferenceCountedData<IndexBufferData> 00161 { 00162 public: 00163 virtual ~IndexBuffer(); 00164 00166 bool Create(); 00168 void Free(); 00169 00171 inline operator GLuint() const { return Data().object; } 00172 00174 bool Load(uint16 *bytes, nuint indexCount, GLenum usage=GL_STATIC_DRAW); 00175 00177 void BindAndSetState(); 00179 void Unbind(); 00180 }; 00181 00183 class AutoBindIndexBuffer 00184 { 00185 public: 00187 inline AutoBindIndexBuffer(IndexBuffer &ibToBind): ib(ibToBind) { ib.BindAndSetState(); } 00189 inline ~AutoBindIndexBuffer() { ib.Unbind(); } 00190 00191 private: 00192 IndexBuffer &ib; 00193 00194 //prevent copying 00195 AutoBindIndexBuffer(const AutoBindIndexBuffer &o); 00196 bool operator=(const AutoBindIndexBuffer &o); 00197 }; 00198 00199 class BufferMapper 00200 { 00201 //TODO: Memory mappying of buffers 00202 }; 00203 } 00204 00205 #endif //#ifdef MPMA_COMPILE_GFX