//--------------------------------------- void Frustum::SetCamera( const Vec3f& p, const Vec3f& d, const Vec3f& u ) { Vec3f nc, fc, x, y, z; z = p - d; z.Normalize(); x = u.Cross( z ); x.Normalize(); y = z.Cross( x ); nc = p - z * mNearDist; fc = p - z * mFarDist; mNearPoints[0] = nc + y * mNearH - x * mNearW; mNearPoints[1] = nc + y * mNearH + x * mNearW; mNearPoints[2] = nc - y * mNearH - x * mNearW; mNearPoints[3] = nc - y * mNearH + x * mNearW; mFarPoints[0] = fc + y * mFarH - x * mFarW; mFarPoints[1] = fc + y * mFarH + x * mFarW; mFarPoints[2] = fc - y * mFarH - x * mFarW; mFarPoints[3] = fc - y * mFarH + x * mFarW; mPlanes[ FP_TOP ] = Planef( mNearPoints[1], mNearPoints[0], mFarPoints[0] ); mPlanes[ FP_BOTTOM ] = Planef( mNearPoints[2], mNearPoints[3], mFarPoints[3] ); mPlanes[ FP_LEFT ] = Planef( mNearPoints[0], mNearPoints[2], mFarPoints[2] ); mPlanes[ FP_RIGHT ] = Planef( mNearPoints[3], mNearPoints[1], mFarPoints[3] ); mPlanes[ FP_NEAR ] = Planef( mNearPoints[0], mNearPoints[1], mNearPoints[3] ); mPlanes[ FP_FAR ] = Planef( mFarPoints[1], mFarPoints[0], mFarPoints[2] ); }
void QRenderOutputWidget::Pan(float DownDegrees, float RightDegrees) { Vec3f LoS = FocalPoint - Position; Vec3f right = LoS.Cross(ViewUp); Vec3f orthogUp = LoS.Cross(right); right.Normalize(); orthogUp.Normalize(); const float Length = (FocalPoint - Position).Length(); const unsigned int WindowWidth = this->Image.Width(); const float U = Length * (RightDegrees / WindowWidth); const float V = Length * (DownDegrees / WindowWidth); Position = Position + right * U - ViewUp * V; FocalPoint = FocalPoint + right * U - ViewUp * V; }
void QRenderOutputWidget::Orbit(float DownDegrees, float RightDegrees) { Vec3f ReverseLoS = Position - FocalPoint; Vec3f right = ViewUp.Cross(ReverseLoS); Vec3f orthogUp = ReverseLoS.Cross(right); Vec3f Up = Vec3f(0.0f, 1.0f, 0.0f); ReverseLoS.RotateAxis(right, DownDegrees); ReverseLoS.RotateAxis(Up, RightDegrees); ViewUp.RotateAxis(right, DownDegrees); ViewUp.RotateAxis(Up, RightDegrees); Position = ReverseLoS + FocalPoint; }
void updateOrientation() { if ( orientationDirty == false ) { return; } orientationDirty = false; float decay = 0.9f; float hyst = cos( ToRadians( app_orientationHysteresis.GetVal() ) ); if ( GotCompassUpdate ) { if ( headingSmooth.Dot( heading ) < hyst ) { headingSmooth *= decay; headingSmooth += (1.0f - decay) * heading; } headingSmooth.Normalize(); } if ( accelSmooth.Dot( accel ) < hyst ) { accelSmooth *= decay; accelSmooth += (1.0f - decay) * accel; } accelSmooth.Normalize(); Vec3f up = -accelSmooth; up.Normalize(); Vec3f north = headingSmooth; north -= up * up.Dot( north ); north.Normalize(); Matrix3f toTrueNorth = Rotationf( up, ToRadians( trueHeadingDiff ) ).GetMatrix3(); north = toTrueNorth * north; Vec3f east = north.Cross( up ); Matrix4f o; o.SetRow( 0, Vec4f( east.x, east.y, east.z, 0.0f ) ); o.SetRow( 1, Vec4f( north.x, north.y, north.z, 0.0f ) ); o.SetRow( 2, Vec4f( up.x, up.y, up.z, 0.0f ) ); o.SetRow( 3, Vec4f( 0.0, 0.0, 0.0, 1.0f ) ); platformOrientation = o.Transpose(); }
Terrain::Terrain(void) { _width = 1024; _height = 1024; ifstream textFile("height.dat"); if(!textFile.is_open()) { cerr << "Error whilst reading heightmap" << endl; EndGame(1); } else { for(int z = 0; z < 1024; z++) { getline(textFile, _line); if(_line.empty()) { break; } for(int x = 0; x < 1024; x++) { pos = _line.find(' '); if(pos == string::npos) { break; } string _line2(_line, 0, pos); stringstream strh(_line2); strh >> _heightData[z][x]; _line.erase(0,pos+1); } } textFile.close(); } glGenTextures(1, &_tex); glBindTexture(GL_TEXTURE_2D, _tex); // GL_MODULATE for correct lighting and shit, not really needed because of shaders. // When the texture area is small, bilinear filter the closest mipmap. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); // When texture area is large, bilinear filter the original. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); if(glfwLoadTexture2D("terrain1.tga", GLFW_BUILD_MIPMAPS_BIT) != GL_TRUE) { cerr << "Error while reading the terrain texture!" << endl; EndGame(1); } // Calculate the normals. _normals = new Vec3f*[_height]; Vec3f** normals2 = new Vec3f*[_height]; for(int i = 0; i < _height; i++) { _normals[i] = new Vec3f[_width]; normals2[i] = new Vec3f[_width]; } // Rough normals. for(int z = 0; z < _height; z++) { for(int x = 0; x < _width; x++) { Vec3f sum(0.0, 0.0, 0.0); Vec3f down; if(z > 0) { down = Vec3f(0.0, GetHeight(x, z-1) - GetHeight(x, z), -1.0); } Vec3f up; if(z < _height-1) { up = Vec3f(0.0, GetHeight(x, z+1) - GetHeight(x,z), 1.0); } Vec3f left; if(x > 0) { left = Vec3f(-1.0, GetHeight(x-1, z) - GetHeight(x,z), 0.0); } Vec3f right; if(x < _width-1) { right = Vec3f(1.0, GetHeight(x+1, z) - GetHeight(x,z), 0.0); } if(x > 0 && z > 0) { sum += down.Cross(left).Normalize(); } if(x > 0 && z < _height-1) { sum += left.Cross(up).Normalize(); } if(x < _width-1 && z < _height-1) { sum += up.Cross(right).Normalize(); } if(x < _width-1 && z > 0) { sum += right.Cross(down).Normalize(); } normals2[x][z] = sum; } } // Smooth notmals. const float FALLOUT_RATIO = 0.5f; for(int z = 0; z < _height; z++) { for(int x = 0; x < _width; x++) { Vec3f sum = normals2[x][z]; if(x > 0) { sum += normals2[x-1][z] * FALLOUT_RATIO; } if(x < _width-1) { sum += normals2[x+1][z] * FALLOUT_RATIO; } if(z > 0) { sum += normals2[x][z-1] * FALLOUT_RATIO; } if(z < _height-1) { sum += normals2[x][z+1] * FALLOUT_RATIO; } if(sum.Magnitude() == 0) { sum = Vec3f(0.0, 1.0, 0.0); } _normals[x][z] = sum; } } // Delete the first array. for(int i = 0; i < _height; i++) { delete [] normals2[i]; } delete [] normals2; }