/// <summary>Renders a background</summary> /// <param name="Data">The background to render</param> /// <param name="Alpha">The alpha level</param> /// <param name="Scale">The scale</param> public void Render(BackgroundHandle Data, float Alpha, float Scale) { DynamicBackground dynamicBackground = Data as DynamicBackground; StaticBackground staticBackground = Data as StaticBackground; BackgroundObject backgroundObject = Data as BackgroundObject; if (dynamicBackground != null) { RenderDynamicBackground(dynamicBackground, Alpha, Scale); } if (staticBackground != null) { if (renderer.currentOptions.IsUseNewRenderer) { RenderStaticBackground(staticBackground, Alpha, Scale); } else { RenderStaticBackgroundImmediate(staticBackground, Alpha, Scale); } } if (backgroundObject != null) { RenderBackgroundObject(backgroundObject); } }
/// <summary>Renders a static frustrum based background</summary> /// <param name="data">The background to render</param> /// <param name="alpha">The alpha level</param> /// <param name="scale">The scale</param> private void RenderStaticBackgroundRetained(StaticBackground data, float alpha, float scale) { Texture t = data.Texture; if (data.Texture != null && renderer.currentHost.LoadTexture(ref t, OpenGlTextureWrapMode.RepeatClamp)) { renderer.LastBoundTexture = t.OpenGlTextures[(int)OpenGlTextureWrapMode.RepeatClamp]; if (alpha == 1.0f) { GL.Disable(EnableCap.Blend); } else { GL.Enable(EnableCap.Blend); } if (data.VAO == null) { data.CreateVAO(renderer.DefaultShader.VertexLayout, renderer); } renderer.DefaultShader.Activate(); renderer.ResetShader(renderer.DefaultShader); // matrix renderer.DefaultShader.SetCurrentProjectionMatrix(renderer.CurrentProjectionMatrix); renderer.DefaultShader.SetCurrentModelViewMatrix(Matrix4D.Scale(scale) * renderer.CurrentViewMatrix); // fog if (renderer.OptionFog) { renderer.DefaultShader.SetIsFog(true); renderer.DefaultShader.SetFog(renderer.Fog); } // texture GL.BindTexture(TextureTarget.Texture2D, t.OpenGlTextures[(int)OpenGlTextureWrapMode.RepeatClamp].Name); renderer.LastBoundTexture = null; // alpha test renderer.SetAlphaFunc(AlphaFunction.Greater, 0.0f); // blend mode renderer.DefaultShader.SetOpacity(alpha); // render polygon VertexArrayObject VAO = (VertexArrayObject)data.VAO; VAO.Bind(); renderer.lastVAO = VAO.handle; for (int i = 0; i + 11 < 32 * 12; i += 12) { VAO.Draw(PrimitiveType.Triangles, i, 12); } renderer.RestoreBlendFunc(); } }
/// <summary>Renders a static frustrum based background</summary> /// <param name="data">The background to render</param> /// <param name="alpha">The alpha level</param> /// <param name="scale">The scale</param> private void RenderStaticBackground(StaticBackground data, float alpha, float scale) { if (renderer.AvailableNewRenderer) { RenderStaticBackgroundRetained(data, alpha, scale); } else { RenderStaticBackgroundImmediate(data, alpha, scale); } }
/// <summary>Renders a static frustrum based background</summary> /// <param name="data">The background to render</param> /// <param name="scale">The scale</param> private void RenderStaticBackground(StaticBackground data, float scale) { if (renderer.currentOptions.IsUseNewRenderer) { RenderStaticBackground(data, 1.0f, scale); } else { RenderStaticBackgroundImmediate(data, 1.0f, scale); } }
/// <summary>Renders a background</summary> /// <param name="Data">The background to render</param> /// <param name="Alpha">The alpha level</param> /// <param name="Scale">The scale</param> public void Render(BackgroundHandle Data, float Alpha, float Scale) { DynamicBackground dynamicBackground = Data as DynamicBackground; StaticBackground staticBackground = Data as StaticBackground; BackgroundObject backgroundObject = Data as BackgroundObject; if (dynamicBackground != null) { RenderDynamicBackground(dynamicBackground, Alpha, Scale); } if (staticBackground != null) { RenderStaticBackground(staticBackground, Alpha, Scale); } if (backgroundObject != null) { RenderBackgroundObject(backgroundObject); } }
public static void CreateVAO(this StaticBackground background) { float y0, y1; if (background.KeepAspectRatio) { int tw = background.Texture.Width; int th = background.Texture.Height; double hh = System.Math.PI * background.BackgroundImageDistance * th / (tw * background.Repetition); y0 = (float)(-0.5 * hh); y1 = (float)(1.5 * hh); } else { y0 = (float)(-0.125 * background.BackgroundImageDistance); y1 = (float)(0.375 * background.BackgroundImageDistance); } const int n = 32; Vector3f[] bottom = new Vector3f[n]; Vector3f[] top = new Vector3f[n]; double angleValue = 2.61799387799149 - 3.14159265358979 / n; const double angleIncrement = 6.28318530717958 / n; /* * To ensure that the whole background cylinder is rendered inside the viewing frustum, * the background is rendered before the scene with z-buffer writes disabled. Then, * the actual distance from the camera is irrelevant as long as it is inside the frustum. * */ for (int i = 0; i < n; i++) { float x = (float)(background.BackgroundImageDistance * System.Math.Cos(angleValue)); float z = (float)(background.BackgroundImageDistance * System.Math.Sin(angleValue)); bottom[i] = new Vector3f(x, y0, z); top[i] = new Vector3f(x, y1, z); angleValue += angleIncrement; } float textureStart = 0.5f * (float)background.Repetition / n; float textureIncrement = -(float)background.Repetition / n; float textureX = textureStart; List <LibRenderVertex> vertexData = new List <LibRenderVertex>(); List <ushort> indexData = new List <ushort>(); for (int i = 0; i < n; i++) { int j = (i + 1) % n; int indexOffset = vertexData.Count; // side wall vertexData.Add(new LibRenderVertex { Position = top[i], UV = new Vector2f(textureX, 0.005f), Color = Color128.White }); vertexData.Add(new LibRenderVertex { Position = bottom[i], UV = new Vector2f(textureX, 0.995f), Color = Color128.White }); vertexData.Add(new LibRenderVertex { Position = bottom[j], UV = new Vector2f(textureX + textureIncrement, 0.995f), Color = Color128.White }); vertexData.Add(new LibRenderVertex { Position = top[j], UV = new Vector2f(textureX + textureIncrement, 0.005f), Color = Color128.White }); indexData.AddRange(new[] { 0, 1, 2, 3 }.Select(x => x + indexOffset).Select(x => (ushort)x)); // top cap vertexData.Add(new LibRenderVertex { Position = new Vector3f(0.0f, top[i].Y, 0.0f), UV = new Vector2f(textureX + 0.5f * textureIncrement, 0.1f), Color = Color128.White }); indexData.AddRange(new[] { 0, 3, 4 }.Select(x => x + indexOffset).Select(x => (ushort)x)); // bottom cap vertexData.Add(new LibRenderVertex { Position = new Vector3f(0.0f, bottom[i].Y, 0.0f), UV = new Vector2f(textureX + 0.5f * textureIncrement, 0.9f), Color = Color128.White }); indexData.AddRange(new[] { 5, 2, 1 }.Select(x => x + indexOffset).Select(x => (ushort)x)); // finish textureX += textureIncrement; } VertexArrayObject VAO = (VertexArrayObject)background.VAO; VAO?.UnBind(); VAO?.Dispose(); VAO = new VertexArrayObject(); VAO.Bind(); VAO.SetVBO(new VertexBufferObject(vertexData.ToArray(), BufferUsageHint.StaticDraw)); VAO.SetIBO(new IndexBufferObject(indexData.ToArray(), BufferUsageHint.StaticDraw)); VAO.UnBind(); background.VAO = VAO; }
/// <summary>Renders a static frustrum based background</summary> /// <param name="data">The background to render</param> /// <param name="alpha">The alpha level</param> /// <param name="scale">The scale</param> private void RenderStaticBackgroundImmediate(StaticBackground data, float alpha, float scale) { //return; if (data.Texture != null && renderer.currentHost.LoadTexture(data.Texture, OpenGlTextureWrapMode.RepeatClamp)) { GL.MatrixMode(MatrixMode.Projection); GL.PushMatrix(); GL.LoadIdentity(); OpenTK.Matrix4d perspective = OpenTK.Matrix4d.Perspective(renderer.Camera.VerticalViewingAngle, -renderer.Screen.AspectRatio, 0.2, 1000.0); GL.MultMatrix(ref perspective); double dx = renderer.Camera.AbsoluteDirection.X; double dy = renderer.Camera.AbsoluteDirection.Y; double dz = renderer.Camera.AbsoluteDirection.Z; double ux = renderer.Camera.AbsoluteUp.X; double uy = renderer.Camera.AbsoluteUp.Y; double uz = renderer.Camera.AbsoluteUp.Z; OpenTK.Matrix4d lookat = OpenTK.Matrix4d.LookAt(0.0, 0.0, 0.0, dx, dy, dz, ux, uy, uz); GL.MatrixMode(MatrixMode.Modelview); GL.PushMatrix(); GL.LoadMatrix(ref lookat); GL.Disable(EnableCap.Lighting); GL.Enable(EnableCap.Texture2D); if (alpha == 1.0f) { GL.Disable(EnableCap.Blend); } else { GL.Enable(EnableCap.Blend); } GL.BindTexture(TextureTarget.Texture2D, data.Texture.OpenGlTextures[(int)OpenGlTextureWrapMode.RepeatClamp].Name); renderer.LastBoundTexture = data.Texture.OpenGlTextures[(int)OpenGlTextureWrapMode.RepeatClamp]; GL.Color4(1.0f, 1.0f, 1.0f, alpha); if (renderer.OptionFog) { GL.Enable(EnableCap.Fog); } if (data.DisplayList > 0) { GL.CallList(data.DisplayList); GL.Disable(EnableCap.Texture2D); GL.Enable(EnableCap.Blend); GL.PopMatrix(); GL.MatrixMode(MatrixMode.Projection); GL.PopMatrix(); return; } data.DisplayList = GL.GenLists(1); GL.NewList(data.DisplayList, ListMode.Compile); float y0, y1; if (data.KeepAspectRatio) { int tw = data.Texture.Width; int th = data.Texture.Height; double hh = Math.PI * data.BackgroundImageDistance * th / (tw * data.Repetition); y0 = (float)(-0.5 * hh); y1 = (float)(1.5 * hh); } else { y0 = (float)(-0.125 * data.BackgroundImageDistance); y1 = (float)(0.375 * data.BackgroundImageDistance); } const int n = 32; Vector3[] bottom = new Vector3[n]; Vector3[] top = new Vector3[n]; double angleValue = 2.61799387799149 - 3.14159265358979 / n; const double angleIncrement = 6.28318530717958 / n; /* * To ensure that the whole background cylinder is rendered inside the viewing frustum, * the background is rendered before the scene with z-buffer writes disabled. Then, * the actual distance from the camera is irrelevant as long as it is inside the frustum. * */ for (int i = 0; i < n; i++) { float x = (float)(data.BackgroundImageDistance * Math.Cos(angleValue)); float z = (float)(data.BackgroundImageDistance * Math.Sin(angleValue)); bottom[i] = new Vector3(scale * x, scale * y0, scale * z); top[i] = new Vector3(scale * x, scale * y1, scale * z); angleValue += angleIncrement; } float textureStart = 0.5f * (float)data.Repetition / n; float textureIncrement = -(float)data.Repetition / n; double textureX = textureStart; for (int i = 0; i < n; i++) { int j = (i + 1) % n; // side wall GL.Begin(PrimitiveType.Quads); GL.TexCoord2(textureX, 0.005f); GL.Vertex3(top[i].X, top[i].Y, top[i].Z); GL.TexCoord2(textureX, 0.995f); GL.Vertex3(bottom[i].X, bottom[i].Y, bottom[i].Z); GL.TexCoord2(textureX + textureIncrement, 0.995f); GL.Vertex3(bottom[j].X, bottom[j].Y, bottom[j].Z); GL.TexCoord2(textureX + textureIncrement, 0.005f); GL.Vertex3(top[j].X, top[j].Y, top[j].Z); GL.End(); // top cap GL.Begin(PrimitiveType.Triangles); GL.TexCoord2(textureX, 0.005f); GL.Vertex3(top[i].X, top[i].Y, top[i].Z); GL.TexCoord2(textureX + textureIncrement, 0.005f); GL.Vertex3(top[j].X, top[j].Y, top[j].Z); GL.TexCoord2(textureX + 0.5 * textureIncrement, 0.1f); GL.Vertex3(0.0f, top[i].Y, 0.0f); // bottom cap GL.TexCoord2(textureX + 0.5 * textureIncrement, 0.9f); GL.Vertex3(0.0f, bottom[i].Y, 0.0f); GL.TexCoord2(textureX + textureIncrement, 0.995f); GL.Vertex3(bottom[j].X, bottom[j].Y, bottom[j].Z); GL.TexCoord2(textureX, 0.995f); GL.Vertex3(bottom[i].X, bottom[i].Y, bottom[i].Z); GL.End(); // finish textureX += textureIncrement; } GL.EndList(); GL.CallList(data.DisplayList); GL.Disable(EnableCap.Texture2D); GL.Enable(EnableCap.Blend); GL.PopMatrix(); GL.MatrixMode(MatrixMode.Projection); GL.PopMatrix(); } }
/// <summary>Renders a static frustrum based background</summary> /// <param name="data">The background to render</param> /// <param name="scale">The scale</param> private void RenderStaticBackground(StaticBackground data, float scale) { RenderStaticBackground(data, 1.0f, scale); }
/// <summary>Renders a static frustrum based background</summary> /// <param name="data">The background to render</param> /// <param name="alpha">The alpha level</param> /// <param name="scale">The scale</param> private void RenderStaticBackground(StaticBackground data, float alpha, float scale) { if (data.Texture != null && renderer.currentHost.LoadTexture(data.Texture, OpenGlTextureWrapMode.RepeatClamp)) { GL.Enable(EnableCap.Texture2D); if (alpha == 1.0f) { GL.Disable(EnableCap.Blend); } else { GL.Enable(EnableCap.Blend); } if (data.VAO == null) { data.CreateVAO(); } renderer.DefaultShader.Activate(); renderer.ResetShader(renderer.DefaultShader); // matrix renderer.DefaultShader.SetCurrentProjectionMatrix(renderer.CurrentProjectionMatrix); renderer.DefaultShader.SetCurrentModelViewMatrix(Matrix4D.Scale(scale) * renderer.CurrentViewMatrix); // fog if (renderer.OptionFog) { renderer.DefaultShader.SetIsFog(true); renderer.DefaultShader.SetFogStart(renderer.Fog.Start); renderer.DefaultShader.SetFogEnd(renderer.Fog.End); renderer.DefaultShader.SetFogColor(new Color4(renderer.Fog.Color.R, renderer.Fog.Color.G, renderer.Fog.Color.B, 255)); } // texture renderer.DefaultShader.SetIsTexture(true); renderer.DefaultShader.SetTexture(0); GL.BindTexture(TextureTarget.Texture2D, data.Texture.OpenGlTextures[(int)OpenGlTextureWrapMode.RepeatClamp].Name); // alpha test renderer.SetAlphaFunc(AlphaFunction.Greater, 0.0f); // blend mode renderer.DefaultShader.SetOpacity(alpha); // render polygon VertexArrayObject VAO = (VertexArrayObject)data.VAO; VAO.Bind(); for (int i = 0; i + 9 < 32 * 10; i += 10) { VAO.Draw(renderer.DefaultShader.VertexLayout, PrimitiveType.Quads, i, 4); VAO.Draw(renderer.DefaultShader.VertexLayout, PrimitiveType.Triangles, i + 4, 3); VAO.Draw(renderer.DefaultShader.VertexLayout, PrimitiveType.Triangles, i + 7, 3); } VAO.UnBind(); GL.BindTexture(TextureTarget.Texture2D, 0); renderer.DefaultShader.Deactivate(); GL.Disable(EnableCap.Texture2D); renderer.RestoreBlendFunc(); } }
private void initializeGame() { myScale = Screen.width / SettingsHandler.horizontalRows; mainBackgroundWidth = Screen.width - myScale * 2; // Initializing main background height as a % of the screen height. for (int i = 0; mainBackgroundHeight < (heightPercentage * Screen.height); i++) { mainBackgroundHeight = myScale * i; } mainBackgroundHeight -= myScale * 2; //Debug.Log("Width: " + mainBackgroundWidth + " Height: " + mainBackgroundHeight); // Initializing backgrounds. StaticBackground myStaticBackground = new StaticBackground(); myStaticBackground.Spawn(new Vector2(mainBackgroundWidth, mainBackgroundHeight), new Vector2(0, 0), Color.black, "MainStaticBackground"); //StaticBackground myScoreBackground = new StaticBackground(); //myStaticBackground.Spawn(new Vector2((float)(Screen.width * scoreBoardWidthFactor)-myScale, Screen.height - mainBackgroundHeight -myScale), new Vector2((float)(((-Screen.width * (1 - scoreBoardWidthFactor)) /2) - (myScale / 2)), ((Screen.height-mainBackgroundHeight)/2)+(mainBackgroundHeight/2)+(myScale/2)), Color.black, "ScoreBackground"); //StaticBackground myPauseBackground = new StaticBackground(); //myStaticBackground.Spawn(new Vector2(Screen.height - mainBackgroundHeight - myScale, Screen.height - mainBackgroundHeight - myScale), new Vector2((float)(Screen.width * scoreBoardWidthFactor) / 2, ((Screen.height - mainBackgroundHeight) / 2) + (mainBackgroundHeight / 2) + (myScale / 2)), Color.yellow, "PauseBackground"); StaticBackground myScoreBackground = new StaticBackground(); myStaticBackground.Spawn(new Vector2(Screen.width - (Screen.height - mainBackgroundHeight), Screen.height - mainBackgroundHeight - myScale * 3), new Vector2(((Screen.width - (Screen.height - mainBackgroundHeight)) / 2) - (Screen.width / 2) + myScale, ((Screen.height - mainBackgroundHeight) / 2) + (mainBackgroundHeight / 2) + (myScale / 2) - myScale), Color.black, "ScoreBackground"); StaticBackground myPauseBackground = new StaticBackground(); myStaticBackground.Spawn(new Vector2(Screen.height - mainBackgroundHeight - myScale * 3, Screen.height - mainBackgroundHeight - myScale * 3), new Vector2((float)(((Screen.width) / 2) - (Screen.height - mainBackgroundHeight - myScale) / 2), ((Screen.height - mainBackgroundHeight) / 2) + (mainBackgroundHeight / 2) + (myScale / 2) - myScale), Color.black, "PauseBackground"); GameObject.Find("PauseBackground").AddComponent <PauseButtonScript>(); GameObject.Find("PauseBackground").AddComponent <BoxCollider2D>(); // initializing my snake array position for (int i = 0; i < mySnakePartArray.Length; i++) { SnakePart tempSnakePart = new SnakePart(); Vector2 tempVector = new Vector2(((-mainBackgroundWidth / 2) + myScale / 2) - (i * myScale) + 4 * myScale, (-mainBackgroundHeight / 2) + (myScale / 2) + 2 * myScale); if (i == 0) { tempSnakePart.Spawn(tempVector, myScale, Color.cyan); } else { tempSnakePart.Spawn(tempVector, myScale, Color.blue); } mySnakePartArray[i] = tempSnakePart; //Debug.Log("MySnakePosition:" + mySnakePartArray[i].getPosition()); } snakeDirection.x = myScale; snakeDirection.y = 0; // Initializing food position. myFood.SpawnRandomSpot(mainBackgroundWidth, mainBackgroundHeight, myScale, mySnakePartArray); }
/// <summary>Creates a new dynamic background</summary> /// <param name="Backgrounds">The list of static backgrounds which make up the dynamic background</param> internal DynamicBackground(StaticBackground[] Backgrounds) { this.Backgrounds = Backgrounds; }
public StaticScene(string textureFile) { m_TextureFile = textureFile; m_Background = new StaticBackground(m_TextureFile); AddComponent(m_Background); }
public void Create() { _internalStaticTexture = StaticBackground.ToTexture2D(GraphicsDevice); }