/// <summary> /// Allocate resources for rendering. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ObjectsControl_ContextCreated(object sender, GlControlEventArgs e) { // Wrap GL context with GraphicsContext _Context = new GraphicsContext(e.DeviceContext, e.RenderContext); // Scene _CubeScene = new SceneGraph(); _CubeScene.Link(new SceneObjectCamera()); // Global lighting SceneObjectLightZone globalLightZone = new SceneObjectLightZone(); _GlobalLightObject = new SceneObjectLightDirectional(); _GlobalLightObject.Direction = (-Vertex3f.UnitX + Vertex3f.UnitY - Vertex3f.UnitZ).Normalized; globalLightZone.Link(_GlobalLightObject); SceneObjectLightPoint localLightObject = new SceneObjectLightPoint(); localLightObject.LocalModel.Translate(0.0, -5.0f, 0.0); localLightObject.AttenuationFactors.Y = 0.1f; //globalLightZone.AddChild(localLightObject); _CubeScene.Link(globalLightZone); // Cube float Size = (float)Math.Sqrt(0); const float Multiplier = 10.0f; int materialIndex = 0; for (float x = -Size / 2.0f * Multiplier; x < Size / 2.0f * Multiplier; x += Multiplier) { for (float y = -Size / 2.0f * Multiplier; y < Size / 2.0f * Multiplier; y += Multiplier, materialIndex++) { SceneObjectGeometry cubeInstance = CreateCubeGeometry(); cubeInstance.LocalModel.Translate(x, 0.0f, y); cubeInstance.LocalModel.Scale(0.25f); // Enable/Disable blending if ((materialIndex % 2) == 0) { cubeInstance.ObjectState.DefineState(new BlendState(BlendEquationModeEXT.FuncAdd, BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha)); } // Enable/Disable blending switch (materialIndex % 1) { case 0: cubeInstance.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+LambertVertex", new ShaderCompilerContext("GLO_COLOR_PER_VERTEX")); break; case 1: cubeInstance.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+Color"); break; case 2: cubeInstance.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+PhongFragment"); break; } globalLightZone.Link(cubeInstance); } } //SceneObjectGeometry cubeLambert = CreateCubeGeometry(); //cubeLambert.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+LambertVertex", new ShaderCompilerContext("GLO_COLOR_PER_VERTEX")); //globalLightZone.Link(cubeLambert); //SceneObjectGeometry cubeBlinn = CreateCubeGeometry(); //cubeBlinn.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+PhongFragment", new ShaderCompilerContext("GLO_COLOR_PER_VERTEX")); //cubeBlinn.LocalModel.Translate(0.0f, 10.0f, 0.0f); //globalLightZone.Link(cubeBlinn); //SceneObjectGeometry cubeColored = CreateCubeGeometry(); //cubeColored.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+Color"); //cubeColored.LocalModel.Translate(0.0f, 5.0f, 0.0f); //globalLightZone.Link(cubeColored); globalLightZone.Link(CreateBumbleBeeVH()); globalLightZone.Link(CreateBumbleBeeRB()); globalLightZone.Link(CreateSphere("squarebricks")); // Skybox //if ((sceneObject = CreateSkyBoxObject()) != null) // _CubeScene.AddChild(sceneObject); _CubeScene.Create(_Context); Gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f); }
/// <summary> /// Allocate resources for rendering. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ObjectsControl_ContextCreated(object sender, GlControlEventArgs e) { // Wrap GL context with GraphicsContext _Context = new GraphicsContext(e.DeviceContext, e.RenderContext); // Scene _CubeScene = new SceneGraph( SceneGraphFlags.CullingViewFrustum | SceneGraphFlags.StateSorting | SceneGraphFlags.Lighting | SceneGraphFlags.ShadowMaps //| SceneGraphFlags.BoundingVolumes ); _CubeScene.SceneRoot = new SceneObjectGeometry(); _CubeScene.SceneRoot.ObjectState.DefineState(new DepthTestState(DepthFunction.Less)); _CubeScene.CurrentView = new SceneObjectCamera(); _CubeScene.SceneRoot.Link(_CubeScene.CurrentView); fontTitle = FontFactory.CreateFont(System.Drawing.FontFamily.GenericSerif, 24, System.Drawing.FontStyle.Regular, FontFactory.FontType.Textured, new FontFxShadow()); fontTitle.Create(_Context); fontTitleV = FontFactory.CreateFont(System.Drawing.FontFamily.GenericSerif, 24, System.Drawing.FontStyle.Regular, FontFactory.FontType.Vector, new FontFxShadow()); fontTitleV.Create(_Context); fontPatch = FontFactory.CreateFont(System.Drawing.FontFamily.GenericMonospace, 16, System.Drawing.FontStyle.Regular, FontFactory.FontType.Vector); fontPatch.Create(_Context); // Global lighting SceneObjectLightZone globalLightZone = new SceneObjectLightZone(); _GlobalLightObject = new SceneObjectLightDirectional(); _GlobalLightObject.Direction = (-Vertex3f.UnitX + Vertex3f.UnitY - Vertex3f.UnitZ).Normalized; //globalLightZone.Link(_GlobalLightObject); spotLight = new SceneObjectLightSpot(); spotLight.LocalModel.Translate(0.0f, 35.0f, 0.0f); spotLight.Direction = -Vertex3f.UnitY; spotLight.FalloffAngle = 10.5f; spotLight.FalloffExponent = 0.0f; globalLightZone.Link(spotLight); SceneObjectLightPoint localLightObject = new SceneObjectLightPoint(); localLightObject.LocalModel.Translate(0.0, -5.0f, 0.0); localLightObject.AttenuationFactors.Y = 0.1f; //globalLightZone.AddChild(localLightObject); _CubeScene.SceneRoot.Link(globalLightZone); // Horizontal plane globalLightZone.Link(CreatePlane()); // Cube float Size = (float)Math.Sqrt(1); const float Multiplier = 10.0f; int materialIndex = 0; for (float x = -Size / 2.0f * Multiplier; x < Size / 2.0f * Multiplier; x += Multiplier) { for (float y = -Size / 2.0f * Multiplier; y < Size / 2.0f * Multiplier; y += Multiplier, materialIndex++) { SceneObjectGeometry cubeInstance = CreateCubeGeometry(); cubeInstance.LocalModel.Translate(x, 0.0f, y); cubeInstance.LocalModel.Scale(0.25f); // materialIndex = 1; // Enable/Disable blending if ((materialIndex % 2) == 0) { cubeInstance.ObjectState.DefineState(new BlendState(BlendEquationMode.FuncAdd, BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha)); } // Enable/Disable blending switch (materialIndex % 3) { case 0: cubeInstance.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+Color"); break; case 1: cubeInstance.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+LambertVertex", new ShaderCompilerContext("GLO_COLOR_PER_VERTEX")); break; case 2: cubeInstance.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+PhongFragment"); break; } globalLightZone.Link(cubeInstance); } } //SceneObjectGeometry cubeLambert = CreateCubeGeometry(); //cubeLambert.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+LambertVertex", new ShaderCompilerContext("GLO_COLOR_PER_VERTEX")); //globalLightZone.Link(cubeLambert); //SceneObjectGeometry cubeBlinn = CreateCubeGeometry(); //cubeBlinn.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+PhongFragment", new ShaderCompilerContext("GLO_COLOR_PER_VERTEX")); //cubeBlinn.LocalModel.Translate(0.0f, 10.0f, 0.0f); //globalLightZone.Link(cubeBlinn); //SceneObjectGeometry cubeColored = CreateCubeGeometry(); //cubeColored.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+Color"); //cubeColored.LocalModel.Translate(0.0f, 5.0f, 0.0f); //globalLightZone.Link(cubeColored); globalLightZone.Link(CreateBumbleBeeVH()); globalLightZone.Link(CreateBumbleBeeRB()); // globalLightZone.Link(CreateSphere("squarebricks")); // Skybox //if ((sceneObject = CreateSkyBoxObject()) != null) // _CubeScene.AddChild(sceneObject); _CubeScene.Create(_Context); Gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f); Gl.Enable(EnableCap.Multisample); }