/// <summary> /// Recursive binary search to determine rays (x,y,z) intersection point with Terrain. /// </summary> /// <param name="count"></param> /// <param name="start"></param> /// <param name="finish"></param> /// <param name="ray"></param> /// <returns></returns> private Vector3 BinarySearch(int count, float start, float finish, Vector3 ray) { float half = start + ((finish - start) / 2f); if (count >= JConfig.MOUSE_PICKER_RECURSION_COUNT) { Vector3 endPoint = GetPointOnRay(ray, half); JPerlinTerrain terrain = GetTerrain(endPoint.X, endPoint.Z); if (terrain != null) { return(endPoint); } else { return(new Vector3(0, 0, 0)); } } if (IntersectionInRange(start, half, ray)) { return(BinarySearch(count + 1, start, half, ray)); } else { return(BinarySearch(count + 1, half, finish, ray)); } }
/// <summary> /// Create a JMousePicker instance. JMousePicker will project a 2D screen coordinate into a 3D unit vector within the world. /// It can determine intersection with terrain as well as check for intersection with a JBoundingSphere. /// </summary> /// <param name="camera">The JCamera entity.</param> /// <param name="projectionMatrix">The JMasterRenderers Projection Matrix based off of the JCameras properties.</param> /// <param name="terrain">The JTerrain the Mouse Ray will intersect with.</param> /// <param name="gameWindow">The active JGameWindow. We need this to get it's height and width.</param> public JMousePicker(JCamera camera, Matrix4 projectionMatrix, JPerlinTerrain terrain, JGameWindow gameWindow) { Camera = camera; ProjectionMatrix = projectionMatrix; ViewMatrix = JMathUtils.createViewMatrix(camera); Terrain = terrain; GameWindow = gameWindow; }
private void PrepareTerrainModel(JPerlinTerrain terrain) { JRawModel rawModel = terrain.TerrainModel; GL.BindVertexArray(rawModel.vaoID); EnableAttribArrays(); BindTextures(terrain); TerrainShader.LoadShineVariables(1, 0); }
private void BindTextures(JPerlinTerrain terrain) { JTerrainTexturePack texturePack = terrain.TexturePack; TextureUnit texture = TextureUnit.Texture0; for (int i = 0; i < texturePack.TerrainTextures.Count; i++) { GL.ActiveTexture(texture); GL.BindTexture(TextureTarget.Texture2D, texturePack.TerrainTextures[i].TextureID); texture++; } }
/// <summary> /// /// </summary> /// <param name="terrain"></param> public void Translate(JPerlinTerrain terrain) { float distance = currentSpeed * JGameWindow.Delta; float dX = distance * (Orientation.X / Orientation.Length); float dZ = distance * (Orientation.Z / Orientation.Length); upwardSpeed += GRAVITY * JGameWindow.Delta; base.IncreasePosition(dX, upwardSpeed * JGameWindow.Delta, dZ); float terrainHeight = terrain.GetHeightOfTerrain(base.Position.X, base.Position.Z); if (base.Position.Y < terrainHeight) { upwardSpeed = 0; isInAir = false; base.Position = new Vector3(Position.X, terrainHeight, Position.Z); } }
/// <summary> /// Determine whether point falls below Terrain. /// </summary> /// <param name="point"></param> /// <returns></returns> private Boolean IsUnderGround(Vector3 point) { JPerlinTerrain terrain = GetTerrain(point.X, point.Z); float height = 0; if (terrain != null) { height = terrain.GetHeightOfTerrain(point.X, point.Z); } if (point.Y < height) { return(true); } else { return(false); } }
/// <summary> /// Update the position of JBoundedEntity each frame and update the position of JBoundingSphere to match. /// </summary> /// <param name="terrain"></param> public void Move(JPerlinTerrain terrain) { if (Destination.IsSet) { DistanceToDestination(); if (!IsAtDestination) { currentSpeed = RUN_SPEED; } else { currentSpeed = 0; IsAtDestination = true; Destination.IsSet = false; } } Translate(terrain); BoundingSphere.SphereEntity.Position = new Vector3(Position.X, Position.Y + 1, Position.Z); }
public void RenderScene(List <JBoundedEntity> entities, List <JEntity> staticEntities, JPerlinTerrain terrain, JLight light, JCamera camera, Vector4 clippingPlane) { processTerrain(terrain); foreach (JBoundedEntity entity in entities) { ProcessEntity(entity); ProcessEntity(entity.BoundingSphere.SphereEntity); } foreach (JEntity entity in staticEntities) { ProcessEntity(entity); } Render(light, camera, clippingPlane); }
public void processTerrain(JPerlinTerrain terrain) { terrains.Add(terrain); }
/// <summary> /// GraphicsMode used for anitaliasing. /// </summary> public JGameWindow() : base(WIDTH, HEIGHT, new OpenTK.Graphics.GraphicsMode(32, 24, 0, 8), "JModelViewer", GameWindowFlags.Default, DisplayDevice.Default, 3, 0, GraphicsContextFlags.ForwardCompatible) { PlayerTexturePath = JFileUtils.GetPathToResFile("Cowboy\\CowboyTexture.png"); PlayerModelPath = JFileUtils.GetPathToResFile("Cowboy\\Cowboy.obj"); Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; DateTime buildDate = new DateTime(2000, 1, 1).AddDays(version.Build).AddSeconds(version.Revision * 2); string displayVersion = $"{version} ({buildDate})"; Console.WriteLine("JGameEngine v." + displayVersion); Console.WriteLine("OpenGL version: " + GL.GetString(StringName.Version)); Loader = new JLoader(); PlayerModelData = JObjFileLoader.LoadObj(PlayerModelPath); Console.WriteLine("Using .obj file: " + PlayerModelPath); PlayerModel = Loader.LoadToVAO(PlayerModelData.Vertices, PlayerModelData.TextureCoords, PlayerModelData.Normals, PlayerModelData.Indices); Console.WriteLine("Using .png texture file: " + PlayerTexturePath + "..."); JModelTexture texture = new JModelTexture(Loader.loadTexture(PlayerTexturePath)); TexturedModel = new JTexturedModel(PlayerModel, texture); Light = new JLight(new Vector3(0, 0, 0), new Vector3(1, 1, 1)); JTerrainTexture textureWaterDeep = new JTerrainTexture(Loader.loadTexture(JFileUtils.GetPathToResFile("Terrain\\WaterDeep.png"))); JTerrainTexture textureWaterShallow = new JTerrainTexture(Loader.loadTexture(JFileUtils.GetPathToResFile("Terrain\\WaterShallow.png"))); JTerrainTexture textureSand = new JTerrainTexture(Loader.loadTexture(JFileUtils.GetPathToResFile("Terrain\\Sand.png"))); JTerrainTexture textureGrassNatural = new JTerrainTexture(Loader.loadTexture(JFileUtils.GetPathToResFile("Terrain\\GrassNatural.png"))); JTerrainTexture textureGrassLush = new JTerrainTexture(Loader.loadTexture(JFileUtils.GetPathToResFile("Terrain\\GrassLush.png"))); JTerrainTexture textureMountainNatural = new JTerrainTexture(Loader.loadTexture(JFileUtils.GetPathToResFile("Terrain\\MountainNatural.png"))); JTerrainTexture textureMountainRocky = new JTerrainTexture(Loader.loadTexture(JFileUtils.GetPathToResFile("Terrain\\MountainRocky.png"))); JTerrainTexture textureSnow = new JTerrainTexture(Loader.loadTexture(JFileUtils.GetPathToResFile("Terrain\\Snow.png"))); JTerrainTexturePack texturePack = new JTerrainTexturePack(); texturePack.AddTerrainTexture(textureWaterDeep, 0.1f); texturePack.AddTerrainTexture(textureWaterShallow, 0.15f); texturePack.AddTerrainTexture(textureSand, 0.18f); texturePack.AddTerrainTexture(textureGrassNatural, 0.35f); texturePack.AddTerrainTexture(textureGrassLush, 0.45f); texturePack.AddTerrainTexture(textureMountainNatural, 0.6f); texturePack.AddTerrainTexture(textureMountainRocky, 0.7f); texturePack.AddTerrainTexture(textureSnow, 0.8f); MasterRenderer = new JMasterRenderer(texturePack); WaterBuffer = new JWaterFrameBuffer(this); WaterShader = new JWaterShader(); waterTiles = new List <JWaterTile>(); WaterTile = new JWaterTile(400, -400, 8, textureWaterShallow); waterTiles.Add(WaterTile); waterRenderer = new JWaterRenderer(Loader, WaterShader, MasterRenderer.projectionMatrix, WaterBuffer, WaterTile); terrain = new JPerlinTerrain(0, -1, Loader, texturePack); Entity = new JBoundedEntity(TexturedModel, new Vector3(50, 0, -50), new Vector3(0, 0, 1), 0.1f, Loader); Entity2 = new JBoundedEntity(TexturedModel, new Vector3(100, 0, -50), new Vector3(0, 0, 1), 0.1f, Loader); EntityList = new List <JBoundedEntity>(); EntityList.Add(Entity); EntityList.Add(Entity2); // Generate Trees JEntityGenerator entityGenerator = new JEntityGenerator(Loader, terrain, WaterTile); StaticEntities = entityGenerator.GenerateTrees(); Camera = new JCameraFree(); picker = new JMousePicker(Camera, MasterRenderer.projectionMatrix, terrain, this); LastFrameTime = GetCurrentTime(); }
private void LoadModelMatrix(JPerlinTerrain terrain) { Matrix4 transformationMatrix = JMathUtils.createTransformationMatrix(new Vector3(terrain.X, 0, terrain.Z), 0, 0, 0, 1); TerrainShader.LoadTransformationMatrix(transformationMatrix); }
/// <summary> /// /// </summary> /// <param name="loader"></param> /// <param name="terrain"></param> public JEntityGenerator(JLoader loader, JPerlinTerrain terrain, JWaterTile waterTile) { Loader = loader; Terrain = terrain; WaterTile = waterTile; }