41 static bool illuminating =
false;
47 : ndetail(0), terrain(terr), rect(r), water(0), min_height(1e9), max_height(-1e9)
62 loc = (p1 + p2) * 0.5;
73 int ty =
rect.
y + i * tscale;
78 if (ty > patch->
Height()-1)
82 int tx =
rect.
x + (PATCH_SIZE-1 - j) * tscale;
87 if (tx > patch->
Width()-1)
88 tx = patch->
Width()-1;
91 float alt = (float) (red *
mtnscale);
115 for (i = 0; i < layers.
size(); i++) {
120 if (i < layers.
size()-1)
126 mtl =
new(__FILE__,__LINE__)
Material;
158 : ndetail(0), terrain(terr), rect(r), water(0)
173 loc = (p1 + p2) * 0.5;
184 *pHeight++ = (float) sea_level;
212 for (
int i = 0; i <
MAX_LOD; i++) {
236 static int mcomp(
const void* a,
const void* b)
251 static void bisect(
VertexSet* vset,
int v[4])
253 double d1 = fabs(vset->
loc[ v[0] ].
y -
254 vset->
loc[ v[3] ].
y);
256 double d2 = fabs(vset->
loc[ v[1] ].
y -
257 vset->
loc[ v[2] ].
y);
260 int odd[4] = { v[1], v[3], v[0], v[2] };
261 for (
int i = 0; i < 4; i++)
271 int detail_size = 1 << level;
272 int ds1 = detail_size+1;
283 const int NUM_STRIPS = 4;
285 const int NUM_INDICES_QUAD = 6;
287 int nverts = ds1*ds1 + ds1*2*NUM_STRIPS;
288 int npolys = detail_size*detail_size*2;
289 int strip_len = detail_size;
290 int total = npolys + strip_len*NUM_STRIPS;
305 s->
AddIndices(npolys*NUM_INDICES_TRI + strip_len*NUM_STRIPS*NUM_INDICES_QUAD);
311 ZeroMemory(vset->
loc, nverts *
sizeof(
Vec3));
312 ZeroMemory(vset->
diffuse, nverts *
sizeof(DWORD));
313 ZeroMemory(vset->
specular, nverts *
sizeof(DWORD));
314 ZeroMemory(vset->
tu, nverts *
sizeof(
float));
315 ZeroMemory(vset->
tv, nverts *
sizeof(
float));
317 ZeroMemory(vset->
tu1, nverts *
sizeof(
float));
318 ZeroMemory(vset->
tv1, nverts *
sizeof(
float));
320 ZeroMemory(vset->
rw, nverts *
sizeof(
float));
324 float* pTu = vset->
tu;
325 float* pTv = vset->
tv;
326 float* pTu1 = vset->
tu1;
327 float* pTv1 = vset->
tv1;
331 double dt = 0.0625 / (ds1-1);
332 double dtt = 2.0000 / (ds1-1);
333 double tu0 = (double)
rect.
x /
rect.
w / 16.0 + 1.0/16.0;
337 for (i = 0; i < ds1; i++) {
338 for (j = 0; j < ds1; j++) {
344 *pTu++ = (float) (-j*dtt);
345 *pTv++ = (float) ( i*dtt);
347 if (level >= 4 && !
water) {
348 *pTu1++ = (float) (-j*dtt*3);
349 *pTv1++ = (float) ( i*dtt*3);
356 *pTu++ = (float) (tu0 - j*dt);
357 *pTv++ = (float) (tv0 + i*dt);
366 for (i = 0; i < ds1; i += detail_size) {
367 for (j = 0; j < ds1; j++) {
380 *pTu++ = (float) (-j*dtt);
381 *pTv++ = (float) ( i*dtt);
385 *pTu++ = (float) (tu0 - j*dt);
386 *pTv++ = (float) (tv0 + i*dt);
395 *pTu++ = (float) (-j*dtt);
396 *pTv++ = (float) ( i*dtt);
400 *pTu++ = (float) (tu0 - j*dt);
401 *pTv++ = (float) (tv0 + i*dt);
407 for (j = 0; j < ds1; j += detail_size) {
408 for (i = 0; i < ds1; i++) {
421 *pTu++ = (float) (-j*dtt);
422 *pTv++ = (float) ( i*dtt);
426 *pTu++ = (float) (tu0 - j*dt);
427 *pTv++ = (float) (tv0 + i*dt);
436 *pTu++ = (float) (-j*dtt);
437 *pTv++ = (float) ( i*dtt);
441 *pTu++ = (float) (tu0 - j*dt);
442 *pTv++ = (float) (tv0 + i*dt);
451 for (i = 0; i < npolys; i++) {
459 if (level >= 2 && !
water) {
465 for (i = npolys; i < total; i++) {
477 for (i = 0; i < detail_size; i++) {
478 for (j = 0; j < detail_size; j++) {
481 (ds1 * (i ) + (j+1)),
482 (ds1 * (i+1) + (j )),
483 (ds1 * (i+1) + (j+1)) };
493 if (level >= 2 && !
water) {
505 if (level >= 2 && !
water) {
516 for (i = 0; i < NUM_STRIPS; i++) {
518 int base_index = ds1*ds1 + ds1*2*i;
520 for (j = 0; j < strip_len; j++) {
521 int v = base_index + j * 2;
524 if (i == 1 || i == 2) {
550 for (i = 0; i < total; i++) {
570 for (
int n = 0; n < s->
NumPolys(); n++) {
579 segment =
new(__FILE__,__LINE__)
Segment;
582 segment->
polys = &spolys[n];
597 for (i = 0; i < ds1; i++) {
598 for (j = 0; j < ds1; j++) {
605 else if (dscale > 1) {
613 for (
int dy = -dscale/2; dy < dscale/2; dy += step) {
614 for (
int dx = -dscale/2; dx < dscale/2; dx += step) {
615 int ix =
rect.
x + (ds1-1-j)*dscale + dx;
616 int iy =
rect.
y + i*dscale + dy;
624 normal +=
Point((128-vbn.
x)/127.0, (vbn.
z-128)/127.0, (vbn.
y-128)/127.0);
629 vset->
nrm[i*ds1+j] = normal;
635 Point normal =
Point((128-vbn.
x)/127.0, (vbn.
z-128)/127.0, (vbn.
y-128)/127.0);
636 vset->
nrm[i*ds1+j] = normal;
642 pVert = &vset->
nrm[ds1*ds1];
645 for (i = 0; i < ds1; i += detail_size) {
646 for (j = 0; j < ds1; j++) {
647 Vec3 vn = vset->
nrm[i*ds1 + j];
655 for (j = 0; j < ds1; j += detail_size) {
656 for (i = 0; i < ds1; i++) {
657 Vec3 vn = vset->
nrm[i*ds1 + j];
687 else if (scale > 0.8)
690 scale = (scale - 0.2) / 0.6;
696 DWORD val = (DWORD) (scale*255);
715 for (
int i = 0; i < poly->
nverts; i++) {
718 if (h >= 0 && h < y) {
723 if (y <= terrain->
GetLayers().first()->GetMinHeight())
747 for (
int i = 0; i < vset->
nverts; i++)
748 vset->
loc[i].
y = 0.0f;
815 int nverts = vset->
nverts;
816 DWORD aval = ambient.
Value();
818 for (
int i = 0; i < nverts; i++) {
826 if (trgn && !first) {
850 for (
int i = 0; i < nverts; i++) {
853 double gain = vl * nrm;
856 val = light->
Intensity() * (0.85 * gain);
870 for (
int i = 0; i < nverts; i++) {
880 illuminating =
false;
922 ZeroMemory(details,
sizeof(details));
972 Point light_pos = obj_pos + dir * len;
973 int impact = light_pos.
y < -100;
978 if (illuminating || impact) {
982 if (obj_pos.
x != 0 || obj_pos.
y != 0 || obj_pos.
z != 0) {
992 double dlen = d1.
length();
1002 if (closest * dir < 0)
1021 dir.
x = tmp *
Vec3(xform(0,0), xform(0,1), xform(0,2));
1022 dir.
y = tmp * Vec3(xform(1,0), xform(1,1), xform(1,2));
1023 dir.
z = tmp * Vec3(xform(2,0), xform(2,1), xform(2,2));
1027 obj_pos.
x = tmp * Vec3(xform(0,0), xform(0,1), xform(0,2));
1028 obj_pos.
y = tmp * Vec3(xform(1,0), xform(1,1), xform(1,2));
1029 obj_pos.
z = tmp * Vec3(xform(2,0), xform(2,1), xform(2,2));
1031 double min = 2 * len;
1036 for (
int i = 0; i < s->
NumPolys(); i++) {
1040 double denom = dir*v;
1042 if (denom < -1.0e-5) {
1044 double ilen = ((P-obj_pos)*v)/denom;
1046 if (ilen > 0 && ilen < min) {
1047 Point intersect = obj_pos + dir * ilen;
1084 if (x0 >= 0 && x0 < PATCH_SIZE && z0 >= 0 && z0 <
PATCH_SIZE) {
1096 if (h[0] == h[1] && h[1] == h[2] && h[2] == h[3]) {
1102 double hl = h[0]*(1-dz) + h[1]*dz;
1103 double hr = h[2]*(1-dz) + h[3]*dz;
1105 height = hl*(1-dx) + hr*dx + 5;