//----------------------------------------------------------------------------- // CreateDefaultObjects // Creates the default objects //----------------------------------------------------------------------------- void CRenderer::CreateDefaultObjects( void ) { // Texture m_nDefaultTexture = LoadTexture2D( "Assets/Textures/DefaultTexture.png" ); // Default mesh m_nDefaultMesh = CreateMesh(); // debug sphere m_nSphereMesh = LoadMesh( "Assets/meshes/sphere.mesh" ); // debug box m_nDebugBox = CreateDynamicBox(); ////////////////////////////////////////// // Load Shaders LoadShaders(); // a vertex shader for drawing lines VPosColor pLineVertices[] = { { RVector3( 0.0f, 0.0f, 0.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, 1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, }; m_pLineBuffer = m_pDevice->CreateVertexBuffer( sizeof( pLineVertices ), pLineVertices ); // Set the defaults SetVertexShader( eVS3DPosNorTexStd ); SetPixelShader( ePS3DStd ); SetSamplerState( eSamplerLinear ); m_pDevice->SetPrimitiveType( GFX_PRIMITIVE_TRIANGLELIST ); static float Vtx[] = { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, }; static uint16 Idx[] = { 0, 1, 2, 0, 2, 3, }; m_pFSRectVB = m_pDevice->CreateVertexBuffer( sizeof( Vtx ), Vtx ); m_pFSRectIB = m_pDevice->CreateIndexBuffer ( sizeof( Idx ), Idx ); }
std::vector < RVector3 > loadRVector3(const std::string & fileName){ std::vector < RVector3 > l; std::fstream file; openInFile(fileName, & file, true); std::vector < std::string > row; while (! file.eof()){ row = getNonEmptyRow(file); switch (row.size()){ case 1 : l.push_back(RVector3(toDouble(row[0]), 0.0, 0.0)); break; case 2 : l.push_back(RVector3(toDouble(row[0]), toDouble(row[1]), 0.0)); break; case 3 : l.push_back(RVector3(toDouble(row[0]), toDouble(row[1]), toDouble(row[2]))); break; } } file.close(); return l; }
template <> RVector3 RVector3::cross(const RVector3 & p) const{ // r[0] = (a2 * b3 - a3 * b2); // r[1] = (a3 * b1 - a1 * b3); // r[2] = (a1 * b2 - a2 * b1); return (RVector3((*this)[1] * p[2] - (*this)[2] * p[1], (*this)[2] * p[0] - (*this)[0] * p[2], (*this)[0] * p[1] - (*this)[1] * p[0])); }
RVector3 sphTangential2Initerial(const RVector3 &V, double lat, double lon){ double th = lat * PI/180.0; double ph = lon * PI/180.0; double ct = std::cos(th); double st = std::sin(th); double cp = std::cos(ph); double sp = std::sin(ph); return RVector3( (V[0]* ct + V[1] * -st) * cp + V[2] * -sp, (V[0]* ct + V[1] * -st) * sp + V[2] * cp, V[0]* st + V[1] * ct); }
RVector interpolate(const Mesh & mesh, const RVector & data, const RVector & x, const RVector & y, const RVector & z, bool verbose){ if (x.size() != y.size() || x.size() != z.size()) { throwLengthError(EXIT_VECTOR_SIZE_INVALID, " x.size invalid y.size invalid z.size() " + toStr(x.size()) + " != " + toStr(y.size()) + " != " + toStr(z.size())); } std::vector < RVector3 > pos(x.size()); for (uint i = 0; i < x.size(); i ++) pos[i] = RVector3(x[i], y[i], z[i]); RVector iData; interpolate(mesh, data, pos, iData, verbose); return iData; }
Mesh createMesh2D(const Mesh & mesh, const RVector & y, int front, int back, bool adjustBack){ Mesh mesh2(2); bool first = true; for (Index iy = 0; iy < y.size(); iy ++){ for (Index in = 0; in < mesh.nodeCount(); in ++){ int marker = 0; if (first) marker = mesh.node(in).marker(); mesh2.createNode(mesh.node(in).pos() + RVector3(0.0, y[iy]), marker); } first = false; } std::vector < Node * > nodes; for (Index iy = 1; iy < y.size(); iy ++){ first = true; for (Index in = 0; in < mesh.boundaryCount(); in ++){ uint nn = mesh.boundary(in).nodeCount(); nodes.resize(nn * 2) ; nodes[0] = & mesh2.node((iy - 1) * mesh.nodeCount() + mesh.boundary(in).node(0).id()); nodes[1] = & mesh2.node((iy - 1) * mesh.nodeCount() + mesh.boundary(in).node(1).id()); nodes[2] = & mesh2.node((iy * mesh.nodeCount() + mesh.boundary(in).node(1).id())); nodes[3] = & mesh2.node((iy * mesh.nodeCount() + mesh.boundary(in).node(0).id())); mesh2.createCell(nodes, mesh.boundary(in).marker()); if (iy == 1){ // create top layer boundaries // in revers direction so the outer normal shows downward into the mesh } if (iy == y.size()-1){ // create bottom layer boundaries } } first = false; } return mesh; }
RVector3 MeshEntity::vec(const RVector3 & xyz, const std::vector < RVector3 > & v) const{ return RVector3(pot(xyz,x(v)), pot(xyz,y(v)), pot(xyz, z(v))); }
bool addTriangleBoundary(Mesh & mesh, double xBoundary, double yBoundary, int cellMarker, bool save){ int boundMarker = -5; mesh.createNeighbourInfos(true); Boundary *b = NULL; for (std::vector < Boundary * >::const_iterator it = mesh.boundaries().begin(); it != mesh.boundaries().end(); it ++){ b = (*it); if (! b->leftCell() || ! b->rightCell()) { if (b->marker() != MARKER_BOUND_HOMOGEN_NEUMANN){ b->setMarker(boundMarker); } } } std::vector < Boundary * > markedBoundaries(mesh.findBoundaryByMarker(boundMarker)); Boundary *start(markedBoundaries.front()); std::list < Node * > boundNodes; boundNodes.push_back(& start->node(0)); Boundary * thisBoundary = start; while (thisBoundary != NULL){ Node * thisNode = boundNodes.back(); Boundary * nextBoundary = NULL; for (std::set < Boundary * >::iterator it = thisNode->boundSet().begin(); it != thisNode->boundSet().end(); it++){ if ((*it)->marker() == boundMarker && (*it) != thisBoundary){ nextBoundary = (*it); break; } } if (nextBoundary){ Node * nextNode = NULL; if (&nextBoundary->node(0) != thisNode){ nextNode = &nextBoundary->node(0); } else { nextNode = &nextBoundary->node(1); } //** Check closed polygones here if (find(boundNodes.begin(), boundNodes.end(), nextNode) == boundNodes.end()) { boundNodes.push_back(nextNode); } else { break; } } thisBoundary = nextBoundary; } boundNodes.push_front(& start->node(1)); thisBoundary = start; while (thisBoundary != NULL){ Node * thisNode = boundNodes.front(); Boundary * nextBoundary = NULL; for (std::set < Boundary * >::iterator it = thisNode->boundSet().begin(); it != thisNode->boundSet().end(); it++){ if ((*it)->marker() == boundMarker && (*it) != thisBoundary){ nextBoundary = (*it); break; } } if (nextBoundary){ Node * nextNode = NULL; if (& nextBoundary->node(0) != thisNode){ nextNode = & nextBoundary->node(0); } else { nextNode = & nextBoundary->node(1); } //** Check closed polygones here if (find(boundNodes.begin(), boundNodes.end(), nextNode) == boundNodes.end()) { boundNodes.push_front(nextNode); } else { break; } } thisBoundary = nextBoundary; } Mesh poly; std::vector < Node * > innerBound; for (std::list < Node * >::iterator it = boundNodes.begin(); it != boundNodes.end(); it ++){ innerBound.push_back(poly.createNode((*it)->pos())); } //** looking for polygon ends Node * upperLeftSurface = innerBound.front(); Node * upperRightSurface = innerBound.back(); if (upperLeftSurface->pos()[0] > upperRightSurface->pos()[0]){ upperLeftSurface = innerBound.back(); upperRightSurface = innerBound.front(); } std::vector < Node * > outerBound; outerBound.push_back(upperLeftSurface); Node *n1 = poly.createNode(upperLeftSurface->pos() - RVector3(xBoundary, 0.0)); Node *n2 = poly.createNode(upperLeftSurface->pos() - RVector3(xBoundary, - mesh.ymin() + yBoundary)); Node *n3 = poly.createNode(upperRightSurface->pos() - RVector3(-xBoundary, - mesh.ymin() + yBoundary)); Node *n4 = poly.createNode(upperRightSurface->pos() - RVector3(-xBoundary, 0.0)); outerBound.push_back(upperLeftSurface); RVector dx(increasingRange(4.0, xBoundary, 20)); for (uint i = 1; i < dx.size()-1; i ++) { outerBound.push_back(poly.createNode(upperLeftSurface->pos() + RVector3(-dx[i], 0))); } outerBound.push_back(n1); for (uint i = 0; i < 9; i ++) { outerBound.push_back(poly.createNode(n1->pos() + (n2->pos() - n1->pos()) / 10 * (i + 1))); } outerBound.push_back(n2); for (uint i = 0; i < 9; i ++) { outerBound.push_back(poly.createNode(n2->pos() + (n3->pos() - n2->pos()) / 10 * (i + 1))); } outerBound.push_back(n3); for (uint i = 0; i < 9; i ++) { outerBound.push_back(poly.createNode(n3->pos() + (n4->pos() - n3->pos()) / 10 * (i + 1))); } outerBound.push_back(n4); for (uint i = dx.size()-2; i > 0; i --) { outerBound.push_back(poly.createNode(upperRightSurface->pos() + RVector3(dx[i], 0))); } outerBound.push_back(upperRightSurface); for (uint i = 0; i < outerBound.size() -1; i ++){ poly.createEdge(*outerBound[i], *outerBound[i + 1], -1); } for (uint i = 0; i < innerBound.size() -1; i ++){ poly.createEdge(*innerBound[i], *innerBound[i + 1], boundMarker); } Mesh boundaryMesh; TriangleWrapper(poly, boundaryMesh, "-YpzeAfaq" + str(33)); std::vector < Boundary * > markedBoundaries2(boundaryMesh.findBoundaryByMarker(boundMarker)); if (markedBoundaries.size() == markedBoundaries2.size()){ for (std::vector < Cell * >::const_iterator it = boundaryMesh.cells().begin(); it != boundaryMesh.cells().end(); it ++){ mesh.copyCell(*(*it))->setMarker(cellMarker); } } else { return false; mesh.save("inMesh"); boundaryMesh.save("boundaryMesh"); throwError(1, WHERE_AM_I + " Sorry!! Boundary mesh is not consistent to input mesh boundary. " + toStr(markedBoundaries.size()) + " != " + toStr(markedBoundaries2.size())); } if (save){ mesh.save("shortFOPinMesh"); boundaryMesh.save("shortFOPboundaryMesh"); } //** Fix boundary marker mesh.createNeighbourInfos(true); b = NULL; for (std::vector < Boundary * >::const_iterator it = mesh.boundaries().begin(); it != mesh.boundaries().end(); it ++){ b = (*it); if (! b->leftCell() || ! b->rightCell()) { if (b->center().y() == mesh.ymax()){ b->setMarker(MARKER_BOUND_HOMOGEN_NEUMANN); } else { b->setMarker(MARKER_BOUND_MIXED); } } } return true; }
Mesh createMesh3D(const Mesh & mesh, const RVector & z, int topLayer, int bottomLayer){ Mesh mesh3(3); if (z.size() < 2){ std::cout << "Warning!: " << WHERE_AM_I << "extrusion vector size need z be greater than 1" << std::endl; } bool first = true; for (Index iz = 0; iz < z.size(); iz ++){ for (Index ic = 0; ic < mesh.nodeCount(); ic ++){ int marker = 0; if (first) marker = mesh.node(ic).marker(); mesh3.createNode(mesh.node(ic).pos() + RVector3(0.0, 0.0, z[iz]), marker); } first = false; } std::vector < Node * > nodes; for (Index iz = 1; iz < z.size(); iz ++){ first = true; for (Index ic = 0; ic < mesh.cellCount(); ic ++){ uint nC = mesh.cell(ic).nodeCount(); nodes.resize(nC * 2) ; for (Index k = 0; k < nC; k ++){ nodes[k] = & mesh3.node((iz-1) * mesh.nodeCount() + mesh.cell(ic).node(k).id()); } for (Index k = 0; k < nC; k ++){ nodes[nC + k] = & mesh3.node(iz * mesh.nodeCount() + mesh.cell(ic).node(k).id()); } mesh3.createCell(nodes, mesh.cell(ic).marker()); if (iz == 1){ // create top layer boundaries // in revers direction so the outer normal shows downward into the mesh std::vector < Node * > nBound(nC); for (Index k = 0; k < nC; k ++) nBound[nC - k - 1] = nodes[k]; mesh3.createBoundary(nBound, topLayer); } if (iz == z.size()-1){ // create bottom layer boundaries std::vector < Node * > nBound(nC); for (Index k = 0; k < nC; k ++) nBound[k] = nodes[nC + k]; mesh3.createBoundary(nBound, bottomLayer); } } first = false; } nodes.resize(4); for (Index iz = 1; iz < z.size(); iz ++){ for (Index ib = 0; ib < mesh.boundaryCount(); ib ++){ if (mesh.boundary(ib).marker() != 0){ nodes[0] = & mesh3.node((iz-1) * mesh.nodeCount() + mesh.boundary(ib).node(0).id()); nodes[1] = & mesh3.node((iz-1) * mesh.nodeCount() + mesh.boundary(ib).node(1).id()); nodes[3] = & mesh3.node(iz * mesh.nodeCount() + mesh.boundary(ib).node(0).id()); nodes[2] = & mesh3.node(iz * mesh.nodeCount() + mesh.boundary(ib).node(1).id()); mesh3.createBoundary(nodes, mesh.boundary(ib).marker()); } } } return mesh3; }
//----------------------------------------------------------------------------- // GetDefaultMeshData // Returns the default mesh data //----------------------------------------------------------------------------- VPosNormalTex* CRenderer::GetDefaultMeshData( void ) { static VPosNormalTex vertices[] = { { RVector3( -1.0f, 1.0f, -1.0f ), RVector3( 0.0f, 1.0f, 0.0f ), RVector2( 0.0f, 0.0f ) }, { RVector3( 1.0f, 1.0f, -1.0f ), RVector3( 0.0f, 1.0f, 0.0f ), RVector2( 1.0f, 0.0f ) }, { RVector3( 1.0f, 1.0f, 1.0f ), RVector3( 0.0f, 1.0f, 0.0f ), RVector2( 1.0f, 1.0f ) }, { RVector3( -1.0f, 1.0f, 1.0f ), RVector3( 0.0f, 1.0f, 0.0f ), RVector2( 0.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, -1.0f ), RVector3( 0.0f, -1.0f, 0.0f ), RVector2( 0.0f, 0.0f ) }, { RVector3( 1.0f, -1.0f, -1.0f ), RVector3( 0.0f, -1.0f, 0.0f ), RVector2( 1.0f, 0.0f ) }, { RVector3( 1.0f, -1.0f, 1.0f ), RVector3( 0.0f, -1.0f, 0.0f ), RVector2( 1.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, 1.0f ), RVector3( 0.0f, -1.0f, 0.0f ), RVector2( 0.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, 1.0f ), RVector3( -1.0f, 0.0f, 0.0f ), RVector2( 0.0f, 0.0f ) }, { RVector3( -1.0f, -1.0f, -1.0f ), RVector3( -1.0f, 0.0f, 0.0f ), RVector2( 1.0f, 0.0f ) }, { RVector3( -1.0f, 1.0f, -1.0f ), RVector3( -1.0f, 0.0f, 0.0f ), RVector2( 1.0f, 1.0f ) }, { RVector3( -1.0f, 1.0f, 1.0f ), RVector3( -1.0f, 0.0f, 0.0f ), RVector2( 0.0f, 1.0f ) }, { RVector3( 1.0f, -1.0f, 1.0f ), RVector3( 1.0f, 0.0f, 0.0f ), RVector2( 0.0f, 0.0f ) }, { RVector3( 1.0f, -1.0f, -1.0f ), RVector3( 1.0f, 0.0f, 0.0f ), RVector2( 1.0f, 0.0f ) }, { RVector3( 1.0f, 1.0f, -1.0f ), RVector3( 1.0f, 0.0f, 0.0f ), RVector2( 1.0f, 1.0f ) }, { RVector3( 1.0f, 1.0f, 1.0f ), RVector3( 1.0f, 0.0f, 0.0f ), RVector2( 0.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, -1.0f ), RVector3( 0.0f, 0.0f, -1.0f ), RVector2( 0.0f, 0.0f ) }, { RVector3( 1.0f, -1.0f, -1.0f ), RVector3( 0.0f, 0.0f, -1.0f ), RVector2( 1.0f, 0.0f ) }, { RVector3( 1.0f, 1.0f, -1.0f ), RVector3( 0.0f, 0.0f, -1.0f ), RVector2( 1.0f, 1.0f ) }, { RVector3( -1.0f, 1.0f, -1.0f ), RVector3( 0.0f, 0.0f, -1.0f ), RVector2( 0.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, 1.0f ), RVector3( 0.0f, 0.0f, 1.0f ), RVector2( 0.0f, 0.0f ) }, { RVector3( 1.0f, -1.0f, 1.0f ), RVector3( 0.0f, 0.0f, 1.0f ), RVector2( 1.0f, 0.0f ) }, { RVector3( 1.0f, 1.0f, 1.0f ), RVector3( 0.0f, 0.0f, 1.0f ), RVector2( 1.0f, 1.0f ) }, { RVector3( -1.0f, 1.0f, 1.0f ), RVector3( 0.0f, 0.0f, 1.0f ), RVector2( 0.0f, 1.0f ) }, }; return vertices; }
sint CRenderer::CreateDynamicBox( void ) { ////////////////////////////////////////// // Define vertex buffer VPosColor vertices[] = { { RVector3( -1.0f, 1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, 1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, 1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, 1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, -1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, -1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, 1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, 1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, -1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, -1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, 1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, 1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, -1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, 1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, 1.0f, -1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, -1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, -1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( 1.0f, 1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, { RVector3( -1.0f, 1.0f, 1.0f ), RVector4( 1.0f, 1.0f, 1.0f, 1.0f ) }, }; ////////////////////////////////////////// // Define the index buffer uint16 indices[] = { 3,1,0, 2,1,3, 6,4,5, 7,4,6, 11,9,8, 10,9,11, 14,12,13, 15,12,14, 19,17,16, 18,17,19, 22,20,21, 23,20,22 }; sint nMesh = CreateMesh( VPosColor::VertexStride, ARRAY_LENGTH( vertices ), sizeof(uint16), ARRAY_LENGTH( indices ), vertices, indices, GFX_BUFFER_USAGE_DEFAULT ); return nMesh; }