PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/SolarSystem.Wpf/Skybox.cs

https://bitbucket.org/efouts/solarsystem
C# | 109 lines | 87 code | 16 blank | 6 comment | 0 complexity | 34bdfde211c481eed3de3e327a5147a1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using SharpGL;
  2. using SharpGL.SceneGraph.Assets;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace SolarSystem.Wpf
  8. {
  9. public class Skybox
  10. {
  11. private Double height;
  12. private Double length;
  13. private Double width;
  14. private Texture[] textures;
  15. public Skybox(Double height, Double length, Double width)
  16. {
  17. this.height = height;
  18. this.length = length;
  19. this.width = width;
  20. this.textures = new Texture[6];
  21. }
  22. public void LoadTextures(OpenGL gl)
  23. {
  24. textures[0] = new Texture();
  25. textures[0].Create(gl, @"Textures\skybox_front.jpg");
  26. textures[1] = new Texture();
  27. textures[1].Create(gl, @"Textures\skybox_back.jpg");
  28. textures[2] = new Texture();
  29. textures[2].Create(gl, @"Textures\skybox_left.jpg");
  30. textures[3] = new Texture();
  31. textures[3].Create(gl, @"Textures\skybox_right.jpg");
  32. textures[4] = new Texture();
  33. textures[4].Create(gl, @"Textures\skybox_top.jpg");
  34. textures[5] = new Texture();
  35. textures[5].Create(gl, @"Textures\skybox_bottom.jpg");
  36. }
  37. public void Render(OpenGL gl, Double centerX, Double centerY, Double centerZ)
  38. {
  39. var x = centerX - width / 2;
  40. var y = centerY - height / 2;
  41. var z = centerZ - length / 2;
  42. // Front
  43. textures[0].Bind(gl);
  44. gl.Begin(OpenGL.GL_QUADS);
  45. gl.TexCoord(0.0f, 0.0f); gl.Vertex(x, y, z + length);
  46. gl.TexCoord(0.0f, 1.5f); gl.Vertex(x, y + height, z + length);
  47. gl.TexCoord(1.5f, 1.5f); gl.Vertex(x + width, y + height, z + length);
  48. gl.TexCoord(1.5f, 0.0f); gl.Vertex(x + width, y, z + length);
  49. gl.End();
  50. // Back
  51. textures[1].Bind(gl);
  52. gl.Begin(OpenGL.GL_QUADS);
  53. gl.TexCoord(1.5f, 0.0f); gl.Vertex(x + width, y, z);
  54. gl.TexCoord(1.5f, 1.5f); gl.Vertex(x + width, y + height, z);
  55. gl.TexCoord(0.0f, 1.5f); gl.Vertex(x, y + height, z);
  56. gl.TexCoord(0.0f, 0.0f); gl.Vertex(x, y, z);
  57. gl.End();
  58. // Left
  59. textures[2].Bind(gl);
  60. gl.Begin(OpenGL.GL_QUADS);
  61. gl.TexCoord(0.0f, 1.5f); gl.Vertex(x, y + height, z);
  62. gl.TexCoord(1.5f, 1.5f); gl.Vertex(x, y + height, z + length);
  63. gl.TexCoord(1.5f, 0.0f); gl.Vertex(x, y, z + length);
  64. gl.TexCoord(0.0f, 0.0f); gl.Vertex(x, y, z);
  65. gl.End();
  66. // Right
  67. textures[3].Bind(gl);
  68. gl.Begin(OpenGL.GL_QUADS);
  69. gl.TexCoord(0.0f, 0.0f); gl.Vertex(x + width, y, z);
  70. gl.TexCoord(1.5f, 0.0f); gl.Vertex(x + width, y, z + length);
  71. gl.TexCoord(1.5f, 1.5f); gl.Vertex(x + width, y + height, z + length);
  72. gl.TexCoord(0.0f, 1.5f); gl.Vertex(x + width, y + height, z);
  73. gl.End();
  74. // Up
  75. textures[4].Bind(gl);
  76. gl.Begin(OpenGL.GL_QUADS);
  77. gl.TexCoord(0.0f, 0.0f); gl.Vertex(x + width, y + height, z);
  78. gl.TexCoord(1.5f, 0.0f); gl.Vertex(x + width, y + height, z + length);
  79. gl.TexCoord(1.5f, 1.5f); gl.Vertex(x, y + height, z + length);
  80. gl.TexCoord(0.0f, 1.5f); gl.Vertex(x, y + height, z);
  81. gl.End();
  82. //// Down
  83. textures[5].Bind(gl);
  84. gl.Begin(OpenGL.GL_QUADS);
  85. gl.TexCoord(0.0f, 0.0f); gl.Vertex(x, y, z);
  86. gl.TexCoord(1.5f, 0.0f); gl.Vertex(x, y, z + length);
  87. gl.TexCoord(1.5f, 1.5f); gl.Vertex(x + width, y, z + length);
  88. gl.TexCoord(0.0f, 1.5f); gl.Vertex(x + width, y, z);
  89. gl.End();
  90. gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);
  91. }
  92. }
  93. }