void cCube2::SetCube(const Vector3 &vMin, const Vector3 &vMax ) { if (m_vtxBuff.GetVertexCount() <= 0) InitCube(); const Vector3 center = (vMin + vMax) / 2.f; const Vector3 v1 = vMin - vMax; const Vector3 v2 = m_max - m_min; Vector3 scale(sqrt(v1.x*v1.x) / sqrt(v2.x*v2.x), sqrt(v1.y*v1.y) / sqrt(v2.y*v2.y), sqrt(v1.z*v1.z) / sqrt(v2.z*v2.z)); Matrix44 S; S.SetScale(scale); Matrix44 T; T.SetTranslate( center ); Matrix44 tm = S * T; sVertexNormTex *vbuff = (sVertexNormTex*)m_vtxBuff.Lock(); for (int i=0; i < m_vtxBuff.GetVertexCount(); ++i) vbuff[ i].p *= tm; m_vtxBuff.Unlock(); m_min = vMin; m_max = vMax; }
cWater::cWater() : m_isRenderSurface(false) { Matrix44 mWaterWorld; mWaterWorld.SetTranslate(Vector3(0,10,0)); sInitInfo waterInitInfo; waterInitInfo.dirLight.Init(cLight::LIGHT_DIRECTIONAL); waterInitInfo.mtrl.InitWhite(); waterInitInfo.vertRows = 64; waterInitInfo.vertCols = 64; waterInitInfo.dx = 1.0f; waterInitInfo.dz = 1.0f; waterInitInfo.waveMapFilename0 = "wave0.dds"; waterInitInfo.waveMapFilename1 = "wave1.dds"; waterInitInfo.waveMapVelocity0 = Vector2(0.09f, 0.06f); waterInitInfo.waveMapVelocity1 = Vector2(-0.05f, 0.08f); waterInitInfo.texScale = 10.0f; waterInitInfo.refractBias = 0.1f; waterInitInfo.refractPower = 2.0f; //waterInitInfo.rippleScale = Vector2(0.06f, 0.03f); waterInitInfo.rippleScale = Vector2(0.06f, 0.0f); waterInitInfo.toWorld = mWaterWorld; m_initInfo = waterInitInfo; m_waveMapOffset0 = Vector2(0.0f, 0.0f); m_waveMapOffset1 = Vector2(0.0f, 0.0f); }
void Init() { ReadModelFile("../../media/model_normal.dat", g_vertices, g_indices, g_normals); g_matWorld1.SetTranslate(Vector3(0,0,0)); Vector3 dir = g_cameraLookat - g_cameraPos; dir.Normalize(); g_matView.SetView(g_cameraPos, dir, Vector3(0,1,0)); g_matProjection.SetProjection( MATH_PI / 4.f, 1.0f, 1.0f, 100.0f ); RECT cr; ::GetClientRect(g_hWnd, &cr); const float width = (float)(cr.right-cr.left); const float height = (float)(cr.bottom - cr.top); g_matViewPort.SetViewport(width, height); }
// screen pixel 좌표 pos 값이 스프라이트 안에 있다면 true를 리턴한다. bool cSprite::IsContain(const Vector2 &pos) { Vector3 leftTop(0,0,0); Vector3 rightBottom = Vector3(m_rect.Width(), m_rect.Height(), 0); Matrix44 S; S.SetScale(m_scale); Matrix44 C; C.SetTranslate(-m_center); Matrix44 tm = S * C * m_accTM; leftTop *= tm; rightBottom *= tm; return ((leftTop.x <= pos.x) && (leftTop.y <= pos.y) && (rightBottom.x >= pos.x) && (rightBottom.y >= pos.y)); }
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 여기에 코드를 입력합니다. MSG msg; HACCEL hAccelTable; // 전역 문자열을 초기화합니다. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_MY2DRENDERER, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // vertices1 // (-50,+50, +50) ----------------- (+50, +50, +50) // | | // | + | // | | // (-50,+50, -50) ----------------- (+50, +50, -50) // // (-50,-50, +50) ----------------- (+50, -50, +50) // | | // | + | // | | // (-50,-50, -50) ----------------- (+50, -50, -50) const float w = 80.f; g_vertices1.reserve(128); g_vertices1.push_back( Vector3(-w,w,w) ); g_vertices1.push_back( Vector3(w,w,w) ); g_vertices1.push_back( Vector3(w,w,-w) ); g_vertices1.push_back( Vector3(-w,w,-w) ); g_vertices1.push_back( Vector3(-w,-w,w) ); g_vertices1.push_back( Vector3(w,-w,w) ); g_vertices1.push_back( Vector3(w,-w,-w) ); g_vertices1.push_back( Vector3(-w,-w,-w) ); g_indices.reserve(128); // top g_indices.push_back(0); g_indices.push_back(2); g_indices.push_back(3); g_indices.push_back(0); g_indices.push_back(1); g_indices.push_back(2); // front g_indices.push_back(3); g_indices.push_back(2); g_indices.push_back(7); g_indices.push_back(2); g_indices.push_back(6); g_indices.push_back(7); // back g_indices.push_back(1); g_indices.push_back(4); g_indices.push_back(5); g_indices.push_back(1); g_indices.push_back(0); g_indices.push_back(4); // left g_indices.push_back(3); g_indices.push_back(4); g_indices.push_back(0); g_indices.push_back(7); g_indices.push_back(4); g_indices.push_back(3); // right g_indices.push_back(2); g_indices.push_back(5); g_indices.push_back(6); g_indices.push_back(2); g_indices.push_back(1); g_indices.push_back(5); // bottom g_indices.push_back(4); g_indices.push_back(7); g_indices.push_back(6); g_indices.push_back(4); g_indices.push_back(6); g_indices.push_back(5); g_matWorld1.SetTranslate(Vector3(150,200,0)); g_matWorld2.SetTranslate(Vector3(400,200,0)); g_matWorld3.SetTranslate(Vector3(600,200,0)); // 응용 프로그램 초기화를 수행합니다. if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY2DRENDERER)); // 기본 메시지 루프입니다. int oldT = GetTickCount(); while (1) { if (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } if(msg.message == WM_QUIT) break; } const int curT = GetTickCount(); const int elapseT = curT - oldT; if (elapseT > 30) { oldT = curT; MainLoop(elapseT); } } return (int) msg.wParam; }
void CMapView::OnMouseMove(UINT nFlags, CPoint point) { cCamera &camera = cMapController::Get()->GetCamera(); if (m_LButtonDown) { CPoint pos = point - m_curPos; m_curPos = point; if (cMapController::Get()->GetEditMode() == EDIT_MODE::MODE_BRUSH) { cMapController::Get()->Brush(point); } } else if (m_RButtonDown) { CPoint pos = point - m_curPos; m_curPos = point; camera.Pitch2(pos.y * 0.005f); camera.Yaw2(pos.x * 0.005f); } else if (m_MButtonDown) { CPoint pos = point - m_curPos; m_curPos = point; Vector3 dir = camera.GetDirection(); dir.y = 0; dir.Normalize(); const float len = camera.GetDistance(); camera.MoveRight( -pos.x * len * 0.001f ); camera.MoveAxis( dir, pos.y * len * 0.001f ); } //추가 else if( cMapController::Get()->GetEditMode() == EDIT_MODE::MODE_OBJECT ) { CPoint pos = point - m_curPos; m_curPos = point; if( graphic::cModel* pCurrObject = cMapController::Get()->GetCurrObject() ) { m_ray.Create(m_curPos.x, m_curPos.y, VIEW_WIDTH, VIEW_HEIGHT, camera.GetProjectionMatrix(), camera.GetViewMatrix() ); Vector3 pickPos; cMapController::Get()->GetTerrain().Pick( m_ray.orig, m_ray.dir, pickPos); Matrix44 matT; matT.SetTranslate( pickPos ); pCurrObject->SetTM( matT ); } //추가 else if( m_currSelectObj ) { Quaternion q( Vector3(0,1,0), pos.x * -0.005f ); Matrix44 matR = q.GetMatrix(); m_currSelectObj->SetTM( matR * m_currSelectObj->GetTM() ); m_currSelectObj->GetCube().SetTransform( m_currSelectObj->GetTM() ); } } else { m_curPos = point; if ((cMapController::Get()->GetEditMode() == EDIT_MODE::MODE_BRUSH) || (cMapController::Get()->GetEditMode() == EDIT_MODE::MODE_MODEL)) { m_ray.Create(m_curPos.x, m_curPos.y, VIEW_WIDTH, VIEW_HEIGHT, camera.GetProjectionMatrix(), camera.GetViewMatrix() ); Vector3 pickPos; cMapController::Get()->GetTerrain().Pick(m_ray.orig, m_ray.dir, pickPos); cMapController::Get()->GetTerrainCursor().UpdateCursor(cMapController::Get()->GetTerrain(), pickPos ); } } CView::OnMouseMove(nFlags, point); }
void CMapView::OnLButtonUp(UINT nFlags, CPoint point) { ReleaseCapture(); // 지형위에 모델을 위치 시킨다. if (m_LButtonDown && (cMapController::Get()->GetEditMode() == EDIT_MODE::MODE_MODEL)) { // 모델이 선택되어 있는 상태라면, 모델을 지형위에 위치 시킨다. if (cMapController::Get()->GetTerrainCursor().IsSelectModel()) { if (const graphic::cModel *model = cMapController::Get()->GetTerrainCursor().GetSelectModel()) { cMapController::Get()->GetTerrain().AddRigidModel(*model); cMapController::Get()->UpdatePlaceModel(); } } else { // 모델이 선택되어 있지 않다면, 지형위의 모델을 피킹해서 선택한다. cCamera &camera = cMapController::Get()->GetCamera(); m_ray.Create(point.x, point.y, VIEW_WIDTH, VIEW_HEIGHT, camera.GetProjectionMatrix(), camera.GetViewMatrix() ); // 모델 피킹. if (graphic::cModel *model = cMapController::Get()->GetTerrain().PickModel(m_ray.orig, m_ray.dir)) { cMapController::Get()->GetTerrain().RemoveRigidModel(model, false); cMapController::Get()->GetTerrainCursor().SelectModel(model); cMapController::Get()->UpdatePlaceModel(); } } } //추가 if( m_LButtonDown && cMapController::Get()->GetEditMode() == EDIT_MODE::MODE_OBJECT ) { graphic::cModel* pCurrObject = cMapController::Get()->GetCurrObject(); m_ray.Create(point.x, point.y, VIEW_WIDTH, VIEW_HEIGHT, cMapController::Get()->GetCamera().GetProjectionMatrix(), cMapController::Get()->GetCamera().GetViewMatrix() ); if( pCurrObject ) { Vector3 pickPos; cMapController::Get()->GetTerrain().Pick(m_ray.orig, m_ray.dir, pickPos); Matrix44 matPos; matPos.SetTranslate( pickPos ); graphic::cModel* pNewObj = new graphic::cModel( m_objectCount++ ); pNewObj->Create( cMapController::Get()->GetCurrObjFileName(), graphic::MODEL_TYPE::RIGID ); pNewObj->SetTM( matPos ); pNewObj->CreateCube(); pNewObj->SetRenderBoundingBox( true ); cMapController::Get()->AddObject( pNewObj ); } else if( !pCurrObject ) { vector<graphic::cModel*>& rObj = cMapController::Get()->GetObject(); if( rObj.empty() == false ) { if( m_currSelectObj ) m_currSelectObj = NULL; for( auto it = rObj.begin(); it != rObj.end(); ++it ) { if( (*it)->Pick( m_ray.orig, m_ray.dir ) == true ) { m_currSelectObj = (*it); break; } //if } //for } //if } //else if } //if m_LButtonDown = false; CView::OnLButtonUp(nFlags, point); }
bool cGameApp::OnInit() { srand(time(NULL)); ReadModelFile("box.dat" , _VB , _vSize , _IB , _vSizeFace , 0 , 255 , 0); ReadModelFile("ground.dat" , _groundVB , _groundSize , _groundIB, _groundSizeFace ,255 , 0 , 0); ReadModelFile("cone.dat" , _coneVB , _coneSize , _coneIB, _coneSizeFace , 100 , 20 ,255); _material.InitBlue(); Vector4 color(1 , 1 ,1 ,1); _light.Init(graphic::light::LIGHT_DIRECTIONAL, color * 0.4f, color , color * 0.6f , Vector3(1,0,0)); //// 버텍스 버퍼 생성. //if (FAILED(graphic::GetDevice()->CreateVertexBuffer( 8 * sizeof(Vertex), // D3DUSAGE_WRITEONLY, Vertex::FVF, // D3DPOOL_MANAGED, &_VB, NULL))) //{ // return false; //} // _VB.Create(8 , sizeof(Vertex) , Vertex::FVF); //if (FAILED(_VB->Lock( 0, sizeof(Vertex), (void**)&_vertex, 0))) // return false; /*_VB.Lock(); float width = 10.0f; _vertex[ 0] = Vertex(-width, -width, -width , D3DCOLOR_ARGB(0 , 255 , 255 , 0)); _vertex[ 1] = Vertex(-width, width, -width , D3DCOLOR_ARGB(0 , 255 , 255 , 0)); _vertex[ 2] = Vertex(width, width, -width , D3DCOLOR_ARGB(0 , 255 , 255 , 0)); _vertex[ 3] = Vertex(width, -width, -width , D3DCOLOR_ARGB(0 , 255 , 255 , 0)); _vertex[ 4] = Vertex(-width, -width, width , D3DCOLOR_ARGB(0 , 0 , 255 , 0)); _vertex[ 5] = Vertex(-width, width, width , D3DCOLOR_ARGB(0 , 0 , 255 , 0)); _vertex[ 6] = Vertex(width, width, width , D3DCOLOR_ARGB(0 , 0 , 255 , 0)); _vertex[ 7] = Vertex(width, -width, width , D3DCOLOR_ARGB(0 , 0 , 255 , 0)); _VB.Unlock();*/ //if (FAILED(graphic::GetDevice()->CreateIndexBuffer(36*sizeof(WORD), // D3DUSAGE_WRITEONLY, // D3DFMT_INDEX16, // D3DPOOL_MANAGED, // &_IB, NULL))) //{ // return false; //} //WORD *indices = NULL; //_IB->Lock(0, 0, (void**)&indices, 0); //int index = 0; //// front //indices[ index++] = 0; indices[ index++] = 1; indices[ index++] = 2; //indices[ index++] = 0; indices[ index++] = 2; indices[ index++] = 3; //// back //indices[ index++] = 4; indices[ index++] = 6; indices[ index++] = 5; //indices[ index++] = 4; indices[ index++] = 7; indices[ index++] = 6; //// left //indices[ index++] = 4; indices[ index++] = 5; indices[ index++] = 1; //indices[ index++] = 4; indices[ index++] = 1; indices[ index++] = 0; //// right //indices[ index++] = 3; indices[ index++] = 2; indices[ index++] = 6; //indices[ index++] = 3; indices[ index++] = 6; indices[ index++] = 7; //// top //indices[ index++] = 1; indices[ index++] = 5; indices[ index++] = 6; //indices[ index++] = 1; indices[ index++] = 6; indices[ index++] = 2; //// bottom //indices[ index++] = 4; indices[ index++] = 0; indices[ index++] = 3; //indices[ index++] = 4; indices[ index++] = 3; indices[ index++] = 7; //_IB->Unlock(); Matrix44 V; Vector3 dir = Vector3(0,0,0)-Vector3(0,0,-5); dir.Normalize(); V.SetView(Vector3(0,0,-100), dir, Vector3(0,1,0)); graphic::GetDevice()->SetTransform(D3DTS_VIEW, (D3DMATRIX*)&V); Matrix44 proj; proj.SetProjection(MATH_PI * 0.5f, (float)800 / (float) 600, 1.f, 1000.0f) ; graphic::GetDevice()->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)&proj) ; graphic::GetDevice()->SetRenderState(D3DRS_LIGHTING, true); //graphic::GetDevice() ->SetRenderState(D3DRS_FILLMODE , D3DFILL_WIREFRAME); _move = _z = 0; for(int i = 0; i < 5; i ++) { Matrix44 down , rota , groMat; down.SetTranslate(Vector3(0 , -70 , 300 * i )); rota.SetRotationX(3.1f); _groundLocal[i] = rota* down; } for(int i = 0 ; i < CONEMAX ; i++) { _coneLocal[i].SetTranslate(Vector3( (rand() % 250) - 150 , -70 , (rand() % 200) + 300)); } _jumpFlag = false; _jumpY = 8; _light.Bind(0); graphic::GetDevice() ->LightEnable( 0, true); return true; }
//랜더 void Render(int timeDelta) { //화면 청소 if (SUCCEEDED(g_pDevice->Clear( 0, //청소할 영역의 D3DRECT 배열 갯수 ( 전체 클리어 0 ) NULL, //청소할 영역의 D3DRECT 배열 포인터 ( 전체 클리어 NULL ) D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, //청소될 버퍼 플레그 ( D3DCLEAR_TARGET 컬러버퍼, D3DCLEAR_ZBUFFER 깊이버퍼, D3DCLEAR_STENCIL 스텐실버퍼 D3DCOLOR_XRGB(150, 150, 150), //컬러버퍼를 청소하고 채워질 색상( 0xAARRGGBB ) 1.0f, //깊이버퍼를 청소할값 ( 0 ~ 1 0 이 카메라에서 제일가까운 1 이 카메라에서 제일 먼 ) 0 //스텐실 버퍼를 채울값 ))) { //화면 청소가 성공적으로 이루어 졌다면... 랜더링 시작 g_pDevice->BeginScene(); //fps출력 static WORD frameCnt = 0; static DWORD ntimeDelay = 0; static float fps = 0.f; frameCnt++; ntimeDelay += timeDelta; if( ntimeDelay >= 1000 ) { fps = (float)frameCnt; frameCnt = 0; ntimeDelay -= ntimeDelay; } string outputFps = format( "%0.3f", fps ); RECT rc; SetRect( &rc, 150, 100, 0, 0 ); //다이렉트 생성할때의 화면 크기의 좌표로 적용됨 global->font->DrawTextA( NULL, //A : 아스키코드 // "global->font->DrawText", outputFps.c_str(), -1, &rc, DT_NOCLIP, //다 출력하겠다라는 의미(화면 크기 연연하지 않겠다인듯??) D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) ); //쿼터니언 응용 /* Quaternion quat; quat.SetRotationArc(Vector3(0,1,0),Vector3(0,0,1)); //첫번째 인자방향 기준으로 두번째 인자 방향으로 Matrix44 qt = quat.GetMatrix(); */ static float ftheta = 0.f; ftheta += 0.0005f; const float fradian = ftheta / MATH_PI; if( ftheta >= 360.f ) ftheta = 0.f; Matrix44 mat; mat.SetTranslate(Vector3(5, sin(fradian) * 10.f, -490)); //sin(fradian) : 각도에 따른 +-변화 //10.f 곱하기는 범위 // Matrix44 m = global->localTm * mat; // g_pDevice->SetTransform(D3DTS_WORLD, (D3DXMATRIX*)&m); //마우스 회전 잠금 g_pDevice->SetTransform(D3DTS_WORLD, (D3DXMATRIX*)&mat); global->mesh3DText->DrawSubset( 0 ); Vector3 mainPos(-10, 0, -490); Vector3 targetLook = mat.GetPosition() - mainPos; //대상물체와 메인물체와의 거리 판단 targetLook.Normalize(); Quaternion quat; // mainPos.Normalize(); // quat.SetRotationArc(mainPos, targetLook); //첫번째 인자를 물체의 위치로 하니 회전하는 방향이 생각과는 다르게 표현된다...역시 고정된 방향이어야 되나보다... quat.SetRotationArc(Vector3(1,0,0), targetLook); //양의 x축방향으로 대상물체를 바라본다 Matrix44 qm = quat.GetMatrix(); mat.SetTranslate(mainPos); qm *= mat; g_pDevice->SetTransform(D3DTS_WORLD, (D3DXMATRIX*)&qm); global->mesh3DText->DrawSubset( 0 ); //랜더링 끝 g_pDevice->EndScene(); //랜더링이 끝났으면 랜더링된 내용 화면으로 전송 g_pDevice->Present( NULL, NULL, NULL, NULL ); } }