NX_INLINE CVoid setupColor(NxU32 color) { NxF32 Blue = NxF32((color)&0xff)/255.0f; NxF32 Green = NxF32((color>>8)&0xff)/255.0f; NxF32 Red = NxF32((color>>16)&0xff)/255.0f; glColor3f(Red, Green, Blue); }
void DrawEllipse(NxU32 nbSegments, const NxMat34& matrix, const NxVec3& color, const NxF32 radius1, const NxF32 radius2, const bool semicircle) { NxF32 step = NxTwoPiF32/NxF32(nbSegments); NxU32 segs = nbSegments; if(semicircle) { segs /= 2; } for(NxU32 i=0;i<segs;i++) { NxU32 j=i+1; if(j==nbSegments) j=0; NxF32 angle0 = NxF32(i)*step; NxF32 angle1 = NxF32(j)*step; NxVec3 p0,p1; matrix.multiply(NxVec3(radius1 * sinf(angle0), radius2 * cosf(angle0), 0.0f), p0); matrix.multiply(NxVec3(radius1 * sinf(angle1), radius2 * cosf(angle1), 0.0f), p1); DrawLine(p0, p1, color); } }
bool MapPal::Load(const char *name) { FILE *fph; char string[256]; #define RATIO (1.0f/256.0f) fph = fopen(name, "rb"); if ( !fph ) return false; ColVector list; for (NxI32 i=0; i<256; i++) { bool eof = readline(fph,string); if (eof ) break; NxI32 red,green,blue; if ( *string != '#' ) { NxI32 count = sscanf(string,"%d %d %d",&red,&green,&blue); if ( count == 3 ) { Col c; c.r = NxU8(red); c.g = NxU8(green); c.b = NxU8(blue); list.push_back(c); } } } fclose(fph); { NxI32 size = list.size(); if ( !size ) return false; for (NxI32 i=0; i<SMOOTH_SIZE; i++) { // ok, compute the index... NxI32 scount = size-1; NxI32 iloc1 = (scount*i) / SMOOTH_SIZE; NxI32 iloc2 = iloc1+1; // current color... // next color.. // now slerp between them... NxF32 f0 = NxF32(i); NxF32 f1 = NxF32(SMOOTH_SIZE*(iloc1)) / NxF32(scount); NxF32 f2 = NxF32(SMOOTH_SIZE*(iloc2)) / NxF32(scount); if ( iloc2 >= size ) iloc2 = size-1; NxF32 r1,g1,b1; NxF32 r2,g2,b2; r1 = NxF32( list[iloc1].r ) * RATIO; g1 = NxF32( list[iloc1].g ) * RATIO; b1 = NxF32( list[iloc1].b ) * RATIO; r2 = NxF32( list[iloc2].r ) * RATIO; g2 = NxF32( list[iloc2].g ) * RATIO; b2 = NxF32( list[iloc2].b ) * RATIO; // slerp along f1-f2 f0-=f1; f1 = f2-f1; NxF32 recip1 = f0 / f1; NxF32 recip2 = 1.0f - recip1; NxF32 r = r1*recip2 + r2*recip1; NxF32 g = g1*recip2 + g2*recip1; NxF32 b = b1*recip2 + b2*recip1; NxI32 index = i*3; mEntries[index+0] = r; mEntries[index+1] = g; mEntries[index+2] = b; } } return true; }
void TerrainData::init(NxU32 s, NxF32 o, NxF32 w, NxF32 c, bool flat, const NxVec3* pos) { release(); size = s; offset = o; width = w; chaos = c; nbVerts = size*size; nbFaces = (size-1)*(size-1)*2; //////// // Initialize terrain vertices verts = new NxVec3[nbVerts]; for(NxU32 y=0;y<size;y++) { for(NxU32 x=0;x<size;x++) { verts[x+y*size] = NxVec3(NxF32(x)-(NxF32(size-1)*0.5f), 0.0f, NxF32(y)-(NxF32(size-1)*0.5f)) * width; } } // Initialize terrain colors { colors = new NxVec3[nbVerts]; for(NxU32 y=0;y<size;y++) { for(NxU32 x=0;x<size;x++) { colors[x+y*size] = NxVec3(0.5f, 0.4f, 0.2f); } } } // Initialize terrain faces faces = new NxU32[nbFaces*3]; NxU32 k = 0; for(NxU32 j=0;j<size-1;j++) { for(NxU32 i=0;i<size-1;i++) { // Create first triangle faces[k++] = i + j*size; faces[k++] = i + (j+1)*size; faces[k++] = i+1 + (j+1)*size; // Create second triangle faces[k++] = i + j*size; faces[k++] = i+1 + (j+1)*size; faces[k++] = i+1 + j*size; } } struct Local { static void _Compute(bool* done, NxVec3* field, NxU32 x0, NxU32 y0, NxU32 currentSize, NxF32 value, NxU32 initSize) { // Compute new size currentSize>>=1; if(!currentSize) return; // Compute new heights NxF32 v0 = (value * NxF32(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); NxF32 v1 = (value * NxF32(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); NxF32 v2 = (value * NxF32(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); NxF32 v3 = (value * NxF32(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); NxF32 v4 = (value * NxF32(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); NxU32 x1 = (x0+currentSize) % initSize; NxU32 x2 = (x0+currentSize+currentSize) % initSize; NxU32 y1 = (y0+currentSize) % initSize; NxU32 y2 = (y0+currentSize+currentSize) % initSize; if(!done[x1 + y0*initSize]) field[x1 + y0*initSize].y = v0 + 0.5f * (field[x0 + y0*initSize].y + field[x2 + y0*initSize].y); if(!done[x0 + y1*initSize]) field[x0 + y1*initSize].y = v1 + 0.5f * (field[x0 + y0*initSize].y + field[x0 + y2*initSize].y); if(!done[x2 + y1*initSize]) field[x2 + y1*initSize].y = v2 + 0.5f * (field[x2 + y0*initSize].y + field[x2 + y2*initSize].y); if(!done[x1 + y2*initSize]) field[x1 + y2*initSize].y = v3 + 0.5f * (field[x0 + y2*initSize].y + field[x2 + y2*initSize].y); if(!done[x1 + y1*initSize]) field[x1 + y1*initSize].y = v4 + 0.5f * (field[x0 + y1*initSize].y + field[x2 + y1*initSize].y); done[x1 + y0*initSize] = true; done[x0 + y1*initSize] = true; done[x2 + y1*initSize] = true; done[x1 + y2*initSize] = true; done[x1 + y1*initSize] = true; // Recurse through 4 corners value *= 0.5f; _Compute(done, field, x0, y0, currentSize, value, initSize); _Compute(done, field, x0, y1, currentSize, value, initSize); _Compute(done, field, x1, y0, currentSize, value, initSize); _Compute(done, field, x1, y1, currentSize, value, initSize); } };