void CBigMap::DrawBigMap() { CFRect rtWnd = CFRect( 0, 0, (float)GetWndWidth(), (float)GetWndHeight() ); CColor uColor = CColor::White; CVector2f vStart( (float)GetWndHandle()->m_xScreen, (float)GetWndHandle()->m_yScreen ); CVector2f rtScreen[4] = { vStart , vStart + CVector2f((float)GetWndHandle()->m_Width,0.0f) , vStart + CVector2f(0.0f,(float)GetWndHandle()->m_Height) , vStart + CVector2f((float)GetWndHandle()->m_Width,(float)GetWndHandle()->m_Height)}; float Zoom = GetRootWnd()->GetZoomSize(); IMAGE& image = GetWndHandle()->m_Enable.GetImage(0); CFRect rtImage((float)image.posWnd.x, (float)image.posWnd.y, image.posWnd.x + abs(image.rtTexture.Width()), image.posWnd.y + abs(image.rtTexture.Height())); rtImage.top *= Zoom; rtImage.left *= Zoom; rtImage.bottom *= Zoom; rtImage.right *= Zoom; CFRect rtDraw = rtWnd.Intersection(rtImage).Offset(0.0f,0.0f); CFRect rtText( image.rtTexture.left, image.rtTexture.top, ( rtImage.right - rtImage.left )*image.rtTexture.Width() /rtImage.Width() + image.rtTexture.left, ( rtImage.bottom - rtImage.top )*image.rtTexture.Height()/rtImage.Height() + image.rtTexture.top); if(m_bOpen&&!m_bClose) DrawRect(m_MapTextureOpen, rtDraw, uColor, &rtText,false); else DrawRect( rtScreen, uColor,m_pAlphaTexture,m_MapTextureOpen,m_MapTextureClose/*,&rtText,m_BackTexUV*/, RS_GUI_BIGMAP, GetWndHandle()->m_fZ ); }
void CToolHideTileRegionInView::RenderOneTileRegion( const int nTileRegionIndex, const int nSubTileRegionIndex, ITexture *tex ) { CTerrainMesh *pTerrain = CMapEditApp::GetInst()->GetEditingMesh(); STileRegion *pTileregion = pTerrain->GetTileRegion(nTileRegionIndex); int ntest = CTerrainMgr::MAP_SIZE; STileRenderRegionUnit *pRenderRegionUnit = pTileregion->vRenderRegionUnits[nSubTileRegionIndex]; SGrid &leftbottomgrid = pTerrain->GetGrid(pRenderRegionUnit->vGridIndices[0]); SGrid &rightbottomgrid = pTerrain->GetGrid(pRenderRegionUnit->vGridIndices[CTerrainMgr::MAP_SIZE - 1]); SGrid &lefttopgrid = pTerrain->GetGrid(pRenderRegionUnit->vGridIndices[nGridCountInRenderTileRegion - CTerrainMgr::MAP_SIZE]); SGrid &righttopgrid = pTerrain->GetGrid(pRenderRegionUnit->vGridIndices[nGridCountInRenderTileRegion - 1]); static CVector3f vertex[4]; static CVector2f uv0[4]; static VerColorTex rvertex[6]; const SVertex& leftbottom = pTerrain->GetVertex(leftbottomgrid.dwVertexIndex[0]); const SVertex& lefttop = pTerrain->GetVertex(lefttopgrid.dwVertexIndex[3]); const SVertex& righttop = pTerrain->GetVertex(righttopgrid.dwVertexIndex[2]); const SVertex& rightbottom= pTerrain->GetVertex(rightbottomgrid.dwVertexIndex[1]); vertex[0] = leftbottom.vPosition; uv0[0] = CVector2f(0.0f, 1.0f); vertex[1] = lefttop.vPosition; uv0[1] = CVector2f(0.0f, 0.0f); vertex[2] = rightbottom.vPosition; uv0[2] = CVector2f(1.0f, 1.0f); vertex[3] = righttop.vPosition; uv0[3] = CVector2f(1.0f,0.0f); rvertex[0] = VerColorTex(vertex[0].x, vertex[0].y, vertex[0].z, 0x00ffffff, uv0[0].x, uv0[0].y); rvertex[1] = VerColorTex(vertex[1].x, vertex[1].y, vertex[1].z, 0x00ffffff, uv0[1].x, uv0[1].y); rvertex[2] = VerColorTex(vertex[2].x, vertex[2].y, vertex[2].z, 0x00ffffff, uv0[2].x, uv0[2].y); rvertex[3] = VerColorTex(vertex[1].x, vertex[1].y, vertex[1].z, 0x00ffffff, uv0[1].x, uv0[1].y); rvertex[4] = VerColorTex(vertex[3].x, vertex[3].y, vertex[3].z, 0x00ffffff, uv0[3].x, uv0[3].y); rvertex[5] = VerColorTex(vertex[2].x, vertex[2].y, vertex[2].z, 0x00ffffff, uv0[2].x, uv0[2].y); CRenderDevice::GetInst()->DrawLayer3DSolidQuad(&m_pRS, rvertex, tex, 1); m_pRS->m_TexOP_S0 = TOP_MODULATE; m_pRS->m_TexArg1_S0 = TA_TEXTURE; m_pRS->m_TexArg2_S0 = TA_DIFFUSE; m_pRS->m_AlpOP_S0 = TOP_SELECTARG1; m_pRS->m_AlpArg1_S0 = TA_TEXTURE; m_pRS->m_AlphaTestEnable = true; m_pRS->m_Alpharef = 0x40; m_pRS->m_AlphaTestFun = CMP_GREATER; m_pRS->m_AlphaBlendEnable = false; m_pRS->m_LightEnable = false; m_pRS->m_ZTestEnable = true; m_pRS->m_ZWrite = true; m_pRS->m_Cull = CULLT_CCW; }
CVector2f CVector2f::Normalize() const { float f = Norm(); if (f != 0.0f) { return CVector2f(*this) / f; } return CVector2f(*this); }
void CBigMap::InitMapData( const TCHAR* szMapFile) { ClearUp(); m_SceneName = szMapFile; //读取区域名字和文件名对应关系 if(!LoadConfig(m_SceneName)) return; //创建m_MapTextureClose //创建m_MapTextureOpen CPkgFile::EnableAsyncRead(false); string FileName = ""; format(FileName,"%s/BigMap/open.dds",m_SceneName.c_str()); if(m_bOpen) GetGraphic()->CreateTexture(PATH_ALIAS_RES.c_str(), FileName.c_str(),&m_MapTextureOpen); else { m_MapTextureOpen = NULL; return; } FileName.clear(); format(FileName,"%s/BigMap/close.dds",m_SceneName.c_str()); if(m_bClose) GetGraphic()->CreateTexture(PATH_ALIAS_RES.c_str(), FileName.c_str(),&m_MapTextureClose); else { m_MapTextureClose = NULL; return; } SafeRelease(m_pAlphaTexture); m_pAlphaTexture = GetGraphic()->CreateRenderTarget(); m_pAlphaTexture->SetWidth(uint32(GetWndWidth())); m_pAlphaTexture->SetHeight(uint32(GetWndHeight())); m_pAlphaTexture->SetUseDepthBuffer(false); m_pAlphaTexture->SetFormat(TFMT_A8R8G8B8); m_pAlphaTexture->Create(); m_BackTexUV[2] = CVector2f( 0.0f , float(m_pAlphaTexture->GetHeight())); m_BackTexUV[0] = CVector2f( 0.0f , 0.0f ); m_BackTexUV[3] = CVector2f( float(m_pAlphaTexture->GetWidth()) , float(m_pAlphaTexture->GetHeight()) ); m_BackTexUV[1] = CVector2f( float(m_pAlphaTexture->GetWidth()) , 0.0f); m_fRTWidth = float(m_pAlphaTexture->GetWidth()); m_fRTHeight = float(m_pAlphaTexture->GetHeight()); if(m_pAlphaTexture==NULL) return; //string testdir = szMapFile; //testdir+= "yhl.dds"; //m_pAlphaTexture->SaveToFile(testdir); }
// ============================================================================= // CWindow::DrawDebugPoint // Draw a cross at a position // ----------------------------------------------------------------------------- void CWindow::DrawDebugPoint(const CVector2f thePoint, CColour theColour /* = CColour::Red */) { sf::Vertex lines[] = { sf::Vertex(CVector2f(thePoint.x-5, thePoint.y), theColour), sf::Vertex(CVector2f(thePoint.x+5, thePoint.y), theColour), sf::Vertex(CVector2f(thePoint.x, thePoint.y-5), theColour), sf::Vertex(CVector2f(thePoint.x, thePoint.y+5), theColour) }; draw(lines, 2, sf::Lines); draw(&lines[2], 2, sf::Lines); }
void CComponent_PlayerInput::UpdateNoClip(float dtTime) { if(game_local.KeyPressedIngame(NSKey::NSK_a)) { physical->rect.pos += CVector2f(-2500.0f * dtTime, 0.0f); } else if(game_local.KeyPressedIngame(NSKey::NSK_d)) { physical->rect.pos += CVector2f(2500.0f * dtTime, 0.0f); } if(game_local.KeyPressedIngame(NSKey::NSK_w)) { physical->rect.pos += CVector2f(0.0f, 2500.0f * dtTime); } else if(game_local.KeyPressedIngame(NSKey::NSK_s)) { physical->rect.pos += CVector2f(0.0f, -2500.0f * dtTime); } }
//------------------------------------------------------------------------------ CEnvMapWaterEffect::CEnvMapWaterEffect() { m_pEnvironmentMap = NULL; m_pWaveNormalMap[0] = NULL; m_pWaveNormalMap[1] = NULL; SetEffectID(CWaterMgr::EffectTypeToID(CWaterMgr::EnvironmentMapping)); CGraphic::GetInst()->CreateTexture(PATH_ALIAS_RES.c_str(), "tile/scenewater/sky.dds",&m_pEnvironmentMap); CGraphic::GetInst()->CreateTexture(PATH_ALIAS_RES.c_str(), "tile/scenewater/wave0.dds",&m_pWaveNormalMap[0]); CGraphic::GetInst()->CreateTexture(PATH_ALIAS_RES.c_str(), "tile/scenewater/wave1.dds",&m_pWaveNormalMap[1]); m_WaveSpeed[0] = CVector2f(0.0225f, 0.015f); m_WaveSpeed[1] = CVector2f(-0.0125f, 0.02f); m_WaveOffset = CVector4f(0,0,0,0); }
void CMovingPlatform::ReactToCollisionWith(CPlayer *player, CVector2f cv) { if (cv.GetDirection() == CVector2f(0.0f, -1.0f)) { mAttachedPlayer = player; } }
//********************************************************************************************************* void CDisplayerVisualActivitySequence::addWanderSteps(const CVector &pos) { //H_AUTO(R2_CDisplayerVisualActivitySequence_addWanderSteps) CDecal *decal = new CDecal; decal->setTexture(CV_WanderDecalTexture.get(), true, true); decal->setWorldMatrixForSpot(CVector2f(pos.x, pos.y), CV_WanderDecalSize.get()); _Decals.push_back(decal); }
void CWidget_Button::Draw() { CColor color = isMouseHovered ? CColor(255, 255, 255) : CColor(128, 128, 128); if(isMouseHovered) { CVector2f textSize = font->GetTextSize(text); float selectWidth = textSize.x * 2.0f; graphics->DrawRect(CRect(CVector2f(pos.x - selectWidth / 2, pos.y - (textSize.y / 2)), (int)selectWidth, 32), CColor(0, 0, 0, 128)); } graphics->DrawText(font, pos, color, text, true); }
void CZoomPos::buildMatrix() { m_matImgToCaneva = m_matCanevaToImg = CMatrix3f::identity(); const CSizef t2 ( getTranslation2() ); const CSizef s1 ( getScale1() ); const CSizef t0 ( getTranslation0() ); // TRACEFN("t2(%.2f,%.2f)\n", t2.cx, t2.cy); // TRACEFN("s1(%.2f,%.2f)\n", s1.cx, s1.cy); // TRACEFN("t0(%.2f,%.2f)\n", t0.cx, t0.cy); m_matImgToCaneva = CMatrix3f::identity() * CMatrix3f::translation( CVector2f(t2.cx, t2.cy) ) * CMatrix3f::scaling ( CVector2f(s1.cx, s1.cy) ) * CMatrix3f::translation( CVector2f(t0.cx, t0.cy) ); m_matImgToCaneva.inverse(m_matCanevaToImg); }
// ============================================================================= // CPullingSwing::AttenuateGravity // ----------------------------------------------------------------------------- CVector2f CPullingSwing::AttenuateGravity(CVector2f gravity) { // Have no gravity when on this swing if (mAttached) { gravity = CVector2f(0.0f, 0.0f); } return gravity; }
void CFrigate::Initialize() { m_fRotSpeed = 3.00f; m_fMaxSpeed = 150.0f; m_fAcceleration = 45.0f; m_fHealth = 0; m_fFuel = 0; m_nPatiencelvl = 0; m_vCenter = CVector2f(0.0f, 0.0f); m_pPipes = new CPipeObject(END_PIPE); m_pPipes->SetConnector(0, 0); }
bool CMovingPlatform::IsPlayerStillOnPlatform() { bool onPlatform = false; CRectangleShape sensor(mWidth, 1.0f); sensor.setPosition(GetPosition() - CVector2f(0.0f, 1.0f)); CVector2f cv; if (CollisionHandler::AreColliding(sensor, mAttachedPlayer->GetHitbox(), &cv)) { onPlatform = true; } return onPlatform; }
void CComponent_PlayerInput::Update(float dtTime) { if(noclip) { UpdateNoClip(dtTime); return; } if(game_local.KeyPressedIngame(NSKey::NSK_a)) { physical->AddVelocity(CVector2f(-25.0f, 0.0f)); animation->ChangeAnimationState("Walk_Left"); animation->AdvanceFrame(); } else if(game_local.KeyPressedIngame(NSKey::NSK_d)) { physical->AddVelocity(CVector2f(25.0f, 0.0f)); animation->ChangeAnimationState("Walk_Right"); animation->AdvanceFrame(); } else { if (animation->currentAnimState == "Walk_Left") { animation->ChangeAnimationState("Idle_Left"); } else if (animation->currentAnimState == "Walk_Right") { animation->ChangeAnimationState("Idle_Right"); } } if(game_local.KeyPressedIngame(NSKey::NSK_SPACE)) { if(!physical->isInAir) { physical->AddVelocity(CVector2f(0.0f, 2600.0f)); } } }
void CProjectile::ReactToCollision(CGameObject *theOtherObject, CVector2f correctionVector) { if (theOtherObject->IsA("Wall")) { if (IsActive()) { KillAfterTime(CTime::Seconds(2.0f)); mVelocity = CVector2f(0.0f, 0.0f); } } else if (theOtherObject->IsA("Player") && IsActive()) { MarkAsDead(); } }
CSquare::CSquare(ISceneNode * pParent, string texname, float fSizeInWidth, float fSizeInDepth, DWORD dwColor) : ISceneNode(pParent), m_strLockTextureName(texname), m_fSizeInWidth(fSizeInWidth), m_fSizeInDepth(fSizeInDepth), m_dwColor(dwColor) { m_vPos[0] = CVector3f(-m_fSizeInWidth*0.5f, 0.0f, -m_fSizeInDepth*0.5f); m_vPos[1] = CVector3f(-m_fSizeInWidth*0.5f, 0.0f, m_fSizeInDepth*0.5f); m_vPos[2] = CVector3f(m_fSizeInWidth*0.5f, 0.0f, -m_fSizeInDepth*0.5f); m_vPos[3] = CVector3f(m_fSizeInWidth*0.5f, 0.0f, m_fSizeInDepth*0.5f); m_dwClr[0] = dwColor; m_dwClr[1] = dwColor; m_dwClr[2] = dwColor; m_dwClr[3] = dwColor; m_vUV[0] = CVector2f(0.0f,1.0f); m_vUV[1] = CVector2f(0.0f,0.0f); m_vUV[2] = CVector2f(1.0f,1.0f); m_vUV[3] = CVector2f(1.0f,0.0f); ////添加自己的SceneNode名字 //m_strSceneNodeName = "SquareSceneNode"; m_pRS = NULL; m_pLockTexture = CRenderDevice::GetInst()->GetTexture(m_strLockTextureName.c_str()); }
CLevel::CLevel() { mPlatforms.push_back(new CPlatform(CVector2f(0.0f, 0.0f), 50.0f, GameOptions::viewHeight)); mPlatforms.push_back(new CPlatform(CVector2f(GameOptions::viewWidth - 50.0f, 0.0f), 50.0f, GameOptions::viewHeight)); mPlatforms.push_back(new CPlatform(CVector2f(0.0f, GameOptions::viewHeight - 50.0f), GameOptions::viewWidth, 50.0f)); mPlatforms.push_back(new CPlatform(CVector2f(0.0f, 0.0f), GameOptions::viewWidth, 50.0f)); mPlatforms.push_back(new CPlatform(CVector2f(200.0f, GameOptions::viewHeight - 250.0f), 300.0f, 50.0f)); mPlatforms.push_back(new CPlatform(CVector2f(600.0f, GameOptions::viewHeight - 400.0f), 300.0f, 50.0f)); mPlatforms.push_back(new CPlatform(CVector2f(200.0f, GameOptions::viewHeight - 550.0f), 300.0f, 50.0f)); }
void CLevel::StartLevel() { mPlayer.Init(CVector2f(800.0f, GameOptions::viewHeight - (50.0f + CGlobals::playerHalfWidth))); }
#include "CGlobals.hpp" float CGlobals::playerSize = 50.0f; float CGlobals::switchSize = 40.0f; CVector2f CGlobals::gravity = CVector2f(0.0f, 3000.0f); CInput CGlobals::leftInput = CInput(CKeyboard::A); CInput CGlobals::rightInput = CInput(CKeyboard::D); CInput CGlobals::jumpInput = CInput(CKeyboard::Space); CInput CGlobals::resetInput = CInput(CKeyboard::R);
void CArena::Draw(CWindow *theWindow) { // Center the view on the player CView theOriginalView = CView(theWindow->getView()); CView theView = theOriginalView; CVector2f viewSize = theView.getSize(); CVector2f viewCenter; viewCenter.x = mPlayer->GetPosition().x; viewCenter.y = mPlayer->GetPosition().y; float minmaxPixel = (mArenaSize / 2.0f) + 150.0f; float rightmostPixel = viewCenter.x + (viewSize.x / 2.0f); float leftmostPixel = viewCenter.x - (viewSize.x / 2.0f); if (rightmostPixel > minmaxPixel) { float offset = rightmostPixel - minmaxPixel; viewCenter.x -= offset; } if (leftmostPixel < -minmaxPixel) { float offset = -minmaxPixel - leftmostPixel; viewCenter.x += offset; } float topmostPixel = viewCenter.y - (viewSize.y / 2.0f); float bottommostPixel = viewCenter.y + (viewSize.y / 2.0f); if (bottommostPixel > minmaxPixel) { float offset = bottommostPixel - minmaxPixel; viewCenter.y -= offset; } if (topmostPixel < -minmaxPixel) { float offset = -minmaxPixel - topmostPixel; viewCenter.y += offset; } theView.setCenter(viewCenter); theWindow->setView(theView); // Draw static objects for (CGameObject *theObject : mStaticObjects) { theObject->Draw(theWindow); } // Draw floor pattern int interval = 50; for (int xy = interval - 500; xy < 500; xy += interval) { CLine theHorizontalLine = CLine(CVector2f(-500, xy), CVector2f(500, xy)); CLine theVerticalLine = CLine(CVector2f(xy, -500), CVector2f(xy, 500)); theWindow->DrawLine(theHorizontalLine, CColour::Black); theWindow->DrawLine(theVerticalLine, CColour::Black); } // Draw dynamic objects for (CGameObject *theObject : mObjects) { theObject->Draw(theWindow); } theWindow->setView(theOriginalView); }
// ============================================================================= // CPullingSwing::RespondToAttach // ----------------------------------------------------------------------------- void CPullingSwing::RespondToAttach() { // Throw away all player velocity, we'll give them a vel towards the origin // in update mBob->SetVelocity(CVector2f(0.0f, 0.0f)); }
CVector2f CVector2f::operator *(float f) const { return CVector2f(m_fX * f, m_fY * f); }
// ********************************************************* void CPrimRender::updatePos() { //H_AUTO(R2_CPrimRender_updatePos) // world map if (_AddedToWorldMap) { setWorldMapNumVertices(_Look.VertexLook.WorldMapTexture.empty() ? 0 : (uint)_Vertices.size()); setWorldMapNumEdges(_Look.EdgeLook.WorldMapTexture.empty() ? 0 : _NumEdges); } // if (!_Vertices.empty()) { // edges update switch(_Look.Shape) { case CPrimLook::Star: { CVector centerPos = _Vertices[0]; for(uint k = 1; k < _Vertices.size(); ++k) { // if (!_EdgeShapeInstances.empty()) { updateEdge(_EdgeShapeInstances[k - 1], centerPos, _Vertices[k]); } if (!_EdgeDecals.empty()) { updateEdgeDecal(*(_EdgeDecals[k - 1]), centerPos, _Vertices[k], _Look.FirstVertexLook.DecalDistToEdgeDecal, _Look.VertexLook.DecalDistToEdgeDecal); } } } break; case CPrimLook::PolyLine: case CPrimLook::ClosedPolyLine: { for(sint k = 0; k < _NumEdges; ++k) { // if (!_EdgeShapeInstances.empty()) { updateEdge(_EdgeShapeInstances[k], _Vertices[k], _Vertices[(k + 1) % _Vertices.size()]); } if (!_EdgeDecals.empty()) { updateEdgeDecal(*(_EdgeDecals[k]), _Vertices[k], _Vertices[(k + 1) % _Vertices.size()], k == 0 ? _Look.VertexLook.DecalDistToEdgeDecal : _Look.FirstVertexLook.DecalDistToEdgeDecal, _Look.VertexLook.DecalDistToEdgeDecal ); } } } break; default: nlassert(0); break; } } // update vertices for(uint k = 0; k < _Vertices.size(); ++k) { if (!_Look.VertexShapeName.empty()) { if (!_VertexShapeInstances[k].empty()) { CMatrix vertexMat; vertexMat.setScale(_Look.VertexShapeScale); vertexMat.setPos(_Vertices[k]); _VertexShapeInstances[k].setTransformMode(UTransform::DirectMatrix); _VertexShapeInstances[k].setMatrix(vertexMat); } } if (!_VertexDecals.empty()) { _VertexDecals[k]->setWorldMatrixForSpot(CVector2f(_Vertices[k].x, _Vertices[k].y), k == 0 ? _Look.FirstVertexLook.DecalSize : _Look.VertexLook.DecalSize); } } // if (_AddedToWorldMap) { updateWorldMapDisplay(); } }
void CGraphic::DrawRectGroup( ITexture* pTexture, ERenderStyle RS, const RectGroup* rt, int nNum ) { static TRIAGLE_INDEX Index; if ( pTexture && pTexture->IsValid() ) { float fInvWidth = 1.0f/pTexture->GetWidth(); float fInvHeight = 1.0f/pTexture->GetHeight(); void* pIB; Vertex2D* pVB; int tempNum = nNum; do { nNum = tempNum; if ( nNum == 1 ) GetRenderStack( RS, pTexture, NULL, PT_TRIANGLESTRIP, 4*nNum, 2*nNum, Vertex2D::Format, sizeof(Vertex2D), (void**)&pVB, NULL ); else { if ( nNum > MAX_TRI_INDEXNUM ) nNum = MAX_TRI_INDEXNUM; GetRenderStack( RS, pTexture, NULL, PT_TRIANGLELIST, 4*nNum, 2*nNum, Vertex2D::Format, sizeof(Vertex2D), (void**)&pVB, &pIB ); memcpy( pIB, Index, nNum*12 ); } for ( int i = 0; i < nNum; i++ ) { int n = i<<2; CVector2f vecText[4] = { CVector2f( 0.00001f, 1.00001f ), CVector2f( 0.00001f, 0.00001f ), CVector2f( 1.00001f, 1.00001f ), CVector2f( 1.00001f, 0.00001f ) }; if ( rt[i].bFullText == false ) { vecText[0].x = ( rt[i].rtText[0].x + 0.25f )*fInvWidth; vecText[0].y = ( rt[i].rtText[0].y + 0.25f )*fInvHeight; vecText[1].x = ( rt[i].rtText[1].x + 0.25f )*fInvWidth; vecText[1].y = ( rt[i].rtText[1].y + 0.25f )*fInvHeight; vecText[2].x = ( rt[i].rtText[2].x + 0.25f )*fInvWidth; vecText[2].y = ( rt[i].rtText[2].y + 0.25f )*fInvHeight; vecText[3].x = ( rt[i].rtText[3].x + 0.25f )*fInvWidth; vecText[3].y = ( rt[i].rtText[3].y + 0.25f )*fInvHeight; } pVB[ n + 0 ].Set( rt[i].vec[0].x, rt[i].vec[0].y, rt[i].z, rt[i].Color, vecText[0].x, vecText[0].y ); pVB[ n + 1 ].Set( rt[i].vec[1].x, rt[i].vec[1].y, rt[i].z, rt[i].Color, vecText[1].x, vecText[1].y ); pVB[ n + 2 ].Set( rt[i].vec[2].x, rt[i].vec[2].y, rt[i].z, rt[i].Color, vecText[2].x, vecText[2].y ); pVB[ n + 3 ].Set( rt[i].vec[3].x, rt[i].vec[3].y, rt[i].z, rt[i].Color, vecText[3].x, vecText[3].y ); } tempNum -= nNum; rt += nNum; } while (tempNum > 0); } else { VerColor2D* pVB; void* pIB; int tempNum = nNum; do { nNum = tempNum; if ( nNum == 1 ) GetRenderStack( RS, NULL, NULL, PT_TRIANGLESTRIP, 4*nNum, 2*nNum, VerColor2D::Format, sizeof(VerColor2D), (void**)&pVB, NULL ); else { if ( nNum > MAX_TRI_INDEXNUM ) nNum = MAX_TRI_INDEXNUM; GetRenderStack( RS, NULL, NULL, PT_TRIANGLELIST, 4*nNum, 2*nNum, VerColor2D::Format, sizeof(VerColor2D), (void**)&pVB, &pIB ); memcpy( pIB, Index, nNum*12 ); } for ( int i = 0; i < nNum; i++ ) { int n = i<<2; pVB[ n + 0 ].Set( rt[i].vec[0].x, rt[i].vec[0].y, rt[i].z, rt[i].Color ); pVB[ n + 1 ].Set( rt[i].vec[1].x, rt[i].vec[1].y, rt[i].z, rt[i].Color ); pVB[ n + 2 ].Set( rt[i].vec[2].x, rt[i].vec[2].y, rt[i].z, rt[i].Color ); pVB[ n + 3 ].Set( rt[i].vec[3].x, rt[i].vec[3].y, rt[i].z, rt[i].Color ); } tempNum -= nNum; rt += nNum; } while (tempNum > 0); } }
void CWavefrontObjParser::parseMesh(CMesh &mesh, bool flip) { enum EOBJElement { OE_POSITION, OE_NORMAL, OE_UV, OE_NONE, }; int vertexElementTypeOrder[] = {-1, -1, -1}; int vertexElementTypeCount = -1; EOBJElement LastElementType = OE_NONE; std::cout << "Parsing mesh" << std::endl; struct sUniqueVertexIndex { sUniqueVertexIndex(){} sUniqueVertexIndex(size_t pos,size_t normal,size_t uv) : mPosition(pos), mNormal(normal), mUv(uv){} sUniqueVertexIndex(const sUniqueVertexIndex &other) : mPosition(other.mPosition), mNormal(other.mNormal), mUv(other.mUv){} CHash GetHash() const { CHash hash(mPosition); hash.next_hash(mNormal); hash.next_hash(mUv); return hash; } size_t mPosition; size_t mNormal; size_t mUv; }; std::vector<CVector3f> positions; std::vector<CVector3f> normals; std::vector<CVector2f> uvs; size_t vertex_capacity = mesh.m_vertex_capacity; positions.reserve(vertex_capacity); normals.reserve(vertex_capacity); uvs.reserve(vertex_capacity); CVector3f t_vertex; CVector3f t_normal; CVector2f t_uv; enum {MAX_FACE_EDGE_COUNT = 64}; int tmpFace[MAX_FACE_EDGE_COUNT * 3]; std::vector<sUniqueVertexIndex> vVertexIndices; CHashMapInt *sUniqueVertices = new CHashMapInt(); //TODO: read these in properely mesh.mVertexBuffer.Lock(); mesh.mIndexBuffer.Lock(); // The while-read only consumes the first part of the header. #ifdef USETIMER CHighResolutionTimer ctimer; ctimer.StartHRTimer(); #endif while(fscanf_s(mPfile,"%s",mReadHeader,sizeof(mReadHeader)) != EOF ) { // Match a Vertex if (strcmp(mReadHeader,"v") == 0) { fscanf_s(mPfile,"%f%f%f",&t_vertex[0],&t_vertex[1],&t_vertex[2]); // range-based looper for each element (doesn't work for this compiler) positions.push_back(t_vertex * mesh_scale_factor); if (LastElementType != OE_POSITION) { ++vertexElementTypeCount; vertexElementTypeOrder[OE_POSITION] = vertexElementTypeCount; LastElementType = OE_POSITION; } } else if (strcmp(mReadHeader,"vn") == 0) { fscanf_s(mPfile,"%f%f%f",&t_normal[0],&t_normal[1],&t_normal[2]); normals.push_back(t_normal); if (LastElementType != OE_NORMAL) { ++vertexElementTypeCount; vertexElementTypeOrder[OE_NORMAL] = vertexElementTypeCount; LastElementType = OE_NORMAL; } } else if (strcmp(mReadHeader,"vt") == 0) { fscanf_s(mPfile,"%f%f",&t_uv.x,&t_uv.y); uvs.push_back(t_uv); if (LastElementType != OE_UV) { ++vertexElementTypeCount; vertexElementTypeOrder[OE_UV] = vertexElementTypeCount; LastElementType = OE_UV; } } else //mMesh.beg int lineNumber = 0; if (strcmp(mReadHeader,"f") == 0) { char szLine[4096]; fgets(szLine, 4096, mPfile); int cidx = 0; int pcidx = 0; int element_count = 0; int max_element_count =0; const char *ch = szLine; while (!isEOL(*ch) && (*ch)) { if (IsWhiteSpace(*ch)) { if (element_count > max_element_count) max_element_count = element_count; element_count = 0; skipWhiteSpace(ch); } const char *startch = ch; while (isalnum(*ch)) { ++ch; } tmpFace[cidx++] = atoiSimpleCount(startch, ch - startch); if (*ch == '/') { ++element_count; while (*ch == '/') { ++ch; } } } //std::cout << lineNumber << std::endl; element_count = max_element_count + 1; size_t vcount = (cidx) / element_count; std::vector<size_t> polygon_indices; int *cface= tmpFace; if (uvs.size() == 0) uvs.push_back(CVector2f(0,0)); for (unsigned int i = 0; i < vcount; ++i) { //sUniqueVertexIndex vref = (element_count == 2) ? sUniqueVertexIndex(cface[vertexElementTypeOrder[0]] - 1, cface[vertexElementTypeOrder[1]] - 1, 0) : sUniqueVertexIndex(cface[vertexElementTypeOrder[0]] - 1, cface[vertexElementTypeOrder[1]] - 1, cface[vertexElementTypeOrder[2]] - 1); sUniqueVertexIndex vref = (element_count == 2) ? sUniqueVertexIndex(cface[0] - 1, cface[1] - 1, 0) : sUniqueVertexIndex(cface[0] - 1, cface[2] - 1, cface[1] - 1); cface += element_count; CHash hash = vref.GetHash(); //hash.next_hash(t_uvIndex[i]) //auto itr = sUniqueVertices->find(hash); size_t vIndex = 0; //if (itr == sUniqueVertices->end()) { vIndex = vVertexIndices.size(); //could //sUniqueVertices->insert(CHashMapInt::value_type(hash, vIndex)); vVertexIndices.push_back(vref); //OBJ starts indices at 1 but C++ starts at 0 if ((vref.mPosition>positions.size()) || (vref.mNormal>normals.size()) || (vref.mUv>uvs.size())) { int a = 10; } mesh.mVertexBuffer.AddVertex(SVertex_P_D4B_N_UV(positions[vref.mPosition], vref.mNormal < normals.size() ? normals[vref.mNormal] : CVector3f(1,0,0), vref.mUv < uvs.size() ? uvs[vref.mUv] : CVector2f(0,0), 0xFFFFFFFF)); //add } /*else { vIndex = itr->second; }*/ polygon_indices.push_back(vIndex); //++lineNumber; } mesh.add_polygon(polygon_indices, flip); //std::cout << mesh.m_index_count / 3 << std::endl; } } if (normals.size() == 0) mesh.calculate_normals(); #ifdef USE_TIMER ctimer.StopHRTimer(); std::cout << "Obj loader took " << ctimer.GetElapsedTime() << std::endl; ctimer.StartHRTimer(); #endif mesh.mIndexBuffer.UnLock(); //Copy back to GPU mesh.mVertexBuffer.UnLock(); //mesh.CalculateGeometryMetrics(); #ifdef USE_TIMER ctimer.StopHRTimer(); std::cout << "Uploading data to GPU took " << ctimer.GetElapsedTime() << std::endl; ctimer.StartHRTimer(); #endif //Very, very slow in windows and in debug (fine in release, fine in Linux) so for the sake of sanity, I'm causing a memory leak in debug. #ifndef _DEBUG sUniqueVertices->clear(); delete sUniqueVertices; #endif #ifdef USE_TIMER ctimer.StopHRTimer(); std::cout << "Clearing map took " << ctimer.GetElapsedTime() << std::endl; #endif fclose(mPfile); }
CVector2f CVertex::GetTex() const { return CVector2f(&(m_vec[TX])); }
CVector2f operator *(float f,const CVector2f& v) { return CVector2f(v.m_fX * f, v.m_fY * f); }
CVector2f CTriangleMesh::GetTextureProjection(CVector3d pointInObjectSpace) const { // TODO return CVector2f(); }
Void CText::UpdateBuffer( const UInt32 offset, const UInt32 count ) { if( m_State.GetBit( nState_UpdateBuffer ) == FALSE || m_Font == NULL ) return; if( m_State.GetBit( nState_RecreateBuffer ) ) { CreateBuffer(); } UInt32 index, i; Int32 x = 0, y=0; Float32 textureWidth = (Float32)m_Font->GetTextureWidth(); Float32 textureHeight = (Float32)m_Font->GetTextureHeight(); SVertex* buffer = (SVertex*)m_VertexBuffer.Lock(0 * 6, 0 *6); x= 0; i = 0; m_CtrlCharCount = 0; CRect2Di globalBBox( CVector2i::Zero, CVector2i::Zero ); for(index = 0; index<count; index++) { const CTextFont::SChar &c = m_Font->GetChar(m_Text[index]); if( m_Text[index] == '\n' ) { x = 0; y -= m_Font->GetLineSpacing(); m_CtrlCharCount ++; } else { Float32 s = m_FontSize; // If we are not on the first char, re adjust x so that it start not at the beginning of it's bounding rect, // but at the beginning of it's glyph pos if( x > 0 ) x -= c.OffsetX; buffer[i*6 + 0].Position = CVector2f( (Float32)x, (Float32)y-c.OffsetY ); buffer[i*6 + 1].Position = CVector2f( (Float32)x+c.BoundWidth*s, (Float32)y-c.OffsetY ); buffer[i*6 + 2].Position = CVector2f( (Float32)x, (Float32)y-c.OffsetY-c.BoundHeight*s ); buffer[i*6 + 5].Position = CVector2f( (Float32)x+c.BoundWidth*s, (Float32)y-c.OffsetY-c.BoundHeight*s ); buffer[i*6 + 0].TexCoord0 = CVector2f( (Float32)c.BoundX, (Float32)c.BoundY); buffer[i*6 + 1].TexCoord0 = CVector2f( (Float32)c.BoundX + c.BoundWidth, (Float32)c.BoundY); buffer[i*6 + 2].TexCoord0 = CVector2f( (Float32)c.BoundX, (Float32)c.BoundY + c.BoundHeight); buffer[i*6 + 5].TexCoord0 = CVector2f( (Float32)c.BoundX + c.BoundWidth, (Float32)c.BoundY + c.BoundHeight); buffer[i*6 + 3] = buffer[i*6 + 2]; buffer[i*6 + 4] = buffer[i*6 + 1]; Int32 width = (Int32)((c.CharWidth+c.OffsetX)*s); Int32 height = (Int32)(c.OffsetY+c.BoundHeight*s); globalBBox.Add( CRect2Di( x, y, c.BoundWidth*s, c.BoundHeight*s ) ); x += width; i++; } } m_Size = globalBBox.GetSize(); m_VertexBuffer.UnLock(); m_State.ClearBit( nState_UpdateBuffer ); }