bool cViewer::OnInit() { DragAcceptFiles(m_hWnd, TRUE); ReadModelFile("../media/cube.dat", m_vtxBuff, m_idxBuff, m_texture); m_mtrl.InitWhite(); Vector4 color(1,1,1,1); m_light.Init( graphic::cLight::LIGHT_DIRECTIONAL, color * 0.4f, color, color * 0.6f, Vector3(1,0,0)); m_light.Bind(0); Matrix44 V; Vector3 dir = Vector3(0,0,0)-Vector3(0,0,-5); dir.Normalize(); V.SetView(Vector3(0,0,-500), dir, Vector3(0,1,0)); graphic::GetDevice()->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); const int WINSIZE_X = 1024; //초기 윈도우 가로 크기 const int WINSIZE_Y = 768; //초기 윈도우 세로 크기 Matrix44 proj; proj.SetProjection(D3DX_PI * 0.5f, (float)WINSIZE_X / (float) WINSIZE_Y, 1.f, 1000.0f) ; graphic::GetDevice()->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX*)&proj) ; graphic::GetDevice()->LightEnable ( 0, // 활성화/ 비활성화 하려는 광원 리스트 내의 요소 true); // true = 활성화 , false = 비활성화 return true; }
void UpdateCamera() { Vector3 dir = g_lookAtPos - g_camPos; dir.Normalize(); g_matView.SetView(g_camPos, dir, Vector3(0,1,0)); graphic::GetDevice()->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&g_matView); }
void cViewer::UpdateCamera() { Matrix44 V; Vector3 dir = m_lookAtPos - m_camPos; dir.Normalize(); V.SetView(m_camPos, dir, Vector3(0,1,0)); graphic::GetDevice()->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); }
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); }
bool InitVertexBuffer() { ReadModelFile("../../media/data2.dat", global->vb, global->vtxSize, global->ib, global->faceSize); CreateFaceNormalLine(global->vb, global->vtxSize, global->ib, global->faceSize, global->fnvb, global->faceNormalVtxSize); CreateVertexNormalLine(global->vb, global->vtxSize, global->ib, global->faceSize, global->vnvb, global->vertexNormalVtxSize); D3DXCreateTextureFromFileA(g_pDevice, "../../media/강소라2.jpg", &global->texture); ZeroMemory(&global->mtrl, sizeof(global->mtrl)); global->mtrl.Ambient = D3DXCOLOR(1,1,1,1); global->mtrl.Diffuse = D3DXCOLOR(1,1,1,1); global->mtrl.Specular = D3DXCOLOR(1,1,1,1); global->mtrl.Emissive = D3DXCOLOR(0,0,0,1); global->mtrl.Power = 32.f; D3DXCOLOR color(1,1,1,1); ZeroMemory(&global->light, sizeof(global->light)); global->light.Type = D3DLIGHT_DIRECTIONAL; global->light.Ambient = color * 0.4f; global->light.Diffuse = color; global->light.Specular = color * 0.6f; global->light.Direction = *(D3DXVECTOR3*)&Vector3(0,0,1); g_pDevice->SetLight(0, &global->light); // 광원 설정. Matrix44 V; Vector3 dir = Vector3(0,0,0)-Vector3(0,0,-500); dir.Normalize(); V.SetView(Vector3(0,0,-500), dir, Vector3(0,1,0)); g_pDevice->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); Matrix44 proj; proj.SetProjection(D3DX_PI * 0.5f, (float)WINSIZE_X / (float) WINSIZE_Y, 1.f, 1000.0f) ; g_pDevice->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX*)&proj) ; g_pDevice->LightEnable(0, true); //g_pDevice->SetRenderState(D3DRS_SPECULARENABLE, true); //g_pDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true) ; return true; }
bool InitVertexBuffer() { HRESULT hr = D3DXCreateFont( g_pDevice, // D3D device 0, // Height 0, // Width FW_BOLD, // Weight 1, // MipLevels, 0 = autogen mipmaps FALSE, // Italic DEFAULT_CHARSET, // 그냥 디폴트 OUT_DEFAULT_PRECIS, // 정밀도 DEFAULT_QUALITY, // 그냥 디폴트 DEFAULT_PITCH | FF_DONTCARE, // 디폴트 L"굴림", // pFaceName &global->font ); // ppFont if (FAILED(hr)) return false; if (FAILED(hr = D3DXCreateSprite(g_pDevice, &global->textSprite))) return false; CreateD3DXTextMesh(g_pDevice, &global->mesh3DText, "굴림", 0, FALSE, FALSE); Matrix44 V; Vector3 dir = Vector3(0,0,0)-Vector3(0,0,-500); dir.Normalize(); V.SetView(Vector3(0,0,-500), dir, Vector3(0,1,0)); g_pDevice->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); Matrix44 proj; proj.SetProjection(D3DX_PI * 0.5f, (float)WINSIZE_X / (float) WINSIZE_Y, 1.f, 1000.0f) ; g_pDevice->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX*)&proj) ; g_pDevice->LightEnable(0, true); //g_pDevice->SetRenderState(D3DRS_SPECULARENABLE, true); //g_pDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true) ; return true; }
bool InitVertexBuffer() { ReadModelFile("../../media/cube.dat", g_pVB, g_VtxSize, g_pIB, g_FaceSize); D3DXCreateTextureFromFileA(g_pDevice, "../../media/강소라2.jpg", &g_Texture); ZeroMemory(&g_Mtrl, sizeof(g_Mtrl)); g_Mtrl.Ambient = D3DXCOLOR(1,1,1,1); g_Mtrl.Diffuse = D3DXCOLOR(1,1,1,1); g_Mtrl.Specular = D3DXCOLOR(1,1,1,1); g_Mtrl.Emissive = D3DXCOLOR(0,0,0,1); g_Mtrl.Power = 0.f; D3DXCOLOR color(1,1,1,1); ZeroMemory(&g_Light, sizeof(g_Light)); g_Light.Type = D3DLIGHT_DIRECTIONAL; g_Light.Ambient = color * 0.4f; g_Light.Diffuse = color; g_Light.Specular = color * 0.6f; g_Light.Direction = *(D3DXVECTOR3*)&Vector3(0,0,1); Matrix44 V; Vector3 dir = Vector3(0,0,0)-Vector3(0,0,-5); dir.Normalize(); V.SetView(Vector3(0,0,-500), dir, Vector3(0,1,0)); g_pDevice->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); Matrix44 proj; proj.SetProjection(D3DX_PI * 0.5f, (float)WINSIZE_X / (float) WINSIZE_Y, 1.f, 1000.0f) ; g_pDevice->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX*)&proj) ; //g_pDevice->SetRenderState(D3DRS_CULLMODE, false); //g_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); g_pDevice->SetLight(0, &g_Light); // 광원 설정. g_pDevice->LightEnable( 0, // 활성화/ 비활성화 하려는 광원 리스트 내의 요소 true); // true = 활성화 , false = 비활성화 return true; }
bool InitVertexBuffer() { //D3DXCreateBox(g_pDevice, 2, 2, 2, &g_pMesh, 0); //D3DXCreateSphere(g_pDevice, 1, 10, 10, &g_pMesh, 0); //D3DXCreateCylinder(g_pDevice, 1, 1, 2, 10, 10, &g_pMesh, 0); D3DXCreateTeapot( g_pDevice, &g_pMesh, 0) ; //D3DXCreateTorus(g_pDevice, 0.5f, 2.f, 20, 20, &g_pMesh, 0) ; Matrix44 V; Vector3 dir = Vector3(0,0,0)-Vector3(0,0,-5); dir.Normalize(); V.SetView(Vector3(0,0,-5), dir, Vector3(0,1,0)); g_pDevice->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); Matrix44 proj; proj.SetProjection(D3DX_PI * 0.5f, (float)WINSIZE_X / (float) WINSIZE_Y, 1.f, 1000.0f) ; g_pDevice->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX*)&proj) ; g_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); return true; }
bool InitVertexBuffer() { ReadModelFile("../../media/vase.dat", global->vb, global->vtxSize, global->ib, global->faceSize); //재질2 ZeroMemory(&global->mtrl, sizeof(global->mtrl)); global->mtrl.Ambient = D3DXCOLOR(1,0,0,1); global->mtrl.Diffuse = D3DXCOLOR(1,0,0,1); global->mtrl.Specular = D3DXCOLOR(1,0,0,1); global->mtrl.Emissive = D3DXCOLOR(0,0,0,1); global->mtrl.Power = 0.f; //광원2 (스나이프에서 3으로 되어있다!) D3DXCOLOR color(1,1,1,1); //광원이니깐 일반적으로 흰색 ZeroMemory(&global->light, sizeof(global->light)); global->light.Type = D3DLIGHT_DIRECTIONAL; global->light.Ambient = color * 0.4f; global->light.Diffuse = color; global->light.Specular = color * 0.6f; global->light.Direction = *(D3DXVECTOR3*)&Vector3(1,0,0); //광원4 g_pDevice->SetLight(0, &global->light); // 광원 설정. Matrix44 V; Vector3 dir = Vector3(0,0,0)-Vector3(0,0,-500); dir.Normalize(); V.SetView(Vector3(0,0,-500), dir, Vector3(0,1,0)); g_pDevice->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); Matrix44 proj; proj.SetProjection(D3DX_PI * 0.5f, (float)WINSIZE_X / (float) WINSIZE_Y, 1.f, 1000.0f) ; g_pDevice->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX*)&proj) ; g_pDevice->LightEnable ( 0, // 활성화/ 비활성화 하려는 광원 리스트 내의 요소 true); // true = 활성화 , false = 비활성화 return true; }
bool InitVertexBuffer() { ZeroMemory(&g_Mtrl, sizeof(g_Mtrl)); g_Mtrl.Ambient = D3DXCOLOR(1,1,1,1); g_Mtrl.Diffuse = D3DXCOLOR(1,1,1,1); g_Mtrl.Specular = D3DXCOLOR(1,1,1,1); g_Mtrl.Emissive = D3DXCOLOR(0,0,0,1); g_Mtrl.Power = 0.f; D3DXCOLOR color(1,1,1,1); ZeroMemory(&g_Light, sizeof(g_Light)); g_Light.Type = D3DLIGHT_DIRECTIONAL; g_Light.Ambient = color * 0.4f; g_Light.Diffuse = color; g_Light.Specular = color * 0.6f; g_Light.Direction = *(D3DXVECTOR3*)&Vector3(0,-1,0); Matrix44 V; Vector3 camPos(0,200,-500); Vector3 lookAt(0, 0, 0); Vector3 dir = lookAt - camPos; dir.Normalize(); V.SetView(camPos, dir, Vector3(0,1,0)); g_pDevice->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); Matrix44 proj; proj.SetProjection(D3DX_PI * 0.5f, (float)WINSIZE_X / (float) WINSIZE_Y, 1.f, 1000.0f) ; g_pDevice->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX*)&proj) ; g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); g_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); g_pDevice->SetLight(0, &g_Light); // 광원 설정. g_pDevice->LightEnable( 0, // 활성화/ 비활성화 하려는 광원 리스트 내의 요소 true); // true = 활성화 , false = 비활성화 return true; }
void cGameApp::OnUpdate(const float elapseT) { _z += 3; Matrix44 V; Vector3 dir = Vector3(0,0,0)-Vector3(0,0,-5); dir.Normalize(); V.SetView(Vector3(0,0,-100 +_z ), dir, Vector3(0,1,0)); graphic::GetDevice()->SetTransform(D3DTS_VIEW, (D3DMATRIX*)&V); for(int i = 0; i < 5; i++) { if(_groundLocal[i]._43 + 130 < _local._43) { _groundLocal[i]._43 = _groundLocal[i]._43 + 1500; } } for(int i = 0; i < CONEMAX; i++) { if(_coneLocal[i]._43 + 50 < _local._43) { _coneLocal[i].SetTranslate(Vector3( (rand() % 300) - 150 , -70 , _coneLocal[i]._43 + (rand() % 200) + 800)); } } if(_jumpFlag == true) { _jumpY -= 0.5f; if(_jumpY == -8) { _jumpFlag = false; _jumpY = 8; } } }
bool InitVertexBuffer() { HRESULT hr; ID3DXBuffer *adjBuff = NULL; ID3DXBuffer *mtrlBuff = NULL; DWORD numMtrls = 0; // 메쉬 정보 로드 hr = D3DXLoadMeshFromXA( "../../media/bigship1.x", D3DXMESH_MANAGED, g_pDevice, &adjBuff, &mtrlBuff, 0, &numMtrls, &g_pMesh ); if (FAILED(hr)) return false; ///////////////////////////////////////////////////////////////////////////// // 매터리얼 설정 if( mtrlBuff && numMtrls > 0 ) { D3DXMATERIAL *mtrls = (D3DXMATERIAL*)mtrlBuff->GetBufferPointer(); for( int i=0; i < (int)numMtrls; ++i ) { mtrls[ i].MatD3D.Ambient = mtrls[ i].MatD3D.Diffuse; g_Mtrls.push_back( mtrls[ i].MatD3D ); if( mtrls[ i].pTextureFilename ) { IDirect3DTexture9 *tex = NULL; string filePath = "../../media/"; filePath += mtrls[ i].pTextureFilename; D3DXCreateTextureFromFileA( g_pDevice, filePath.c_str(), &tex ); g_Texture.push_back( tex ); } else { g_Texture.push_back( 0 ); } } } mtrlBuff->Release(); // 메쉬 최적화 hr = g_pMesh->OptimizeInplace( D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE, (DWORD*)adjBuff->GetBufferPointer(), (DWORD*)adjBuff->GetBufferPointer(), 0, 0 ); if (FAILED(hr)) return false; // 프로그레시브 메쉬 생성. hr = D3DXGeneratePMesh(g_pMesh, (DWORD*)adjBuff->GetBufferPointer(), 0, 0, 1, D3DXMESHSIMP_FACE, &g_pPMesh ); g_pMesh->Release(); g_pMesh = NULL; adjBuff->Release(); if (FAILED(hr)) return false; //DWORD maxFaces = g_pPMesh->GetMaxFaces(); //g_pPMesh->SetNumFaces( maxFaces ); D3DXCOLOR color(1,1,1,1); ZeroMemory(&g_Light, sizeof(g_Light)); g_Light.Type = D3DLIGHT_DIRECTIONAL; g_Light.Ambient = color * 0.4f; g_Light.Diffuse = color; g_Light.Specular = color * 0.6f; g_Light.Direction = *(D3DXVECTOR3*)&Vector3(0,-1,0); Matrix44 V; Vector3 camPos(0,0,-20); Vector3 lookAtPos(0,0,0); Vector3 dir = lookAtPos - camPos; dir.Normalize(); V.SetView(camPos, dir, Vector3(0,1,0)); g_pDevice->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); Matrix44 proj; proj.SetProjection(D3DX_PI * 0.5f, (float)WINSIZE_X / (float) WINSIZE_Y, 1.f, 1000.0f) ; g_pDevice->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX*)&proj) ; g_pDevice->SetLight(0, &g_Light); // 광원 설정. g_pDevice->LightEnable( 0, // 활성화/ 비활성화 하려는 광원 리스트 내의 요소 true); // true = 활성화 , false = 비활성화 g_pDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true); g_pDevice->SetRenderState(D3DRS_SPECULARENABLE, true); return true; }
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; }
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_SOFTWARERENDERER, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Y Z // | / // | / // -----------------------> X const float axisLineLength = 300.f; g_axises.reserve(8); g_axises.push_back( Vector3(0,0,0) ); g_axises.push_back( Vector3(axisLineLength,0,0) ); // x-axis g_axises.push_back( Vector3(0,0,0) ); g_axises.push_back( Vector3(0,axisLineLength,0) ); // y-axis g_axises.push_back( Vector3(0,0,0) ); g_axises.push_back( Vector3(0,0,axisLineLength) ); // z-axis // 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 = 30.f; g_vertices.reserve(128); g_vertices.push_back( Vector3(-w,w,w) ); g_vertices.push_back( Vector3(w,w,w) ); g_vertices.push_back( Vector3(w,w,-w) ); g_vertices.push_back( Vector3(-w,w,-w) ); g_vertices.push_back( Vector3(-w,-w,w) ); g_vertices.push_back( Vector3(w,-w,w) ); g_vertices.push_back( Vector3(w,-w,-w) ); g_vertices.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_matWorld.SetIdentity(); g_matWorld.Translate(Vector3(0,0,0)); g_matLocal.SetIdentity(); g_matView.SetIdentity(); 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 ); const float width = 800.f; const float height = 600.f; g_matViewPort.SetIdentity(); g_matViewPort._11 = width/2; g_matViewPort._22 = -height/2; g_matViewPort._33 = 0; g_matViewPort._41 = width/2; g_matViewPort._42 = height/2; g_matViewPort._43 = 0; if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } MSG msg; HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOFTWARERENDERER)); 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 > 15) { oldT = curT; MainLoop(elapseT); } } return (int) msg.wParam; }
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch (wmId) { case IDM_ABOUT: break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); Paint(hWnd, hdc); EndPaint(hWnd, &ps); break; case WM_ERASEBKGND: return 1; case WM_KEYDOWN: switch (wParam) { case VK_TAB: g_WireFrame = !g_WireFrame; break; case VK_UP: case VK_DOWN: { Matrix44 mat; mat.SetRotationX((wParam==VK_UP)? 0.1f : -0.1f); g_matLocal *= mat; } break; case VK_LEFT: case VK_RIGHT: { Matrix44 mat; mat.SetRotationY((wParam==VK_LEFT)? 0.1f : -0.1f); g_matLocal *= mat; } break; case 'W': case 'S': { Vector3 dir = g_cameraLookat - g_cameraPos; dir.Normalize(); const bool isForward = 'W' == wParam; g_cameraPos += dir * (isForward? 10.f : -10.f); g_cameraLookat += dir * (isForward? 10.f : -10.f); Vector3 dir2 = g_cameraLookat - g_cameraPos; dir2.Normalize(); g_matView.SetView(g_cameraPos, dir2, Vector3(0,1,0)); } break; case 'A': case 'D': { Vector3 dir = g_cameraLookat - g_cameraPos; dir.Normalize(); Vector3 right = Vector3(0,1,0).CrossProduct(dir); right.Normalize(); const bool isRight = 'A' == wParam; g_cameraPos += right * (isRight? 10.f : -10.f); g_cameraLookat += right * (isRight? 10.f : -10.f); Vector3 dir2 = g_cameraLookat - g_cameraPos; dir2.Normalize(); g_matView.SetView(g_cameraPos, dir2, Vector3(0,1,0)); } break; case 'E': case 'C': { Matrix44 mat; mat.SetRotationY((wParam=='E')? 0.1f : -0.1f); g_cameraPos = g_cameraPos * mat; Vector3 dir2 = g_cameraLookat - g_cameraPos; dir2.Normalize(); g_matView.SetView(g_cameraPos, dir2, Vector3(0,1,0)); } break; case VK_ESCAPE: DestroyWindow(hWnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
// // 윈도우 프로시져 함수 ( 메시지 큐에서 받아온 메시지를 처리한다 ) // LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps; HDC hdc; switch (msg) { case WM_ERASEBKGND: return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); Paint(hWnd, hdc); EndPaint(hWnd, &ps); break; case WM_KEYDOWN: switch (wParam) { case VK_ESCAPE: ::DestroyWindow(hWnd); break; case 'D': case 'A': { g_cameraPos.x += (wParam=='D')? 100.f : -100.f; Vector3 dir = g_cameraLookat - g_cameraPos; dir.Normalize(); g_matView.SetView(g_cameraPos, dir, Vector3(0,1,0)); } break; case 'W': case 'S': { g_cameraPos.z += (wParam=='W')? 100.f : -100.f; Vector3 dir = g_cameraLookat - g_cameraPos; dir.Normalize(); g_matView.SetView(g_cameraPos, dir, Vector3(0,1,0)); } break; case VK_UP: case VK_DOWN: { Matrix44 mat; mat.SetRotationX((wParam==VK_UP)? 0.1f : -0.1f); g_matLocal1 *= mat; } break; case VK_LEFT: case VK_RIGHT: { Matrix44 mat; mat.SetRotationY((wParam==VK_LEFT)? 0.1f : -0.1f); g_matLocal1 *= mat; } break; } break; case WM_DESTROY: //윈도우가 파괴된다면.. PostQuitMessage(0); //프로그램 종료 요청 ( 메시지 루프를 빠져나가게 된다 ) break; } return DefWindowProc( hWnd, msg, wParam, lParam ); }