nux-1.16.0
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #include "GLResource.h" 00024 #include "GLDeviceObjects.h" 00025 #include "IOpenGLVertexDeclaration.h" 00026 00027 namespace nux 00028 { 00029 00030 NUX_IMPLEMENT_OBJECT_TYPE (IOpenGLVertexDeclaration); 00031 00032 IOpenGLVertexDeclaration::IOpenGLVertexDeclaration(const VERTEXELEMENT* pVertexElements) 00033 : IOpenGLResource(RTVERTEXDECLARATION) 00034 { 00035 for(int i = 0; i < 8; i++) 00036 { 00037 _stride[i] = 0; 00038 vertex_buffer_array.push_back(ObjectPtr<IOpenGLVertexBuffer>(NULL)); 00039 } 00040 00041 int index = 0; 00042 _declarations_array.clear(); 00043 00044 while (pVertexElements[index].Stream != 0xFF) 00045 { 00046 VERTEXELEMENT VtxElement = pVertexElements[index]; 00047 _declarations_array.push_back(VtxElement); 00048 _stride[VtxElement.Stream] += GetVertexElementSize(VtxElement); 00049 ++index; 00050 } 00051 00052 // Add in the invalid vertex element stream at the end. 00053 //VERTEXELEMENT *pVtxElement = new VERTEXELEMENT; 00054 //pVtxElement->Stream = 0xFF; // invalid stream 00055 _declarations_array.push_back (DECL_END); 00056 }; 00057 00058 IOpenGLVertexDeclaration::~IOpenGLVertexDeclaration() 00059 { 00060 // IOpenGLVertexDeclaration is an abstraction. Is does not have an opengl id. 00061 _OpenGLID = 0; 00062 } 00063 00064 // int IOpenGLVertexDeclaration::GetDeclaration ( 00065 // VERTEXELEMENT& pDecl, 00066 // unsigned int *pNumElements) 00067 // { 00068 // *pNumElements = (unsigned int) _declarations_array.size(); 00069 // pDecl = _declarations_array[0]; 00070 // 00071 // return 1; 00072 // } 00073 00074 00075 VERTEXELEMENT IOpenGLVertexDeclaration::GetUsage (ATTRIB_USAGE_DECL usage) 00076 { 00077 VERTEXELEMENT vtxelt; 00078 return vtxelt; 00079 // vtxelt.Stream = 0xFF; // invalid stream; 00080 // 00081 // for (unsigned int i = 0; _declarations_array.size(); i++) 00082 // { 00083 // if (_declarations_array[i].Usage == usage) 00084 // { 00085 // vtxelt = _declarations_array[i]; 00086 // return vtxelt; 00087 // } 00088 // } 00089 // 00090 // return vtxelt; 00091 } 00092 00093 // This is a simple check to comply with the documentation of DrawPrimitiveUP in DirectX 00094 bool IOpenGLVertexDeclaration::IsUsingMoreThanStreamZero() 00095 { 00096 for (unsigned int i = 0; i < _declarations_array.size(); i++) 00097 { 00098 if (_declarations_array[i].Stream != 0) 00099 { 00100 return true; 00101 } 00102 } 00103 00104 return false; 00105 } 00106 00107 int IOpenGLVertexDeclaration::GetStride(int vertex_input_number) 00108 { 00109 NUX_RETURN_VALUE_IF_FALSE(vertex_input_number >= 0, 0); 00110 NUX_RETURN_VALUE_IF_FALSE(vertex_input_number < 8, 0); 00111 00112 return _stride[vertex_input_number]; 00113 } 00114 00115 00116 void IOpenGLVertexDeclaration::SetVertexBuffer(int input_index, ObjectPtr<IOpenGLVertexBuffer> vertex_buffer) 00117 { 00118 NUX_RETURN_IF_FALSE(input_index >= 0); 00119 NUX_RETURN_IF_FALSE(input_index < 8); 00120 00121 vertex_buffer_array[input_index] = vertex_buffer; 00122 } 00123 00124 ObjectPtr<IOpenGLVertexBuffer> IOpenGLVertexDeclaration::GetVertexBuffer(int input_index) 00125 { 00126 NUX_RETURN_VALUE_IF_FALSE(input_index >= 0, ObjectPtr<IOpenGLVertexBuffer>(NULL)); 00127 NUX_RETURN_VALUE_IF_FALSE(input_index < 8, ObjectPtr<IOpenGLVertexBuffer>(NULL)); 00128 00129 return vertex_buffer_array[input_index]; 00130 } 00131 00132 void IOpenGLVertexDeclaration::SetVertexShaderAttributeLocation(int input_index, int shader_attribute_location) 00133 { 00134 NUX_RETURN_IF_FALSE(input_index >= 0); 00135 NUX_RETURN_IF_FALSE(input_index < 8); 00136 00137 shader_attribute_location_array[input_index] = shader_attribute_location; 00138 } 00139 00140 int IOpenGLVertexDeclaration::GetVertexShaderAttributeLocation(int input_index) 00141 { 00142 NUX_RETURN_VALUE_IF_FALSE(input_index >= 0, -1); 00143 NUX_RETURN_VALUE_IF_FALSE(input_index < 8, -1); 00144 00145 return shader_attribute_location_array[input_index]; 00146 } 00147 }