/// <summary> /// Adjusts camera's settings according to bounding box. /// <para>Use this when bounding box's size or positon is changed.</para> /// </summary> /// <param name="camera"></param> /// <param name="boundingBox"></param> public static void ZoomCamera(this IPerspectiveViewCamera camera, IBoundingBox boundingBox) { vec3 length = boundingBox.MaxPosition - boundingBox.MinPosition; float size = Math.Max(Math.Max(length.x, length.y), length.z); { vec3 target = boundingBox.MaxPosition / 2 + boundingBox.MinPosition / 2; vec3 target2Position = (camera.Position - camera.Target).normalize(); vec3 position = target + target2Position * (size * 2 + 1); camera.Position = position; camera.Target = target; //camera.UpVector = new vec3(0f, 1f, 0f); } { int[] viewport = new int[4]; OpenGL.GetInteger(GetTarget.Viewport, viewport); int width = viewport[2]; int height = viewport[3]; camera.FieldOfView = 60; camera.AspectRatio = (double)width / (double)height; camera.Near = 0.01; camera.Far = size * 3 + 1;// double.MaxValue; } }
/// <summary> /// Initializes a new instance of the <see cref="SharpGLForm"/> class. /// </summary> public SharpGLForm() { InitializeComponent(); int radius = 1; legacyModel.Build( new SharpGL.SceneGraph.Vertex(-radius, -0, -radius), new SharpGL.SceneGraph.Vertex(radius, 0, radius)); vertexArrayModel.Build( new SharpGL.SceneGraph.Vertex(-radius, -radius, -radius), new SharpGL.SceneGraph.Vertex(radius, radius, radius)); VBOModel.Build( new SharpGL.SceneGraph.Vertex(-radius, -radius, -radius), new SharpGL.SceneGraph.Vertex(radius, radius, radius)); ScientificCamera camera = new ScientificCamera(CameraTypes.Perspecitive); camera.Target = new SharpGL.SceneGraph.Vertex(0, 0, 0); camera.Position = new SharpGL.SceneGraph.Vertex(-2, 2, -2); camera.UpVector = new SharpGL.SceneGraph.Vertex(0, 1, 0); IPerspectiveViewCamera perspective = camera; perspective.AspectRatio = (double)Width / (double)Height; perspective.Far = 100; perspective.Near = 0.01; perspective.FieldOfView = 60; this.element = new ScientificModelElement(VBOModel, camera); }
/// <summary> /// Apply specifed viewType to camera according to bounding box's size and position. /// <para> +-------+ </para> /// <para> /| /| </para> /// <para> +-------+ | </para> /// <para> | | | | </para> /// <para> | O-----|-+---X</para> /// <para> |/ |/ </para> /// <para> +-------+ </para> /// <para> / | </para> /// <para>Y Z </para> /// <para>其边长为(2 * Math.Sqrt(3)), 所在的坐标系如下</para> /// <para> O---X</para> /// <para> /| </para> /// <para> Y | </para> /// <para> Z </para> /// </summary> /// <param name="camera"></param> /// <param name="boundingBox"></param> /// <param name="viewType"></param> public static void ApplyViewType(this IPerspectiveViewCamera camera, IBoundingBox boundingBox, ViewTypes viewType) { vec3 length = boundingBox.MaxPosition - boundingBox.MinPosition; float size = Math.Max(Math.Max(length.x, length.y), length.z); { vec3 target = boundingBox.MaxPosition / 2 + boundingBox.MinPosition / 2; vec3 target2Position; vec3 upVector; GetBackAndUp(out target2Position, out upVector, viewType); vec3 position = target + target2Position * (size * 2 + 1); camera.Position = position; camera.Target = target; camera.UpVector = upVector; } { int[] viewport = new int[4]; OpenGL.GetInteger(GetTarget.Viewport, viewport); int width = viewport[2]; int height = viewport[3]; IPerspectiveCamera perspectiveCamera = camera; perspectiveCamera.FieldOfView = 60; perspectiveCamera.AspectRatio = (double)width / (double)height; perspectiveCamera.Near = 0.01; perspectiveCamera.Far = size * 3 + 1;// double.MaxValue; } }
/// <summary> /// Adjusts camera's settings according to bounding box. /// <para>Use this when bounding box's size or positon is changed.</para> /// </summary> /// <param name="camera"></param> /// <param name="boundingBox"></param> /// <param name="openGL"></param> public static void AdjustCamera(this IPerspectiveViewCamera camera, IBoundingBox boundingBox, OpenGL openGL) { float sizeX, sizeY, sizeZ; boundingBox.GetBoundDimensions(out sizeX, out sizeY, out sizeZ); float size = Math.Max(Math.Max(sizeX, sizeY), sizeZ); { float centerX, centerY, centerZ; boundingBox.GetCenter(out centerX, out centerY, out centerZ); Vertex target = new Vertex(centerX, centerY, centerZ); Vertex target2Position = camera.Position - camera.Target; target2Position.Normalize(); Vertex position = target + target2Position * (size * 2 + 1); camera.Position = position; camera.Target = target; //camera.UpVector = new Vertex(0f, 1f, 0f); } { int[] viewport = new int[4]; openGL.GetInteger(SharpGL.Enumerations.GetTarget.Viewport, viewport); int width = viewport[2]; int height = viewport[3]; camera.FieldOfView = 60; camera.AspectRatio = (double)width / (double)height; camera.Near = 0.01; camera.Far = size * 3 + 1;// double.MaxValue; } }
void IHasObjectSpace.PushObjectSpace(OpenGL gl) { // Update matrices. IScientificCamera camera = this.Camera; if (camera == null) { return; } if (camera.CameraType == CameraTypes.Perspecitive) { IPerspectiveViewCamera perspective = camera; this.projectionMatrix = perspective.GetProjectionMat4(); this.viewMatrix = perspective.GetViewMat4(); } else if (camera.CameraType == CameraTypes.Ortho) { IOrthoViewCamera ortho = camera; this.projectionMatrix = ortho.GetProjectionMat4(); this.viewMatrix = ortho.GetViewMat4(); } else { throw new NotImplementedException(); } modelMatrix = mat4.identity(); }
///// <summary> ///// Adjusts camera's settings according to bounding box. ///// <para>Use this when bounding box's size or positon is changed.</para> ///// </summary> ///// <param name="camera"></param> ///// <param name="boundingBox"></param> //public static void ZoomCamera(this IScientificCamera camera, IBoundingBox boundingBox) //{ // float sizeX, sizeY, sizeZ; // boundingBox.GetBoundDimensions(out sizeX, out sizeY, out sizeZ); // float size = Math.Max(Math.Max(sizeX, sizeY), sizeZ); // { // float centerX, centerY, centerZ; // boundingBox.GetCenter(out centerX, out centerY, out centerZ); // vec3 target = new vec3(centerX, centerY, centerZ); // vec3 target2Position = camera.Position - camera.Target; // target2Position.Normalize(); // vec3 position = target + target2Position * (size * 2 + 1); // camera.Position = position; // camera.Target = target; // //camera.UpVector = new vec3(0f, 1f, 0f); // } // { // int[] viewport = new int[4]; // GL.GetInteger(GetTarget.Viewport, viewport); // int width = viewport[2]; int height = viewport[3]; // IPerspectiveViewCamera perspectiveViewCamera = camera; // perspectiveViewCamera.FieldOfView = 60; // perspectiveViewCamera.AspectRatio = (double)width / (double)height; // perspectiveViewCamera.Near = 0.01; // perspectiveViewCamera.Far = size * 3 + 1;// double.MaxValue; // } // { // int[] viewport = new int[4]; // GL.GetInteger(GetTarget.Viewport, viewport); // int width = viewport[2]; int height = viewport[3]; // IOrthoViewCamera orthoViewCamera = camera; // if (width > height) // { // orthoViewCamera.Left = -size * width / height; // orthoViewCamera.Right = size * width / height; // orthoViewCamera.Bottom = -size; // orthoViewCamera.Top = size; // } // else // { // orthoViewCamera.Left = -size; // orthoViewCamera.Right = size; // orthoViewCamera.Bottom = -size * height / width; // orthoViewCamera.Top = size * height / width; // } // orthoViewCamera.Near = 0; // orthoViewCamera.Far = size * 3 + 1;// double.MaxValue; // } //} /// <summary> /// Adjusts camera's settings according to bounding box. /// <para>Use this when bounding box's size or positon is changed.</para> /// </summary> /// <param name="camera"></param> /// <param name="boundingBox"></param> public static void ZoomCamera(this IPerspectiveViewCamera camera, IBoundingBox boundingBox) { float sizeX, sizeY, sizeZ; boundingBox.GetBoundDimensions(out sizeX, out sizeY, out sizeZ); float size = Math.Max(Math.Max(sizeX, sizeY), sizeZ); { float centerX, centerY, centerZ; boundingBox.GetCenter(out centerX, out centerY, out centerZ); vec3 target = new vec3(centerX, centerY, centerZ); vec3 target2Position = (camera.Position - camera.Target).normalize(); vec3 position = target + target2Position * (size * 2 + 1); camera.Position = position; camera.Target = target; //camera.UpVector = new vec3(0f, 1f, 0f); } { int[] viewport = new int[4]; GL.GetInteger(GetTarget.Viewport, viewport); int width = viewport[2]; int height = viewport[3]; camera.FieldOfView = 60; camera.AspectRatio = (double)width / (double)height; camera.Near = 0.01; camera.Far = size * 3 + 1;// double.MaxValue; } }
protected void BeforeRendering(OpenGL gl, RenderMode renderMode) { IScientificCamera camera = this.camera; if (camera != null) { if (camera.CameraType == CameraTypes.Perspecitive) { IPerspectiveViewCamera perspective = camera; this.projectionMatrix = perspective.GetProjectionMat4(); this.viewMatrix = perspective.GetViewMat4(); } else if (camera.CameraType == CameraTypes.Ortho) { IOrthoViewCamera ortho = camera; this.projectionMatrix = ortho.GetProjectionMat4(); this.viewMatrix = ortho.GetViewMat4(); } else { throw new NotImplementedException(); } } modelMatrix = glm.scale(mat4.identity(), new vec3(1, 1, this.ZAxisScale)); gl.Enable(OpenGL.GL_VERTEX_PROGRAM_POINT_SIZE); gl.Enable(OpenGL.GL_POINT_SPRITE_ARB); gl.TexEnv(OpenGL.GL_POINT_SPRITE_ARB, OpenGL.GL_COORD_REPLACE_ARB, OpenGL.GL_TRUE); gl.Enable(OpenGL.GL_POINT_SMOOTH); gl.Hint(OpenGL.GL_POINT_SMOOTH_HINT, OpenGL.GL_NICEST); gl.Enable(OpenGL.GL_BLEND); gl.BlendEquation(OpenGL.GL_FUNC_ADD_EXT); gl.BlendFuncSeparate(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA, OpenGL.GL_ONE, OpenGL.GL_ONE); ShaderProgram shaderProgram = this.shaderProgram; int[] viewport = new int[4]; gl.GetInteger(OpenGL.GL_VIEWPORT, viewport); shaderProgram.Bind(gl); shaderProgram.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); shaderProgram.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shaderProgram.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); shaderProgram.SetUniform1(gl, "canvasWidth", viewport[2] + 0.0f); shaderProgram.SetUniform1(gl, "canvasHeight", viewport[3] + 0.0f); shaderProgram.SetUniform1(gl, "opacity", this.Opacity); this.texture.Bind(gl); shaderProgram.SetUniform1(gl, "tex", this.texture.TextureName); shaderProgram.SetUniform1(gl, "brightness", this.Brightness); }
protected void BeforeRendering(OpenGL gl, RenderMode renderMode) { IScientificCamera camera = this.camera; if (camera != null) { if (camera.CameraType == CameraTypes.Perspecitive) { IPerspectiveViewCamera perspective = camera; this.projectionMatrix = perspective.GetProjectionMat4(); this.viewMatrix = perspective.GetViewMat4(); } else if (camera.CameraType == CameraTypes.Ortho) { IOrthoViewCamera ortho = camera; this.projectionMatrix = ortho.GetProjectionMat4(); this.viewMatrix = ortho.GetViewMat4(); } else { throw new NotImplementedException(); } } modelMatrix = mat4.identity(); gl.Enable(OpenGL.GL_VERTEX_PROGRAM_POINT_SIZE); gl.Enable(OpenGL.GL_POINT_SPRITE_ARB); gl.TexEnv(OpenGL.GL_POINT_SPRITE_ARB, OpenGL.GL_COORD_REPLACE_ARB, OpenGL.GL_TRUE); gl.Enable(OpenGL.GL_POINT_SMOOTH); gl.Hint(OpenGL.GL_POINT_SMOOTH_HINT, OpenGL.GL_NICEST); gl.Enable(OpenGL.GL_BLEND); gl.BlendEquation(OpenGL.GL_FUNC_ADD_EXT); gl.BlendFuncSeparate(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA, OpenGL.GL_ONE, OpenGL.GL_ONE); gl.BindTexture(OpenGL.GL_TEXTURE_2D, this.texture[0]); int[] viewport = new int[4]; gl.GetInteger(OpenGL.GL_VIEWPORT, viewport); ShaderProgram shader = this.shaderProgram; shader.Bind(gl); shader.SetUniform1(gl, "tex", this.texture[0]); shader.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); shader.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shader.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); shader.SetUniform1(gl, "pointSize", this.textureWidth * 1.0f); shader.SetUniform3(gl, "textColor", this.textColor.x, this.textColor.y, this.textColor.z); }
//private void GeneratePoints(out float[] vertices, out float[] colors) //{ // const int length = 256 * 3; // vertices = new float[length]; colors = new float[length]; // Random random = new Random(); // int direction = this.positiveGrowth ? 1 : -1; // // points // for (int i = 0; i < length; i++) // { // vertices[i] = direction * (float)i / (float)length; // colors[i] = (float)((random.NextDouble() * 2 - 1) * 1); // //colors[i] = (float)i / (float)length; // } // //// triangles // //for (int i = 0; i < length / 9; i++) // //{ // // var x = random.NextDouble(); var y = random.NextDouble(); var z = random.NextDouble(); // // for (int j = 0; j < 3; j++) // // { // // vertices[i * 9 + j * 3] = (float)(x + random.NextDouble() / 5 - 1); // // } // // for (int j = 0; j < 3; j++) // // { // // vertices[i * 9 + j * 3 + 1] = (float)(y + random.NextDouble() / 5 - 1); // // } // // for (int j = 0; j < 3; j++) // // { // // vertices[i * 9 + j * 3 + 2] = (float)(z + random.NextDouble() / 5 - 1); // // } // //} //} #region IRenderable 成员 void IRenderable.Render(OpenGL gl, RenderMode renderMode) { // Update matrices. IScientificCamera camera = this.Camera; if (camera != null) { if (camera.CameraType == CameraTypes.Perspecitive) { IPerspectiveViewCamera perspective = camera; this.projectionMatrix = perspective.GetProjectionMat4(); this.viewMatrix = perspective.GetViewMat4(); } else if (camera.CameraType == CameraTypes.Ortho) { IOrthoViewCamera ortho = camera; this.projectionMatrix = ortho.GetProjectionMat4(); this.viewMatrix = ortho.GetViewMat4(); } else { throw new NotImplementedException(); } } modelMatrix = mat4.identity(); gl.PointSize(3); var shader = (renderMode == RenderMode.HitTest) ? pickingShaderProgram : shaderProgram; // Bind the shader, set the matrices. shader.Bind(gl); shader.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); shader.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shader.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); if (renderMode == RenderMode.HitTest) { shader.SetUniform1(gl, "pickingBaseID", ((IColorCodedPicking)this).PickingBaseID); } // Bind the out vertex array. vertexBufferArray.Bind(gl); // Draw the square. gl.DrawArrays((uint)this.mode, 0, positions.Length / 3); // Unbind our vertex array and shader. vertexBufferArray.Unbind(gl); shader.Unbind(gl); }
/// <summary> /// Adjusts camera's settings according to bounding box. /// <para>Use this when bounding box's size or positon is changed.</para> /// </summary> /// <param name="camera"></param> /// <param name="boundingBox"></param> public static void ZoomCamera(this IPerspectiveViewCamera camera, IBoundingBox boundingBox) { vec3 length = boundingBox.MaxPosition - boundingBox.MinPosition; float size = Math.Max(Math.Max(length.x, length.y), length.z); { vec3 target = boundingBox.MaxPosition / 2 + boundingBox.MinPosition / 2; vec3 target2Position = (camera.Position - camera.Target).normalize(); vec3 position = target + target2Position * (size * 2 + 1); camera.Position = position; camera.Target = target; //camera.UpVector = new vec3(0f, 1f, 0f); } }
protected void BeforeRendering(OpenGL gl, RenderMode renderMode) { IScientificCamera camera = this.camera; if (camera != null) { if (camera.CameraType == CameraTypes.Perspecitive) { IPerspectiveViewCamera perspective = camera; this.projectionMatrix = perspective.GetProjectionMat4(); this.viewMatrix = perspective.GetViewMat4(); } else if (camera.CameraType == CameraTypes.Ortho) { IOrthoViewCamera ortho = camera; this.projectionMatrix = ortho.GetProjectionMat4(); this.viewMatrix = ortho.GetViewMat4(); } else { throw new NotImplementedException(); } } gl.Enable(OpenGL.GL_TEXTURE_2D); this.texture.Bind(gl); modelMatrix = mat4.identity(); ShaderProgram shaderProgram = this.shaderProgram; // Bind the shader, set the matrices. shaderProgram.Bind(gl); shaderProgram.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); shaderProgram.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shaderProgram.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); shaderProgram.SetUniform1(gl, "tex", this.texture.TextureName); shaderProgram.SetUniform1(gl, "brightness", this.Brightness); gl.Enable(OpenGL.GL_POLYGON_SMOOTH); gl.Hint(OpenGL.GL_POLYGON_SMOOTH_HINT, OpenGL.GL_NICEST); }
void ScientificVisual3DControl_Resized(object sender, EventArgs e) { ScientificCamera camera = this.sceneControl.Scene.CurrentCamera as ScientificCamera; if (camera.CameraType == CameraTypes.Perspecitive) { IPerspectiveViewCamera perspecitive = camera; perspecitive.AdjustCamera(this.modelContainer.BoundingBox, this.sceneControl.OpenGL); } else if (camera.CameraType == CameraTypes.Ortho) { IOrthoViewCamera orthoCamera = camera; orthoCamera.AdjustCamera(this.modelContainer.BoundingBox, this.sceneControl.OpenGL); } else { throw new NotImplementedException(); } ManualRender(this.sceneControl); }
/// <summary> /// Apply specifed viewType to camera according to bounding box's size and position. /// <para> +-------+ </para> /// <para> /| /| </para> /// <para> +-------+ | </para> /// <para> | | | | </para> /// <para> | O-----|-+---X</para> /// <para> |/ |/ </para> /// <para> +-------+ </para> /// <para> / | </para> /// <para>Y Z </para> /// <para>其边长为(2 * Math.Sqrt(3)), 所在的坐标系如下</para> /// <para> O---X</para> /// <para> /| </para> /// <para> Y | </para> /// <para> Z </para> /// </summary> /// <param name="camera"></param> /// <param name="boundingBox"></param> /// <param name="openGL"></param> /// <param name="viewType"></param> public static void ApplyViewType(this IPerspectiveViewCamera camera, IBoundingBox boundingBox, OpenGL openGL, ViewTypes viewType, CoordinateSystem coordinateSystem) { float sizeX, sizeY, sizeZ; boundingBox.GetBoundDimensions(out sizeX, out sizeY, out sizeZ); float size = Math.Max(Math.Max(sizeX, sizeY), sizeZ); { float centerX, centerY, centerZ; boundingBox.GetCenter(out centerX, out centerY, out centerZ); Vertex target = new Vertex(centerX, centerY, centerZ); Vertex target2Position; Vertex upVector; GetBackAndUp(out target2Position, out upVector, viewType, coordinateSystem); Vertex position = target + target2Position * (size * 2 + 1); camera.Position = position; camera.Target = target; camera.UpVector = upVector; } { int[] viewport = new int[4]; openGL.GetInteger(SharpGL.Enumerations.GetTarget.Viewport, viewport); int width = viewport[2]; int height = viewport[3]; IPerspectiveCamera perspectiveCamera = camera; perspectiveCamera.FieldOfView = 60; perspectiveCamera.AspectRatio = (double)width / (double)height; perspectiveCamera.Near = 0.01; perspectiveCamera.Far = size * 3 + 1;// double.MaxValue; } }
/// <summary> /// Update camera when resized, model container's bounding box updated, etc. /// </summary> public void UpdateCamera() { ScientificCamera camera = this.Scene.CurrentCamera; //if (camera.CameraType == CameraTypes.Perspecitive) { IPerspectiveViewCamera perspecitive = camera; perspecitive.AdjustCamera(this.modelContainer.BoundingBox, this.OpenGL); } //else if (camera.CameraType == CameraTypes.Ortho) { IOrthoViewCamera orthoCamera = camera; orthoCamera.AdjustCamera(this.modelContainer.BoundingBox, this.OpenGL); } //else { //throw new NotImplementedException(); } //camera.Scale(-camera.scaleFactor); //// force CameraRotation to udpate. //this.CameraRotation.Camera = this.Scene.CurrentCamera; ManualRender(this); }
/// <summary> /// Extension method for <see cref="IPerspectiveViewCamera"/> to get view matrix. /// </summary> /// <param name="camera"></param> /// <returns></returns> public static mat4 GetViewMat4(this IPerspectiveViewCamera camera) { return(GetViewMat4((IViewCamera)camera)); }
void FormMain_Load(object sender, EventArgs e) { { var camera = new Camera( new vec3(0, 0, (float)(Earth.revolutionRadius * 1.1)), new vec3(0, 0, 0), new vec3(0, 1, 0), CameraType.Perspecitive, this.glCanvas1.Width, this.glCanvas1.Height); IPerspectiveViewCamera perspecitve = camera; perspecitve.Near = 1; perspecitve.Far = Earth.revolutionRadius * 5; var rotator = new SatelliteRotator(camera); this.camera = camera; this.rotator = rotator; } { const int latitude = 180; //从南到北,纬度共有180° const int hour = 24; //24小时,24个时区 const int longitudePerHour = 15; // 每个时区占有的经度为15° const int longitude = hour * longitudePerHour; // 从东到西,经度共有360° IBufferable bufferable = new CelestialBody(1, latitude, longitude); var shaderCodes = new ShaderCode[2]; shaderCodes[0] = new ShaderCode(File.ReadAllText(@"shaders\CelestialBody.vert"), ShaderType.VertexShader); shaderCodes[1] = new ShaderCode(File.ReadAllText(@"shaders\CelestialBody.frag"), ShaderType.FragmentShader); var map = new PropertyNameMap(); map.Add("inPosition", CelestialBody.strPosition); map.Add("inUV", CelestialBody.strUV); var earthRenderer = new PickableRenderer(bufferable, shaderCodes, map, CelestialBody.strPosition); earthRenderer.Initialize(); earthRenderer.Name = "Earth 地球"; this.earthRenderer = earthRenderer; } { // Ecliptic: 黄道 IBufferable bufferable = new Circle((float)Earth.revolutionRadius); var shaderCodes = new ShaderCode[2]; shaderCodes[0] = new ShaderCode(File.ReadAllText(@"shaders\Circle.vert"), ShaderType.VertexShader); shaderCodes[1] = new ShaderCode(File.ReadAllText(@"shaders\Circle.frag"), ShaderType.FragmentShader); var map = new PropertyNameMap(); map.Add("inPosition", Circle.strPosition); var eclipticRenderer = new Renderer(bufferable, shaderCodes, map); eclipticRenderer.Initialize(); eclipticRenderer.Name = "Ecliptic 黄道"; this.eclipticRenderer = eclipticRenderer; } { const int latitude = 180; //从南到北,纬度共有180° const int hour = 24; //24小时,24个时区 const int longitudePerHour = 15; // 每个时区占有的经度为15° const int longitude = hour * longitudePerHour; // 从东到西,经度共有360° IBufferable bufferable = new CelestialBody((float)Sun.radius, latitude, longitude); var shaderCodes = new ShaderCode[2]; shaderCodes[0] = new ShaderCode(File.ReadAllText(@"shaders\Sun.vert"), ShaderType.VertexShader); shaderCodes[1] = new ShaderCode(File.ReadAllText(@"shaders\Sun.frag"), ShaderType.FragmentShader); var map = new PropertyNameMap(); map.Add("inPosition", CelestialBody.strPosition); var sunRenderer = new PickableRenderer(bufferable, shaderCodes, map, CelestialBody.strPosition); sunRenderer.Initialize(); sunRenderer.Name = "Sun 太阳"; this.sunRenderer = sunRenderer; } { var sunshineRenderer = new SunshineRenderer(); this.sunshineRenderer = sunshineRenderer; } { var backgroundStars = new BackgroundStarsRenderer(2000, Earth.revolutionRadius); backgroundStars.Initialize(); backgroundStars.Name = "stars 星星"; this.backgroundStars = backgroundStars; } { var glAxis = new GLAxis(AnchorStyles.Left | AnchorStyles.Bottom, new Padding(5, 5, 5, 5), new Size(100, 100), -100, 100); glAxis.Initialize(); this.glAxis = glAxis; } { var uiRoot = new GLControl(this.glCanvas1.Size, -100, 100); uiRoot.Initialize(); this.uiRoot = uiRoot; } { this.uiRoot.Controls.Add(this.glAxis); } { var earthColorTexture = new sampler2D(); var bitmap = new Bitmap(@"Images\earth-color-map-10800-5400.jpg"); earthColorTexture.Initialize(bitmap); bitmap.Dispose(); this.earthColorTexture = earthColorTexture; } { this.eclipticRenderer.SetUniform("color", new vec4(1.0f, 1.0f, 0.0f, 0.1f)); this.eclipticRenderer.SwitchList.Add(new PolygonModeSwitch(PolygonModes.Points)); } { this.earthRenderer.SetUniform("colorTexture", new samplerValue(BindTextureTarget.Texture2D, this.earthColorTexture.Id, OpenGL.GL_TEXTURE0)); //this.earthRenderer.SwitchList.Add(new CullFaceSwitch()); //this.sunRenderer.SwitchList.Add(new CullFaceSwitch()); } { var earth = new Earth(); this.earth = earth; this.thingList.Add(earth); } { var sun = new Sun(); this.sun = sun; this.thingList.Add(sun); } { var tracer = new CameraTracer(this.camera, this.earth, this.sun); this.tracer = tracer; this.thingList.Add(tracer); } { var frmPropertyGrid = new FormProperyGrid(); frmPropertyGrid.DisplayObject(this); frmPropertyGrid.Show(); } { var frmPropertyGrid = new FormProperyGrid(); frmPropertyGrid.DisplayObject(this.earthRenderer); frmPropertyGrid.Show(); } { var frmPropertyGrid = new FormProperyGrid(); frmPropertyGrid.DisplayObject(this.sunRenderer); frmPropertyGrid.Show(); } { var frmPropertyGrid = new FormProperyGrid(); frmPropertyGrid.DisplayObject(this.eclipticRenderer); frmPropertyGrid.Show(); } { var frmPropertyGrid = new FormProperyGrid(); frmPropertyGrid.DisplayObject(this.tracer); frmPropertyGrid.Show(); } { this.TimeSpeed = 14400; } }
void IRenderable.Render(OpenGL gl, RenderMode renderMode) { if (!this.RenderModel) { return; } if (!initialised) { this.Initialise(gl); } // Update matrices. IScientificCamera camera = this.Camera; if (camera != null) { if (camera.CameraType == CameraTypes.Perspecitive) { IPerspectiveViewCamera perspective = camera; this.projectionMatrix = perspective.GetProjectionMat4(); this.viewMatrix = perspective.GetViewMat4(); } else if (camera.CameraType == CameraTypes.Ortho) { IOrthoViewCamera ortho = camera; this.projectionMatrix = ortho.GetProjectionMat4(); this.viewMatrix = ortho.GetViewMat4(); } else { throw new NotImplementedException(); } } modelMatrix = glm.scale(mat4.identity(), new vec3(1, 1, this.ZAxisScale)); //gl.PointSize(3); var shader = (renderMode == RenderMode.HitTest) ? pickingShaderProgram : shaderProgram; // Bind the shader, set the matrices. shader.Bind(gl); shader.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); shader.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shader.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); if (renderMode == RenderMode.HitTest) { shader.SetUniform1(gl, "pickingBaseID", ((IColorCodedPicking)this).PickingBaseID); } // Bind the out vertex array. vertexBufferArray.Bind(gl); // Draw the square. ScientificModel model = this.Model; if (model.First != null && model.Count != null && model.PrimitiveCount > 0) { gl.MultiDrawArrays((uint)model.Mode, model.First, model.Count, model.PrimitiveCount); } else { gl.DrawArrays((uint)this.Model.Mode, 0, this.Model.VertexCount); } // Unbind our vertex array and shader. vertexBufferArray.Unbind(gl); shader.Unbind(gl); }
public PerspectiveCameraController(IPerspectiveViewCamera camera = null) { InitializeComponent(); this.camera = camera; }