Raw Raw heightmap 1 bool Terrain read Raw
높이맵 • 높이맵 만들기 – 포토샾 등을 이용, Raw 파일 생성 • Raw 파일 로딩하기(높이맵) : _heightmap는 1차원 배열 bool Terrain: : read. Raw. File(std: : string file. Name) { std: : vector<BYTE> in( _num. Vertices ); std: : ifstream in. File(file. Name. c_str(), std: : ios_base: : binary); if( in. File == 0 ) return false; in. File. read((char*)&in[0], in. size()); // number of bytes to read into buffer in. File. close(); // copy BYTE vector to int vector _heightmap. resize( _num. Vertices ); for(int i = 0; i < in. size(); i++) _heightmap[i] = in[i]; return true; } 게임 프로그래밍-I 강의노트 © 2006 한성대학교 멀티미디어공학과 4
높이맵 • 높이맵 접근과 수정 int Terrain: : get. Heightmap. Entry(int row, int col) { // 1차원 배열을 2차원 배열로 생각하기 return _heightmap[row * _num. Verts. Per. Row + col]; } void Terrain: : set. Heightmap. Entry(int row, int col, int value) { _heightmap[row * _num. Verts. Per. Row + col] = value; } 255 150 100 50 70 100 100 200 100 70 150 100 70 100 _num. Verts. Per. Row 게임 프로그래밍-I 강의노트 © 2006 한성대학교 멀티미디어공학과 5
지형 기하정보(버텍스) 생성하기 • 필요한 데이터 – – – – _num. Verts. Per. Row; // row당 버텍스의 수 _num. Verts. Per. Col; // col당 버텍스 수 _num. Cells. Per. Row; // _num. Verts. Per. Row-1 _num. Cells. Per. Col; // _num. Verts. Per. Col-1 _width; // 넓이 _depth; // 깊이 _num. Vertices; // _num. Verts. Per. Row* _num. Verts. Per. Col _num. Triangles; // _num. Verts. Per. Row* _num. Verts. Per. Col*2 게임 프로그래밍-I 강의노트 © 2006 한성대학교 멀티미디어공학과 7
지형 기하정보(버텍스) 생성하기 • 필요한 데이터: Example – – – – _num. Verts. Per. Row 4 _num. Verts. Per. Col 4 _num. Cells. Per. Row 4 -1 _num. Cells. Per. Col 4 -1 _width; // 정하기 나름 _depth; // 정하기 나름 _num. Vertices 16(4*4) _num. Triangles 32 게임 프로그래밍-I 강의노트 © 2006 한성대학교 멀티미디어공학과 255 0 1 2 3 100 3 8
지형 기하정보(버텍스) 생성하기 • 버텍스의 구조: 6장 참조 struct Terrain. Vertex { Terrain. Vertex(){} Terrain. Vertex(float x, float y, float z, float u, float v) { _x = x; _y = y; _z = z; _u = u; _v = v; } float _x, _y, _z; float _u, _v; static const DWORD FVF; }; 게임 프로그래밍-I 강의노트 © 2006 한성대학교 멀티미디어공학과 9
지형 기하정보(버텍스) 생성하기 • 버텍스 계산하기 i=0, J=0 start. Z= d/2 start. X= -w/2 hr = _device->Create. Vertex. Buffer(. . . ); int start. X = -_width / 2; int start. Z = _depth / 2; int end. X = _width / 2; int end. Z = -_depth / 2; float u. Coord = 1. 0 f / (float)_num. Cells. Per. Row; float v. Coord = 1. 0 f / (float)_num. Cells. Per. Col; Terrain. Vertex* v = 0; _vb->Lock(0, 0, (void**)&v, 0); i= num. Verts. Per. Row-1, int i = 0; J= _num. Verts. Per. Col-1 for(int z = start. Z; z >= end. Z; z -= _cell. Spacing, i++){ int j = 0; for(int x = start. X; x <= end. X; x += _cell. Spacing, j++){ +Z int index = i * _num. Verts. Per. Row + j; v[index] = Terrain. Vertex( (float)x, (float)_heightmap[index], +X (float)z, (float)j * u. Coord , end. Z= -d/2 (float)i * v. Coord ); end. X= w/2 } } 게임 프로그래밍-I 강의노트 © 2006 한성대학교 멀티미디어공학과 10
지형 기하정보(버텍스) 생성하기 • 인덱스 계산, 삼각형 정의하기 – 인덱스를 사용 한다, why? i=0, J=0 1 2 3 j col I row 6 i= num. Verts. Per. Row-1, J= _num. Verts. Per. Col-1 C A B D A: i*num. Verts. Per. Row+j start. Z= d/2 start. X= -w/2 +Z B: i*num. Verts. Per. Row+j+1 C: (i+1)*num. Verts. Per. Row+j +X D: (i+1)*num. Verts. Per. Row+j+1 end. Z= -d/2 end. X= w/2 게임 프로그래밍-I 강의노트 © 2006 한성대학교 멀티미디어공학과 11
지형 기하정보(버텍스) 생성하기 • 인덱스 계산, 삼각형 정의하기 bool Terrain: : compute. Indices(){ HRESULT hr = 0; hr = _device->Create. Index. Buffer( if(FAILED(hr)) return false; …); WORD* indices = 0; _ib->Lock(0, 0, (void**)&indices, 0); int base. Index = 0; // loop through and compute the triangles of each quad for(int i = 0; i < _num. Cells. Per. Col; i++) { for(int j = 0; j < _num. Cells. Per. Row; j++) { indices[base. Index] = i * _num. Verts. Per. Row + j; indices[base. Index + 1] = i * _num. Verts. Per. Row + j + 1; indices[base. Index + 2] = (i+1) * _num. Verts. Per. Row + j; indices[base. Index + 3] = (i+1) * _num. Verts. Per. Row + j; indices[base. Index + 4] = i * _num. Verts. Per. Row + j + 1; indices[base. Index + 5] = (i+1) * _num. Verts. Per. Row + j + 1; base. Index += 6; } } _ib->Unlock(); return true; 게임 프로그래밍-I 강의노트 } © 2006 한성대학교 멀티미디어공학과 12
텍스처링 • 절차적 방식 – 높이가 높으면 밝은 색으로 텍스처링 – 높이가 낮으면 어두운 색으로 텍스처링 사각형의 좌측 상단의 높이로 텍스처링 컬러 선택 int tex. Width = _num. Cells. Per. Row; int tex. Height = _num. Cells. Per. Col; DWORD* image. Data = (DWORD*)locked. Rect. p. Bits; for(int i = 0; i < tex. Height; i++) { for(int j = 0; j < tex. Width; j++) { D 3 DXCOLOR c; float height = (float)get. Heightmap. Entry(i, j) / _height. Scale; if( (height) < 42. 5 f ) c = d 3 d: : BEACH_SAND; else if( (height) < 85. 0 f ) c = d 3 d: : LIGHT_YELLOW_GREEN; else if( (height) < 127. 5 f ) c = d 3 d: : PUREGREEN; else if( (height) < 170. 0 f ) c = d 3 d: : DARK_YELLOW_GREEN; else if( (height) < 212. 5 f ) c = d 3 d: : DARKBROWN; else c = d 3 d: : WHITE; image. Data[i * locked. Rect. Pitch / 4 + j] = (D 3 DCOLOR)c; } 게임 프로그래밍-I 강의노트 } © 2006 한성대학교 멀티미디어공학과 13
조명 • 지형에 음영 입히기 bool Terrain: : light. Terrain(D 3 DXVECTOR 3* direction. To. Light){ HRESULT hr = 0; D 3 DSURFACE_DESC texture. Desc; _tex->Get. Level. Desc(0 /*level*/, &texture. Desc); if( texture. Desc. Format != D 3 DFMT_X 8 R 8 G 8 B 8 ) return false; D 3 DLOCKED_RECT locked. Rect; _tex->Lock. Rect(…); DWORD* image. Data = (DWORD*)locked. Rect. p. Bits; for(int i = 0; i < texture. Desc. Height; i++){ for(int j = 0; j < texture. Desc. Width; j++){ int index = i * locked. Rect. Pitch / 4 + j; D 3 DXCOLOR c( image. Data[index] ); c *= compute. Shade(i, j, direction. To. Light); ; image. Data[index] = (D 3 DCOLOR)c; } } _tex->Unlock. Rect(0); return true; } 게임 프로그래밍-I 강의노트 © 2006 한성대학교 멀티미디어공학과 17
- Slides: 23