void SprsArray<Dtype>::Create(const Array<Dtype>& array) { // verify the array if (array.GetDimCnt() != 2) { std::cout << "[ERROR] the input array must be 2-dimensional" << std::endl; exit(-1); } // ENDIF: array // obtain basic variables rowCnt_ = array.GetDimLen(0); colCnt_ = array.GetDimLen(1); nnzCnt_ = 0; std::size_t eleCnt = array.GetEleCnt(); const Dtype* pData = array.GetDataPtr(); for (std::size_t eleIdx = 0; eleIdx < eleCnt; ++eleIdx) { if (ABS(pData[eleIdx]) > kEpsilon) { nnzCnt_++; } // ENDIF: ABS } // ENDFOR: eleIdx // release space occupied by previous pointers (if any) Delete(); // allocate space for pointers val_ = new Dtype[nnzCnt_]; idx_ = new MKL_INT[nnzCnt_]; ptrb_ = new MKL_INT[rowCnt_]; ptre_ = new MKL_INT[rowCnt_]; // convert the dense array to CSR format, zero-based indexing) std::size_t nnzIdx = 0; for (std::size_t rowIdx = 0; rowIdx < rowCnt_; ++rowIdx) { // initialize variables for the current row const Dtype* pData = array.GetDataPtr() + rowIdx * colCnt_; bool isFirst = true; // scan through the current row for (std::size_t colIdx = 0; colIdx < colCnt_; ++colIdx) { if (ABS(pData[colIdx]) > kEpsilon) { val_[nnzIdx] = pData[colIdx]; idx_[nnzIdx] = colIdx; if (isFirst) { isFirst = false; ptrb_[rowIdx] = nnzIdx; } // ENDIF: isFirst ptre_[rowIdx] = ++nnzIdx; } // ENDIF: ABS } // ENDFOR: colIdx } // ENDFOR: rowIdx }
void GlGeometry::Create( const VertexAttribs & attribs, const Array< TriangleIndex > & indices ) { vertexCount = attribs.position.GetSizeI(); indexCount = indices.GetSizeI(); glGenBuffers( 1, &vertexBuffer ); glGenBuffers( 1, &indexBuffer ); glGenVertexArraysOES_( 1, &vertexArrayObject ); glBindVertexArrayOES_( vertexArrayObject ); glBindBuffer( GL_ARRAY_BUFFER, vertexBuffer ); Array< uint8_t > packed; PackVertexAttribute( packed, attribs.position, VERTEX_ATTRIBUTE_LOCATION_POSITION, GL_FLOAT, 3 ); PackVertexAttribute( packed, attribs.normal, VERTEX_ATTRIBUTE_LOCATION_NORMAL, GL_FLOAT, 3 ); PackVertexAttribute( packed, attribs.tangent, VERTEX_ATTRIBUTE_LOCATION_TANGENT, GL_FLOAT, 3 ); PackVertexAttribute( packed, attribs.binormal, VERTEX_ATTRIBUTE_LOCATION_BINORMAL, GL_FLOAT, 3 ); PackVertexAttribute( packed, attribs.color, VERTEX_ATTRIBUTE_LOCATION_COLOR, GL_FLOAT, 4 ); PackVertexAttribute( packed, attribs.uv0, VERTEX_ATTRIBUTE_LOCATION_UV0, GL_FLOAT, 2 ); PackVertexAttribute( packed, attribs.uv1, VERTEX_ATTRIBUTE_LOCATION_UV1, GL_FLOAT, 2 ); PackVertexAttribute( packed, attribs.jointIndices, VERTEX_ATTRIBUTE_LOCATION_JOINT_INDICES, GL_INT, 4 ); PackVertexAttribute( packed, attribs.jointWeights, VERTEX_ATTRIBUTE_LOCATION_JOINT_WEIGHTS, GL_FLOAT, 4 ); glBufferData( GL_ARRAY_BUFFER, packed.GetSize() * sizeof( packed[0] ), packed.GetDataPtr(), GL_STATIC_DRAW ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBuffer ); glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices.GetSizeI() * sizeof( indices[0] ), indices.GetDataPtr(), GL_STATIC_DRAW ); glBindVertexArrayOES_( 0 ); glDisableVertexAttribArray( VERTEX_ATTRIBUTE_LOCATION_POSITION ); glDisableVertexAttribArray( VERTEX_ATTRIBUTE_LOCATION_NORMAL ); glDisableVertexAttribArray( VERTEX_ATTRIBUTE_LOCATION_TANGENT ); glDisableVertexAttribArray( VERTEX_ATTRIBUTE_LOCATION_BINORMAL ); glDisableVertexAttribArray( VERTEX_ATTRIBUTE_LOCATION_COLOR ); glDisableVertexAttribArray( VERTEX_ATTRIBUTE_LOCATION_UV0 ); glDisableVertexAttribArray( VERTEX_ATTRIBUTE_LOCATION_UV1 ); glDisableVertexAttribArray( VERTEX_ATTRIBUTE_LOCATION_JOINT_INDICES ); glDisableVertexAttribArray( VERTEX_ATTRIBUTE_LOCATION_JOINT_WEIGHTS ); }
void PackVertexAttribute( Array< uint8_t > & packed, const Array< _attrib_type_ > & attrib, const int glLocation, const int glType, const int glComponents ) { if ( attrib.GetSize() > 0 ) { const size_t offset = packed.GetSize(); const size_t size = attrib.GetSize() * sizeof( attrib[0] ); packed.Resize( offset + size ); memcpy( &packed[offset], attrib.GetDataPtr(), size ); glEnableVertexAttribArray( glLocation ); glVertexAttribPointer( glLocation, glComponents, glType, false, sizeof( attrib[0] ), (void *)( offset ) ); } else { glDisableVertexAttribArray( glLocation ); } }
void GlGeometry::Update( const VertexAttribs & attribs ) { vertexCount = attribs.position.GetSizeI(); glBindVertexArrayOES_( vertexArrayObject ); glBindBuffer( GL_ARRAY_BUFFER, vertexBuffer ); Array< uint8_t > packed; PackVertexAttribute( packed, attribs.position, VERTEX_ATTRIBUTE_LOCATION_POSITION, GL_FLOAT, 3 ); PackVertexAttribute( packed, attribs.normal, VERTEX_ATTRIBUTE_LOCATION_NORMAL, GL_FLOAT, 3 ); PackVertexAttribute( packed, attribs.tangent, VERTEX_ATTRIBUTE_LOCATION_TANGENT, GL_FLOAT, 3 ); PackVertexAttribute( packed, attribs.binormal, VERTEX_ATTRIBUTE_LOCATION_BINORMAL, GL_FLOAT, 3 ); PackVertexAttribute( packed, attribs.color, VERTEX_ATTRIBUTE_LOCATION_COLOR, GL_FLOAT, 4 ); PackVertexAttribute( packed, attribs.uv0, VERTEX_ATTRIBUTE_LOCATION_UV0, GL_FLOAT, 2 ); PackVertexAttribute( packed, attribs.uv1, VERTEX_ATTRIBUTE_LOCATION_UV1, GL_FLOAT, 2 ); PackVertexAttribute( packed, attribs.jointIndices, VERTEX_ATTRIBUTE_LOCATION_JOINT_INDICES, GL_INT, 4 ); PackVertexAttribute( packed, attribs.jointWeights, VERTEX_ATTRIBUTE_LOCATION_JOINT_WEIGHTS, GL_FLOAT, 4 ); glBufferData( GL_ARRAY_BUFFER, packed.GetSize() * sizeof( packed[0] ), packed.GetDataPtr(), GL_STATIC_DRAW ); }